// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rsi, %rbx
movl %edi, %r14d
leaq -0x138(%rbp), %r15
movq %r15, -0x10(%r15)
movl $0x736e6f63, (%r15)     # imm = 0x736E6F63
movw $0x74, 0x4(%r15)
movq $0x5, -0x8(%r15)
leaq 0x11bbe(%rip), %rdi     # 0x26030
leaq -0x148(%rbp), %rsi
callq 0x14ca2
movl $0x1, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14495
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x746e69, 0x10(%rsi)   # imm = 0x746E69
movq $0x3, 0x8(%rsi)
leaq 0x11b7b(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0x2, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x144d1
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x72616863, 0x10(%rsi) # imm = 0x72616863
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x11b3b(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0x3, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14511
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x64696f76, 0x10(%rsi) # imm = 0x64696F76
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x11afb(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0x4, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14551
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movw $0x6669, 0x10(%rsi)     # imm = 0x6669
movq $0x2, 0x8(%rsi)
movb $0x0, 0x12(%rsi)
leaq 0x11abc(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0x5, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14590
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x65736c65, 0x10(%rsi) # imm = 0x65736C65
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x11a7c(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0x6, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x145d0
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movw $0x6f64, 0x10(%rsi)     # imm = 0x6F64
movq $0x2, 0x8(%rsi)
movb $0x0, 0x12(%rsi)
leaq 0x11a3d(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0x7, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1460f
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x6c696877, 0x10(%rsi) # imm = 0x6C696877
movw $0x65, 0x14(%rsi)
movq $0x5, 0x8(%rsi)
leaq 0x119fb(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0x8, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14651
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x74697773, 0x10(%rsi) # imm = 0x74697773
movw $0x6863, 0x14(%rsi)     # imm = 0x6863
movq $0x6, 0x8(%rsi)
movb $0x0, 0x16(%rsi)
leaq 0x119b5(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0x9, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14697
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x65736163, 0x10(%rsi) # imm = 0x65736163
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x11975(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0xa, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x146d7
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x61666564, 0x10(%rsi) # imm = 0x61666564
movl $0x746c7561, 0x13(%rsi) # imm = 0x746C7561
movq $0x7, 0x8(%rsi)
movb $0x0, 0x17(%rsi)
leaq 0x1192e(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0xb, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1471e
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x6e616373, 0x10(%rsi) # imm = 0x6E616373
movw $0x66, 0x14(%rsi)
movq $0x5, 0x8(%rsi)
leaq 0x118ec(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0xc, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14760
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x6e697270, 0x10(%rsi) # imm = 0x6E697270
movw $0x6674, 0x14(%rsi)     # imm = 0x6674
movq $0x6, 0x8(%rsi)
movb $0x0, 0x16(%rsi)
leaq 0x118a6(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0xd, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x147a6
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x75746572, 0x10(%rsi) # imm = 0x75746572
movw $0x6e72, 0x14(%rsi)     # imm = 0x6E72
movq $0x6, 0x8(%rsi)
movb $0x0, 0x16(%rsi)
leaq 0x11860(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0xe, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x147ec
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x6e69616d, 0x10(%rsi) # imm = 0x6E69616D
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x11820(%rip), %rdi     # 0x26030
callq 0x14ca2
movl $0xf, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1482c
callq 0x32a0
movaps 0x824d(%rip), %xmm0     # 0x1ca80
movups %xmm0, 0x1169e(%rip)    # 0x25ed8
movl $0x16, 0x116a8(%rip)    # 0x25eec
movabsq $0x2300000019, %rax     # imm = 0x2300000019
movq %rax, 0x116cb(%rip)     # 0x25f20
movl $0x1b, 0x116c9(%rip)    # 0x25f28
movabsq $0x1e0000001d, %rax     # imm = 0x1E0000001D
movq %rax, 0x11660(%rip)     # 0x25ed0
movl $0x1f, 0x11722(%rip)    # 0x25f9c
movl $0x20, 0x11720(%rip)    # 0x25fa4
movl $0x21, 0x1178e(%rip)    # 0x2601c
movl $0x22, 0x1178c(%rip)    # 0x26024
movabsq $0x2600000025, %rax     # imm = 0x2600000025
movq %rax, 0x1166f(%rip)     # 0x25f18
cmpl $0x2, %r14d
jl 0x148b5
movq 0x8(%rbx), %rbx
jmp 0x148bc
leaq 0x81d4(%rip), %rbx      # 0x1ca90
movq 0x117ad(%rip), %r14     # 0x26070
leaq 0x1179e(%rip), %r15     # 0x26068
movq %rbx, %rdi
callq 0x3120
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x33f0
leaq 0x1179c(%rip), %rdi     # 0x26088
movq %r15, %rsi
movl $0x8, %edx
callq 0x3260
leaq -0x48(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x8198(%rip), %rsi      # 0x1caa0
leaq 0x819d(%rip), %rdx      # 0x1caac
leaq -0x58(%rbp), %rdi
callq 0x14396
leaq -0x28(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x8216(%rip), %rsi      # 0x1cb3d
leaq 0x821c(%rip), %rdx      # 0x1cb4a
leaq -0x38(%rbp), %rdi
callq 0x14396
leaq -0x148(%rbp), %rdi
callq 0x32e0
leaq -0x238(%rbp), %rdi
callq 0x32e0
movq -0x58(%rbp), %rsi
leaq -0x148(%rbp), %rdi
movl $0x10, %edx
callq 0x3290
movq -0x38(%rbp), %rsi
leaq -0x238(%rbp), %rdi
movl $0x10, %edx
callq 0x3290
movq 0x11910(%rip), %rax     # 0x26290
leaq 0x11909(%rip), %rdi     # 0x26290
addq -0x18(%rax), %rdi
leaq -0x148(%rbp), %rsi
callq 0x34b0
movq 0x11a02(%rip), %rax     # 0x263a0
leaq 0x119fb(%rip), %rdi     # 0x263a0
addq -0x18(%rax), %rdi
leaq -0x238(%rbp), %rsi
callq 0x34b0
movq 0x11d14(%rip), %rax     # 0x266d0
leaq 0x11d0d(%rip), %rdi     # 0x266d0
addq -0x18(%rax), %rdi
movq 0xe602(%rip), %rax      # 0x22fd0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq 0xe8(%rax,%rcx), %rsi
callq 0x34b0
callq 0xe59c
callq 0xd6dc
testb %al, %al
jne 0x14b6b
movq 0xe5d5(%rip), %rdi      # 0x22fd0
leaq 0x80ab(%rip), %rsi      # 0x1caad
movl $0x11, %edx
callq 0x3340
movq 0xe5bd(%rip), %rdi      # 0x22fd0
leaq 0x80a5(%rip), %rsi      # 0x1cabf
movl $0xd, %edx
callq 0x3340
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq 0xe59d(%rip), %rdi      # 0x22fd0
callq 0x3340
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31d0
callq 0x16afc
movq 0xe562(%rip), %rdi      # 0x22fd0
leaq 0x8058(%rip), %rsi      # 0x1cacd
movl $0xe, %edx
callq 0x3340
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq 0xe542(%rip), %rdi      # 0x22fd0
callq 0x3340
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31d0
movq 0xe50c(%rip), %rdi      # 0x22fd0
leaq 0x8011(%rip), %rsi      # 0x1cadc
movl $0x44, %edx
callq 0x3340
movq 0xe4f4(%rip), %rbx      # 0x22fd0
leaq 0x803e(%rip), %rsi      # 0x1cb21
movl $0x29, %edx
movq %rbx, %rdi
callq 0x3340
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq 0xe4c2(%rip), %rdi      # 0x22fd0
callq 0x3030
movq %rax, %rdi
callq 0x31d0
leaq 0x11566(%rip), %rdi     # 0x26088
callq 0x3090
leaq -0x238(%rbp), %rdi
callq 0x3240
leaq -0x148(%rbp), %rdi
callq 0x3240
movq -0x38(%rbp), %rdi
cmpq %r15, %rdi
je 0x14b4d
callq 0x32a0
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x14b5b
callq 0x32a0
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x3360
movq %rax, %rbx
jmp 0x14bc7
movq %rax, %rbx
jmp 0x14bd3
movq %rax, %rbx
jmp 0x14be1
movq %rax, %rbx
jmp 0x14bef
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
jmp 0x14ba5
movq %rax, %rbx
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
jne 0x14bea
jmp 0x14bef
jmp 0x14bb8
movq %rax, %rbx
leaq -0x238(%rbp), %rdi
callq 0x3240
leaq -0x148(%rbp), %rdi
callq 0x3240
movq -0x38(%rbp), %rdi
cmpq %r15, %rdi
je 0x14be1
callq 0x32a0
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x14bef
callq 0x32a0
movq %rbx, %rdi
callq 0x3480
nop