// File: /workspace/github_c_makefile_star2_O3/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 0x5cf7
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq (%rdi), %r15
xorl %r13d, %r13d
leaq 0x2c3b(%rip), %r12      # 0x8850
xorl %eax, %eax
movl (%r15), %ecx
cmpl (%r12,%r13,4), %ecx
jne 0x5c77
cmpl $0x1, 0x20(%r15)
jne 0x5c77
movq (%rbx), %r14
cmpl $0x2, 0x20(%r14)
jl 0x5c50
movl $0x1, %r12d
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rdi
callq 0x5001
incq %r12
movslq 0x20(%r14), %rax
cmpq %rax, %r12
jl 0x5c37
movq 0x28(%r15), %rax
movq (%rax), %r15
movq %r15, (%rbx)
movq 0x28(%r14), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movl $0x1, %eax
leaq 0x2bd9(%rip), %r12      # 0x8850
incq %r13
cmpq $0x8, %r13
jne 0x5c17
cmpl $0x3a, 0x4(%r15)
jne 0x5cab
movq (%rbx), %r14
movq 0x28(%r15), %rax
movq 0x8(%rax), %r15
movq %r15, (%rbx)
movq 0x28(%r14), %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movl $0x1, %eax
xorl %r13d, %r13d
testl %eax, %eax
movl $0x0, %eax
jne 0x5c17
movq (%rbx), %rax
cmpl $0x0, 0x20(%rax)
jle 0x5ce9
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x28(%rax), %rdi
addq %r14, %rdi
callq 0x5bee
incq %r15
movq (%rbx), %rax
movslq 0x20(%rax), %rcx
addq $0x8, %r14
cmpq %rcx, %r15
jl 0x5cca
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq