// File: /workspace/github_c_makefile_star2_O2/gregtour[P]minimal-compiler/parser.c
// Function: ParseSucceeds
int ParseSucceeds(L_TOKEN* input, 
                  LR_TABLE parser, 
                  GRAMMAR_TABLE grammar)
{
    int successful = 0;
    // the program's syntax tree
    SYNTAX_TREE* ast = NULL;

    // set ip and init stack
    L_TOKEN* ip = input;
    StackPushState(0);

    // 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)
        {
            // error
            successful = -1;
            break;
        }

        if (ip == NULL) {
            successful = 0;
            break;
        }
        
        // 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)
        {
            RULE r;
            int rhs;
            
            // output the production A -> B
            if (action.value < 0 || action.value >= grammar.numRules) 
            {
                // error, free
                successful = -1;
                break;
            }
            r = grammar.rules[action.value];

            // pop 2*|B| symbols off the stack;
            for (rhs = 0; rhs < r.rhsLength; rhs++)
            {
                StackPop();
                StackPop();
            }
                
            // let s' be the state now on top of the stack;
            s = StackPeek();
            if (s.token || s.state == -1 || s.state >= parser.numStates)
            {
                successful = -1;
                break;
            }
            
            // push A then goto[s', A] on top of the stack;
            StackPushTree(NULL);
            StackPushState(GotoTable(parser, s.state, r.lhs));
        }
        else if (action.type == ACTION_ACCEPT)
        {
            // accept
            successful = 1;
            break;
        }
        else
        {
            // error
            if (ip->token == gSymbolEOF)
                successful = 0;
            else
                successful = -1;
            break;
        }
    }
    
    // free the stack
    while (gParseStack)
        StackPop();
    //FreeParseStack();

    // no errors
    return successful;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
leaq 0x10(%rbp), %r14
xorl %edi, %edi
callq 0x4b12
movl 0x4(%r14), %eax
movl %eax, -0x2c(%rbp)
movl 0x8(%r14), %r13d
movl 0x50(%rbp), %eax
movl %eax, -0x30(%rbp)
movq 0x58(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x18(%r14), %rax
movq %rax, -0x38(%rbp)
leaq -0x60(%rbp), %r14
movq 0x1353f(%rip), %rax     # 0x18350
testq %rax, %rax
je 0x4f62
cmpq $0x0, (%rax)
movl 0xc(%rax), %edi
setne %al
cmpl $-0x1, %edi
sete %cl
orb %al, %cl
cmpl %r13d, %edi
setge %al
orb %cl, %al
movzbl %al, %eax
movl %eax, %r15d
negl %r15d
testb %al, %al
jne 0x4f85
testq %rbx, %rbx
je 0x4f85
movl (%rbx), %r12d
leaq 0x10(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl %r12d, %esi
callq 0x4a94
movq %rax, %r15
shrq $0x20, %r15
cmpl $0x2, %eax
jne 0x4f3f
testq %rax, %rax
js 0x4f62
cmpl %r15d, -0x30(%rbp)
jle 0x4f62
shlq $0x4, %r15
movq -0x40(%rbp), %rax
movl (%rax,%r15), %r12d
movl 0x4(%rax,%r15), %r15d
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
subl $0x1, %r15d
jb 0x4ec5
movq %r14, %rdi
callq 0x4c3b
movq %r14, %rdi
callq 0x4c3b
jmp 0x4ead
movq 0x13484(%rip), %rax     # 0x18350
testq %rax, %rax
je 0x4ede
movl 0xc(%rax), %r15d
cmpq $0x0, (%rax)
sete %al
jmp 0x4ee6
movb $0x1, %al
pushq $-0x1
popq %rcx
movl %ecx, %r15d
testb %al, %al
je 0x4f62
cmpl $-0x1, %r15d
pushq $-0x1
popq %rax
je 0x4f82
cmpl %r13d, %r15d
jge 0x4f82
xorl %edi, %edi
callq 0x4bb3
pushq $-0x1
popq %rax
movl %eax, %edi
testl %r15d, %r15d
js 0x4f35
movl %eax, %edi
andl $0xffffefff, %r12d      # imm = 0xFFFFEFFF
jle 0x4f35
movl %eax, %edi
cmpl -0x2c(%rbp), %r12d
jg 0x4f35
imull -0x2c(%rbp), %r15d
addl %r12d, %r15d
movq -0x38(%rbp), %rax
movl -0x4(%rax,%r15,4), %edi
callq 0x4b12
jmp 0x4e0a
cmpl $0x3, %eax
je 0x4f66
cmpl $0x1, %eax
jne 0x4f6c
movq %rbx, %rdi
callq 0x4b5f
movl %r15d, %edi
callq 0x4b12
movq 0x18(%rbx), %rbx
jmp 0x4e0a
pushq $-0x1
jmp 0x4f68
pushq $0x1
popq %r15
jmp 0x4f85
leaq 0x6145(%rip), %rax      # 0xb0b8
xorl %r15d, %r15d
cmpl (%rax), %r12d
setne %r15b
negl %r15d
jmp 0x4f85
movl %eax, %r15d
leaq -0x60(%rbp), %rbx
cmpq $0x0, 0x133b7(%rip)     # 0x18348
je 0x4f9d
movq %rbx, %rdi
callq 0x4c3b
jmp 0x4f89
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq