// File: /workspace/github_cpp_makefile_star2_O1/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 0x11b02(%rip), %rdi     # 0x26030
leaq -0x148(%rbp), %rsi
callq 0x14d5e
movl $0x1, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14551
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x746e69, 0x10(%rsi)   # imm = 0x746E69
movq $0x3, 0x8(%rsi)
leaq 0x11abf(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0x2, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1458d
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x72616863, 0x10(%rsi) # imm = 0x72616863
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x11a7f(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0x3, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x145cd
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x64696f76, 0x10(%rsi) # imm = 0x64696F76
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x11a3f(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0x4, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1460d
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movw $0x6669, 0x10(%rsi)     # imm = 0x6669
movq $0x2, 0x8(%rsi)
movb $0x0, 0x12(%rsi)
leaq 0x11a00(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0x5, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1464c
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x65736c65, 0x10(%rsi) # imm = 0x65736C65
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x119c0(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0x6, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1468c
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movw $0x6f64, 0x10(%rsi)     # imm = 0x6F64
movq $0x2, 0x8(%rsi)
movb $0x0, 0x12(%rsi)
leaq 0x11981(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0x7, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x146cb
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x6c696877, 0x10(%rsi) # imm = 0x6C696877
movw $0x65, 0x14(%rsi)
movq $0x5, 0x8(%rsi)
leaq 0x1193f(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0x8, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1470d
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 0x118f9(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0x9, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14753
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x65736163, 0x10(%rsi) # imm = 0x65736163
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x118b9(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0xa, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14793
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 0x11872(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0xb, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x147da
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x6e616373, 0x10(%rsi) # imm = 0x6E616373
movw $0x66, 0x14(%rsi)
movq $0x5, 0x8(%rsi)
leaq 0x11830(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0xc, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x1481c
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 0x117ea(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0xd, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x14862
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 0x117a4(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0xe, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x148a8
callq 0x32a0
leaq -0x148(%rbp), %rsi
movq %r15, (%rsi)
movl $0x6e69616d, 0x10(%rsi) # imm = 0x6E69616D
movq $0x4, 0x8(%rsi)
movb $0x0, 0x14(%rsi)
leaq 0x11764(%rip), %rdi     # 0x26030
callq 0x14d5e
movl $0xf, (%rax)
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
je 0x148e8
callq 0x32a0
movaps 0x81d1(%rip), %xmm0     # 0x1cac0
movups %xmm0, 0x115e2(%rip)    # 0x25ed8
movl $0x16, 0x115ec(%rip)    # 0x25eec
movabsq $0x2300000019, %rax     # imm = 0x2300000019
movq %rax, 0x1160f(%rip)     # 0x25f20
movl $0x1b, 0x1160d(%rip)    # 0x25f28
movabsq $0x1e0000001d, %rax     # imm = 0x1E0000001D
movq %rax, 0x115a4(%rip)     # 0x25ed0
movl $0x1f, 0x11666(%rip)    # 0x25f9c
movl $0x20, 0x11664(%rip)    # 0x25fa4
movl $0x21, 0x116d2(%rip)    # 0x2601c
movl $0x22, 0x116d0(%rip)    # 0x26024
movabsq $0x2600000025, %rax     # imm = 0x2600000025
movq %rax, 0x115b3(%rip)     # 0x25f18
cmpl $0x2, %r14d
jl 0x14971
movq 0x8(%rbx), %rbx
jmp 0x14978
leaq 0x8158(%rip), %rbx      # 0x1cad0
movq 0x116f1(%rip), %r14     # 0x26070
leaq 0x116e2(%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 0x116e0(%rip), %rdi     # 0x26088
movq %r15, %rsi
movl $0x8, %edx
callq 0x3260
leaq -0x48(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x811c(%rip), %rsi      # 0x1cae0
leaq 0x8121(%rip), %rdx      # 0x1caec
leaq -0x58(%rbp), %rdi
callq 0x14454
leaq -0x28(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x819a(%rip), %rsi      # 0x1cb7d
leaq 0x81a0(%rip), %rdx      # 0x1cb8a
leaq -0x38(%rbp), %rdi
callq 0x14454
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 0x11854(%rip), %rax     # 0x26290
leaq 0x1184d(%rip), %rdi     # 0x26290
addq -0x18(%rax), %rdi
leaq -0x148(%rbp), %rsi
callq 0x34b0
movq 0x11946(%rip), %rax     # 0x263a0
leaq 0x1193f(%rip), %rdi     # 0x263a0
addq -0x18(%rax), %rdi
leaq -0x238(%rbp), %rsi
callq 0x34b0
movq 0x11c58(%rip), %rax     # 0x266d0
leaq 0x11c51(%rip), %rdi     # 0x266d0
addq -0x18(%rax), %rdi
movq 0xe546(%rip), %rax      # 0x22fd0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq 0xe8(%rax,%rcx), %rsi
callq 0x34b0
callq 0xe55c
callq 0xd6dc
testb %al, %al
jne 0x14c27
movq 0xe519(%rip), %rdi      # 0x22fd0
leaq 0x802f(%rip), %rsi      # 0x1caed
movl $0x11, %edx
callq 0x3340
movq 0xe501(%rip), %rdi      # 0x22fd0
leaq 0x8029(%rip), %rsi      # 0x1caff
movl $0xd, %edx
callq 0x3340
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq 0xe4e1(%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 0x16b8c
movq 0xe4a6(%rip), %rdi      # 0x22fd0
leaq 0x7fdc(%rip), %rsi      # 0x1cb0d
movl $0xe, %edx
callq 0x3340
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq 0xe486(%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 0xe450(%rip), %rdi      # 0x22fd0
leaq 0x7f95(%rip), %rsi      # 0x1cb1c
movl $0x44, %edx
callq 0x3340
movq 0xe438(%rip), %rbx      # 0x22fd0
leaq 0x7fc2(%rip), %rsi      # 0x1cb61
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 0xe406(%rip), %rdi      # 0x22fd0
callq 0x3030
movq %rax, %rdi
callq 0x31d0
leaq 0x114aa(%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 0x14c09
callq 0x32a0
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x14c17
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 0x14c83
movq %rax, %rbx
jmp 0x14c8f
movq %rax, %rbx
jmp 0x14c9d
movq %rax, %rbx
jmp 0x14cab
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
jmp 0x14c61
movq %rax, %rbx
movq -0x148(%rbp), %rdi
cmpq %r15, %rdi
jne 0x14ca6
jmp 0x14cab
jmp 0x14c74
movq %rax, %rbx
leaq -0x238(%rbp), %rdi
callq 0x3240
leaq -0x148(%rbp), %rdi
callq 0x3240
movq -0x38(%rbp), %rdi
cmpq %r15, %rdi
je 0x14c9d
callq 0x32a0
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x14cab
callq 0x32a0
movq %rbx, %rdi
callq 0x3480
nop