# Path: core.py
# class BBlock:
#     def __init__(self, addr):
#         self.addr = addr
#         self.items = []
#         self.props = {}
# 
#     def add(self, s):
#         self.items.append(s)
# 
#     def __getitem__(self, i):
#         return self.items[i]
# 
#     def def_addrs(self, regs_only=True):
#         """Return all variable definitions for this basic block,
#         as set of (var, inst_addr) pairs. Note that this includes
#         multiple entries for the same var, if it is redefined
#         multiple times within the basic block.
#         """
#         defs = set()
#         for i in self.items:
#             inst_defs = i.defs(regs_only)
#             for d in inst_defs:
#                 defs.add((d, i.addr))
#         return defs
# 
#     def defs(self, regs_only=True):
#         """Return set of all variables defined in this basic block."""
#         defs = set()
#         for i in self.items:
#             defs |= i.defs(regs_only)
#         return defs
# 
#     def uses(self):
#         """Return set of all variables used in this basic block."""
#         uses = set()
#         for i in self.items:
#             uses |= i.uses()
#         return uses
# 
#     def __repr__(self):
#         return "%s(%s)" % (self.__class__.__name__, self.addr)
# 
#     def write(self, stream, indent, s):
#         for l in str(s).splitlines():
#             stream.write("  " * indent)
#             stream.write(l + "\n")
# 
#     def dump(self, stream, indent=0, printer=str):
#         for s in self.items:
#             out = printer(s)
#             if out is not None:
#                 self.write(stream, indent, out)
# 
# class CFGPrinter:
#     """Print BBlocks in a CFG. Various printing params can be overriden
#     via methods."""
# 
#     header_reg_prefix = "$"
#     addr_in_header = False
# 
#     def __init__(self, cfg, stream=sys.stdout):
#         self.cfg = cfg
#         self.stream = stream
#         # Current bblock addr
#         self.addr = 0
#         # Current CFG node properties
#         self.node_props = None
#         # Current BBlock
#         self.bblock = None
#         # Current BBlock properties
#         self.bblock_props = None
#         self.inst_printer = str
#         self.no_dead = False
# 
#     def bblock_order(self):
#         "Return iterator over bblocks to be printed."
#         return self.cfg.iter_sorted_nodes()
# 
#     def print_graph_header(self):
#         if self.cfg.props:
#             print("// Graph props:", file=self.stream)
#             for k in sorted(self.cfg.props.keys()):
#                 v = self.cfg.props[k]
#                 v = utils.repr_stable(v)
#                 print("//  %s: %s" % (k, v), file=self.stream)
#             print(file=self.stream)
# 
# 
#     def print_header(self):
#         if self.addr_in_header:
#             print("// %s" % self.addr, file=self.stream)
#         print("// Predecessors: %s" % sorted(self.cfg.pred(self.addr)), file=self.stream)
# 
#         if self.node_props:
#             print("// Node props:", file=self.stream)
#             for k in sorted(self.node_props.keys()):
#                 v = self.node_props[k]
#                 v = utils.repr_stable(v)
#                 v = v.replace("$", self.header_reg_prefix)
#                 print("//  %s: %s" % (k, v), file=self.stream)
# 
#         if self.bblock_props:
#             print("// BBlock props:", file=self.stream)
# 
#             for k in sorted(self.bblock_props.keys()):
#                 v = self.bblock_props[k]
#                 if k.startswith("state_"):
#                     v = repr_state(v)
#                 else:
#                     v = utils.repr_stable(v)
#                 v = v.replace("$", self.header_reg_prefix)
#                 print("//  %s: %s" % (k, v), file=self.stream)
# 
# 
#     def print_trailer(self):
#         succ = self.cfg.succ(self.addr)
#         exits = [(self.cfg.edge(self.addr, x).get("cond"), x) for x in succ]
#         print("Exits:", sorted(exits, key=lambda x: utils.natural_sort_key(str(x))), file=self.stream)
# 
# 
#     def print_label(self):
#         print("%s:" % self.addr, file=self.stream)
# 
#     def print_inst(self, inst):
#         if inst.op == "DEAD" and self.no_dead:
#             return None
#         return self.inst_printer(inst)
# 
#     def print_separator(self):
#         self.stream.write("\n")
# 
#     def print(self):
#         self.print_graph_header()
#         cnt = 0
#         for self.addr, info in self.bblock_order():
#             self.node_props = info.copy()
#             self.bblock = self.node_props.pop("val")
#             self.bblock_props = self.bblock.props
#             if cnt > 0:
#                 self.print_separator()
#             self.print_header()
#             self.print_label()
#             if self.bblock is not None:
#                 self.bblock.dump(self.stream, 0, self.print_inst)
#             else:
#                 print("   ", self.bblock, file=self.stream)
#             self.print_trailer()
#             cnt += 1


from core import BBlock, CFGPrinter


def dump(cfg):
    CFGPrinter(cfg).print()


def save_cfg(cfg, suffix):
    with open(cfg.filename + suffix, "w") as out:

# Path: core.py
# class BBlock:
#     def __init__(self, addr):
#         self.addr = addr
#         self.items = []
#         self.props = {}
# 
#     def add(self, s):
#         self.items.append(s)
# 
#     def __getitem__(self, i):
#         return self.items[i]
# 
#     def def_addrs(self, regs_only=True):
#         """Return all variable definitions for this basic block,
#         as set of (var, inst_addr) pairs. Note that this includes
#         multiple entries for the same var, if it is redefined
#         multiple times within the basic block.
#         """
#         defs = set()
#         for i in self.items:
#             inst_defs = i.defs(regs_only)
#             for d in inst_defs:
#                 defs.add((d, i.addr))
#         return defs
# 
#     def defs(self, regs_only=True):
#         """Return set of all variables defined in this basic block."""
#         defs = set()
#         for i in self.items:
#             defs |= i.defs(regs_only)
#         return defs
# 
#     def uses(self):
#         """Return set of all variables used in this basic block."""
#         uses = set()
#         for i in self.items:
#             uses |= i.uses()
#         return uses
# 
#     def __repr__(self):
#         return "%s(%s)" % (self.__class__.__name__, self.addr)
# 
#     def write(self, stream, indent, s):
#         for l in str(s).splitlines():
#             stream.write("  " * indent)
#             stream.write(l + "\n")
# 
#     def dump(self, stream, indent=0, printer=str):
#         for s in self.items:
#             out = printer(s)
#             if out is not None:
#                 self.write(stream, indent, out)
# 
# class CFGPrinter:
#     """Print BBlocks in a CFG. Various printing params can be overriden
#     via methods."""
# 
#     header_reg_prefix = "$"
#     addr_in_header = False
# 
#     def __init__(self, cfg, stream=sys.stdout):
#         self.cfg = cfg
#         self.stream = stream
#         # Current bblock addr
#         self.addr = 0
#         # Current CFG node properties
#         self.node_props = None
#         # Current BBlock
#         self.bblock = None
#         # Current BBlock properties
#         self.bblock_props = None
#         self.inst_printer = str
#         self.no_dead = False
# 
#     def bblock_order(self):
#         "Return iterator over bblocks to be printed."
#         return self.cfg.iter_sorted_nodes()
# 
#     def print_graph_header(self):
#         if self.cfg.props:
#             print("// Graph props:", file=self.stream)
#             for k in sorted(self.cfg.props.keys()):
#                 v = self.cfg.props[k]
#                 v = utils.repr_stable(v)
#                 print("//  %s: %s" % (k, v), file=self.stream)
#             print(file=self.stream)
# 
# 
#     def print_header(self):
#         if self.addr_in_header:
#             print("// %s" % self.addr, file=self.stream)
#         print("// Predecessors: %s" % sorted(self.cfg.pred(self.addr)), file=self.stream)
# 
#         if self.node_props:
#             print("// Node props:", file=self.stream)
#             for k in sorted(self.node_props.keys()):
#                 v = self.node_props[k]
#                 v = utils.repr_stable(v)
#                 v = v.replace("$", self.header_reg_prefix)
#                 print("//  %s: %s" % (k, v), file=self.stream)
# 
#         if self.bblock_props:
#             print("// BBlock props:", file=self.stream)
# 
#             for k in sorted(self.bblock_props.keys()):
#                 v = self.bblock_props[k]
#                 if k.startswith("state_"):
#                     v = repr_state(v)
#                 else:
#                     v = utils.repr_stable(v)
#                 v = v.replace("$", self.header_reg_prefix)
#                 print("//  %s: %s" % (k, v), file=self.stream)
# 
# 
#     def print_trailer(self):
#         succ = self.cfg.succ(self.addr)
#         exits = [(self.cfg.edge(self.addr, x).get("cond"), x) for x in succ]
#         print("Exits:", sorted(exits, key=lambda x: utils.natural_sort_key(str(x))), file=self.stream)
# 
# 
#     def print_label(self):
#         print("%s:" % self.addr, file=self.stream)
# 
#     def print_inst(self, inst):
#         if inst.op == "DEAD" and self.no_dead:
#             return None
#         return self.inst_printer(inst)
# 
#     def print_separator(self):
#         self.stream.write("\n")
# 
#     def print(self):
#         self.print_graph_header()
#         cnt = 0
#         for self.addr, info in self.bblock_order():
#             self.node_props = info.copy()
#             self.bblock = self.node_props.pop("val")
#             self.bblock_props = self.bblock.props
#             if cnt > 0:
#                 self.print_separator()
#             self.print_header()
#             self.print_label()
#             if self.bblock is not None:
#                 self.bblock.dump(self.stream, 0, self.print_inst)
#             else:
#                 print("   ", self.bblock, file=self.stream)
#             self.print_trailer()
#             cnt += 1


# Path: cfgutils.py
from core import BBlock, CFGPrinter



def dump(cfg):
    CFGPrinter(cfg).print()


def save_cfg(cfg, suffix):
    with open(cfg.filename + suffix, "w") as out:

        p = CFGPrinter(cfg, out)