// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0xbe8, %rsp            # imm = 0xBE8
cmpl $0x2, %edi
jg 0x56bc
movq 0x12920(%rip), %rdi     # 0x17fc8
leaq 0xaa3d(%rip), %rsi      # 0x100ec
callq 0x3230
pushq $0x1
popq %rbx
jmp 0x5a25
movq 0x8(%rsi), %rax
movq 0x10(%rsi), %rbx
leaq -0x960(%rbp), %rdi
pushq $0x8
popq %rdx
movq %rax, %rsi
callq 0x3410
leaq -0x60(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
leaq -0x8e8(%rbp), %rdi
callq 0x30b0
testb %al, %al
je 0x59fa
leaq -0xc00(%rbp), %rdi
leaq -0x960(%rbp), %rsi
callq 0x6f80
leaq -0x550(%rbp), %rdi
leaq -0xc00(%rbp), %rsi
callq 0x7426
leaq -0x60(%rbp), %r14
leaq -0x550(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x6b7e
movq %r15, %rdi
callq 0x6090
leaq -0x148(%rbp), %rdi
movq %r14, %rsi
callq 0x81ac
leaq -0x148(%rbp), %rdi
callq 0x81fa
movq %rax, %r15
movq 0x12865(%rip), %rdi     # 0x17fc8
leaq 0xa9e3(%rip), %rsi      # 0x1014d
callq 0x3230
movq %rax, %rdi
callq 0x3150
leaq -0xd0(%rbp), %rdi
movq %r15, %rsi
callq 0xa688
leaq -0xd0(%rbp), %rdi
callq 0xa6c4
movq %rax, %r14
movq 0x1282c(%rip), %rdi     # 0x17fc8
leaq 0xa9c7(%rip), %rsi      # 0x1016a
callq 0x3230
movq %rax, %rdi
callq 0x3150
leaq -0x100(%rbp), %rdi
movq %r15, %rsi
callq 0xba58
leaq -0x48(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xba7a
movq 0x127f2(%rip), %rdi     # 0x17fc8
leaq 0xa9a5(%rip), %rsi      # 0x10182
callq 0x3230
movq %rax, %rdi
callq 0x3150
leaq -0x30(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x5e7e
leaq -0xc00(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movq %r14, %rdx
callq 0xd08c
leaq -0x30(%rbp), %rdi
callq 0x60ca
leaq -0xc0(%rbp), %rdi
leaq -0xc00(%rbp), %rsi
callq 0xd1f0
movq 0x1279b(%rip), %rdi     # 0x17fc8
leaq -0xc0(%rbp), %rsi
callq 0x3210
movq 0x12788(%rip), %rdi     # 0x17fc8
leaq 0xa955(%rip), %rsi      # 0x1019c
callq 0x3230
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3230
movq %rax, %rdi
callq 0x3150
leaq 0xa942(%rip), %rsi      # 0x101a8
leaq -0x550(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x3410
movq -0x550(%rbp), %rax
movq -0x18(%rax), %rax
movq -0x468(%rbp,%rax), %rsi
leaq -0x90(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0xa0(%rbp), %rdi
pushq $-0x1
popq %rdx
xorl %ecx, %ecx
movl %edx, %r8d
callq 0x6dbe
leaq 0xa907(%rip), %rsi      # 0x101b5
leaq -0x758(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x3410
movq -0x758(%rbp), %rax
movq -0x18(%rax), %rax
movq -0x670(%rbp,%rax), %rsi
leaq -0x70(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x80(%rbp), %rdi
pushq $-0x1
popq %rdx
xorl %ecx, %ecx
movl %edx, %r8d
callq 0x6dbe
movq %rbx, %rdi
callq 0x3400
leaq -0x348(%rbp), %rdi
callq 0x33c0
leaq -0x348(%rbp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x3050
leaq -0x348(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x3210
leaq -0x348(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x3210
leaq -0x348(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x3210
leaq -0x550(%rbp), %rdi
callq 0x3090
leaq -0x758(%rbp), %rdi
callq 0x3090
leaq -0x348(%rbp), %rdi
callq 0x3300
movq 0x12658(%rip), %rdi     # 0x17fc8
leaq 0xa84b(%rip), %rsi      # 0x101c2
callq 0x3230
movq %rax, %rdi
callq 0x3150
leaq -0x348(%rbp), %rdi
callq 0x3330
leaq -0x80(%rbp), %rdi
callq 0x3140
leaq -0x758(%rbp), %rdi
callq 0x30e0
leaq -0xa0(%rbp), %rdi
callq 0x3140
leaq -0x550(%rbp), %rdi
callq 0x30e0
leaq -0xc0(%rbp), %rdi
callq 0x3140
leaq -0xc00(%rbp), %rdi
callq 0x5eb8
leaq -0x48(%rbp), %rdi
callq 0x60ca
leaq -0xf0(%rbp), %rdi
callq 0x60ca
leaq -0x128(%rbp), %rdi
callq 0x3140
xorl %ebx, %ebx
jmp 0x5a10
movq 0x125c7(%rip), %rdi     # 0x17fc8
leaq 0xa72f(%rip), %rsi      # 0x10137
callq 0x3230
pushq $0x1
popq %rbx
leaq -0x60(%rbp), %rdi
callq 0x6090
leaq -0x960(%rbp), %rdi
callq 0x30e0
movl %ebx, %eax
addq $0xbe8, %rsp            # imm = 0xBE8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5aa0
movq %rax, %rbx
jmp 0x5aa9
movq %rax, %rbx
jmp 0x5ab5
movq %rax, %rbx
jmp 0x5ac1
jmp 0x5a8c
movq %rax, %rbx
jmp 0x5ad9
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x60ca
jmp 0x5ae5
jmp 0x5a80
movq %rax, %rbx
jmp 0x5aee
jmp 0x5a87
jmp 0x5a87
jmp 0x5a78
jmp 0x5a78
jmp 0x5a78
movq %rax, %rbx
jmp 0x5b06
movq %rax, %rbx
jmp 0x5ae5
jmp 0x5a87
movq %rax, %rbx
jmp 0x5afa
movq %rax, %rbx
jmp 0x5acd
movq %rax, %rbx
leaq -0x348(%rbp), %rdi
callq 0x3330
leaq -0x80(%rbp), %rdi
callq 0x3140
leaq -0x758(%rbp), %rdi
callq 0x30e0
leaq -0xa0(%rbp), %rdi
callq 0x3140
leaq -0x550(%rbp), %rdi
callq 0x30e0
leaq -0xc0(%rbp), %rdi
callq 0x3140
leaq -0xc00(%rbp), %rdi
callq 0x5eb8
leaq -0x48(%rbp), %rdi
callq 0x60ca
leaq -0xf0(%rbp), %rdi
callq 0x60ca
leaq -0x128(%rbp), %rdi
callq 0x3140
leaq -0x60(%rbp), %rdi
callq 0x6090
leaq -0x960(%rbp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x33e0
nop