broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(code):
    code = [list(i) for i in code]
    for _ in range(50):
        changed = False
        out = []
        i = 0
        while i < len(code):
            ins = code[i]
            if (i + 2 < len(code) and ins[0] == 'push' and code[i + 1][0] == 'push'
                    and code[i + 2][0] in ('add', 'sub', 'mul', 'div')):
                a, b, op = ins[1], code[i + 1][1], code[i + 2][0]
                if not (op == 'div' and b == 0):
                    if op == 'add':
                        v = a + b
                    elif op == 'sub':
                        v = a - b
                    elif op == 'mul':
                        v = a * b
                    else:
                        v = abs(a) // abs(b) * (1 if (a < 0) == (b < 0) else -1)
                    if abs(v) <= 32767:
                        out.append(['push', v])
                        i += 3
                        changed = True
                        continue
            if ins[0] == 'not' and i + 1 < len(code) and code[i + 1][0] == 'not':
                i += 2
                changed = True
                continue
            if ins[0] == 'jmp' and i + 1 < len(code) and code[i + 1] == ['label', ins[1]]:
                i += 1
                changed = True
                continue
            out.append(ins)
            i += 1
            if ins[0] in ('ret', 'jmp'):
                j = i
                while j < len(code) and code[j][0] != 'label':
                    j += 1
                if j > i:
                    changed = True
                i = j
        code = out
        if not changed:
            break
    return code
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: folding cascades across passes',
   ([['push', 2], ['push', 4], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 19], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 1], ['ret']],),
   [['push', -32768], ['push', 1], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 201], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 201], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 1], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -9],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -4], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 1], ['not'], ['not'], ['ret']],), [['push', 1], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 5], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 23], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 2], ['ret']],),
   [['push', -32768], ['push', 2], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 202], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 202], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 2], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -11],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -5], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 2], ['not'], ['not'], ['ret']],), [['push', 2], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 6], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 27], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 3], ['ret']],),
   [['push', -32768], ['push', 3], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 203], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 203], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 3], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -13],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -6], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 3], ['not'], ['not'], ['ret']],), [['push', 3], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 7], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 31], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 4], ['ret']],),
   [['push', -32768], ['push', 4], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 204], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 204], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 4], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -15],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -7], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 4], ['not'], ['not'], ['ret']],), [['push', 4], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 8], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 35], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 5], ['ret']],),
   [['push', -32768], ['push', 5], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 205], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 205], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 5], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -17],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -8], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 5], ['not'], ['not'], ['ret']],), [['push', 5], ['ret']])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(code):
    code = [list(i) for i in code]
    for _ in range(50):
        changed = False
        out = []
        i = 0
        while i < len(code):
            ins = code[i]
            if (i + 2 < len(code) and ins[0] == 'push' and code[i + 1][0] == 'push'
                    and code[i + 2][0] in ('add', 'sub', 'mul', 'div')):
                a, b, op = ins[1], code[i + 1][1], code[i + 2][0]
                if not (op == 'div' and b == 0):
                    if op == 'add':
                        v = a + b
                    elif op == 'sub':
                        v = a - b
                    elif op == 'mul':
                        v = a * b
                    else:
                        v = abs(a) // abs(b) * (1 if (a < 0) == (b < 0) else -1)
                    if -32768 <= v < 32767:
                        out.append(['push', v])
                        i += 3
                        changed = True
                        continue
            if ins[0] == 'not' and i + 1 < len(code) and code[i + 1][0] == 'not':
                i += 2
                changed = True
                continue
            if ins[0] == 'jmp' and i + 1 < len(code) and code[i + 1] == ['label', ins[1]]:
                i += 1
                changed = True
                continue
            out.append(ins)
            i += 1
            if ins[0] in ('ret', 'jmp'):
                j = i
                while j < len(code) and code[j][0] != 'label':
                    j += 1
                if j > i:
                    changed = True
                i = j
        code = out
        if not changed:
            break
    return code
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: folding cascades across passes',
   ([['push', 2], ['push', 4], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 19], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 1], ['ret']],),
   [['push', -32768], ['push', 1], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 201], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 201], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 1], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -9],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -4], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 1], ['not'], ['not'], ['ret']],), [['push', 1], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 5], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 23], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 2], ['ret']],),
   [['push', -32768], ['push', 2], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 202], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 202], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 2], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -11],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -5], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 2], ['not'], ['not'], ['ret']],), [['push', 2], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 6], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 27], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 3], ['ret']],),
   [['push', -32768], ['push', 3], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 203], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 203], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 3], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -13],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -6], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 3], ['not'], ['not'], ['ret']],), [['push', 3], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 7], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 31], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 4], ['ret']],),
   [['push', -32768], ['push', 4], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 204], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 204], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 4], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -15],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -7], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 4], ['not'], ['not'], ['ret']],), [['push', 4], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 8], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 35], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 5], ['ret']],),
   [['push', -32768], ['push', 5], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 205], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 205], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 5], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -17],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -8], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 5], ['not'], ['not'], ['ret']],), [['push', 5], ['ret']])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Constant expressions producing the int16 minimum are left unfolded.

Rewrite stack bytecode until a pass makes no change (at most 50 passes). Rules, applied left to right in one scan per pass: push a, push b, add/sub/mul/div folds to push (a op b) when the result fits in int16 (div truncates toward zero and is never folded by zero); not, not is removed; jmp L immediately followed by label L is removed; after ret or jmp, instructions up to the next label are deleted. Return the optimized instruction list.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(code):
    code = [list(i) for i in code]
    for _ in range(50):
        changed = False
        out = []
        i = 0
        while i < len(code):
            ins = code[i]
            if (i + 2 < len(code) and ins[0] == 'push' and code[i + 1][0] == 'push'
                    and code[i + 2][0] in ('add', 'sub', 'mul', 'div')):
                a, b, op = ins[1], code[i + 1][1], code[i + 2][0]
                if not (op == 'div' and b == 0):
                    if op == 'add':
                        v = a + b
                    elif op == 'sub':
                        v = a - b
                    elif op == 'mul':
                        v = a * b
                    else:
                        v = abs(a) // abs(b) * (1 if (a < 0) == (b < 0) else -1)
                    if -32768 <= v <= 32767:
                        out.append(['push', v])
                        i += 3
                        changed = True
                        continue
            if ins[0] == 'not' and i + 1 < len(code) and code[i + 1][0] == 'not':
                i += 2
                changed = True
                continue
            if ins[0] == 'jmp' and i + 1 < len(code) and code[i + 1][0] == 'label':
                i += 1
                changed = True
                continue
            out.append(ins)
            i += 1
            if ins[0] in ('ret', 'jmp'):
                j = i
                while j < len(code) and code[j][0] != 'label':
                    j += 1
                if j > i:
                    changed = True
                i = j
        code = out
        if not changed:
            break
    return code
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: folding cascades across passes',
   ([['push', 2], ['push', 4], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 19], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 1], ['ret']],),
   [['push', -32768], ['push', 1], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 201], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 201], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 1], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -9],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -4], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 1], ['not'], ['not'], ['ret']],), [['push', 1], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 5], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 23], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 2], ['ret']],),
   [['push', -32768], ['push', 2], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 202], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 202], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 2], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -11],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -5], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 2], ['not'], ['not'], ['ret']],), [['push', 2], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 6], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 27], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 3], ['ret']],),
   [['push', -32768], ['push', 3], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 203], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 203], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 3], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -13],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -6], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 3], ['not'], ['not'], ['ret']],), [['push', 3], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 7], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 31], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 4], ['ret']],),
   [['push', -32768], ['push', 4], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 204], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 204], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 4], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -15],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -7], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 4], ['not'], ['not'], ['ret']],), [['push', 4], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 8], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 35], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 5], ['ret']],),
   [['push', -32768], ['push', 5], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 205], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 205], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 5], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -17],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -8], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 5], ['not'], ['not'], ['ret']],), [['push', 5], ['ret']])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(code):
    code = [list(i) for i in code]
    for _ in range(50):
        changed = False
        out = []
        i = 0
        while i < len(code):
            ins = code[i]
            if (i + 2 < len(code) and ins[0] == 'push' and code[i + 1][0] == 'push'
                    and code[i + 2][0] in ('add', 'sub', 'mul', 'div')):
                a, b, op = ins[1], code[i + 1][1], code[i + 2][0]
                if not (op == 'div' and b == 0):
                    if op == 'add':
                        v = a + b
                    elif op == 'sub':
                        v = a - b
                    elif op == 'mul':
                        v = a * b
                    else:
                        v = abs(a) // abs(b) * (1 if (a < 0) == (b < 0) else -1)
                    if -32768 <= v <= 32767:
                        out.append(['push', v])
                        i += 3
                        changed = True
                        continue
            if ins[0] == 'not' and i + 1 < len(code) and code[i + 1][0] == 'not':
                i += 2
                changed = True
                continue
            if ins[0] == 'jmp' and i + 1 < len(code) and ['label', ins[1]] in code[i + 1:i + 3]:
                i += 1
                changed = True
                continue
            out.append(ins)
            i += 1
            if ins[0] in ('ret', 'jmp'):
                j = i
                while j < len(code) and code[j][0] != 'label':
                    j += 1
                if j > i:
                    changed = True
                i = j
        code = out
        if not changed:
            break
    return code
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: folding cascades across passes',
   ([['push', 2], ['push', 4], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 19], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 1], ['ret']],),
   [['push', -32768], ['push', 1], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 201], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 201], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 1], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -9],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -4], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 1], ['not'], ['not'], ['ret']],), [['push', 1], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 5], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 23], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 2], ['ret']],),
   [['push', -32768], ['push', 2], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 202], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 202], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 2], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -11],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -5], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 2], ['not'], ['not'], ['ret']],), [['push', 2], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 6], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 27], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 3], ['ret']],),
   [['push', -32768], ['push', 3], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 203], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 203], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 3], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -13],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -6], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 3], ['not'], ['not'], ['ret']],), [['push', 3], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 7], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 31], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 4], ['ret']],),
   [['push', -32768], ['push', 4], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 204], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 204], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 4], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -15],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -7], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 4], ['not'], ['not'], ['ret']],), [['push', 4], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 8], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 35], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 5], ['ret']],),
   [['push', -32768], ['push', 5], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 205], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 205], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 5], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -17],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -8], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 5], ['not'], ['not'], ['ret']],), [['push', 5], ['ret']])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A jmp that skips a labelled block is removed, so the skipped code now executes.

Rewrite stack bytecode until a pass makes no change (at most 50 passes). Rules, applied left to right in one scan per pass: push a, push b, add/sub/mul/div folds to push (a op b) when the result fits in int16 (div truncates toward zero and is never folded by zero); not, not is removed; jmp L immediately followed by label L is removed; after ret or jmp, instructions up to the next label are deleted. Return the optimized instruction list.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(code):
    code = [list(i) for i in code]
    for _ in range(50):
        changed = False
        out = []
        i = 0
        while i < len(code):
            ins = code[i]
            if (i + 2 < len(code) and ins[0] == 'push' and code[i + 1][0] == 'push'
                    and code[i + 2][0] in ('add', 'sub', 'mul', 'div')):
                a, b, op = ins[1], code[i + 1][1], code[i + 2][0]
                if not (op == 'div' and b == 0):
                    if op == 'add':
                        v = a + b
                    elif op == 'sub':
                        v = a - b
                    elif op == 'mul':
                        v = a * b
                    else:
                        v = abs(a) // abs(b) * (1 if (a < 0) == (b < 0) else -1)
                    if -32768 <= v <= 32767:
                        out.append(['push', v])
                        i += 3
                        changed = True
                        continue
            if ins[0] == 'not' and i + 1 < len(code) and code[i + 1][0] == 'not':
                i += 2
                changed = True
                continue
            if ins[0] == 'jmp' and i + 1 < len(code) and code[i + 1] == ['label', ins[1]]:
                i += 1
                changed = True
                continue
            out.append(ins)
            i += 1
            if ins[0] == 'ret':
                j = i
                while j < len(code) and code[j][0] != 'label':
                    j += 1
                if j > i:
                    changed = True
                i = j
        code = out
        if not changed:
            break
    return code
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: folding cascades across passes',
   ([['push', 2], ['push', 4], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 19], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 1], ['ret']],),
   [['push', -32768], ['push', 1], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 201], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 201], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 1], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -9],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -4], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 1], ['not'], ['not'], ['ret']],), [['push', 1], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 5], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 23], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 2], ['ret']],),
   [['push', -32768], ['push', 2], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 202], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 202], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 2], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -11],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -5], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 2], ['not'], ['not'], ['ret']],), [['push', 2], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 6], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 27], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 3], ['ret']],),
   [['push', -32768], ['push', 3], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 203], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 203], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 3], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -13],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -6], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 3], ['not'], ['not'], ['ret']],), [['push', 3], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 7], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 31], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 4], ['ret']],),
   [['push', -32768], ['push', 4], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 204], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 204], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 4], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -15],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -7], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 4], ['not'], ['not'], ['ret']],), [['push', 4], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 8], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 35], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 5], ['ret']],),
   [['push', -32768], ['push', 5], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 205], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 205], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 5], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -17],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -8], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 5], ['not'], ['not'], ['ret']],), [['push', 5], ['ret']])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(code):
    code = [list(i) for i in code]
    for _ in range(50):
        changed = False
        out = []
        i = 0
        while i < len(code):
            ins = code[i]
            if (i + 2 < len(code) and ins[0] == 'push' and code[i + 1][0] == 'push'
                    and code[i + 2][0] in ('add', 'sub', 'mul', 'div')):
                a, b, op = ins[1], code[i + 1][1], code[i + 2][0]
                if not (op == 'div' and b == 0):
                    if op == 'add':
                        v = a + b
                    elif op == 'sub':
                        v = a - b
                    elif op == 'mul':
                        v = a * b
                    else:
                        v = abs(a) // abs(b) * (1 if (a < 0) == (b < 0) else -1)
                    if -32768 <= v <= 32767:
                        out.append(['push', v])
                        i += 3
                        changed = True
                        continue
            if ins[0] == 'not' and i + 1 < len(code) and code[i + 1][0] == 'not':
                i += 2
                changed = True
                continue
            if ins[0] == 'jmp' and i + 1 < len(code) and code[i + 1] == ['label', ins[1]]:
                i += 1
                changed = True
                continue
            out.append(ins)
            i += 1
            if ins[0] in ('ret', 'jmp', 'jz'):
                j = i
                while j < len(code) and code[j][0] != 'label':
                    j += 1
                if j > i:
                    changed = True
                i = j
        code = out
        if not changed:
            break
    return code
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: folding cascades across passes',
   ([['push', 2], ['push', 4], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 19], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 1], ['ret']],),
   [['push', -32768], ['push', 1], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 201], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 201], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 1], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -9],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -4], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 1], ['not'], ['not'], ['ret']],), [['push', 1], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 5], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 23], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 2], ['ret']],),
   [['push', -32768], ['push', 2], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 202], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 202], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 2], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -11],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -5], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 2], ['not'], ['not'], ['ret']],), [['push', 2], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 6], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 27], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 3], ['ret']],),
   [['push', -32768], ['push', 3], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 203], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 203], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 3], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -13],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -6], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 3], ['not'], ['not'], ['ret']],), [['push', 3], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 7], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 31], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 4], ['ret']],),
   [['push', -32768], ['push', 4], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 204], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 204], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 4], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -15],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -7], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 4], ['not'], ['not'], ['ret']],), [['push', 4], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 8], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 35], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 5], ['ret']],),
   [['push', -32768], ['push', 5], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 205], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 205], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 5], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -17],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -8], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 5], ['not'], ['not'], ['ret']],), [['push', 5], ['ret']])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Unreachable instructions after unconditional jumps survive and block jmp-to-next removal.

Rewrite stack bytecode until a pass makes no change (at most 50 passes). Rules, applied left to right in one scan per pass: push a, push b, add/sub/mul/div folds to push (a op b) when the result fits in int16 (div truncates toward zero and is never folded by zero); not, not is removed; jmp L immediately followed by label L is removed; after ret or jmp, instructions up to the next label are deleted. Return the optimized instruction list.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(code):
    code = [list(i) for i in code]
    for _ in range(50):
        changed = False
        out = []
        i = 0
        while i < len(code):
            ins = code[i]
            if (i + 2 < len(code) and ins[0] == 'push' and code[i + 1][0] == 'push'
                    and code[i + 2][0] in ('add', 'sub', 'mul', 'div')):
                a, b, op = ins[1], code[i + 1][1], code[i + 2][0]
                if not (op == 'div' and b == 0):
                    if op == 'add':
                        v = a + b
                    elif op == 'sub':
                        v = a - b
                    elif op == 'mul':
                        v = a * b
                    else:
                        v = a // b
                    if -32768 <= v <= 32767:
                        out.append(['push', v])
                        i += 3
                        changed = True
                        continue
            if ins[0] == 'not' and i + 1 < len(code) and code[i + 1][0] == 'not':
                i += 2
                changed = True
                continue
            if ins[0] == 'jmp' and i + 1 < len(code) and code[i + 1] == ['label', ins[1]]:
                i += 1
                changed = True
                continue
            out.append(ins)
            i += 1
            if ins[0] in ('ret', 'jmp'):
                j = i
                while j < len(code) and code[j][0] != 'label':
                    j += 1
                if j > i:
                    changed = True
                i = j
        code = out
        if not changed:
            break
    return code
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: folding cascades across passes',
   ([['push', 2], ['push', 4], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 19], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 1], ['ret']],),
   [['push', -32768], ['push', 1], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 201], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 201], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 1], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -9],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -4], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 1], ['not'], ['not'], ['ret']],), [['push', 1], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 5], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 23], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 2], ['ret']],),
   [['push', -32768], ['push', 2], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 202], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 202], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 2], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -11],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -5], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 2], ['not'], ['not'], ['ret']],), [['push', 2], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 6], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 27], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 3], ['ret']],),
   [['push', -32768], ['push', 3], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 203], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 203], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 3], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -13],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -6], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 3], ['not'], ['not'], ['ret']],), [['push', 3], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 7], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 31], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 4], ['ret']],),
   [['push', -32768], ['push', 4], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 204], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 204], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 4], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -15],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -7], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 4], ['not'], ['not'], ['ret']],), [['push', 4], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 8], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 35], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 5], ['ret']],),
   [['push', -32768], ['push', 5], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 205], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 205], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 5], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -17],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -8], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 5], ['not'], ['not'], ['ret']],), [['push', 5], ['ret']])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(code):
    code = [list(i) for i in code]
    for _ in range(50):
        changed = False
        out = []
        i = 0
        while i < len(code):
            ins = code[i]
            if (i + 2 < len(code) and ins[0] == 'push' and code[i + 1][0] == 'push'
                    and code[i + 2][0] in ('add', 'sub', 'mul', 'div')):
                a, b, op = ins[1], code[i + 1][1], code[i + 2][0]
                if not (op == 'div' and b == 0):
                    if op == 'add':
                        v = a + b
                    elif op == 'sub':
                        v = a - b
                    elif op == 'mul':
                        v = a * b
                    else:
                        v = -(abs(a) // abs(b)) if a < 0 else abs(a) // abs(b)
                    if -32768 <= v <= 32767:
                        out.append(['push', v])
                        i += 3
                        changed = True
                        continue
            if ins[0] == 'not' and i + 1 < len(code) and code[i + 1][0] == 'not':
                i += 2
                changed = True
                continue
            if ins[0] == 'jmp' and i + 1 < len(code) and code[i + 1] == ['label', ins[1]]:
                i += 1
                changed = True
                continue
            out.append(ins)
            i += 1
            if ins[0] in ('ret', 'jmp'):
                j = i
                while j < len(code) and code[j][0] != 'label':
                    j += 1
                if j > i:
                    changed = True
                i = j
        code = out
        if not changed:
            break
    return code
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: folding cascades across passes',
   ([['push', 2], ['push', 4], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 19], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 1], ['ret']],),
   [['push', -32768], ['push', 1], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 201], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 201], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 1], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 1], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 1], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 10], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -9],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -4], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 1], ['not'], ['not'], ['ret']],), [['push', 1], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 5], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 23], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 2], ['ret']],),
   [['push', -32768], ['push', 2], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 202], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 202], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 2], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 2], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 2], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 11], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -11],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -5], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 2], ['not'], ['not'], ['ret']],), [['push', 2], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 6], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 27], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 3], ['ret']],),
   [['push', -32768], ['push', 3], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 203], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 203], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 3], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 3], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 3], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 12], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -13],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -6], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 3], ['not'], ['not'], ['ret']],), [['push', 3], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 7], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 31], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 4], ['ret']],),
   [['push', -32768], ['push', 4], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 204], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 204], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 4], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 4], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 4], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 13], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -15],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -7], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 4], ['not'], ['not'], ['ret']],), [['push', 4], ['ret']])],
 [('regression: folding cascades across passes',
   ([['push', 2], ['push', 8], ['add'], ['push', 4], ['mul'], ['push', 5], ['sub'], ['ret']],),
   [['push', 35], ['ret']]),
  ('fold reaching the int16 minimum',
   ([['push', -16384], ['push', 2], ['mul'], ['push', 5], ['ret']],),
   [['push', -32768], ['push', 5], ['ret']]),
  ('fold reaching the int16 maximum',
   ([['push', 32766], ['push', 1], ['add'], ['push', 200], ['push', 205], ['mul'], ['ret']],),
   [['push', 32767], ['push', 200], ['push', 205], ['mul'], ['ret']]),
  ('jump over code to a later label is kept',
   ([['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']],),
   [['jmp', 'L2'], ['label', 'L1'], ['push', 5], ['label', 'L2'], ['ret']]),
  ('jump to the second of two adjacent labels',
   ([['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']],),
   [['jmp', 'L'], ['label', 'M'], ['label', 'L'], ['push', 5], ['ret']]),
  ('dead code after an unconditional jump',
   ([['jmp', 'E'], ['push', 1], ['push', 5], ['label', 'E'], ['ret']],),
   [['label', 'E'], ['ret']]),
  ('conditional jump keeps its fallthrough',
   ([['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']],),
   [['push', 0], ['jz', 'E'], ['push', 14], ['ret'], ['label', 'E'], ['push', 1], ['ret']]),
  ('division folds truncate toward zero',
   ([['push', -17],
     ['push', 2],
     ['div'],
     ['push', 7],
     ['push', -2],
     ['div'],
     ['push', 5],
     ['push', 0],
     ['div'],
     ['ret']],),
   [['push', -8], ['push', -3], ['push', 5], ['push', 0], ['div'], ['ret']]),
  ('control: double negation removed', ([['push', 5], ['not'], ['not'], ['ret']],), [['push', 5], ['ret']])]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Folded code computes -9 / 2 as -5 while the interpreter computes -4.

