// File: /workspace/github_c_makefile_star2_O0/gregtour[P]minimal-compiler/preprocess.c
// Function: RefactorElseStatements
void RefactorElseStatements(SYNTAX_TREE** astPosition)
{
    SYNTAX_TREE* branch;
    SYNTAX_TREE* sourceSyntax;
    int i;

    const int BLOCK_FSTMT_CHILD = 0;
    const int IF_PROD_CODE_CHILD = 4;
    const int IF_PROD_ELSE_CHILD = 5;

    sourceSyntax = *astPosition;

    // <fstmt> ::= if ( <expr> ) <block> <else> 
    for (; sourceSyntax->production == PROD_FSTMT_IF;)
    {
        SYNTAX_TREE* elseBlock;
        SYNTAX_TREE* codeBlock;

        elseBlock = sourceSyntax->children[IF_PROD_ELSE_CHILD];

        // <else> ::= else <block>
        if (elseBlock &&
            elseBlock->production == PROD_ELSE_ELSE)
        {
            break;
        }

        // <else> ::= <epsilon>
        codeBlock = sourceSyntax->children[IF_PROD_CODE_CHILD];

        // <block> ::= <fstmt>
        while (codeBlock &&
               codeBlock->production == PROD_BLOCK_FSTMT)
        {
            SYNTAX_TREE* blockStmt = codeBlock->children[BLOCK_FSTMT_CHILD];

            // <fstmt> ::= if ( <expr> ) <block> <else>
            if (blockStmt && blockStmt->production == PROD_FSTMT_IF)
            {
                SYNTAX_TREE* innerElse = blockStmt->children[IF_PROD_ELSE_CHILD];
                if (innerElse &&
                    innerElse->production == PROD_ELSE_ELSE)
                {
                    // <else> ::= else <block>
                    sourceSyntax->children[IF_PROD_ELSE_CHILD] = innerElse;
                    blockStmt->children[IF_PROD_ELSE_CHILD] = elseBlock;
                    break;
                }
                else
                {
                    // <else> ::= <epsilon>
                    codeBlock = blockStmt->children[IF_PROD_CODE_CHILD];
                }
            }
            else
            {
                // <fstmt> ::= ...
                codeBlock = NULL;
            }
        }

        break; 
    }

    // apply for all sub-trees
    branch = *astPosition;
    for (i = 0; i < branch->numChildren; i++)
    {
        RefactorElseStatements(&branch->children[i]);
        branch = *astPosition;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x4, -0x24(%rbp)
movl $0x5, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x18, 0x4(%rax)
jne 0x855a
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x84a0
movq -0x30(%rbp), %rax
cmpl $0x19, 0x4(%rax)
jne 0x84a0
jmp 0x855a
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x38(%rbp)
movb %al, -0x49(%rbp)
je 0x84ca
movq -0x38(%rbp), %rax
cmpl $0xf, 0x4(%rax)
sete %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0x84d6
jmp 0x8558
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x854b
movq -0x40(%rbp), %rax
cmpl $0x18, 0x4(%rax)
jne 0x854b
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x8539
movq -0x48(%rbp), %rax
cmpl $0x19, 0x4(%rax)
jne 0x8539
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x28(%rax)
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq %rcx, 0x28(%rax)
jmp 0x8558
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x8553
movq $0x0, -0x38(%rbp)
jmp 0x84b0
jmp 0x855a
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x85a6
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0x8440
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x856c
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)