// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq 0x1424a(%rip), %rbx     # 0x19350
movl $0x20, %edi
callq 0x2100
leaq 0x10(%rbx), %rcx
testq %rbx, %rbx
leaq 0x1422a(%rip), %rdx     # 0x19348
cmovneq %rcx, %rdx
movq %rax, %r12
movq %rax, (%rdx)
movq %rbx, 0x18(%rax)
movq %rax, 0x1421d(%rip)     # 0x19350
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movl 0x18(%rbp), %esi
movl 0xc(%rax), %eax
cmpq $0x0, (%r12)
sete %cl
cmpl $-0x1, %eax
setne %dl
andb %cl, %dl
movl %esi, -0x34(%rbp)
cmpl %esi, %eax
setl %cl
andb %dl, %cl
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
cmpb $0x1, %cl
jne 0x539c
movl 0x10(%rbp), %esi
movl 0x14(%rbp), %ecx
movl %ecx, -0x30(%rbp)
movq 0x20(%rbp), %rcx
movq %rcx, -0x60(%rbp)
movl 0x50(%rbp), %ecx
movl %ecx, -0x38(%rbp)
movq 0x58(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq 0x28(%rbp), %rcx
movq %rcx, -0x48(%rbp)
leaq -0x80(%rbp), %r15
movq %r14, %rbx
movl %esi, -0x3c(%rbp)
testq %rbx, %rbx
je 0x5374
movl (%rbx), %ecx
testl %eax, %eax
js 0x5389
movl %ecx, %edx
andl $0xffffdfff, %edx       # imm = 0xFFFFDFFF
jle 0x5389
cmpl %esi, %edx
jg 0x5389
imull %esi, %eax
addl %edx, %eax
movq -0x60(%rbp), %rsi
movl -0x8(%rsi,%rax,8), %edx
movl -0x4(%rsi,%rax,8), %r14d
cmpl $0x1, %edx
je 0x52f5
cmpl $0x2, %edx
jne 0x537d
testl %r14d, %r14d
js 0x539c
cmpl %r14d, -0x38(%rbp)
jle 0x539c
movq %rbx, -0x58(%rbp)
shlq $0x4, %r14
movq -0x50(%rbp), %rax
movl (%rax,%r14), %ebx
movl 0x4(%rax,%r14), %r14d
testl %r14d, %r14d
jle 0x5238
movq %r15, %rdi
callq 0x4f3d
movq %r15, %rdi
callq 0x4f3d
decl %r14d
jne 0x521c
movq 0x14118(%rip), %r12     # 0x19350
testq %r12, %r12
je 0x539c
cmpq $0x0, (%r12)
jne 0x539c
movl 0xc(%r12), %r14d
cmpl $-0x1, %r14d
je 0x539c
cmpl -0x34(%rbp), %r14d
jge 0x539c
movl $0x20, %edi
callq 0x2100
movq %rax, %r13
movq %rax, 0x10(%r12)
movq %r12, 0x18(%rax)
movq $0x0, (%rax)
movabsq $-0xfffffffe, %rax      # imm = 0xFFFFFFFF00000002
movq %rax, 0x8(%r13)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %r14d, %r14d
js 0x52b8
andl $0xffffefff, %ebx       # imm = 0xFFFFEFFF
jle 0x52b8
cmpl -0x30(%rbp), %ebx
jg 0x52b8
imull -0x30(%rbp), %r14d
addl %ebx, %r14d
movq -0x48(%rbp), %rax
movl -0x4(%rax,%r14,4), %eax
movl $0x20, %edi
movl %eax, %ebx
callq 0x2100
movq %rax, %r12
movq %rax, 0x10(%r13)
movq %r13, 0x18(%rax)
movq %rax, 0x1407a(%rip)     # 0x19350
xorl %eax, %eax
movq %rax, 0x10(%r12)
movq %rax, (%r12)
movl $0x0, 0x8(%r12)
movl %ebx, 0xc(%r12)
movq -0x58(%rbp), %rbx
jmp 0x5355
movl $0x20, %edi
callq 0x2100
movq %rax, %r13
movq %rax, 0x10(%r12)
movq %r12, 0x18(%rax)
movq %rbx, (%rax)
movabsq $-0xffffffff, %rax      # imm = 0xFFFFFFFF00000001
movq %rax, 0x8(%r13)
movl $0x20, %edi
callq 0x2100
movq %rax, %r12
movq %rax, 0x10(%r13)
movq %r13, 0x18(%rax)
movq %rax, 0x14018(%rip)     # 0x19350
xorl %eax, %eax
movq %rax, 0x10(%r12)
movq %rax, (%r12)
movl $0x0, 0x8(%r12)
movl %r14d, 0xc(%r12)
movq 0x18(%rbx), %rbx
cmpq $0x0, (%r12)
jne 0x539c
movl 0xc(%r12), %eax
cmpl $-0x1, %eax
je 0x539c
cmpl -0x34(%rbp), %eax
movl -0x3c(%rbp), %esi
jl 0x51a2
jmp 0x539c
movl $0x0, -0x2c(%rbp)
jmp 0x539c
movl $0x1, -0x2c(%rbp)
cmpl $0x3, %edx
je 0x539c
leaq 0x6d28(%rip), %rax      # 0xc0b8
xorl %edx, %edx
cmpl (%rax), %ecx
setne %dl
negl %edx
movl %edx, -0x2c(%rbp)
cmpq $0x0, 0x13fa4(%rip)     # 0x19348
je 0x53bc
leaq -0x80(%rbp), %r14
movq %r14, %rdi
callq 0x4f3d
cmpq $0x0, 0x13f8e(%rip)     # 0x19348
jne 0x53aa
movl -0x2c(%rbp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq