// File: /workspace/github_c_makefile_star2_O2/gregtour[P]minimal-compiler/preprocess.c
// Function: ReduceProgramAST
void ReduceProgramAST(SYNTAX_TREE** astPosition)
{
    SYNTAX_TREE* node;
    SYNTAX_TREE* branch;
    unsigned int reducible = 1;
    const unsigned int empty_production = 0;
    int i;

    unsigned int reducibleTokens[] = 
    { //SYMBOL_BLOCK,
        SYMBOL_EXPR, 
        SYMBOL_CONDITION, 
        SYMBOL_LOGIC, 
        SYMBOL_COMPARISON, 
        SYMBOL_ARITHMETIC, 
        SYMBOL_TERM, 
        SYMBOL_FACTOR, 
        SYMBOL_FINAL
    };

    if (astPosition == NULL) return;
    node = *astPosition;

    // continue while reducible
    while (reducible)
    {
        reducible = 0;

        // productions that reduce to node->children[0]
        for (i = 0; i < sizeof(reducibleTokens)/sizeof(int); i++)
        {
            if (node->token == reducibleTokens[i] &&
                node->numChildren == 1)
            {
                SYNTAX_TREE* cur = *astPosition;
                int child;

                for (child = 1; child < cur->numChildren; child++)
                {
                    FreeParseTree(cur->children[child]);
                }

                *astPosition = node->children[0];
                node = *astPosition;
                free(cur->children);
                free(cur);
                reducible = 1;
            }
        }

        // parentheses expression
        if (node->production == PROD_FINAL)
        {
            SYNTAX_TREE* cur = *astPosition;
            *astPosition = node->children[1];
            node = *astPosition;
            free(cur->children);
            free(cur);
            reducible = 1;
        }
    }

    // apply for all sub-trees
    branch = *astPosition;
    for (i = 0; i < branch->numChildren; i++)
    {
        ReduceProgramAST(&branch->children[i]);
        branch = *astPosition;
    }
}

// Assembly:
testq %rdi, %rdi
je 0x57e2
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq (%rdi), %r12
pushq $0x1
popq %rax
leaq 0x2150(%rip), %r13      # 0x7850
testl %eax, %eax
je 0x57ad
xorl %r15d, %r15d
xorl %eax, %eax
cmpq $0x8, %r15
je 0x5779
movl (%r12), %ecx
cmpl (%r13,%r15,4), %ecx
jne 0x5774
cmpl $0x1, 0x20(%r12)
jne 0x5774
movq %rbx, %r13
movq (%rbx), %r14
pushq $0x1
popq %rbx
movslq 0x20(%r14), %rax
cmpq %rax, %rbx
jge 0x574a
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rdi
callq 0x4cee
incq %rbx
jmp 0x572f
movq 0x28(%r12), %rax
movq (%rax), %r12
movq %r13, %rbx
movq %r12, (%r13)
movq 0x28(%r14), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
pushq $0x1
popq %rax
leaq 0x20dc(%rip), %r13      # 0x7850
incq %r15
jmp 0x570d
cmpl $0x3a, 0x4(%r12)
jne 0x5700
movq (%rbx), %r14
movq 0x28(%r12), %rax
movq 0x8(%rax), %r12
movq %r12, (%rbx)
movq 0x28(%r14), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
pushq $0x1
popq %rax
jmp 0x5700
xorl %r14d, %r14d
xorl %r15d, %r15d
movq (%rbx), %rax
movslq 0x20(%rax), %rcx
cmpq %rcx, %r15
jge 0x57d4
movq 0x28(%rax), %rdi
addq %r14, %rdi
callq 0x56d9
incq %r15
addq $0x8, %r14
jmp 0x57b3
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq