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

N = 1
observations = []
def solve(code, locals_, fuel):
    match = {}
    opens = []
    for i, ins in enumerate(code):
        if ins[0] in ('block', 'loop'):
            opens.append(i)
        elif ins[0] == 'end':
            match[opens.pop()] = i
    loc = list(locals_)
    stack = []
    labels = []
    pc = 0
    steps = 0
    try:
        while pc < len(code):
            if steps == fuel:
                return {'status': 'fuel', 'stack': stack, 'locals': loc}
            steps += 1
            ins = code[pc]
            op = ins[0]
            if op in ('block', 'loop'):
                labels.append({'kind': op, 'pc': pc, 'end': match[pc], 'height': len(stack),
                               'arity': ins[1] if op == 'block' else 0})
                pc += 1
            elif op == 'end':
                labels.pop()
                pc += 1
            elif op in ('br', 'br_if'):
                if op == 'br_if' and stack.pop() == 0:
                    pc += 1
                    continue
                target = labels[len(labels) - 1 - ins[1]]
                keep = []
                del stack[target['height']:]
                stack.extend(keep)
                if target['kind'] == 'loop':
                    del labels[len(labels) - ins[1]:]
                    pc = target['pc'] + 1
                else:
                    del labels[len(labels) - 1 - ins[1]:]
                    pc = target['end'] + 1
            elif op == 'const':
                stack.append(ins[1])
                pc += 1
            elif op in ('add', 'sub'):
                b = stack.pop()
                a = stack.pop()
                stack.append(a + b if op == 'add' else a - b)
                pc += 1
            elif op == 'eqz':
                stack.append(1 if stack.pop() == 0 else 0)
                pc += 1
            elif op == 'get':
                stack.append(loc[ins[1]])
                pc += 1
            elif op == 'set':
                loc[ins[1]] = stack.pop()
                pc += 1
            else:
                stack.pop()
                pc += 1
    except IndexError:
        return {'status': 'trap', 'stack': stack, 'locals': loc}
    return {'status': 'done', 'stack': stack, 'locals': loc}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [2, 0],
    500),
   {'locals': [0, 3], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [2, 0],
    500),
   {'locals': [0, 4], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [4, 0],
    900),
   {'locals': [0, 8], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 41], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [41, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 1], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 10], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [13], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 1], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    21),
   {'locals': [49, 99], 'stack': [], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [3, 0],
    500),
   {'locals': [0, 6], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [3, 0],
    500),
   {'locals': [0, 6], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [5, 0],
    900),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 42], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [42, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 2], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 20], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [23], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 2], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    22),
   {'locals': [49, 99], 'stack': [49], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [4, 0],
    500),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [4, 0],
    500),
   {'locals': [0, 8], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [6, 0],
    900),
   {'locals': [0, 12], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 43], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [43, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 3], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 30], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [33], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 3], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    23),
   {'locals': [49, 99], 'stack': [49, 1], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [5, 0],
    500),
   {'locals': [0, 15], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [5, 0],
    500),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [7, 0],
    900),
   {'locals': [0, 14], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 44], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [44, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 4], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 40], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [43], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 4], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    24),
   {'locals': [49, 99], 'stack': [48], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [6, 0],
    500),
   {'locals': [0, 21], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [6, 0],
    500),
   {'locals': [0, 12], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [8, 0],
    900),
   {'locals': [0, 16], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 45], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [45, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 5], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 50], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [53], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 5], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    25),
   {'locals': [48, 99], 'stack': [], 'status': 'fuel'})]]
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, locals_, fuel):
    match = {}
    opens = []
    for i, ins in enumerate(code):
        if ins[0] in ('block', 'loop'):
            opens.append(i)
        elif ins[0] == 'end':
            match[opens.pop()] = i
    loc = list(locals_)
    stack = []
    labels = []
    pc = 0
    steps = 0
    try:
        while pc < len(code):
            if steps == fuel:
                return {'status': 'fuel', 'stack': stack, 'locals': loc}
            steps += 1
            ins = code[pc]
            op = ins[0]
            if op in ('block', 'loop'):
                labels.append({'kind': op, 'pc': pc, 'end': match[pc], 'height': len(stack),
                               'arity': ins[1] if op == 'block' else 0})
                pc += 1
            elif op == 'end':
                labels.pop()
                pc += 1
            elif op in ('br', 'br_if'):
                if op == 'br_if' and stack.pop() == 0:
                    pc += 1
                    continue
                target = labels[len(labels) - 1 - ins[1]]
                keep = stack[-1:]
                del stack[target['height']:]
                stack.extend(keep)
                if target['kind'] == 'loop':
                    del labels[len(labels) - ins[1]:]
                    pc = target['pc'] + 1
                else:
                    del labels[len(labels) - 1 - ins[1]:]
                    pc = target['end'] + 1
            elif op == 'const':
                stack.append(ins[1])
                pc += 1
            elif op in ('add', 'sub'):
                b = stack.pop()
                a = stack.pop()
                stack.append(a + b if op == 'add' else a - b)
                pc += 1
            elif op == 'eqz':
                stack.append(1 if stack.pop() == 0 else 0)
                pc += 1
            elif op == 'get':
                stack.append(loc[ins[1]])
                pc += 1
            elif op == 'set':
                loc[ins[1]] = stack.pop()
                pc += 1
            else:
                stack.pop()
                pc += 1
    except IndexError:
        return {'status': 'trap', 'stack': stack, 'locals': loc}
    return {'status': 'done', 'stack': stack, 'locals': loc}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [2, 0],
    500),
   {'locals': [0, 3], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [2, 0],
    500),
   {'locals': [0, 4], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [4, 0],
    900),
   {'locals': [0, 8], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 41], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [41, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 1], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 10], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [13], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 1], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    21),
   {'locals': [49, 99], 'stack': [], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [3, 0],
    500),
   {'locals': [0, 6], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [3, 0],
    500),
   {'locals': [0, 6], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [5, 0],
    900),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 42], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [42, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 2], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 20], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [23], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 2], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    22),
   {'locals': [49, 99], 'stack': [49], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [4, 0],
    500),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [4, 0],
    500),
   {'locals': [0, 8], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [6, 0],
    900),
   {'locals': [0, 12], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 43], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [43, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 3], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 30], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [33], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 3], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    23),
   {'locals': [49, 99], 'stack': [49, 1], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [5, 0],
    500),
   {'locals': [0, 15], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [5, 0],
    500),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [7, 0],
    900),
   {'locals': [0, 14], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 44], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [44, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 4], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 40], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [43], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 4], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    24),
   {'locals': [49, 99], 'stack': [48], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [6, 0],
    500),
   {'locals': [0, 21], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [6, 0],
    500),
   {'locals': [0, 12], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [8, 0],
    900),
   {'locals': [0, 16], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 45], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [45, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 5], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 50], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [53], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 5], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    25),
   {'locals': [48, 99], 'stack': [], 'status': 'fuel'})]]
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:
Blocks that produce a value via br end with an empty stack.

