# Path: utils.py
# def pairwise(iterable, trailing=True):
#     "For sequence of (a, b, c) yield pairs of (a, b), (b, c), (c, None)."
#     prev = Ellipsis
#     for it in iterable:
#         if prev is not Ellipsis:
#             yield (prev, it)
#         prev = it
#     if trailing:
#         yield (it, None)
# 
# def natural_sort_key(s):
#     if not isinstance(s, str):
#         return s
#     arr = re.split("([0-9]+)", s)
#     return [int(x) if x.isdigit() else x for x in arr]
# 
# Path: cfgutils.py
# def swap_if_branches(cfg, node):
#     "Assuming `node` is 'if' node, swap its branches and invert condition."
#     succ = cfg.sorted_succ(node)
#     print(succ, cfg[node, succ[0]])
#     cond = cfg[node, succ[0]]["cond"]
#     cfg[node, succ[0]]["cond"] = None
#     cfg[node, succ[1]]["cond"] = cond.neg()


import sys
import progdb
from utils import pairwise, natural_sort_key
from cfgutils import swap_if_branches


no_dead = False

def find_used_labels(cfg):
    labels = set()
    for addr, nxt in pairwise(cfg.iter_rev_postorder()):
        info = cfg[addr]
        bblock = info["val"]
        succs = cfg.sorted_succ(addr)
        if len(succs) > 1 and nxt == succs[0]:
            swap_if_branches(cfg, addr)
            succs = cfg.sorted_succ(addr)
        for succ in succs:
            cond = cfg.edge(addr, succ).get("cond")
            if not cond and nxt and succ == nxt:
                continue
            labels.add(succ)
    return labels


def print_inst(inst):
    if inst.op == "DEAD" and no_dead:
        return
    return str(inst)

def dump_c(cfg, stream=sys.stdout):
    labels = find_used_labels(cfg)
    func_start = True

# Path: utils.py
# def pairwise(iterable, trailing=True):
#     "For sequence of (a, b, c) yield pairs of (a, b), (b, c), (c, None)."
#     prev = Ellipsis
#     for it in iterable:
#         if prev is not Ellipsis:
#             yield (prev, it)
#         prev = it
#     if trailing:
#         yield (it, None)
# 
# def natural_sort_key(s):
#     if not isinstance(s, str):
#         return s
#     arr = re.split("([0-9]+)", s)
#     return [int(x) if x.isdigit() else x for x in arr]
# 
# Path: cfgutils.py
# def swap_if_branches(cfg, node):
#     "Assuming `node` is 'if' node, swap its branches and invert condition."
#     succ = cfg.sorted_succ(node)
#     print(succ, cfg[node, succ[0]])
#     cond = cfg[node, succ[0]]["cond"]
#     cfg[node, succ[0]]["cond"] = None
#     cfg[node, succ[1]]["cond"] = cond.neg()


# Path: cprinter.py
import sys
import progdb
from utils import pairwise, natural_sort_key
from cfgutils import swap_if_branches



no_dead = False

def find_used_labels(cfg):
    labels = set()
    for addr, nxt in pairwise(cfg.iter_rev_postorder()):
        info = cfg[addr]
        bblock = info["val"]
        succs = cfg.sorted_succ(addr)
        if len(succs) > 1 and nxt == succs[0]:
            swap_if_branches(cfg, addr)
            succs = cfg.sorted_succ(addr)
        for succ in succs:
            cond = cfg.edge(addr, succ).get("cond")
            if not cond and nxt and succ == nxt:
                continue
            labels.add(succ)
    return labels


def print_inst(inst):
    if inst.op == "DEAD" and no_dead:
        return
    return str(inst)

def dump_c(cfg, stream=sys.stdout):
    labels = find_used_labels(cfg)
    func_start = True

    for addr, nxt in pairwise(cfg.iter_rev_postorder()):