// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
leaq 0x30(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq 0x10(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
xorl %edi, %edi
callq 0x7300
leaq -0x48(%rbp), %rdi
callq 0x7540
cmpq $0x0, -0x48(%rbp)
jne 0x78b8
cmpl $-0x1, -0x3c(%rbp)
je 0x78b8
movq -0xf0(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jl 0x78c4
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0x7aa0
cmpq $0x0, -0x20(%rbp)
jne 0x78d7
movl $0x0, -0xc(%rbp)
jmp 0x7aa0
movq -0xf0(%rbp), %rax
movl -0x3c(%rbp), %edi
movq -0x20(%rbp), %rcx
movl (%rcx), %esi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x7200
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpl $0x1, -0x28(%rbp)
jne 0x7942
movq -0x20(%rbp), %rdi
callq 0x73c0
movl -0x24(%rbp), %edi
callq 0x7300
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x7a9b
cmpl $0x2, -0x28(%rbp)
jne 0x7a67
cmpl $0x0, -0x24(%rbp)
jl 0x7961
movq -0xf8(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x20(%rcx), %eax
jl 0x796d
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0x7aa0
movq -0xf8(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x60(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x79c4
leaq -0x88(%rbp), %rdi
callq 0x75b0
leaq -0xa8(%rbp), %rdi
callq 0x75b0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x7999
leaq -0xc8(%rbp), %rdi
callq 0x7540
movq -0xc8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x7a18
cmpl $-0x1, -0x3c(%rbp)
je 0x7a18
movq -0xf0(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jl 0x7a21
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0x7aa0
xorl %eax, %eax
movl %eax, %edi
callq 0x7480
movq -0xf0(%rbp), %rax
movl -0x3c(%rbp), %edi
movl -0x60(%rbp), %esi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x7280
movl %eax, %edi
callq 0x7300
jmp 0x7a99
cmpl $0x3, -0x28(%rbp)
jne 0x7a76
movl $0x1, -0xc(%rbp)
jmp 0x7aa0
movq -0x20(%rbp), %rax
movl (%rax), %eax
leaq 0x7625(%rip), %rcx      # 0xf0a8
cmpl (%rcx), %eax
jne 0x7a90
movl $0x0, -0xc(%rbp)
jmp 0x7a97
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
jmp 0x7aa0
jmp 0x7a9b
jmp 0x7893
jmp 0x7aa2
cmpq $0x0, 0x1488e(%rip)     # 0x1c338
je 0x7aba
leaq -0xe8(%rbp), %rdi
callq 0x75b0
jmp 0x7aa2
movl -0xc(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)