// File: /workspace/github_c_makefile_star2_O3/gregtour[P]minimal-compiler/parser.c
// Function: ParseSource
SYNTAX_TREE* ParseSource(L_TOKEN*      input,
                         LR_TABLE      parser,
                         GRAMMAR_TABLE grammar)
{
    // the program's syntax tree
    SYNTAX_TREE* ast = NULL;

    // set ip and init stack
    L_TOKEN* ip = input;
    StackPushState(0);
    // printf("Parsing source...\n");

    // loop forever
    for (;;)
    {
        ACTION action;
        // get the state off the top of the stack
        PARSE_STACK s = StackPeek();
        if (s.token || s.state == -1
                || s.state >= parser.numStates
                || ip == NULL)
        {
            if (ip)
                printf("Parse error: invalid state on top of the stack.\n");
            else
                printf("Parse error: unexpected end of input.\n");
            return 0;
        }
        
        // find the action table entry for the state and input
        action = ActionTable(parser, s.state, ip->token);

        // perform the parse action
        if (action.type == ACTION_SHIFT)
        {
            // push a then s' on top of the stack;
            StackPushToken(ip);
            StackPushState(action.value);
            // advance ip to the next input symbol
            ip = ip->next;
        }
        else if (action.type == ACTION_REDUCE)
        {
            SYNTAX_TREE* node;
            RULE r;
            int rhs;
            
            // output the production A -> B
            if (action.value < 0 || action.value >= grammar.numRules) {
                printf("Parse error: invalid production for reduce action.\n");
                PrintErrorInput(ip, grammar);
                return 0;
            }
            r = grammar.rules[action.value];
            //printf("%s\n", GetElement(r.lhs, grammar));

            // pop 2*|B| symbols off the stack;
            node = (SYNTAX_TREE*)malloc(sizeof(SYNTAX_TREE));
            node->token = r.lhs;
            node->production = action.value;
            node->string = NULL;
            node->length = 0;
            node->line = 0;
            node->children = (SYNTAX_TREE**)malloc(r.rhsLength * sizeof(SYNTAX_TREE*));
            node->numChildren = r.rhsLength;
            for (rhs = 0; rhs < r.rhsLength; rhs++)
            {
                PARSE_STACK symbol;
                int child = r.rhsLength - rhs - 1;

                StackPop(); // state
                symbol = StackPop();
                if (symbol.token == NULL)
                {
                    printf("Parse error: expected token.\n");
                    PrintErrorInput(ip, grammar);
                    return 0;
                }
                
                if (symbol.type == TOKEN_L_TOKEN)
                {
                    L_TOKEN* token = (L_TOKEN*)symbol.token;
                    node->children[child] = (SYNTAX_TREE*)malloc(sizeof(SYNTAX_TREE));
                    node->children[child]->token = token->token;
                    node->children[child]->production = 0;
                    node->children[child]->string = token->string;
                    node->children[child]->length = token->length;
                    node->children[child]->line = token->line;
                    node->children[child]->children = NULL;
                    node->children[child]->numChildren = 0;
                }
                else if (symbol.type == TOKEN_SYNTAX_TREE)
                {
                    node->children[child] = (SYNTAX_TREE*)symbol.token;
                }
                else
                {
                    printf("Parse error: expected token object.\n");
                    PrintErrorInput(ip, grammar);
                }
            }
            if (r.rhsLength) node->line = node->children[0]->line;
                
            // let s' be the state now on top of the stack;
            s = StackPeek();
            if (s.token || s.state == -1 || s.state >= parser.numStates)
            {
                printf("Parse error: expected state on top of stack.\n");
                PrintErrorInput(ip, grammar);

                FreeParseStack();

                return 0;
            }
            
            // push A then goto[s', A] on top of the stack;
            if (r.lhs == gSymbolGoal)
            {
                ast = node;
            }
            StackPushTree(node);
            StackPushState(GotoTable(parser, s.state, r.lhs));
        }
        else if (action.type == ACTION_ACCEPT)
        {
            // accept
            // printf("Parse accepted!\n");
            break;
        }
        else
        {
            // error
            printf("Parse error: illegal action type.\n");
            PrintErrorInput(ip, grammar);

            FreeParseStack();

            return 0;
        }
    }
    
    // free the stack
    while (gParseStack)
    {
        StackPop();
    }

    // no errors
    return ast;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r14
movq 0x13f64(%rip), %rbx     # 0x19350
movl $0x20, %edi
callq 0x2100
movq %rax, %r13
leaq 0x10(%rbx), %rax
testq %rbx, %rbx
leaq 0x13f41(%rip), %rcx     # 0x19348
cmovneq %rax, %rcx
movq %r13, (%rcx)
movq %rbx, 0x18(%r13)
movq %r13, 0x13f37(%rip)     # 0x19350
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq $0x0, 0x10(%r13)
movl 0xc(%r13), %eax
cmpl $-0x1, %eax
je 0x5756
movl 0x18(%rbp), %ecx
movl %ecx, -0x30(%rbp)
movl 0x10(%rbp), %edx
movl 0x14(%rbp), %ecx
movl %ecx, -0x2c(%rbp)
movq 0x20(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq 0x28(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
movq %r14, %rbx
movl %edx, -0x48(%rbp)
cmpl -0x30(%rbp), %eax
jge 0x5759
testq %rbx, %rbx
je 0x5759
testl %eax, %eax
js 0x57da
movl (%rbx), %ecx
movl $0xffffdfff, %esi       # imm = 0xFFFFDFFF
andl %esi, %ecx
jle 0x57da
cmpl %edx, %ecx
jg 0x57da
imull %edx, %eax
addl %ecx, %eax
movq -0x58(%rbp), %rdx
movl -0x8(%rdx,%rax,8), %ecx
movl -0x4(%rdx,%rax,8), %r14d
cmpl $0x1, %ecx
je 0x56e6
cmpl $0x2, %ecx
jne 0x57af
testl %r14d, %r14d
leaq 0x30(%rbp), %r15
js 0x5853
cmpl %r14d, 0x20(%r15)
jle 0x5853
movq %rbx, -0x40(%rbp)
movq 0x28(%r15), %rax
movq %r14, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %r15d
movslq 0x4(%rax,%rcx), %rbx
movl $0x30, %edi
callq 0x2100
movq %rax, %r13
movl %r15d, -0x44(%rbp)
movl %r15d, (%rax)
movl %r14d, 0x4(%rax)
leaq (,%rbx,8), %rdi
callq 0x2100
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r13)
movq %rax, 0x28(%r13)
movl %ebx, 0x20(%r13)
testq %rbx, %rbx
jle 0x55f5
leaq -0x1(%rbx), %r14
movq %rbx, %r12
leaq -0x78(%rbp), %r15
movq %r15, %rdi
callq 0x4f3d
movq %r15, %rdi
callq 0x4f3d
movq -0x78(%rbp), %r15
testq %r15, %r15
je 0x5767
movl -0x70(%rbp), %eax
cmpl $0x2, %eax
je 0x55ad
cmpl $0x1, %eax
jne 0x55b7
movl $0x30, %edi
callq 0x2100
movq 0x28(%r13), %rcx
movq %rax, (%rcx,%r14,8)
movl (%r15), %eax
movq 0x28(%r13), %rcx
movq (%rcx,%r14,8), %rdx
movl %eax, (%rdx)
xorl %esi, %esi
movl %esi, 0x4(%rdx)
movq 0x8(%r15), %rax
movq %rax, 0x8(%rdx)
movl 0x10(%r15), %eax
movq (%rcx,%r14,8), %rcx
movl %eax, 0x10(%rcx)
movl 0x14(%r15), %eax
movl %eax, 0x14(%rcx)
movq $0x0, 0x28(%rcx)
movq 0x28(%r13), %rax
movq (%rax,%r14,8), %rax
movl %esi, 0x20(%rax)
jmp 0x55e9
movq 0x28(%r13), %rax
movq %r15, (%rax,%r14,8)
jmp 0x55e9
leaq 0x30e4(%rip), %rdi      # 0x86a2
callq 0x2050
leaq 0x30(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x40(%rbp), %rdi
callq 0x5051
decq %r14
decq %r12
jne 0x552c
testl %ebx, %ebx
je 0x5607
movq 0x28(%r13), %rax
movq (%rax), %rax
movl 0x14(%rax), %eax
movl %eax, 0x14(%r13)
movq 0x13d42(%rip), %r14     # 0x19350
testq %r14, %r14
movq -0x38(%rbp), %rcx
je 0x5808
cmpq $0x0, (%r14)
jne 0x5808
movl 0xc(%r14), %ebx
cmpl $-0x1, %ebx
je 0x5808
cmpl -0x30(%rbp), %ebx
jge 0x5808
leaq 0x6a6e(%rip), %rax      # 0xc0b0
movl -0x44(%rbp), %r15d
cmpl (%rax), %r15d
cmoveq %r13, %rcx
movq %rcx, -0x38(%rbp)
movl $0x20, %edi
callq 0x2100
movq %rax, %r12
movq %rax, 0x10(%r14)
movq %r14, 0x18(%rax)
movq %r13, (%rax)
movabsq $-0xfffffffe, %rax      # imm = 0xFFFFFFFF00000002
movq %rax, 0x8(%r12)
andl $0xffffefff, %r15d      # imm = 0xFFFFEFFF
testl %ebx, %ebx
sets %al
testl %r15d, %r15d
setle %cl
cmpl -0x2c(%rbp), %r15d
setg %dl
orb %cl, %dl
orb %al, %dl
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jne 0x56ad
imull -0x2c(%rbp), %ebx
addl %r15d, %ebx
movq -0x50(%rbp), %rax
movl -0x4(%rax,%rbx,4), %r14d
movl $0x20, %edi
callq 0x2100
movq %rax, %r13
movq %rax, 0x10(%r12)
movq %r12, 0x18(%rax)
movq %rax, 0x13c86(%rip)     # 0x19350
xorl %eax, %eax
movq %rax, 0x10(%r13)
movq %rax, (%r13)
movl $0x0, 0x8(%r13)
movl %r14d, 0xc(%r13)
movq -0x40(%rbp), %rbx
jmp 0x5744
movl $0x20, %edi
callq 0x2100
movq %rax, %r12
movq %rax, 0x10(%r13)
movq %r13, 0x18(%rax)
movq %rbx, (%rax)
movabsq $-0xffffffff, %rax      # imm = 0xFFFFFFFF00000001
movq %rax, 0x8(%r12)
movl $0x20, %edi
callq 0x2100
movq %rax, %r13
movq %rax, 0x10(%r12)
movq %r12, 0x18(%rax)
movq %rax, 0x13c26(%rip)     # 0x19350
xorl %eax, %eax
movq %rax, 0x10(%r13)
movq %rax, (%r13)
movl $0x0, 0x8(%r13)
movl %r14d, 0xc(%r13)
movq 0x18(%rbx), %rbx
movl 0xc(%r13), %eax
cmpl $-0x1, %eax
movl -0x48(%rbp), %edx
jne 0x5463
jmp 0x5759
movq %r14, %rbx
testq %rbx, %rbx
je 0x579e
leaq 0x2fd7(%rip), %rdi      # 0x873c
jmp 0x57a5
leaq 0x2f58(%rip), %rdi      # 0x86c6
callq 0x2050
leaq 0x30(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x40(%rbp), %rdi
callq 0x5051
jmp 0x583f
leaq 0x2f71(%rip), %rdi      # 0x8716
callq 0x2050
jmp 0x583f
cmpl $0x3, %ecx
jne 0x57da
cmpq $0x0, 0x13b8c(%rip)     # 0x19348
je 0x57d4
leaq -0x78(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f3d
cmpq $0x0, 0x13b76(%rip)     # 0x19348
jne 0x57c2
movq -0x38(%rbp), %rax
jmp 0x5841
leaq 0x2e72(%rip), %rdi      # 0x8653
callq 0x2050
leaq 0x30(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
jmp 0x5835
leaq 0x2e66(%rip), %rdi      # 0x8675
callq 0x2050
leaq 0x30(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x40(%rbp), %rdi
callq 0x5051
callq 0x4fc2
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2e89(%rip), %rdi      # 0x86e3
callq 0x2050
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
jmp 0x5794