// File: /workspace/github_cpp_makefile_star2_O1/taurheim[P]AgNES/main.cpp
// Function: main
int main(int argc, char * argv[]) {
    if (argc < 3) {
        std::cout << "Did not provide enough arguments. Usage: AgNES input_file.c output_file.s\n";
        return 1;
    }
    char * inputFileName = argv[1];
    char * outputFileName = argv[2];

    // First we run Lexical Analysis, to turn the stream of characters into a list of tokens
    // Input: file
    // Output: list of tokens
    std::ifstream file (inputFileName);
    std::list<Token> tokens;
    if (file.is_open()) {
        Scanner scanner = Scanner(file);
        tokens = scanner.scan();
    } else {
        std::cout << "Could not open file \n";
        return 1;
    }

    // Next we run Syntax Analysis/Parsing, where we generate a parse tree
    // Input: list of tokens
    // Output: parse tree
    Parser parser = Parser(tokens);
    ASTNode * astRoot = parser.parse();

    //Now we make a SemanticAnalyzer!
    std::cout << "Running semantic analysis..." << std::endl;
    SemanticAnalyzer analyzer = SemanticAnalyzer(astRoot);
    SymbolTable * symbolTable = analyzer.analyze();

    // Generate intermediate code
    std::cout << "Running IR Generator..." << std::endl;
    IRGenerator irGenerator = IRGenerator(astRoot);
    std::list<TAC> intermediateCode = irGenerator.generate();

    //Generate machine code
    std::cout << "Running Code Generator..." << std::endl;
    CodeGenerator codeGenerator = CodeGenerator(intermediateCode, symbolTable);
    std::string machineCode = codeGenerator.generate();
    std::cout << machineCode;

    // Sandwich our machine code in the middle of the header and footer
    std::cout << "Writing to " << outputFileName << std::endl;
    std::ifstream header("NES_HEADER.s");
    std::string headerStr((std::istreambuf_iterator<char>(header)), std::istreambuf_iterator<char>());
    std::ifstream footer("NES_FOOTER.s");
    std::string footerStr((std::istreambuf_iterator<char>(footer)), std::istreambuf_iterator<char>());
    std::remove(outputFileName);
    std::ofstream outputFile;
    outputFile.open(outputFileName);

    outputFile << headerStr;
    outputFile << machineCode;
    outputFile << footerStr;

    header.close();
    footer.close();
    outputFile.close();

    std::cout << "Finished Compilation." << std::endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xbf0, %rsp            # imm = 0xBF0
cmpl $0x2, %edi
jg 0x5c35
movq 0x123b6(%rip), %rdi     # 0x17fd0
leaq 0xc4a7(%rip), %rsi      # 0x120c8
movl $0x4a, %edx
callq 0x32d0
movl $0x1, %ebx
jmp 0x611c
movq 0x8(%rsi), %rax
movq 0x10(%rsi), %rbx
leaq -0x968(%rbp), %rdi
movq %rax, %rsi
movl $0x8, %edx
callq 0x3450
leaq -0xc8(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq $0x0, 0x10(%rax)
leaq -0x8f0(%rbp), %rdi
callq 0x30c0
testb %al, %al
je 0x5e88
leaq -0xc08(%rbp), %rdi
leaq -0x968(%rbp), %rsi
callq 0x7450
leaq -0x558(%rbp), %rdi
leaq -0xc08(%rbp), %rsi
callq 0x7be0
leaq -0xc8(%rbp), %r14
leaq -0x558(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x6f64
movq %r15, %rdi
callq 0x6604
leaq -0x150(%rbp), %rdi
movq %r14, %rsi
callq 0x87d0
leaq -0x150(%rbp), %rdi
callq 0x8848
movq %rax, %r15
movq 0x122e9(%rip), %r14     # 0x17fd0
leaq 0xc43b(%rip), %rsi      # 0x12129
movl $0x1c, %edx
movq %r14, %rdi
callq 0x32d0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0x122b7(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq -0xd8(%rbp), %rdi
movq %r15, %rsi
callq 0xafd8
leaq -0xd8(%rbp), %rdi
callq 0xb016
movq %rax, %r14
movq 0x12285(%rip), %r12     # 0x17fd0
leaq 0xc3f4(%rip), %rsi      # 0x12146
movl $0x17, %edx
movq %r12, %rdi
callq 0x32d0
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0x12252(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq -0x108(%rbp), %rdi
movq %r15, %rsi
callq 0xc4e4
leaq -0xb0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0xc50a
movq 0x1221c(%rip), %r15     # 0x17fd0
leaq 0xc3a3(%rip), %rsi      # 0x1215e
movl $0x19, %edx
movq %r15, %rdi
callq 0x32d0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0x121ea(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq -0x98(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x63e8
leaq -0xc08(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movq %r14, %rdx
callq 0xe740
leaq -0x98(%rbp), %rdi
callq 0x664a
leaq -0x40(%rbp), %rdi
leaq -0xc08(%rbp), %rsi
callq 0xe8de
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq 0x12189(%rip), %rdi     # 0x17fd0
callq 0x32d0
movq 0x1217d(%rip), %r14     # 0x17fd0
leaq 0xc31e(%rip), %rsi      # 0x12178
movl $0xb, %edx
movq %r14, %rdi
callq 0x32d0
testq %rbx, %rbx
je 0x5eaa
movq %rbx, %rdi
callq 0x3130
movq 0x12155(%rip), %rdi     # 0x17fd0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x32d0
jmp 0x5ec2
movl $0x1, %ebx
movq 0x1213c(%rip), %rdi     # 0x17fd0
leaq 0xc278(%rip), %rsi      # 0x12113
movl $0x15, %edx
callq 0x32d0
jmp 0x6104
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x33f0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0x120f0(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq 0xc290(%rip), %rsi      # 0x12184
leaq -0x558(%rbp), %rdi
movl $0x8, %edx
callq 0x3450
movq -0x558(%rbp), %rax
movq -0x18(%rax), %rax
movq -0x470(%rbp,%rax), %rsi
leaq -0x70(%rbp), %r14
movq %r14, -0x10(%r14)
leaq -0x80(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x7118
leaq 0xc254(%rip), %rsi      # 0x12191
leaq -0x760(%rbp), %rdi
movl $0x8, %edx
callq 0x3450
movq -0x760(%rbp), %rax
movq -0x18(%rax), %rax
movq -0x678(%rbp,%rax), %rsi
leaq -0x50(%rbp), %r15
movq %r15, -0x10(%r15)
leaq -0x60(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x7118
movq %rbx, %rdi
callq 0x3440
leaq -0x350(%rbp), %rdi
callq 0x3400
leaq -0x350(%rbp), %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x3060
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
leaq -0x350(%rbp), %rdi
callq 0x32d0
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq -0x350(%rbp), %rdi
callq 0x32d0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
leaq -0x350(%rbp), %rdi
callq 0x32d0
leaq -0x558(%rbp), %rdi
callq 0x30a0
leaq -0x760(%rbp), %rdi
callq 0x30a0
leaq -0x350(%rbp), %rdi
callq 0x3340
movq 0x11fc2(%rip), %rbx     # 0x17fd0
leaq 0xc189(%rip), %rsi      # 0x1219e
movl $0x15, %edx
movq %rbx, %rdi
callq 0x32d0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0x11f90(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq 0x11f6c(%rip), %rax     # 0x17fc0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x348(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x350(%rbp,%rcx)
callq 0x3210
leaq -0x258(%rbp), %rdi
callq 0x3100
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x6091
callq 0x3250
leaq -0x760(%rbp), %rdi
callq 0x30d0
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x60ab
callq 0x3250
leaq -0x558(%rbp), %rdi
callq 0x30d0
leaq -0x30(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x60c9
callq 0x3250
leaq -0xc08(%rbp), %rdi
callq 0x6424
leaq -0xb0(%rbp), %rdi
callq 0x664a
leaq -0xf8(%rbp), %rdi
callq 0x664a
leaq -0x120(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6102
callq 0x3250
xorl %ebx, %ebx
leaq -0xc8(%rbp), %rdi
callq 0x6604
leaq -0x968(%rbp), %rdi
callq 0x30d0
movl %ebx, %eax
addq $0xbf0, %rsp            # imm = 0xBF0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x619f
movq %rax, %rbx
jmp 0x61ad
movq %rax, %rbx
jmp 0x61b9
movq %rax, %rbx
jmp 0x61c7
jmp 0x618b
movq %rax, %rbx
jmp 0x61e5
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x664a
jmp 0x61f1
jmp 0x617f
movq %rax, %rbx
jmp 0x61fd
jmp 0x6186
jmp 0x6186
jmp 0x6177
jmp 0x6177
jmp 0x6177
movq %rax, %rbx
jmp 0x621e
movq %rax, %rbx
jmp 0x61f1
jmp 0x6186
movq %rax, %rbx
jmp 0x6209
movq %rax, %rbx
jmp 0x61d3
movq %rax, %rbx
leaq -0x350(%rbp), %rdi
callq 0x3370
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x61ad
callq 0x3250
leaq -0x760(%rbp), %rdi
callq 0x30d0
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x61c7
callq 0x3250
leaq -0x558(%rbp), %rdi
callq 0x30d0
leaq -0x30(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x61e5
callq 0x3250
leaq -0xc08(%rbp), %rdi
callq 0x6424
leaq -0xb0(%rbp), %rdi
callq 0x664a
leaq -0xf8(%rbp), %rdi
callq 0x664a
leaq -0x120(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x621e
callq 0x3250
leaq -0xc8(%rbp), %rdi
callq 0x6604
leaq -0x968(%rbp), %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x3420