Rewrite stack bytecode until a pass makes no change (at most 50 passes). Rules, applied left to right in one scan per pass: push a, push b, add/sub/mul/div folds to push (a op b) when the result fits in int16 (div truncates toward zero and is never folded by zero); not, not is removed; jmp L immediately followed by label L is removed; after ret or jmp, instructions up to the next label are deleted. Return the optimized instruction list.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(init, limit, step):
    if step == 0:
        return 'error: for step is zero'
    if isinstance(init, int) and isinstance(step, int):
        if isinstance(limit, float):
            limit = int(limit)
        if (step > 0 and init > limit) or (step < 0 and init < limit):
            return []
        count = (limit - init) // step if step > 0 else (init - limit) // (-step)
        out = []
        v = init
        for _ in range(min(count, 19) + 1):
            out.append(v)
            v += step
        return out
    out = []
    v = float(init)
    while (v <= limit if step > 0 else v >= limit) and len(out) < 20:
        out.append(v)
        v += step
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: ascending integer loop', (1, 4, 1), [1, 2, 3, 4]),
  ('descending loop with remainder', (11, 1, -3), [11, 8, 5, 2]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -3.5, 1), [-5, -4]),
  ('float limit ceils for descending loops', (6, 2.5, -1), [6, 5, 4, 3]),
  ('init equal to limit runs once', (1, 1, 1), [1]),
  ('descending init equal to limit runs once', (1, 1, -1), [1]),
  ('long loop is capped at 20 values',
   (0, 101, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 6, 0), 'error: for step is zero')],
 [('regression: ascending integer loop', (1, 5, 1), [1, 2, 3, 4, 5]),
  ('descending loop with remainder', (12, 1, -3), [12, 9, 6, 3]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -4.5, 1), [-5]),
  ('float limit ceils for descending loops', (7, 2.5, -1), [7, 6, 5, 4, 3]),
  ('init equal to limit runs once', (2, 2, 1), [2]),
  ('descending init equal to limit runs once', (2, 2, -1), [2]),
  ('long loop is capped at 20 values',
   (0, 102, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 7, 0), 'error: for step is zero')],
 [('regression: ascending integer loop', (1, 6, 1), [1, 2, 3, 4, 5, 6]),
  ('descending loop with remainder', (13, 1, -3), [13, 10, 7, 4, 1]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -5.5, 1), []),
  ('float limit ceils for descending loops', (8, 2.5, -1), [8, 7, 6, 5, 4, 3]),
  ('init equal to limit runs once', (3, 3, 1), [3]),
  ('descending init equal to limit runs once', (3, 3, -1), [3]),
  ('long loop is capped at 20 values',
   (0, 103, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 8, 0), 'error: for step is zero')],
 [('regression: ascending integer loop', (1, 7, 1), [1, 2, 3, 4, 5, 6, 7]),
  ('descending loop with remainder', (14, 1, -3), [14, 11, 8, 5, 2]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -6.5, 1), []),
  ('float limit ceils for descending loops', (9, 2.5, -1), [9, 8, 7, 6, 5, 4, 3]),
  ('init equal to limit runs once', (4, 4, 1), [4]),
  ('descending init equal to limit runs once', (4, 4, -1), [4]),
  ('long loop is capped at 20 values',
   (0, 104, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 9, 0), 'error: for step is zero')],
 [('regression: ascending integer loop', (1, 8, 1), [1, 2, 3, 4, 5, 6, 7, 8]),
  ('descending loop with remainder', (15, 1, -3), [15, 12, 9, 6, 3]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -7.5, 1), []),
  ('float limit ceils for descending loops', (10, 2.5, -1), [10, 9, 8, 7, 6, 5, 4, 3]),
  ('init equal to limit runs once', (5, 5, 1), [5]),
  ('descending init equal to limit runs once', (5, 5, -1), [5]),
  ('long loop is capped at 20 values',
   (0, 105, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 10, 0), 'error: for step is zero')]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(init, limit, step):
    if step == 0:
        return 'error: for step is zero'
    if isinstance(init, int) and isinstance(step, int):
        if isinstance(limit, float):
            limit = math.floor(limit)
        if (step > 0 and init > limit) or (step < 0 and init < limit):
            return []
        count = (limit - init) // step if step > 0 else (init - limit) // (-step)
        out = []
        v = init
        for _ in range(min(count, 19) + 1):
            out.append(v)
            v += step
        return out
    out = []
    v = float(init)
    while (v <= limit if step > 0 else v >= limit) and len(out) < 20:
        out.append(v)
        v += step
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: ascending integer loop', (1, 4, 1), [1, 2, 3, 4]),
  ('descending loop with remainder', (11, 1, -3), [11, 8, 5, 2]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -3.5, 1), [-5, -4]),
  ('float limit ceils for descending loops', (6, 2.5, -1), [6, 5, 4, 3]),
  ('init equal to limit runs once', (1, 1, 1), [1]),
  ('descending init equal to limit runs once', (1, 1, -1), [1]),
  ('long loop is capped at 20 values',
   (0, 101, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 6, 0), 'error: for step is zero')],
 [('regression: ascending integer loop', (1, 5, 1), [1, 2, 3, 4, 5]),
  ('descending loop with remainder', (12, 1, -3), [12, 9, 6, 3]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -4.5, 1), [-5]),
  ('float limit ceils for descending loops', (7, 2.5, -1), [7, 6, 5, 4, 3]),
  ('init equal to limit runs once', (2, 2, 1), [2]),
  ('descending init equal to limit runs once', (2, 2, -1), [2]),
  ('long loop is capped at 20 values',
   (0, 102, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 7, 0), 'error: for step is zero')],
 [('regression: ascending integer loop', (1, 6, 1), [1, 2, 3, 4, 5, 6]),
  ('descending loop with remainder', (13, 1, -3), [13, 10, 7, 4, 1]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -5.5, 1), []),
  ('float limit ceils for descending loops', (8, 2.5, -1), [8, 7, 6, 5, 4, 3]),
  ('init equal to limit runs once', (3, 3, 1), [3]),
  ('descending init equal to limit runs once', (3, 3, -1), [3]),
  ('long loop is capped at 20 values',
   (0, 103, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 8, 0), 'error: for step is zero')],
 [('regression: ascending integer loop', (1, 7, 1), [1, 2, 3, 4, 5, 6, 7]),
  ('descending loop with remainder', (14, 1, -3), [14, 11, 8, 5, 2]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -6.5, 1), []),
  ('float limit ceils for descending loops', (9, 2.5, -1), [9, 8, 7, 6, 5, 4, 3]),
  ('init equal to limit runs once', (4, 4, 1), [4]),
  ('descending init equal to limit runs once', (4, 4, -1), [4]),
  ('long loop is capped at 20 values',
   (0, 104, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 9, 0), 'error: for step is zero')],
 [('regression: ascending integer loop', (1, 8, 1), [1, 2, 3, 4, 5, 6, 7, 8]),
  ('descending loop with remainder', (15, 1, -3), [15, 12, 9, 6, 3]),
  ('descending loop with odd step', (7, 0, -2), [7, 5, 3, 1]),
  ('float limit floors for ascending loops', (-5, -7.5, 1), []),
  ('float limit ceils for descending loops', (10, 2.5, -1), [10, 9, 8, 7, 6, 5, 4, 3]),
  ('init equal to limit runs once', (5, 5, 1), [5]),
  ('descending init equal to limit runs once', (5, 5, -1), [5]),
  ('long loop is capped at 20 values',
   (0, 105, 1),
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
  ('float loop reaches its limit exactly', (1.0, 0.0, -0.25), [1.0, 0.75, 0.5, 0.25, 0.0]),
  ('control: zero step', (1, 10, 0), 'error: for step is zero')]]
for label, args, expected in cases[N - 1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
for i = -5, -2.5 runs to -2, one iteration past the limit.

Numeric for-loop preparation (Lua-style FORPREP/FORLOOP). A zero step is an error. With integer init and step, a float limit is floored for positive steps and ceiled for negative ones; the loop is skipped when init is already past the limit in the step direction; otherwise the iteration count (limit - init) // step is precomputed (for negative steps (init - limit) // -step) and the loop runs count + 1 times. Otherwise the loop uses floats and re-tests v <= limit (v >= limit for negative steps). Return at most the first 20 control values.

split: open-access