// File: /workspace/github_cpp_makefile_star2_O2/fondoger[P]C0-Compiler/src/main.cpp
// Function: main
int main(int argc, char *argv[]) {
    initialize();

    source_filename = argc > 1 ? argv[1] : "hello_world.txt";
    source_stream.open(source_filename);

    //midcode_stream.rdbuf(std::cout.rdbuf());
    std::string midcode_filename = "mid_code.txt";
    std::string mipscode_filename = "mips_code.txt";
    std::filebuf buffer1, buffer2;
    buffer1.open(midcode_filename, std::ios_base::out);
    buffer2.open(mipscode_filename, std::ios_base::out);
    midcode_stream.rdbuf(&buffer1);
    mipscode_stream.rdbuf(&buffer2);

    // debug messages
    debug_stream.rdbuf(std::cout.rdbuf());

    // Do syntax check and generate mid-code
    pProgram();

    bool has_error = printCachedErrors();
    if (has_error)
        exit(1);
        
    std::cout << "compile success!\n";
    std::cout << "mid code at: " << midcode_filename << std::endl;
    // convert mid-code to MIPS code
    convertToMIPS();
    std::cout << "mips code at: " << mipscode_filename << std::endl;
    std::cout << "\nIf you want to execute this mips program, using following command:\n"
        << "    $ java -jar mars.jar nc mips_code.txt" << std::endl;

    source_stream.close();
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x220, %rsp            # imm = 0x220
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x6c94(%rip), %rsi      # 0x160df
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11bab(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x1, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x71cb(%rip), %rsi      # 0x16655
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11b6c(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x2, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6c1c(%rip), %rsi      # 0x160e5
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11b2d(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x3, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x7329(%rip), %rsi      # 0x16831
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11aee(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x4, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6ba3(%rip), %rsi      # 0x160ea
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11aaf(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x5, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6b67(%rip), %rsi      # 0x160ed
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11a70(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x6, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6b2d(%rip), %rsi      # 0x160f2
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11a31(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x7, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6af1(%rip), %rsi      # 0x160f5
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x119f2(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x8, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6ab8(%rip), %rsi      # 0x160fb
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x119b3(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0x9, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6a80(%rip), %rsi      # 0x16102
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11974(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0xa, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6a46(%rip), %rsi      # 0x16107
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11935(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0xb, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6a0f(%rip), %rsi      # 0x1610f
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x118f6(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0xc, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x69d6(%rip), %rsi      # 0x16115
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x118b7(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0xd, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x699e(%rip), %rsi      # 0x1611c
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11878(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0xe, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
leaq 0x6966(%rip), %rsi      # 0x16123
leaq -0x100(%rbp), %rdi
leaq -0x230(%rbp), %rdx
callq 0x30f0
leaq 0x11839(%rip), %rdi     # 0x21010
leaq -0x100(%rbp), %rsi
callq 0xfbe6
movl $0xf, (%rax)
leaq -0x100(%rbp), %rdi
callq 0x3498
movaps 0x7254(%rip), %xmm0     # 0x16a50
movups %xmm0, 0x116b5(%rip)    # 0x20eb8
movl $0x16, 0x116bf(%rip)    # 0x20ecc
movabsq $0x2300000019, %rax     # imm = 0x2300000019
movq %rax, 0x116e2(%rip)     # 0x20f00
movl $0x1b, 0x116e0(%rip)    # 0x20f08
movabsq $0x1e0000001d, %rax     # imm = 0x1E0000001D
movq %rax, 0x11677(%rip)     # 0x20eb0
movl $0x1f, 0x11739(%rip)    # 0x20f7c
movl $0x20, 0x11737(%rip)    # 0x20f84
movl $0x21, 0x117a5(%rip)    # 0x20ffc
movl $0x22, 0x117a3(%rip)    # 0x21004
movabsq $0x2600000025, %rax     # imm = 0x2600000025
movq %rax, 0x11686(%rip)     # 0x20ef8
cmpl $0x2, %r14d
jl 0xf87e
movq 0x8(%rbx), %rsi
jmp 0xf885
leaq 0x71db(%rip), %rsi      # 0x16a60
leaq 0x117bc(%rip), %rbx     # 0x21048
movq %rbx, %rdi
callq 0x3470
leaq 0x117cd(%rip), %rdi     # 0x21068
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x3230
leaq 0x71c3(%rip), %rsi      # 0x16a70
leaq -0x140(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x30f0
leaq 0x7246(%rip), %rsi      # 0x16b0d
leaq -0x120(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x30f0
leaq -0x100(%rbp), %rdi
callq 0x32a0
leaq -0x230(%rbp), %rdi
callq 0x32a0
movq -0x140(%rbp), %rsi
leaq -0x100(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x3260
movq -0x120(%rbp), %rsi
leaq -0x230(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x3260
movq 0x1194b(%rip), %rax     # 0x21270
leaq 0x11944(%rip), %rdi     # 0x21270
addq -0x18(%rax), %rdi
leaq -0x100(%rbp), %rsi
callq 0x3450
movq 0x11a3d(%rip), %rax     # 0x21380
leaq 0x11a36(%rip), %rdi     # 0x21380
addq -0x18(%rax), %rdi
leaq -0x230(%rbp), %rsi
callq 0x3450
movq 0x11d4f(%rip), %rax     # 0x216b0
leaq 0x11d48(%rip), %rdi     # 0x216b0
addq -0x18(%rax), %rdi
movq 0xe655(%rip), %rax      # 0x1dfc8
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq 0xe8(%rax,%rcx), %rsi
callq 0x3450
callq 0xa990
callq 0x9e1c
testb %al, %al
jne 0xfa79
movq 0xe628(%rip), %rdi      # 0x1dfc8
leaq 0x70d6(%rip), %rsi      # 0x16a7d
callq 0x3280
movq 0xe615(%rip), %rdi      # 0x1dfc8
leaq 0x70d5(%rip), %rsi      # 0x16a8f
callq 0x3280
leaq -0x140(%rbp), %rsi
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3130
callq 0x1117c
movq 0xe5e6(%rip), %rdi      # 0x1dfc8
leaq 0x70b4(%rip), %rsi      # 0x16a9d
callq 0x3280
leaq -0x120(%rbp), %rsi
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3130
movq 0xe5bc(%rip), %rdi      # 0x1dfc8
leaq 0x7099(%rip), %rsi      # 0x16aac
callq 0x3280
leaq 0x70d2(%rip), %rsi      # 0x16af1
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3130
leaq 0x11632(%rip), %rdi     # 0x21068
callq 0x3080
leaq -0x230(%rbp), %rdi
callq 0x3210
leaq -0x100(%rbp), %rdi
callq 0x3210
leaq -0x120(%rbp), %rdi
callq 0x3498
leaq -0x140(%rbp), %rdi
callq 0x3498
xorl %eax, %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r14
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x3300
movq %rax, %rbx
jmp 0xfaec
movq %rax, %rbx
jmp 0xfaf8
movq %rax, %rbx
jmp 0xfb04
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
jmp 0xfaca
jmp 0xfad6
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
jmp 0xfb0b
movq %rax, %rbx
jmp 0xfb10
jmp 0xfadd
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
callq 0x3210
leaq -0x100(%rbp), %rdi
callq 0x3210
leaq -0x120(%rbp), %rdi
callq 0x3498
leaq -0x140(%rbp), %rdi
callq 0x3498
movq %rbx, %rdi
callq 0x3430