// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %rbx
leaq 0x30(%rbp), %r14
leaq 0x10(%rbp), %r12
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x4df8
movq %r12, %r9
movq %r14, %r8
leaq -0x50(%rbp), %r14
movq 0x141dc(%rip), %rax     # 0x19350
testq %rax, %rax
je 0x5185
movl 0xc(%rax), %ecx
cmpq $0x0, (%rax)
setne %al
jmp 0x518c
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %ecx
sete %dl
orb %al, %dl
jne 0x51ca
movl 0x8(%r9), %r13d
xorl %eax, %eax
cmpl %r13d, %ecx
jge 0x51ca
testq %rbx, %rbx
je 0x51db
movl (%rbx), %eax
xorl %edx, %edx
testl %ecx, %ecx
js 0x51c6
movl %eax, %edi
movl $0x0, %esi
andl $0xffffdfff, %edi       # imm = 0xFFFFDFFF
jle 0x51fa
movl (%r9), %edx
cmpl %edx, %edi
jle 0x51e2
xorl %edx, %edx
xorl %esi, %esi
jmp 0x51fa
xorl %eax, %eax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testb %al, %al
jne 0x516d
jmp 0x5328
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0x51d2
movq 0x10(%r9), %rsi
imull %ecx, %edx
addl %edi, %edx
movslq %edx, %rcx
movl -0x8(%rsi,%rcx,8), %edx
movl -0x4(%rsi,%rcx,8), %esi
shlq $0x20, %rsi
cmpl $0x3, %edx
je 0x5289
movq %rsi, %r12
shrq $0x20, %r12
cmpl $0x2, %edx
je 0x5234
cmpl $0x1, %edx
jne 0x5291
movq %rbx, %rdi
callq 0x4e64
movl %r12d, %edi
callq 0x4df8
leaq 0x10(%rbp), %r9
leaq 0x30(%rbp), %r8
movq 0x18(%rbx), %rbx
movb $0x1, %al
jmp 0x51d2
xorl %eax, %eax
testq %rsi, %rsi
js 0x51cc
cmpl %r12d, 0x20(%r8)
jle 0x51cc
movq 0x28(%r8), %rax
shlq $0x4, %r12
movslq (%rax,%r12), %rcx
movq %rcx, -0x30(%rbp)
movl 0x4(%rax,%r12), %r12d
testl %r12d, %r12d
jle 0x5270
movq %r14, %rdi
callq 0x4f89
movq %r14, %rdi
callq 0x4f89
decl %r12d
jne 0x525b
movq 0x140d9(%rip), %rax     # 0x19350
testq %rax, %rax
je 0x52ab
movl 0xc(%rax), %r12d
cmpq $0x0, (%rax)
sete %cl
jmp 0x52b3
movl $0x1, %r15d
jmp 0x52a4
xorl %r15d, %r15d
leaq 0x6e1d(%rip), %rcx      # 0xc0b8
cmpl (%rcx), %eax
setne %r15b
negl %r15d
xorl %eax, %eax
jmp 0x51d2
movb $0x1, %cl
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x10(%rbp), %r9
xorl %eax, %eax
testb %cl, %cl
leaq 0x30(%rbp), %r8
je 0x51cc
cmpl $-0x1, %r12d
je 0x51cc
cmpl %r13d, %r12d
jge 0x51cc
xorl %edi, %edi
callq 0x4ed7
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
testl %r12d, %r12d
js 0x5314
movq -0x30(%rbp), %rdx
andq $-0x1001, %rdx          # imm = 0xEFFF
testl %edx, %edx
leaq 0x10(%rbp), %rcx
jle 0x5314
movl 0x4(%rcx), %eax
cmpl %eax, %edx
jg 0x5314
movq 0x18(%rcx), %rcx
imull %r12d, %eax
cltq
addq %rdx, %rax
movl -0x4(%rcx,%rax,4), %edi
callq 0x4df8
leaq 0x10(%rbp), %r9
movb $0x1, %al
leaq 0x30(%rbp), %r8
jmp 0x51d2
cmpq $0x0, 0x14018(%rip)     # 0x19348
je 0x5348
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f89
cmpq $0x0, 0x14002(%rip)     # 0x19348
jne 0x5336
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq