// File: /workspace/github_cpp_makefile_star2_O2/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 $0x348, %rsp            # imm = 0x348
leaq 0xe(%rsp), %rdi
callq 0x31a8
leaq 0x193f(%rip), %rdi      # 0xd2c5
leaq 0x174a(%rip), %rsi      # 0xd0d7
callq 0x2210
leaq 0x90(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movq %rax, %rbx
movb $0x0, (%rcx)
leaq 0x80(%rsp), %r14
movq %rbx, %rdi
callq 0x22b0
cmpl $-0x1, %eax
je 0xb9cb
movsbl %al, %esi
movq %r14, %rdi
callq 0x2190
jmp 0xb9b1
leaq 0x110(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x2260
leaq 0x48(%rsp), %rdi
leaq 0xe(%rsp), %rsi
leaq 0x110(%rsp), %rdx
callq 0x31aa
leaq 0x110(%rsp), %rdi
callq 0x20f0
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %r14d, %r14d
pushq $0x28
popq %r15
leaq 0x60(%rsp), %rbx
xorl %r12d, %r12d
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %rax
subq %rcx, %rax
cqto
idivq %r15
cmpq %r12, %rax
jbe 0xba51
movsbl (%rcx,%r14), %esi
movq %rbx, %rdi
callq 0x2190
incq %r12
addq $0x28, %r14
jmp 0xba24
leaq 0xd8(%rsp), %rdi
callq 0x39f8
leaq 0x186c(%rip), %rdi      # 0xd2d1
leaq 0x166b(%rip), %rsi      # 0xd0d7
callq 0x2210
movq %rax, %rbp
leaq 0xc0(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x3b9e
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rax
andq $0x0, 0x10(%rax)
leaq 0x10(%rsp), %rcx
andq $0x0, 0x10(%rcx)
movaps %xmm0, (%rax)
movaps %xmm0, (%rcx)
leaq 0x2e0(%rsp), %r15
leaq 0x250(%rsp), %r12
leaq 0x1e0(%rsp), %r14
leaq 0x130(%rsp), %r13
movq %r15, %rdi
pushq $0x64
popq %rsi
movq %rbp, %rdx
callq 0x22d0
testq %rax, %rax
je 0xbc0b
cmpb $0x23, 0x2e0(%rsp)
jne 0xbb7a
leaq 0x2e1(%rsp), %rdi
leaq 0x17eb(%rip), %rsi      # 0xd2e5
movq %r12, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x21a0
movq %r13, %rdi
movq %r14, %rsi
leaq 0x198(%rsp), %rdx
callq 0x25ae
movq %r13, %rdi
leaq 0x17b7(%rip), %rsi      # 0xd2db
callq 0x36c4
testb %al, %al
je 0xbb3c
movq %r13, %rdi
leaq 0x171f(%rip), %rsi      # 0xd256
callq 0x2370
movsbl 0x250(%rsp), %esi
leaq 0xd8(%rsp), %rdi
movq %r13, %rdx
callq 0x3ab2
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xbe7c
leaq 0x10(%rsp), %rdi
callq 0xbea8
movq %r13, %rdi
callq 0x20f0
jmp 0xbac6
movq %r15, %rdi
leaq 0x175f(%rip), %rsi      # 0xd2e3
movq %r12, %rdx
movq %r14, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x21a0
cmpl $0x3, %eax
jne 0xbac6
movq %r14, %rbx
movq %r15, %r14
movq %r12, %rsi
leaq 0x1c0(%rsp), %r15
movq %r15, %rdi
leaq 0xf(%rsp), %rdx
callq 0x25ae
movsbl 0x1e0(%rsp), %edx
movsbl 0x130(%rsp), %ecx
leaq 0x198(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x2790
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xc156
movq %r13, %rdi
callq 0x20f0
leaq 0x1c0(%rsp), %rdi
callq 0x20f0
movq %r14, %r15
movq %rbx, %r14
jmp 0xbabe
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xbe7c
movq %rbp, %rdi
callq 0x21b0
leaq 0xa0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x2260
leaq 0x1e0(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
pushq $0x1
popq %rcx
callq 0x4430
leaq 0xa0(%rsp), %rdi
callq 0x20f0
leaq 0x250(%rsp), %rdi
callq 0xbeb0
leaq 0x130(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x1e0(%rsp), %r8
leaq 0x30(%rsp), %r9
callq 0x5fd0
leaq 0x1646(%rip), %rbx      # 0xd2ea
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
cmpq %r14, %rcx
jbe 0xbce0
incq %r14
movq (%rax,%r15), %rdx
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x2040
addq $0x20, %r15
jmp 0xbcaa
leaq 0x198(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x7ee4
leaq 0x130(%rsp), %rdi
callq 0x48d0
leaq 0x250(%rsp), %rdi
callq 0xbeda
leaq 0x1e0(%rsp), %rdi
callq 0x56e6
leaq 0x10(%rsp), %rdi
callq 0x75fa
leaq 0x30(%rsp), %rdi
callq 0xbf06
leaq 0xc0(%rsp), %rdi
callq 0x48ae
leaq 0xd8(%rsp), %rdi
callq 0xbf28
leaq 0x60(%rsp), %rdi
callq 0x20f0
leaq 0x48(%rsp), %rdi
callq 0x3766
leaq 0x80(%rsp), %rdi
callq 0x20f0
xorl %eax, %eax
addq $0x348, %rsp            # imm = 0x348
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x48d0
jmp 0xbd94
movq %rax, %rbx
leaq 0x250(%rsp), %rdi
callq 0xbeda
jmp 0xbda6
movq %rax, %rbx
leaq 0x1e0(%rsp), %rdi
callq 0x56e6
jmp 0xbe24
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0xbe1f
jmp 0xbe05
jmp 0xbe05
movq %rax, %rbx
jmp 0xbe45
jmp 0xbe0a
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x20f0
jmp 0xbe66
jmp 0xbe0f
movq %rax, %rbx
jmp 0xbdfb
jmp 0xbe05
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x20f0
leaq 0x1c0(%rsp), %rdi
jmp 0xbe1f
movq %rax, %rbx
jmp 0xbe24
movq %rax, %rbx
jmp 0xbe52
movq %rax, %rbx
jmp 0xbe66
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x20f0
leaq 0x10(%rsp), %rdi
callq 0x75fa
leaq 0x30(%rsp), %rdi
callq 0xbf06
leaq 0xc0(%rsp), %rdi
callq 0x48ae
leaq 0xd8(%rsp), %rdi
callq 0xbf28
leaq 0x60(%rsp), %rdi
callq 0x20f0
leaq 0x48(%rsp), %rdi
callq 0x3766
leaq 0x80(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2330
nop