WebAssembly-style structured control over a value stack. block k / loop / end nest; each block or loop pushes a label recording its operand height and arity (block: k result values, loop: 0). br d targets the d-th enclosing label counting outward from 0; the branch keeps the top arity values, discards operands down to the label height, then for a block pops that label and continues after its end, for a loop keeps the loop label and continues at the first instruction inside the loop. br_if pops a condition and branches when it is non-zero. Other ops: const v, add, sub, eqz, get k, set k, drop. Each instruction costs one fuel. Return status (done/fuel/trap), stack and locals.

split: open-access


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

N = 1
observations = []
def solve(code, locals_, fuel):
    match = {}
    opens = []
    for i, ins in enumerate(code):
        if ins[0] in ('block', 'loop'):
            opens.append(i)
        elif ins[0] == 'end':
            match[opens.pop()] = i
    loc = list(locals_)
    stack = []
    labels = []
    pc = 0
    steps = 0
    try:
        while pc < len(code):
            if steps == fuel:
                return {'status': 'fuel', 'stack': stack, 'locals': loc}
            steps += 1
            ins = code[pc]
            op = ins[0]
            if op in ('block', 'loop'):
                labels.append({'kind': op, 'pc': pc, 'end': match[pc], 'height': len(stack),
                               'arity': ins[1] if op == 'block' else 0})
                pc += 1
            elif op == 'end':
                labels.pop()
                pc += 1
            elif op in ('br', 'br_if'):
                if op == 'br_if' and stack.pop() == 0:
                    pc += 1
                    continue
                target = labels[len(labels) - 1 - ins[1]]
                keep = stack[len(stack) - target['arity']:] if target['arity'] else []
                del stack[len(stack) - target['arity']:]
                stack.extend(keep)
                if target['kind'] == 'loop':
                    del labels[len(labels) - ins[1]:]
                    pc = target['pc'] + 1
                else:
                    del labels[len(labels) - 1 - ins[1]:]
                    pc = target['end'] + 1
            elif op == 'const':
                stack.append(ins[1])
                pc += 1
            elif op in ('add', 'sub'):
                b = stack.pop()
                a = stack.pop()
                stack.append(a + b if op == 'add' else a - b)
                pc += 1
            elif op == 'eqz':
                stack.append(1 if stack.pop() == 0 else 0)
                pc += 1
            elif op == 'get':
                stack.append(loc[ins[1]])
                pc += 1
            elif op == 'set':
                loc[ins[1]] = stack.pop()
                pc += 1
            else:
                stack.pop()
                pc += 1
    except IndexError:
        return {'status': 'trap', 'stack': stack, 'locals': loc}
    return {'status': 'done', 'stack': stack, 'locals': loc}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [2, 0],
    500),
   {'locals': [0, 3], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [2, 0],
    500),
   {'locals': [0, 4], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [4, 0],
    900),
   {'locals': [0, 8], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 41], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [41, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 1], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 10], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [13], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 1], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    21),
   {'locals': [49, 99], 'stack': [], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [3, 0],
    500),
   {'locals': [0, 6], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [3, 0],
    500),
   {'locals': [0, 6], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [5, 0],
    900),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 42], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [42, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 2], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 20], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [23], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 2], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    22),
   {'locals': [49, 99], 'stack': [49], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [4, 0],
    500),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [4, 0],
    500),
   {'locals': [0, 8], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [6, 0],
    900),
   {'locals': [0, 12], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 43], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [43, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 3], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 30], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [33], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 3], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    23),
   {'locals': [49, 99], 'stack': [49, 1], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [5, 0],
    500),
   {'locals': [0, 15], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [5, 0],
    500),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [7, 0],
    900),
   {'locals': [0, 14], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 44], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [44, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 4], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 40], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [43], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 4], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    24),
   {'locals': [49, 99], 'stack': [48], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [6, 0],
    500),
   {'locals': [0, 21], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [6, 0],
    500),
   {'locals': [0, 12], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [8, 0],
    900),
   {'locals': [0, 16], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 45], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [45, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 5], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 50], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [53], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 5], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    25),
   {'locals': [48, 99], 'stack': [], 'status': 'fuel'})]]
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, locals_, fuel):
    match = {}
    opens = []
    for i, ins in enumerate(code):
        if ins[0] in ('block', 'loop'):
            opens.append(i)
        elif ins[0] == 'end':
            match[opens.pop()] = i
    loc = list(locals_)
    stack = []
    labels = []
    pc = 0
    steps = 0
    try:
        while pc < len(code):
            if steps == fuel:
                return {'status': 'fuel', 'stack': stack, 'locals': loc}
            steps += 1
            ins = code[pc]
            op = ins[0]
            if op in ('block', 'loop'):
                labels.append({'kind': op, 'pc': pc, 'end': match[pc], 'height': len(stack),
                               'arity': ins[1] if op == 'block' else 0})
                pc += 1
            elif op == 'end':
                labels.pop()
                pc += 1
            elif op in ('br', 'br_if'):
                if op == 'br_if' and stack.pop() == 0:
                    pc += 1
                    continue
                target = labels[len(labels) - 1 - ins[1]]
                keep = stack[len(stack) - target['arity']:] if target['arity'] else []
                del stack[:target['height']]
                stack.extend(keep)
                if target['kind'] == 'loop':
                    del labels[len(labels) - ins[1]:]
                    pc = target['pc'] + 1
                else:
                    del labels[len(labels) - 1 - ins[1]:]
                    pc = target['end'] + 1
            elif op == 'const':
                stack.append(ins[1])
                pc += 1
            elif op in ('add', 'sub'):
                b = stack.pop()
                a = stack.pop()
                stack.append(a + b if op == 'add' else a - b)
                pc += 1
            elif op == 'eqz':
                stack.append(1 if stack.pop() == 0 else 0)
                pc += 1
            elif op == 'get':
                stack.append(loc[ins[1]])
                pc += 1
            elif op == 'set':
                loc[ins[1]] = stack.pop()
                pc += 1
            else:
                stack.pop()
                pc += 1
    except IndexError:
        return {'status': 'trap', 'stack': stack, 'locals': loc}
    return {'status': 'done', 'stack': stack, 'locals': loc}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [2, 0],
    500),
   {'locals': [0, 3], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [2, 0],
    500),
   {'locals': [0, 4], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [4, 0],
    900),
   {'locals': [0, 8], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 41], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [41, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 1], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 10], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [13], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 1], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    21),
   {'locals': [49, 99], 'stack': [], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [3, 0],
    500),
   {'locals': [0, 6], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [3, 0],
    500),
   {'locals': [0, 6], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [5, 0],
    900),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 42], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [42, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 2], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 20], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [23], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 2], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    22),
   {'locals': [49, 99], 'stack': [49], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [4, 0],
    500),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [4, 0],
    500),
   {'locals': [0, 8], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [6, 0],
    900),
   {'locals': [0, 12], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 43], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [43, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 3], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 30], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [33], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 3], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    23),
   {'locals': [49, 99], 'stack': [49, 1], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [5, 0],
    500),
   {'locals': [0, 15], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [5, 0],
    500),
   {'locals': [0, 10], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [7, 0],
    900),
   {'locals': [0, 14], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 44], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [44, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 4], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 40], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [43], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 4], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    24),
   {'locals': [49, 99], 'stack': [48], 'status': 'fuel'})],
 [('regression: counted loop exits through outer block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [6, 0],
    500),
   {'locals': [0, 21], 'stack': [], 'status': 'done'}),
  ('continue from inside a nested block',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [6, 0],
    500),
   {'locals': [0, 12], 'stack': [], 'status': 'done'}),
  ('continue loop with more iterations',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['block', 0],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['get', 1],
     ['const', 2],
     ['add'],
     ['set', 1],
     ['br', 1],
     ['end'],
     ['const', 999],
     ['set', 1],
     ['end'],
     ['end']],
    [8, 0],
    900),
   {'locals': [0, 16], 'stack': [], 'status': 'done'}),
  ('block result keeps one value above outer operand',
   ([['const', 45], ['block', 1], ['const', 5], ['const', 7], ['br', 0], ['const', 99], ['end']], [], 50),
   {'locals': [], 'stack': [45, 7], 'status': 'done'}),
  ('void block branch discards operands',
   ([['block', 0], ['const', 1], ['const', 5], ['br', 0], ['end'], ['const', 3]], [], 50),
   {'locals': [], 'stack': [3], 'status': 'done'}),
  ('untaken br_if consumes its condition',
   ([['const', 50], ['block', 0], ['const', 0], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [53], 'status': 'done'}),
  ('taken br_if exits the block',
   ([['const', 10], ['block', 0], ['const', 5], ['br_if', 0], ['const', 3], ['add'], ['end']], [], 50),
   {'locals': [], 'stack': [10], 'status': 'done'}),
  ('control: fuel exhaustion',
   ([['block', 0],
     ['loop', 0],
     ['get', 0],
     ['eqz'],
     ['br_if', 1],
     ['get', 1],
     ['get', 0],
     ['add'],
     ['set', 1],
     ['get', 0],
     ['const', 1],
     ['sub'],
     ['set', 0],
     ['br', 0],
     ['end'],
     ['end']],
    [50, 0],
    25),
   {'locals': [48, 99], 'stack': [], 'status': 'fuel'})]]
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:
Intermediate operands pushed inside a block survive a branch out of it.

