// File: /workspace/github_cpp_makefile_star2_O3/davidsun[P]TinyCompiler/main.cpp
// Function: main
int main(void){
    LexicalAnalytics la;
    FILE *fprog = fopen("program.txt", "r");
    string program;
    int ch;
    while ((ch = fgetc(fprog)) != EOF){
        program += (char)ch;
    }
    vector <pair <char, string> > la_result = la.parse(program);
    string la_result_str;
    for (int i = 0; i < la_result.size(); i ++) la_result_str += la_result[i].first;

    LRAnalytics lra;
    FILE *f = fopen("input.txt", "r");
    vector <ItemSet> sets = lra.genStates();
    vector <ListCodeItems> code_items;
    ListCodeItems code_items_tmp;
    char s[100];
    while (fgets(s, 100, f)){
        if (s[0] == '#'){
            char s1[100], s2[100];
            sscanf(s + 1, "%s%s", s1, s2);
            string ss2 = s2;
            if (ss2 == "<EMPTY>") ss2 = "";
            lra.addRule(s1[0], ss2);
            code_items.push_back(code_items_tmp);
            code_items_tmp.clear();
        }  else {
            char s1[100], s2[100], s3[100];
            if (sscanf(s, "%s%s%s", s1, s2, s3) == 3) code_items_tmp.push_back(CodeItem(s1, s2[0], s3[0]));
        }
    }
    code_items.push_back(code_items_tmp);
    fclose(f);

    vector <int> lra_result = lra.parse(la_result_str, true);
    CodeGenerator g;
    vector <string> ret = g.parse(lra, la_result, lra_result, code_items);
    for (int i = 0; i < ret.size(); i ++){
        printf("%d: %s\n", i + 1, ret[i].c_str());
    }

    CodeRunner r;
    r.execute(ret);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
leaq 0xf(%rsp), %rdi
callq 0x3660
leaq 0x1863(%rip), %rdi      # 0x102c5
leaq 0x166e(%rip), %rsi      # 0x100d7
callq 0x2200
movq %rax, %rbx
leaq 0x70(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
leaq 0x60(%rsp), %r14
movq %rbx, %rdi
callq 0x2290
cmpl $-0x1, %eax
je 0xeaa5
movsbl %al, %esi
movq %r14, %rdi
callq 0x2170
jmp 0xea8b
leaq 0x120(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
addq %rsi, %rdx
leaq 0x110(%rsp), %rdi
callq 0x2744
leaq 0x28(%rsp), %rdi
leaq 0xf(%rsp), %rsi
leaq 0x110(%rsp), %rdx
callq 0x3662
movq 0x110(%rsp), %rdi
cmpq %r14, %rdi
je 0xeaff
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x28(%rsp), %rax
cmpq %rax, 0x30(%rsp)
je 0xeb62
xorl %r14d, %r14d
leaq 0x40(%rsp), %rbx
movabsq $-0x3333333333333333, %r15 # imm = 0xCCCCCCCCCCCCCCCD
xorl %r12d, %r12d
movsbl (%rax,%r14), %esi
movq %rbx, %rdi
callq 0x2170
incq %r12
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r15, %rcx
addq $0x28, %r14
cmpq %r12, %rcx
ja 0xeb34
leaq 0x1a0(%rsp), %rdi
callq 0x41e4
leaq 0x175b(%rip), %rdi      # 0x102d1
leaq 0x155a(%rip), %rsi      # 0x100d7
callq 0x2200
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x4466
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
xorl %eax, %eax
movq %rax, 0x90(%rsp)
movq %rax, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
leaq 0x2f0(%rsp), %rdi
movl $0x64, %esi
movq %rbx, %rdx
callq 0x22b0
testq %rax, %rax
je 0xee33
leaq 0x260(%rsp), %r15
leaq 0x1f0(%rsp), %r12
leaq 0x2f0(%rsp), %r14
leaq 0xa0(%rsp), %rbp
leaq 0xc8(%rsp), %r13
movq %rbx, 0xc0(%rsp)
cmpb $0x23, 0x2f0(%rsp)
jne 0xed52
movq %rbp, %r13
movq %r14, %rbp
leaq 0x2f1(%rsp), %rdi
leaq 0x16bb(%rip), %rsi      # 0x102e5
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2180
leaq 0x140(%rsp), %rax
movq %rax, 0x130(%rsp)
movq %r12, %rdi
callq 0x20d0
leaq (%rsp,%rax), %rdx
addq $0x1f0, %rdx            # imm = 0x1F0
leaq 0x130(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x2892
movq %r14, %rdi
leaq 0x1664(%rip), %rsi      # 0x102db
callq 0x2090
testl %eax, %eax
jne 0xec9c
movq 0x138(%rsp), %rdx
movq %r14, %rdi
xorl %esi, %esi
leaq 0x15c2(%rip), %rcx      # 0x10256
xorl %r8d, %r8d
callq 0x2270
movsbl 0x260(%rsp), %esi
leaq 0x1a0(%rsp), %rdi
movq %r14, %rdx
callq 0x4336
leaq 0x80(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xf268
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
cmpq %r15, %r12
je 0xed02
leaq 0x10(%r15), %r14
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0xeced
movq (%r14), %rsi
incq %rsi
callq 0x21b0
leaq 0x28(%r14), %rax
addq $0x18, %r14
cmpq %r12, %r14
movq %rax, %r14
jne 0xecd9
movq %r15, 0x18(%rsp)
movq 0x130(%rsp), %rdi
leaq 0x140(%rsp), %rax
cmpq %rax, %rdi
je 0xed27
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0xc0(%rsp), %rbx
leaq 0x260(%rsp), %r15
leaq 0x1f0(%rsp), %r12
movq %rbp, %r14
movq %r13, %rbp
leaq 0xc8(%rsp), %r13
jmp 0xee1a
movq %r14, %rdi
leaq 0x1587(%rip), %rsi      # 0x102e3
movq %r15, %rdx
movq %r12, %rcx
leaq 0x130(%rsp), %r8
xorl %eax, %eax
callq 0x2180
cmpl $0x3, %eax
jne 0xee1a
leaq 0xb0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq %r15, %rdi
callq 0x20d0
leaq (%rsp,%rax), %rdx
addq $0x260, %rdx            # imm = 0x260
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2892
movsbl 0x1f0(%rsp), %edx
movsbl 0x130(%rsp), %ecx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2a28
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xf52e
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0xedf5
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0xee1a
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %r14, %rdi
movl $0x64, %esi
movq %rbx, %rdx
callq 0x22b0
testq %rax, %rax
jne 0xec07
leaq 0x80(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xf268
movq %rbx, %rdi
callq 0x2190
leaq 0x100(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0xf0(%rsp), %rdi
callq 0x2744
leaq 0x1f0(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
leaq 0xf0(%rsp), %rdx
movl $0x1, %ecx
callq 0x514e
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0xeeb2
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x270(%rsp), %rdi
callq 0x41e4
xorps %xmm0, %xmm0
leaq 0x260(%rsp), %rsi
movups %xmm0, 0x78(%rsi)
movups %xmm0, 0x68(%rsi)
movups %xmm0, 0x58(%rsi)
movups %xmm0, 0x48(%rsi)
movq $0x0, 0x88(%rsi)
leaq 0x130(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x1f0(%rsp), %r8
leaq 0x80(%rsp), %r9
callq 0x6f10
movq 0x130(%rsp), %rax
cmpq %rax, 0x138(%rsp)
je 0xef62
leaq 0x13c2(%rip), %rbx      # 0x102ea
xorl %r15d, %r15d
xorl %r14d, %r14d
incq %r14
movq (%rax,%r15), %rdx
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x2040
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
addq $0x20, %r15
cmpq %r14, %rcx
ja 0xef2e
leaq 0xc8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x9d8c
leaq 0x2a8(%rsp), %rbx
leaq 0x130(%rsp), %rdi
callq 0x57a8
leaq 0x2d8(%rsp), %r14
movq %r14, %rdi
callq 0xf2e4
movq -0x18(%r14), %rdi
testq %rdi, %rdi
leaq 0x70(%rsp), %r14
je 0xefba
movq 0x2d0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x3eb6
leaq 0x290(%rsp), %rbx
movq %rbx, %rdi
callq 0x3190
movq -0x20(%rbx), %rdi
leaq 0x280(%rsp), %rax
cmpq %rax, %rdi
je 0xeff3
movq 0x280(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x1f0(%rsp), %rdi
testq %rdi, %rdi
je 0xf010
movq 0x200(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
leaq 0x10(%rsp), %rdi
callq 0x92ae
leaq 0x80(%rsp), %rdi
callq 0xf2e4
leaq 0x1d8(%rsp), %rdi
callq 0x5760
leaq 0x1c0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3190
movq -0x20(%rbx), %rdi
leaq 0x1b0(%rsp), %rax
cmpq %rax, %rdi
je 0xf065
movq 0x1b0(%rsp), %rsi
incq %rsi
callq 0x21b0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xf081
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x28(%rsp), %rdi
callq 0x3eb6
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xf0a2
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21b0
xorl %eax, %eax
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
leaq 0x130(%rsp), %rdi
callq 0x57a8
jmp 0xf0cb
movq %rax, %r12
leaq 0x260(%rsp), %rdi
callq 0xf294
jmp 0xf0dd
movq %rax, %r12
movq 0x1f0(%rsp), %rdi
testq %rdi, %rdi
je 0xf1e7
movq 0x200(%rsp), %rsi
subq %rdi, %rsi
jmp 0xf1e2
movq %rax, %r12
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0xf1e7
movq 0x100(%rsp), %rsi
jmp 0xf1df
jmp 0xf1b5
jmp 0xf1b5
movq %rax, %r12
jmp 0xf20b
jmp 0xf1ba
movq %rax, %r12
movq 0x110(%rsp), %rdi
cmpq %r14, %rdi
je 0xf23e
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0xf23e
movq %rax, %r12
jmp 0xf23e
movq %rax, %r12
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0xf194
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21b0
jmp 0xf194
movq %rax, %r12
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0xf1e7
movq 0xb0(%rsp), %rsi
jmp 0xf1df
jmp 0xf1b5
movq %rax, %r12
jmp 0xf1e7
movq %rax, %r12
jmp 0xf218
movq %rax, %r12
movq 0x130(%rsp), %rdi
leaq 0x140(%rsp), %rax
cmpq %rax, %rdi
je 0xf1e7
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x10(%rsp), %rdi
callq 0x92ae
leaq 0x80(%rsp), %rdi
callq 0xf2e4
leaq 0x1d8(%rsp), %rdi
callq 0x5760
leaq 0x1a0(%rsp), %rdi
callq 0xf32c
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0xf234
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21b0
leaq 0x28(%rsp), %rdi
callq 0x3eb6
leaq 0x70(%rsp), %r15
jmp 0xf248
movq %rax, %r12
movq 0x60(%rsp), %rdi
cmpq %r15, %rdi
je 0xf25f
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x21b0
movq %r12, %rdi
callq 0x22f0
nop