// File: /workspace/github_cpp_makefile_star2_O2/sighingnow[P]compile-pascal/pl0c.cpp
// Function: main
int main(int argc, char **argv) {
    bool opt;
    if (argc == 3 && string(argv[2]) == "-O") {
        opt = true;
    }
    else {
        opt = false;
    }
    auto parse_tool = ParsecT<decltype(pl0_program)>(pl0_program);
    input_t *in = load_case(argv[1]);
    auto res = parse_tool(in);
    if (!res.status) {
        cout << "Errors occurred during parsing." << endl;
        return 0;
    }
    if (!res.strict) {
        cout << "Extra characters in source text." << endl;
    }

    if (!pl0_tac_program(res.actual)) {
        cout << "Errors occurred during semantic analysing." << endl;
    }

    cout << "\n;; <<<<<<<<<<<<<  All Basic Blocks <<<<<<<<<<<<<<<<<<<<<<<<<<<\n" << endl;

    std::vector<BasicBlock> bbs;
    pl0_block(irb.irs, bbs);
    for (auto && bb: bbs) {
        bb.dump();
        if (opt) {
            cout << ";;;;;;;;;;;;;;;;;;;;;DO DAG Pass" << endl;
            bb.DAGPass();
            bb.dump();
        }
    }

    cout << "\n;; <<<<<<<<<<<<<  Assembly (Intel syntax, nasm)   <<<<<<<<<<<<\n" << endl;

    try { // try exception throwed during assembly code generating.
        pl0_x86_gen(string(argv[1]), bbs);
    } catch (std::exception & e) {
        cout << string(";; !!!Exception: ") + string(e.what()) << endl;
    } catch (...) {
        cout << string(";; !!!Exception: ") + "unknown exception." << endl;
    }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %rbx
cmpl $0x3, %edi
jne 0x577f8
movq 0x10(%rbx), %rsi
leaq 0xf0(%rsp), %r14
leaq 0xb0(%rsp), %rdx
movq %r14, %rdi
callq 0xfa26
leaq 0x3c4a(%rip), %rsi      # 0x5b42e
movq %r14, %rdi
callq 0x15c52
movl %eax, %ebp
movq %r14, %rdi
callq 0x5120
jmp 0x577fa
xorl %ebp, %ebp
leaq 0x3416f(%rip), %rsi     # 0x8b970
leaq 0xf0(%rsp), %rdi
callq 0x57ba8
movq 0x8(%rbx), %rdi
callq 0x57684
leaq 0xb0(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
movq %rax, %rdx
callq 0x57da6
cmpb $0x0, 0xb0(%rsp)
je 0x57922
cmpb $0x0, 0xe8(%rsp)
jne 0x57862
movq 0x3378a(%rip), %rdi     # 0x8afd8
leaq 0x3bfc(%rip), %rsi      # 0x5b451
callq 0x5250
movq %rax, %rdi
callq 0x5130
movq 0xc0(%rsp), %rdi
callq 0x3dafe
testb %al, %al
jne 0x5788e
movq 0x3375e(%rip), %rdi     # 0x8afd8
leaq 0x3bf1(%rip), %rsi      # 0x5b472
callq 0x5250
movq %rax, %rdi
callq 0x5130
movq 0x33743(%rip), %rdi     # 0x8afd8
leaq 0x3c01(%rip), %rsi      # 0x5b49d
callq 0x5250
movq %rax, %rdi
callq 0x5130
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x34d10(%rip), %rdi     # 0x8c5d0
addq $0x10, %rdi
pushq $0x1
popq %rdx
callq 0x4b066
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r13
movq 0x336fb(%rip), %r15     # 0x8afd8
leaq 0x3bfa(%rip), %r12      # 0x5b4de
cmpq %r13, %r14
je 0x5793f
movq %r14, %rdi
callq 0x49b82
testb %bpl, %bpl
je 0x57919
movq %r15, %rdi
movq %r12, %rsi
callq 0x5250
movq %rax, %rdi
callq 0x5130
movq %r14, %rdi
callq 0x4b04e
movq %r14, %rdi
callq 0x49b82
addq $0xc0, %r14
jmp 0x578e4
movq 0x336af(%rip), %rdi     # 0x8afd8
leaq 0x3b01(%rip), %rsi      # 0x5b431
callq 0x5250
movq %rax, %rdi
callq 0x5130
jmp 0x57999
movq 0x33692(%rip), %rdi     # 0x8afd8
leaq 0x3bb2(%rip), %rsi      # 0x5b4ff
callq 0x5250
movq %rax, %rdi
callq 0x5130
movq 0x8(%rbx), %rsi
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0xfa26
leaq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x50410
leaq 0x90(%rsp), %rdi
callq 0x5120
leaq 0x30(%rsp), %rdi
callq 0x57b86
leaq 0xc8(%rsp), %rdi
callq 0x5120
leaq 0xf0(%rsp), %rdi
callq 0x3c02a
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rbx
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x5120
jmp 0x579e2
movq %rdx, %rbx
movq %rax, %r14
movq %r14, %rdi
callq 0x50f0
cmpl $0x2, %ebx
jne 0x57a7c
movq %rax, %r14
leaq 0x3b43(%rip), %rsi      # 0x5b540
leaq 0x50(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0xfa26
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x70(%rsp), %rdi
leaq 0xe(%rsp), %rdx
movq %rax, %rsi
callq 0xfa26
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0xfd33
movq 0x33596(%rip), %rdi     # 0x8afd8
leaq 0x10(%rsp), %rsi
callq 0x5210
movq %rax, %rdi
callq 0x5130
leaq 0x10(%rsp), %rdi
callq 0x5120
leaq 0x70(%rsp), %rdi
callq 0x5120
leaq 0x50(%rsp), %rdi
callq 0x5120
callq 0x53d0
jmp 0x5798f
leaq 0x3abd(%rip), %rsi      # 0x5b540
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0xfa26
leaq 0x3ab9(%rip), %rdx      # 0x5b552
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xfd14
movq 0x33529(%rip), %rdi     # 0x8afd8
leaq 0x10(%rsp), %rsi
callq 0x5210
movq %rax, %rdi
callq 0x5130
leaq 0x10(%rsp), %rdi
callq 0x5120
leaq 0x50(%rsp), %rdi
callq 0x5120
callq 0x53d0
jmp 0x5798f
movq %rax, %rbx
jmp 0x57b07
movq %rax, %rbx
jmp 0x57b11
jmp 0x57b56
movq %rax, %rbx
jmp 0x57b25
movq %rax, %rbx
jmp 0x57b2f
movq %rax, %rbx
jmp 0x57b39
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5120
leaq 0x50(%rsp), %rdi
callq 0x5120
callq 0x53d0
jmp 0x57b59
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5120
leaq 0x70(%rsp), %rdi
callq 0x5120
leaq 0x50(%rsp), %rdi
callq 0x5120
callq 0x53d0
jmp 0x57b59
movq %rax, %rdi
callq 0x16924
jmp 0x57b4a
movq %rax, %rbx
jmp 0x57b70
jmp 0x57b56
movq %rax, %rbx
jmp 0x57b63
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x57b86
leaq 0xc8(%rsp), %rdi
callq 0x5120
leaq 0xf0(%rsp), %rdi
callq 0x3c02a
movq %rbx, %rdi
callq 0x53f0
nop