// File: johan-bjareholt[P]lua-compiler/src/main.cc
// Function: main
int main(int argc, char** argv){
	parse_flags(argc, argv);
	
    // Grammar
    yy::parser parser;
	parser.parse();
    if (debug_grammar){
        std::stringstream ss;
        root->dump(ss);
        std::cout << ss.str();
    }

    // IR
	BBlock* start = new BBlock();
	BBlock* current = start;
	convertStatement(root, &current);
    if (debug_cfg){
        std::stringstream ss;
	    dumpCFG(ss, start);
        for (auto block : funcdefs){
            ss << "Function " << block.first << std::endl;
            dumpCFG(ss, block.second);
        }
        std::cout << ss.str();
    }
   
    // CFG dot graph
    std::ofstream outDotFile("graph.dot");
    std::list<BBlock*> outdotblocks;
    outdotblocks.push_back(start);
    for (auto block : funcdefs){
        outdotblocks.push_back(block.second);
    }
    dumpDot(outDotFile, outdotblocks);
    outDotFile.close();
	
    // Target output
    if (output_mode == OUTPUT_FILE){
        std::ofstream outFile(output_filename);
        outMainBlock(outFile, *start);
        outFile.close();
    }
    else if (output_mode == OUTPUT_STDOUT){
        std::stringstream ss;
        outMainBlock(ss, *start);
        std::cout << ss.str();
    }
	
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa20, %rsp            # imm = 0xA20
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x57b0
leaq -0x30(%rbp), %rdi
movq %rdi, -0xa00(%rbp)
callq 0x19710
movq -0xa00(%rbp), %rdi
callq 0x19d40
jmp 0x51d3
testb $0x1, 0x3912f(%rip)     # 0x3e309
je 0x5294
leaq -0x1c8(%rbp), %rdi
callq 0x31d0
jmp 0x51ee
leaq 0x39293(%rip), %rax     # 0x3e488
movq (%rax), %rdi
leaq -0x1b8(%rbp), %rsi
xorl %edx, %edx
callq 0x75e0
jmp 0x5208
leaq -0x1e8(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x3250
jmp 0x521d
movq 0x38da4(%rip), %rdi     # 0x3dfc8
leaq -0x1e8(%rbp), %rsi
callq 0x3220
jmp 0x5232
leaq -0x1e8(%rbp), %rdi
callq 0x3528
leaq -0x1c8(%rbp), %rdi
callq 0x31e0
jmp 0x5294
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x578f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x5283
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x3528
leaq -0x1c8(%rbp), %rdi
callq 0x31e0
jmp 0x578f
movl $0x48, %edi
callq 0x3270
movq %rax, -0xa08(%rbp)
jmp 0x52a7
movq -0xa08(%rbp), %rdi
callq 0x66a0
jmp 0x52b5
movq -0xa08(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rax
movq %rax, -0x1f8(%rbp)
leaq 0x391b0(%rip), %rax     # 0x3e488
movq (%rax), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x8f20
jmp 0x52e9
testb $0x1, 0x3901a(%rip)     # 0x3e30a
je 0x54b7
leaq -0x380(%rbp), %rdi
callq 0x31d0
jmp 0x5304
leaq -0x370(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x9900
jmp 0x5319
leaq 0x39040(%rip), %rax     # 0x3e360
movq %rax, -0x388(%rbp)
leaq 0x39032(%rip), %rdi     # 0x3e360
callq 0x58c0
movq %rax, -0x390(%rbp)
leaq 0x3901f(%rip), %rdi     # 0x3e360
callq 0x58f0
movq %rax, -0x398(%rbp)
leaq -0x390(%rbp), %rdi
leaq -0x398(%rbp), %rsi
callq 0x5920
testb $0x1, %al
jne 0x5369
jmp 0x544a
leaq -0x390(%rbp), %rdi
callq 0x5950
movq %rax, %rsi
leaq -0x3c0(%rbp), %rdi
callq 0x5980
jmp 0x5386
leaq -0x370(%rbp), %rdi
leaq 0x25c8f(%rip), %rsi     # 0x2b023
callq 0x3260
movq %rax, -0xa10(%rbp)
jmp 0x53a2
movq -0xa10(%rbp), %rdi
leaq -0x3c0(%rbp), %rsi
callq 0x3220
movq %rax, -0xa18(%rbp)
jmp 0x53be
movq -0xa18(%rbp), %rdi
movq 0x38bd4(%rip), %rsi     # 0x3dfa0
callq 0x32a0
jmp 0x53d3
leaq -0x370(%rbp), %rdi
movq -0x3a0(%rbp), %rsi
callq 0x9900
jmp 0x53e8
leaq -0x3c0(%rbp), %rdi
callq 0x59c0
leaq -0x390(%rbp), %rdi
callq 0x59e0
jmp 0x534d
movq -0xa08(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
callq 0x3230
jmp 0x578f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x54a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x3c0(%rbp), %rdi
callq 0x59c0
jmp 0x54a6
leaq -0x3e0(%rbp), %rdi
leaq -0x380(%rbp), %rsi
callq 0x3250
jmp 0x545f
movq 0x38b62(%rip), %rdi     # 0x3dfc8
leaq -0x3e0(%rbp), %rsi
callq 0x3220
jmp 0x5474
leaq -0x3e0(%rbp), %rdi
callq 0x3528
leaq -0x380(%rbp), %rdi
callq 0x31e0
jmp 0x54b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x3e0(%rbp), %rdi
callq 0x3528
leaq -0x380(%rbp), %rdi
callq 0x31e0
jmp 0x578f
leaq 0x25b6f(%rip), %rsi     # 0x2b02d
leaq -0x5e0(%rbp), %rdi
movl $0x10, %edx
callq 0x32d0
jmp 0x54d1
leaq -0x5f8(%rbp), %rdi
movq %rdi, -0xa20(%rbp)
callq 0x5a10
movq -0xa20(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x5a30
jmp 0x54f9
leaq 0x38e60(%rip), %rax     # 0x3e360
movq %rax, -0x600(%rbp)
leaq 0x38e52(%rip), %rdi     # 0x3e360
callq 0x58c0
movq %rax, -0x608(%rbp)
leaq 0x38e3f(%rip), %rdi     # 0x3e360
callq 0x58f0
movq %rax, -0x610(%rbp)
leaq -0x608(%rbp), %rdi
leaq -0x610(%rbp), %rsi
callq 0x5920
testb $0x1, %al
jne 0x5546
jmp 0x55c0
leaq -0x608(%rbp), %rdi
callq 0x5950
movq %rax, %rsi
leaq -0x638(%rbp), %rdi
callq 0x5980
jmp 0x5563
leaq -0x618(%rbp), %rsi
leaq -0x5f8(%rbp), %rdi
callq 0x5a30
jmp 0x5578
leaq -0x638(%rbp), %rdi
callq 0x59c0
leaq -0x608(%rbp), %rdi
callq 0x59e0
jmp 0x552d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x5777
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x638(%rbp), %rdi
callq 0x59c0
jmp 0x5777
leaq -0x650(%rbp), %rdi
leaq -0x5f8(%rbp), %rsi
callq 0x5a70
jmp 0x55d5
leaq -0x5e0(%rbp), %rdi
leaq -0x650(%rbp), %rsi
callq 0x6860
jmp 0x55ea
leaq -0x650(%rbp), %rdi
callq 0x5b30
leaq -0x5e0(%rbp), %rdi
callq 0x33b0
jmp 0x5604
cmpl $0x1, 0x38d25(%rip)     # 0x3e330
jne 0x5699
leaq 0x38d20(%rip), %rsi     # 0x3e338
leaq -0x850(%rbp), %rdi
movl $0x10, %edx
callq 0x34f0
jmp 0x562b
movq -0x1f0(%rbp), %rsi
leaq -0x850(%rbp), %rdi
callq 0x11b30
jmp 0x5640
leaq -0x850(%rbp), %rdi
callq 0x33b0
jmp 0x564e
leaq -0x850(%rbp), %rdi
callq 0x33f0
jmp 0x5743
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x650(%rbp), %rdi
callq 0x5b30
jmp 0x5777
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x850(%rbp), %rdi
callq 0x33f0
jmp 0x5777
cmpl $0x0, 0x38c90(%rip)     # 0x3e330
jne 0x5741
leaq -0x9d8(%rbp), %rdi
callq 0x31d0
jmp 0x56b4
leaq -0x9c8(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x11b30
jmp 0x56c9
leaq -0x9f8(%rbp), %rdi
leaq -0x9d8(%rbp), %rsi
callq 0x3250
jmp 0x56de
movq 0x388e3(%rip), %rdi     # 0x3dfc8
leaq -0x9f8(%rbp), %rsi
callq 0x3220
jmp 0x56f3
leaq -0x9f8(%rbp), %rdi
callq 0x3528
leaq -0x9d8(%rbp), %rdi
callq 0x31e0
jmp 0x5741
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x5733
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x9f8(%rbp), %rdi
callq 0x3528
leaq -0x9d8(%rbp), %rdi
callq 0x31e0
jmp 0x5777
jmp 0x5743
movl $0x0, -0x4(%rbp)
leaq -0x5f8(%rbp), %rdi
callq 0x5b30
leaq -0x5e0(%rbp), %rdi
callq 0x33f0
leaq -0x30(%rbp), %rdi
callq 0x19750
movl -0x4(%rbp), %eax
addq $0xa20, %rsp            # imm = 0xA20
popq %rbp
retq
leaq -0x5f8(%rbp), %rdi
callq 0x5b30
leaq -0x5e0(%rbp), %rdi
callq 0x33f0
leaq -0x30(%rbp), %rdi
callq 0x19750
movq -0x38(%rbp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)