# Path: asmprinter.py
# class AsmPrinter(CFGPrinter):
# 
#     def __init__(self, cfg, stream=sys.stdout):
#         super().__init__(cfg, stream)
#         self.addr_width = 8
#         self.inst_indent = 4
#         self.no_dead = False
# 
#         self.inst_printer = self.print_with_addr
#         self.referenced_labels = set()
#         self.get_jump_labels()
# 
#     def format_addr(self, addr, extra=0):
#         return addr.ljust(self.addr_width + extra)
# 
#     def get_jump_labels(self):
#         def collect_labels(inst):
#             assert isinstance(inst, Inst), "Perhaps, this is structured CFG? Use --format=c for output."
#             addr = inst.jump_addr()
#             if isinstance(addr, list):
#                 self.referenced_labels.update(addr)
#             elif addr is not None:
#                 self.referenced_labels.add(addr)
#         foreach_inst(self.cfg, collect_labels)
# 
#     def print_with_addr(self, inst):
#         if inst.op == "DEAD" and self.no_dead:
#             return
#         addr = inst.addr
#         if addr is None:
#             addr = "?"
#         return self.format_addr(addr, self.inst_indent) + " " + str(inst)
# 
#     def resolve_label(self, addr):
#         return self.cfg.parser.label_from_addr(addr)
# 
#     def print_label(self):
#         # If there was symbolic label originally, print it
#         # otherwise, skip it if it's not referenced by insts
#         # (like bblock start labels).
#         label = self.cfg.parser.label_from_addr(self.addr)
#         if label == self.addr:
#             if self.addr not in self.referenced_labels:
#                 return
#         print("%s %s:" % (self.format_addr(self.addr), label), file=self.stream)
# 
#     def print_graph_header(self):
#         pass
#     def print_header(self):
#         pass
#     def print_separator(self):
#         pass
#     def print_trailer(self):
#         pass
# 
#     def print(self):
#         core.ADDR.resolver = self.resolve_label
#         super().print()


import sys
import argparse
import os.path
import glob
import logging
import yaml
import yamlutils
import core
import dot
import cprinter
import progdb
import bindata
import copy
        import dot
import arch
from parser import *
from dataflow import *
from xform import *
from xform_utils import *
from decomp import *
from asmprinter import AsmPrinter
        dot.show_insts = True

    return args


def handle_file(args):
    try:
        handle_file_unprotected(args)
    except Exception as e:
        print("Error while processing file: " + args.file)
        raise e


def handle_file_unprotected(args):
    p = Parser(args.file)
    cfg = p.parse()
    cfg.parser = p

    # If we want to get asm back, i.e. stay close to the input, don't remove
    # trailing jumps. This will work OK for data flow algos, but will produce
    # broken or confusing output for control flow algos (for which asm output
    # shouldn't be used of course).
    # Update: it's unsafe to use this during dataflow analysis
    #if args.format != "asm":
    #    foreach_bblock(cfg, remove_trailing_jumps)

    if args.debug:
        with open(args.file + ".0.bb", "w") as f:
            dump_bblocks(cfg, f, no_graph_header=args.no_graph_header)
        with open(args.file + ".0.dot", "w") as f:

# Path: asmprinter.py
# class AsmPrinter(CFGPrinter):
# 
#     def __init__(self, cfg, stream=sys.stdout):
#         super().__init__(cfg, stream)
#         self.addr_width = 8
#         self.inst_indent = 4
#         self.no_dead = False
# 
#         self.inst_printer = self.print_with_addr
#         self.referenced_labels = set()
#         self.get_jump_labels()
# 
#     def format_addr(self, addr, extra=0):
#         return addr.ljust(self.addr_width + extra)
# 
#     def get_jump_labels(self):
#         def collect_labels(inst):
#             assert isinstance(inst, Inst), "Perhaps, this is structured CFG? Use --format=c for output."
#             addr = inst.jump_addr()
#             if isinstance(addr, list):
#                 self.referenced_labels.update(addr)
#             elif addr is not None:
#                 self.referenced_labels.add(addr)
#         foreach_inst(self.cfg, collect_labels)
# 
#     def print_with_addr(self, inst):
#         if inst.op == "DEAD" and self.no_dead:
#             return
#         addr = inst.addr
#         if addr is None:
#             addr = "?"
#         return self.format_addr(addr, self.inst_indent) + " " + str(inst)
# 
#     def resolve_label(self, addr):
#         return self.cfg.parser.label_from_addr(addr)
# 
#     def print_label(self):
#         # If there was symbolic label originally, print it
#         # otherwise, skip it if it's not referenced by insts
#         # (like bblock start labels).
#         label = self.cfg.parser.label_from_addr(self.addr)
#         if label == self.addr:
#             if self.addr not in self.referenced_labels:
#                 return
#         print("%s %s:" % (self.format_addr(self.addr), label), file=self.stream)
# 
#     def print_graph_header(self):
#         pass
#     def print_header(self):
#         pass
#     def print_separator(self):
#         pass
#     def print_trailer(self):
#         pass
# 
#     def print(self):
#         core.ADDR.resolver = self.resolve_label
#         super().print()


# Path: apply_xform.py
import sys
import argparse
import os.path
import glob
import logging
import yaml
import yamlutils
import core
import dot
import cprinter
import progdb
import bindata
import copy
        import dot
import arch
from parser import *
from dataflow import *
from xform import *
from xform_utils import *
from decomp import *
from asmprinter import AsmPrinter

        dot.show_insts = True

    return args


def handle_file(args):
    try:
        handle_file_unprotected(args)
    except Exception as e:
        print("Error while processing file: " + args.file)
        raise e


def handle_file_unprotected(args):
    p = Parser(args.file)
    cfg = p.parse()
    cfg.parser = p

    # If we want to get asm back, i.e. stay close to the input, don't remove
    # trailing jumps. This will work OK for data flow algos, but will produce
    # broken or confusing output for control flow algos (for which asm output
    # shouldn't be used of course).
    # Update: it's unsafe to use this during dataflow analysis
    #if args.format != "asm":
    #    foreach_bblock(cfg, remove_trailing_jumps)

    if args.debug:
        with open(args.file + ".0.bb", "w") as f:
            dump_bblocks(cfg, f, no_graph_header=args.no_graph_header)
        with open(args.file + ".0.dot", "w") as f:

            dot.dot(cfg, f)