// File: 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
movq %rsp, %rbp
subq $0x580, %rsp            # imm = 0x580
movl $0x0, -0x4(%rbp)
leaq -0x5(%rbp), %rdi
callq 0x6250
leaq 0x251b(%rip), %rdi      # 0x2547d
leaq 0x2331(%rip), %rsi      # 0x2529a
callq 0x3320
movq %rax, -0x10(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x33a0
movq -0x10(%rbp), %rdi
callq 0x33f0
movl %eax, -0x52c(%rbp)
jmp 0x22f8c
movl -0x52c(%rbp), %eax
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
je 0x22fbc
movsbl -0x34(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x31b0
jmp 0x22fa9
jmp 0x22f7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2360a
leaq -0x80(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x3120
jmp 0x22fcb
leaq -0x60(%rbp), %rdi
leaq -0x5(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x6260
jmp 0x22fde
leaq -0x80(%rbp), %rdi
callq 0x31a0
leaq -0xa0(%rbp), %rdi
callq 0x33a0
movl $0x0, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
movq %rax, -0x538(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x7890
movq %rax, %rcx
movq -0x538(%rbp), %rax
cmpq %rcx, %rax
jae 0x23082
movslq -0xa4(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x122c0
movsbl (%rax), %esi
leaq -0xa0(%rbp), %rdi
callq 0x31b0
jmp 0x23044
jmp 0x23046
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x22ffd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x31a0
jmp 0x2360a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x235f5
leaq -0xe0(%rbp), %rdi
callq 0x7c90
jmp 0x23090
leaq 0x23f2(%rip), %rdi      # 0x25489
leaq 0x21fc(%rip), %rsi      # 0x2529a
callq 0x3320
movq %rax, -0x540(%rbp)
jmp 0x230ac
movq -0x540(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x7f10
jmp 0x230cf
leaq -0x118(%rbp), %rdi
callq 0x23620
leaq -0x130(%rbp), %rdi
callq 0x23640
movq -0xe8(%rbp), %rdx
leaq -0x1a0(%rbp), %rdi
movl $0x64, %esi
callq 0x3410
movq %rax, -0x548(%rbp)
jmp 0x23108
movq -0x548(%rbp), %rax
cmpq $0x0, %rax
je 0x2338e
movsbl -0x1a0(%rbp), %eax
cmpl $0x23, %eax
jne 0x23276
leaq -0x19f(%rbp), %rdi
leaq 0x2366(%rip), %rsi      # 0x2549d
xorl %eax, %eax
leaq -0x210(%rbp), %rdx
leaq -0x280(%rbp), %rcx
movq %rcx, -0x558(%rbp)
callq 0x3260
leaq -0x2a1(%rbp), %rdi
movq %rdi, -0x550(%rbp)
callq 0x3490
movq -0x558(%rbp), %rsi
movq -0x550(%rbp), %rdx
leaq -0x2a0(%rbp), %rdi
callq 0x3870
jmp 0x23182
leaq -0x2a1(%rbp), %rdi
callq 0x32e0
leaq 0x22fe(%rip), %rsi      # 0x25493
leaq -0x2a0(%rbp), %rdi
callq 0x6de0
movb %al, -0x559(%rbp)
jmp 0x231a9
movb -0x559(%rbp), %al
testb $0x1, %al
jne 0x231b5
jmp 0x23228
leaq 0x2252(%rip), %rsi      # 0x2540e
leaq -0x2a0(%rbp), %rdi
callq 0x3390
jmp 0x231ca
jmp 0x23228
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x235e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x235c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2a1(%rbp), %rdi
callq 0x32e0
jmp 0x235c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x31a0
jmp 0x235c5
movsbl -0x210(%rbp), %esi
leaq -0xe0(%rbp), %rdi
leaq -0x2a0(%rbp), %rdx
callq 0x7d80
jmp 0x23244
leaq -0x118(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x23660
jmp 0x23259
leaq -0x130(%rbp), %rdi
callq 0x236d0
leaq -0x2a0(%rbp), %rdi
callq 0x31a0
jmp 0x23389
leaq -0x1a0(%rbp), %rdi
leaq -0x310(%rbp), %rdx
leaq -0x380(%rbp), %rcx
leaq -0x3f0(%rbp), %r8
leaq 0x2202(%rip), %rsi      # 0x2549b
movb $0x0, %al
callq 0x3260
cmpl $0x3, %eax
jne 0x23387
leaq -0x439(%rbp), %rdi
movq %rdi, -0x568(%rbp)
callq 0x3490
movq -0x568(%rbp), %rdx
leaq -0x438(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x3870
jmp 0x232d8
movsbl -0x380(%rbp), %edx
movsbl -0x3f0(%rbp), %ecx
leaq -0x418(%rbp), %rdi
leaq -0x438(%rbp), %rsi
callq 0x3e80
jmp 0x232fb
leaq -0x130(%rbp), %rdi
leaq -0x418(%rbp), %rsi
callq 0x236f0
jmp 0x23310
leaq -0x418(%rbp), %rdi
callq 0x14870
leaq -0x438(%rbp), %rdi
callq 0x31a0
leaq -0x439(%rbp), %rdi
callq 0x32e0
jmp 0x23387
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x23376
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2336a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x418(%rbp), %rdi
callq 0x14870
leaq -0x438(%rbp), %rdi
callq 0x31a0
leaq -0x439(%rbp), %rdi
callq 0x32e0
jmp 0x235c5
jmp 0x23389
jmp 0x230e7
leaq -0x118(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x23660
jmp 0x233a3
movq -0xe8(%rbp), %rdi
callq 0x3270
jmp 0x233b1
leaq -0x478(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x3120
jmp 0x233c6
leaq -0x458(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
leaq -0x478(%rbp), %rdx
movl $0x1, %ecx
callq 0x8e10
jmp 0x233e7
leaq -0x478(%rbp), %rdi
callq 0x31a0
leaq -0x508(%rbp), %rdi
callq 0x23720
jmp 0x23401
leaq -0x520(%rbp), %rdi
leaq -0x508(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
leaq -0x60(%rbp), %rcx
leaq -0x458(%rbp), %r8
leaq -0x118(%rbp), %r9
callq 0x10120
jmp 0x2342f
movl $0x0, -0x524(%rbp)
movslq -0x524(%rbp), %rax
movq %rax, -0x570(%rbp)
leaq -0x520(%rbp), %rdi
callq 0x9a50
movq %rax, %rcx
movq -0x570(%rbp), %rax
cmpq %rcx, %rax
jae 0x23513
movslq -0x524(%rbp), %rsi
movl %esi, %eax
incl %eax
movl %eax, -0x574(%rbp)
leaq -0x520(%rbp), %rdi
callq 0x9a70
movq %rax, %rdi
callq 0x3110
movl -0x574(%rbp), %esi
movq %rax, %rdx
leaq 0x2007(%rip), %rdi      # 0x254a2
xorl %eax, %eax
callq 0x3060
jmp 0x234a4
jmp 0x234a6
movl -0x524(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x524(%rbp)
jmp 0x23439
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x478(%rbp), %rdi
callq 0x31a0
jmp 0x235c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x235b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x235ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x520(%rbp), %rdi
callq 0x9810
jmp 0x235ad
leaq -0x525(%rbp), %rdi
leaq -0x520(%rbp), %rsi
callq 0x17580
jmp 0x23528
movl $0x0, -0x4(%rbp)
leaq -0x520(%rbp), %rdi
callq 0x9810
leaq -0x508(%rbp), %rdi
callq 0x23770
leaq -0x458(%rbp), %rdi
callq 0x9db0
leaq -0x130(%rbp), %rdi
callq 0x149b0
leaq -0x118(%rbp), %rdi
callq 0x237c0
leaq -0x100(%rbp), %rdi
callq 0x9740
leaq -0xe0(%rbp), %rdi
callq 0x23820
leaq -0xa0(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x6fb0
leaq -0x30(%rbp), %rdi
callq 0x31a0
movl -0x4(%rbp), %eax
addq $0x580, %rsp            # imm = 0x580
popq %rbp
retq
leaq -0x508(%rbp), %rdi
callq 0x23770
leaq -0x458(%rbp), %rdi
callq 0x9db0
leaq -0x130(%rbp), %rdi
callq 0x149b0
leaq -0x118(%rbp), %rdi
callq 0x237c0
leaq -0x100(%rbp), %rdi
callq 0x9740
leaq -0xe0(%rbp), %rdi
callq 0x23820
leaq -0xa0(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x6fb0
leaq -0x30(%rbp), %rdi
callq 0x31a0
movq -0x40(%rbp), %rdi
callq 0x3480
nopl (%rax)