WebAssembly-style structured control over a value stack. block k / loop / end nest; each block or loop pushes a label recording its operand height and arity (block: k result values, loop: 0). br d targets the d-th enclosing label counting outward from 0; the branch keeps the top arity values, discards operands down to the label height, then for a block pops that label and continues after its end, for a loop keeps the loop label and continues at the first instruction inside the loop. br_if pops a condition and branches when it is non-zero. Other ops: const v, add, sub, eqz, get k, set k, drop. Each instruction costs one fuel. Return status (done/fuel/trap), stack and locals.

split: open-access


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

N = 1
observations = []
def solve(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4 if ins[0] == 'jz' else 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - addrs[i]
                if off < -128 or off > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds > len(prog):
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + size(i, ins))])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
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(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4 if ins[0] == 'jz' else 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 1)
                if off < -128 or off > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds > len(prog):
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + size(i, ins))])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
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:
Forward branches near the limit stay short and overflow; backward ones are widened needlessly.

Assemble ["label", name], ["data", nbytes], ["jmp", label], ["jz", label]. Branches start in the short form (2 bytes, signed 8-bit displacement from the end of the instruction, valid -128..127). A short branch whose displacement does not fit is widened (jmp.w 3 bytes, jz.w 4 bytes) and addresses are recomputed until no branch changes (branches never shrink). Report duplicate or undefined labels; otherwise return each branch as [address, mnemonic, displacement from its own end] and the total size.

split: open-access


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

N = 1
observations = []
def solve(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4 if ins[0] == 'jz' else 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 2)
                if off < -128 or off > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds > len(prog):
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + 2)])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
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(prog):
    labels_seen = set()
    for ins in prog:
        if ins[0] == 'label':
            if ins[1] in labels_seen:
                return ['duplicate-label', ins[1]]
            labels_seen.add(ins[1])
    for ins in prog:
        if ins[0] in ('jmp', 'jz') and ins[1] not in labels_seen:
            return ['undefined-label', ins[1]]
    long_form = set()
    def size(i, ins):
        if i in long_form:
            return 4 if ins[0] == 'jz' else 3
        return 2
    rounds = 0
    while True:
        rounds += 1
        addr = 0
        addrs = []
        where = {}
        for i, ins in enumerate(prog):
            addrs.append(addr)
            if ins[0] == 'label':
                where[ins[1]] = addr
            elif ins[0] == 'data':
                addr += ins[1]
            else:
                addr += size(i, ins)
        changed = False
        for i, ins in enumerate(prog):
            if ins[0] in ('jmp', 'jz') and i not in long_form:
                off = where[ins[1]] - (addrs[i] + 2)
                if off < -128 or off > 127:
                    long_form.add(i)
                    changed = True
        if not changed or rounds > len(prog):
            break
    out = []
    for i, ins in enumerate(prog):
        if ins[0] in ('jmp', 'jz'):
            suffix = '.w' if i in long_form else ''
            out.append([addrs[i], ins[0] + suffix, where[ins[1]] - (addrs[i] + 3)])
    return {'branches': out, 'size': addr}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: three-step widening cascade',
   ([['data', 1],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[1, 'jmp.w', 129], [14, 'jz.w', 128], [133, 'jmp.w', 128]], 'size': 265}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 1], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [129, 'jmp.w', -132]], 'size': 132}),
  ('forward branch just around the short limit',
   ([['data', 1], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[1, 'jmp', 127], [130, 'jz.w', 128]], 'size': 262}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 201], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 201], [205, 'jmp', -2]], 'size': 207}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 1], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [3, 'jmp', -3]], 'size': 5}),
  ('duplicate label', ([['label', 'a'], ['data', 1], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 1]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 2],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[2, 'jmp.w', 129], [15, 'jz.w', 128], [134, 'jmp.w', 128]], 'size': 266}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 2], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [130, 'jmp.w', -133]], 'size': 133}),
  ('forward branch just around the short limit',
   ([['data', 2], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[2, 'jmp', 127], [131, 'jz.w', 128]], 'size': 263}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 202], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 202], [206, 'jmp', -2]], 'size': 208}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 2], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [4, 'jmp', -4]], 'size': 6}),
  ('duplicate label', ([['label', 'a'], ['data', 2], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 2]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 3],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[3, 'jmp.w', 129], [16, 'jz.w', 128], [135, 'jmp.w', 128]], 'size': 267}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 3], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [131, 'jmp.w', -134]], 'size': 134}),
  ('forward branch just around the short limit',
   ([['data', 3], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[3, 'jmp', 127], [132, 'jz.w', 128]], 'size': 264}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 203], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 203], [207, 'jmp', -2]], 'size': 209}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 3], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [5, 'jmp', -5]], 'size': 7}),
  ('duplicate label', ([['label', 'a'], ['data', 3], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 3]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 4],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[4, 'jmp.w', 129], [17, 'jz.w', 128], [136, 'jmp.w', 128]], 'size': 268}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 4], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [132, 'jmp.w', -135]], 'size': 135}),
  ('forward branch just around the short limit',
   ([['data', 4], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[4, 'jmp', 127], [133, 'jz.w', 128]], 'size': 265}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 204], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 204], [208, 'jmp', -2]], 'size': 210}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 4], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [6, 'jmp', -6]], 'size': 8}),
  ('duplicate label', ([['label', 'a'], ['data', 4], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 4]],), ['undefined-label', 'nowhere'])],
 [('regression: three-step widening cascade',
   ([['data', 5],
     ['jmp', 'A'],
     ['data', 10],
     ['jz', 'B'],
     ['data', 115],
     ['label', 'A'],
     ['jmp', 'C'],
     ['data', 10],
     ['label', 'B'],
     ['data', 118],
     ['label', 'C'],
     ['data', 1]],),
   {'branches': [[5, 'jmp.w', 129], [18, 'jz.w', 128], [137, 'jmp.w', 128]], 'size': 269}),
  ('backward branch exactly -128 stays short',
   ([['label', 'top'], ['data', 126], ['jz', 'top'], ['data', 5], ['jmp', 'top']],),
   {'branches': [[126, 'jz', -128], [133, 'jmp.w', -136]], 'size': 136}),
  ('forward branch just around the short limit',
   ([['data', 5], ['jmp', 'e'], ['data', 127], ['label', 'e'], ['jz', 'f'], ['data', 128], ['label', 'f']],),
   {'branches': [[5, 'jmp', 127], [134, 'jz.w', 128]], 'size': 266}),
  ('wide conditional branch is four bytes',
   ([['jz', 'x'], ['data', 205], ['label', 'x'], ['jmp', 'x']],),
   {'branches': [[0, 'jz.w', 205], [209, 'jmp', -2]], 'size': 211}),
  ('short branches to adjacent labels',
   ([['jz', 'a'], ['label', 'a'], ['data', 5], ['jmp', 'a']],),
   {'branches': [[0, 'jz', 0], [7, 'jmp', -7]], 'size': 9}),
  ('duplicate label', ([['label', 'a'], ['data', 5], ['label', 'a']],), ['duplicate-label', 'a']),
  ('undefined label', ([['jmp', 'nowhere'], ['data', 5]],), ['undefined-label', 'nowhere'])]]
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:
Every widened branch lands one or two bytes past its label.

Assemble ["label", name], ["data", nbytes], ["jmp", label], ["jz", label]. Branches start in the short form (2 bytes, signed 8-bit displacement from the end of the instruction, valid -128..127). A short branch whose displacement does not fit is widened (jmp.w 3 bytes, jz.w 4 bytes) and addresses are recomputed until no branch changes (branches never shrink). Report duplicate or undefined labels; otherwise return each branch as [address, mnemonic, displacement from its own end] and the total size.

split: open-access