// File: /workspace/github_cpp_makefile_star2_O3/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 0x5bb5
movq 0x12436(%rip), %rdi     # 0x17fd0
leaq 0xc527(%rip), %rsi      # 0x120c8
movl $0x4a, %edx
callq 0x32d0
movl $0x1, %ebx
jmp 0x609c
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 0x5e08
leaq -0xc08(%rbp), %rdi
leaq -0x968(%rbp), %rsi
callq 0x7428
leaq -0x558(%rbp), %rdi
leaq -0xc08(%rbp), %rsi
callq 0x7ba2
leaq -0xc8(%rbp), %r14
leaq -0x558(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x6ec6
movq %r15, %rdi
callq 0x6584
leaq -0x150(%rbp), %rdi
movq %r14, %rsi
callq 0x8768
leaq -0x150(%rbp), %rdi
callq 0x87de
movq %rax, %r15
movq 0x12369(%rip), %r14     # 0x17fd0
leaq 0xc4bb(%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 0x12337(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq -0xd8(%rbp), %rdi
movq %r15, %rsi
callq 0xae84
leaq -0xd8(%rbp), %rdi
callq 0xaec2
movq %rax, %r14
movq 0x12305(%rip), %r12     # 0x17fd0
leaq 0xc474(%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 0x122d2(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq -0x108(%rbp), %rdi
movq %r15, %rsi
callq 0xc35c
leaq -0xb0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0xc382
movq 0x1229c(%rip), %r15     # 0x17fd0
leaq 0xc423(%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 0x1226a(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq -0x98(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x6368
leaq -0xc08(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movq %r14, %rdx
callq 0xe5b0
leaq -0x98(%rbp), %rdi
callq 0x65ca
leaq -0x40(%rbp), %rdi
leaq -0xc08(%rbp), %rsi
callq 0xe74e
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq 0x12209(%rip), %rdi     # 0x17fd0
callq 0x32d0
movq 0x121fd(%rip), %r14     # 0x17fd0
leaq 0xc39e(%rip), %rsi      # 0x12178
movl $0xb, %edx
movq %r14, %rdi
callq 0x32d0
testq %rbx, %rbx
je 0x5e2a
movq %rbx, %rdi
callq 0x3130
movq 0x121d5(%rip), %rdi     # 0x17fd0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x32d0
jmp 0x5e42
movl $0x1, %ebx
movq 0x121bc(%rip), %rdi     # 0x17fd0
leaq 0xc2f8(%rip), %rsi      # 0x12113
movl $0x15, %edx
callq 0x32d0
jmp 0x6084
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 0x12170(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq 0xc310(%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 0x7076
leaq 0xc2d4(%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 0x7076
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 0x12042(%rip), %rbx     # 0x17fd0
leaq 0xc209(%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 0x12010(%rip), %rdi     # 0x17fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq 0x11fec(%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 0x6011
callq 0x3250
leaq -0x760(%rbp), %rdi
callq 0x30d0
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x602b
callq 0x3250
leaq -0x558(%rbp), %rdi
callq 0x30d0
leaq -0x30(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6049
callq 0x3250
leaq -0xc08(%rbp), %rdi
callq 0x63a4
leaq -0xb0(%rbp), %rdi
callq 0x65ca
leaq -0xf8(%rbp), %rdi
callq 0x65ca
leaq -0x120(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6082
callq 0x3250
xorl %ebx, %ebx
leaq -0xc8(%rbp), %rdi
callq 0x6584
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 0x611f
movq %rax, %rbx
jmp 0x612d
movq %rax, %rbx
jmp 0x6139
movq %rax, %rbx
jmp 0x6147
jmp 0x610b
movq %rax, %rbx
jmp 0x6165
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x65ca
jmp 0x6171
jmp 0x60ff
movq %rax, %rbx
jmp 0x617d
jmp 0x6106
jmp 0x6106
jmp 0x60f7
jmp 0x60f7
jmp 0x60f7
movq %rax, %rbx
jmp 0x619e
movq %rax, %rbx
jmp 0x6171
jmp 0x6106
movq %rax, %rbx
jmp 0x6189
movq %rax, %rbx
jmp 0x6153
movq %rax, %rbx
leaq -0x350(%rbp), %rdi
callq 0x3370
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0x612d
callq 0x3250
leaq -0x760(%rbp), %rdi
callq 0x30d0
movq -0x80(%rbp), %rdi
cmpq %r14, %rdi
je 0x6147
callq 0x3250
leaq -0x558(%rbp), %rdi
callq 0x30d0
leaq -0x30(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6165
callq 0x3250
leaq -0xc08(%rbp), %rdi
callq 0x63a4
leaq -0xb0(%rbp), %rdi
callq 0x65ca
leaq -0xf8(%rbp), %rdi
callq 0x65ca
leaq -0x120(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x619e
callq 0x3250
leaq -0xc8(%rbp), %rdi
callq 0x6584
leaq -0x968(%rbp), %rdi
callq 0x30d0
movq %rbx, %rdi
callq 0x3420