// File: /workspace/github_c_makefile_star2_O2/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, -0x30(%rbp)
leaq 0x30(%rbp), %r14
leaq 0x10(%rbp), %r12
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x4b12
movl 0x4(%r12), %eax
movl %eax, -0x38(%rbp)
movl 0x8(%r12), %r13d
movq 0x18(%r12), %rax
movq %rax, -0x48(%rbp)
movl %r13d, -0x34(%rbp)
movq 0x13356(%rip), %rax     # 0x18350
testq %rax, %rax
je 0x52bd
cmpq $0x0, (%rax)
jne 0x52bd
movl 0xc(%rax), %edi
cmpl $-0x1, %edi
je 0x52bd
cmpl %r13d, %edi
jge 0x52bd
cmpq $0x0, -0x30(%rbp)
je 0x52bd
movq -0x30(%rbp), %rax
movl (%rax), %esi
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x4a94
movq %rax, %r13
shrq $0x20, %r13
cmpl $0x2, %eax
jne 0x5241
testq %rax, %rax
js 0x5306
cmpl %r13d, 0x20(%r14)
jle 0x5306
movq %r15, -0x58(%rbp)
movq 0x28(%r14), %rax
movq %r13, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %ebx
movslq 0x4(%rax,%rcx), %r15
pushq $0x30
popq %rdi
callq 0x2100
movq %rax, %r14
movl %ebx, -0x3c(%rbp)
movl %ebx, (%rax)
movl %r13d, 0x4(%rax)
movq %r15, %rdi
shlq $0x3, %rdi
callq 0x2100
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
movq %rax, 0x28(%r14)
movl %r15d, 0x20(%r14)
testl %r15d, %r15d
movl $0x0, %r13d
cmovgl %r15d, %r13d
movq %r15, -0x50(%rbp)
decl %r15d
subq $0x1, %r13
jb 0x5194
leaq -0x78(%rbp), %rbx
movq %rbx, %rdi
callq 0x4c3b
movq %rbx, %rdi
callq 0x4c3b
movq -0x78(%rbp), %rbx
testq %rbx, %rbx
je 0x5273
movl -0x70(%rbp), %eax
movslq %r15d, %r12
cmpl $0x2, %eax
je 0x5150
cmpl $0x1, %eax
jne 0x515a
pushq $0x30
popq %rdi
callq 0x2100
movq 0x28(%r14), %rcx
movq %rax, (%rcx,%r12,8)
movl (%rbx), %eax
movq 0x28(%r14), %rcx
movq (%rcx,%r12,8), %rdx
movl %eax, (%rdx)
andl $0x0, 0x4(%rdx)
movq 0x8(%rbx), %rax
movq %rax, 0x8(%rdx)
movl 0x10(%rbx), %eax
movq (%rcx,%r12,8), %rcx
movl %eax, 0x10(%rcx)
movl 0x14(%rbx), %eax
movl %eax, 0x14(%rcx)
andq $0x0, 0x28(%rcx)
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rax
andl $0x0, 0x20(%rax)
jmp 0x518c
movq 0x28(%r14), %rax
movq %rbx, (%rax,%r12,8)
jmp 0x518c
leaq 0x2541(%rip), %rdi      # 0x76a2
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 -0x30(%rbp), %rdi
callq 0x4d34
decl %r15d
jmp 0x50ca
cmpl $0x0, -0x50(%rbp)
je 0x51a8
movq 0x28(%r14), %rax
movq (%rax), %rax
movl 0x14(%rax), %eax
movl %eax, 0x14(%r14)
movq 0x131a1(%rip), %rax     # 0x18350
testq %rax, %rax
movq -0x58(%rbp), %r15
leaq 0x10(%rbp), %r12
movl -0x34(%rbp), %r13d
je 0x51cc
movl 0xc(%rax), %ebx
cmpq $0x0, (%rax)
sete %al
jmp 0x51d3
movb $0x1, %al
pushq $-0x1
popq %rcx
movl %ecx, %ebx
testb %al, %al
je 0x5336
cmpl $-0x1, %ebx
je 0x5336
cmpl %r13d, %ebx
jge 0x5336
leaq 0x5ebc(%rip), %rax      # 0xb0b0
movl -0x3c(%rbp), %ecx
cmpl (%rax), %ecx
cmoveq %r14, %r15
movq %r14, %rdi
movl %ecx, %r14d
callq 0x4bb3
pushq $-0x1
popq %rax
movl %eax, %edi
testl %ebx, %ebx
js 0x5233
movl %eax, %edi
andl $0xffffefff, %r14d      # imm = 0xFFFFEFFF
jle 0x5233
movl %eax, %edi
cmpl -0x38(%rbp), %r14d
jg 0x5233
imull -0x38(%rbp), %ebx
addl %r14d, %ebx
movq -0x48(%rbp), %rax
movl -0x4(%rax,%rbx,4), %edi
callq 0x4b12
leaq 0x30(%rbp), %r14
jmp 0x4ff3
cmpl $0x1, %eax
jne 0x52cd
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x4b5f
movl %r13d, %edi
callq 0x4b12
movq 0x18(%rbx), %rbx
movq %rbx, -0x30(%rbp)
leaq 0x10(%rbp), %r12
movl -0x34(%rbp), %r13d
jmp 0x4ff3
leaq 0x244c(%rip), %rdi      # 0x76c6
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 -0x30(%rbp), %rdi
callq 0x4d34
xorl %r15d, %r15d
movq %r15, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, -0x30(%rbp)
je 0x5325
leaq 0x2471(%rip), %rdi      # 0x773c
jmp 0x532c
cmpl $0x3, %eax
jne 0x52ea
leaq -0x78(%rbp), %rbx
cmpq $0x0, 0x1306a(%rip)     # 0x18348
je 0x52a8
movq %rbx, %rdi
callq 0x4c3b
jmp 0x52d6
leaq 0x2362(%rip), %rdi      # 0x7653
callq 0x2050
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
jmp 0x5351
leaq 0x23d6(%rip), %rdi      # 0x76e3
callq 0x2050
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
jmp 0x528e
leaq 0x23ea(%rip), %rdi      # 0x7716
callq 0x2050
jmp 0x52a5
leaq 0x2338(%rip), %rdi      # 0x7675
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 -0x30(%rbp), %rdi
callq 0x4d34
callq 0x4cb7
jmp 0x52a5