// File: /workspace/github_c_makefile_star2_O0/masyagin1998[P]GCI/src/parser/parser.c
// Function: if_stmt_ast_read
static enum PARSER_CODES if_stmt_ast_read(struct PARSER*parser, struct IF_STMT_AST**if_stmt)
{
    enum PARSER_CODES r;
    struct LOGICAL_OR_EXPR_AST*condition;
    struct BODY_AST*if_body;
    struct BODY_AST*else_body;
    struct IF_STMT_AST*if_stmt_inner;

    if ((parser->tok)->token_type != TOKEN_TYPE_IF) {
        r = PARSER_INVALID_TOKEN;
        set_parser_error(parser, 1, TOKEN_TYPE_IF);
        goto err0;
    }
    token_free((parser->tok));
    lexer_next_token(parser->lexer, &(parser->tok));

    if ((parser->tok)->token_type != TOKEN_TYPE_LPAREN) {
        r = PARSER_INVALID_TOKEN;
        set_parser_error(parser, 1, TOKEN_TYPE_LPAREN);
        goto err0;
    }
    token_free((parser->tok));
    lexer_next_token(parser->lexer, &(parser->tok));

    r = logical_or_expr_ast_read(parser, &condition);
    if (r != PARSER_OK) {
        goto err0;
    }
    if ((parser->tok)->token_type != TOKEN_TYPE_RPAREN) {
        r = PARSER_INVALID_TOKEN;
        set_parser_error(parser, 1, TOKEN_TYPE_RPAREN);
        goto err1;
    }
    token_free((parser->tok));
    lexer_next_token(parser->lexer, &(parser->tok));

    r = body_ast_read(parser, &if_body);
    if (r != PARSER_OK) {
        goto err1;   
    }

    if ((parser->tok)->token_type != TOKEN_TYPE_ELSE) {
        (*if_stmt) = create_if_stmt_ast(condition, if_body, NULL,
                                        (parser->tok)->frag.starting.line,
                                        (parser->tok)->frag.starting.pos);
        return PARSER_OK;
    }
    token_free((parser->tok));
    lexer_next_token(parser->lexer, &(parser->tok));

    if ((parser->tok)->token_type == TOKEN_TYPE_LBRACE) {
        r = body_ast_read(parser, &else_body);
        if (r != PARSER_OK) {
            goto err2;
        }
        (*if_stmt) = create_if_stmt_ast(condition, if_body, else_body,
                                        (parser->tok)->frag.starting.line,
                                        (parser->tok)->frag.starting.pos);
        return PARSER_OK;
    } else if ((parser->tok)->token_type == TOKEN_TYPE_IF) {
        struct STMT_AST**stmts = NULL;
        size_t stmts_len = 0;
        size_t stmts_cap = 0;

        struct STMT_AST*stmt;
        
        r = if_stmt_ast_read(parser, &if_stmt_inner);
        if (r != PARSER_OK) {
            goto err2;
        }

        stmt = create_stmt_ast(if_stmt_inner, AST_STMT_TYPE_IF);
        PUSH_BACK(stmts, stmt);
        else_body = create_body_ast(stmts, stmts_len,
                                    (parser->tok)->frag.starting.line,
                                    (parser->tok)->frag.starting.pos);

        (*if_stmt) = create_if_stmt_ast(condition, if_body, else_body,
                                        (parser->tok)->frag.starting.line,
                                        (parser->tok)->frag.starting.pos);

        return PARSER_OK;
    }

 err2:
    body_ast_free(if_body);
 err1:
    logical_or_expr_ast_free(condition);
 err0:
    (*if_stmt) = NULL;
    return r;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x2, (%rax)
je 0x52be
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
movb $0x0, %al
callq 0x4030
jmp 0x5644
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x31b0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x1fd0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1b, (%rax)
je 0x530d
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x1b, %edx
movb $0x0, %al
callq 0x4030
jmp 0x5644
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x31b0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x1fd0
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x64c0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x5349
jmp 0x5644
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1c, (%rax)
je 0x5377
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x1c, %edx
movb $0x0, %al
callq 0x4030
jmp 0x563b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x31b0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x1fd0
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4560
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x53b3
jmp 0x563b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x3, (%rax)
je 0x53ff
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %r8
xorl %eax, %eax
movl %eax, %edx
callq 0x9ce0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x5655
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x31b0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x1fd0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x20, (%rax)
jne 0x5487
movq -0x10(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x4560
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x5448
jmp 0x5632
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %r8
callq 0x9ce0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x5655
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x2, (%rax)
jne 0x562e
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x5280
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x54cb
jmp 0x5632
movq -0x40(%rbp), %rdi
movl $0x3, %esi
callq 0x90f0
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x55ad
cmpq $0x0, -0x58(%rbp)
jne 0x554b
movq $0x1, -0x58(%rbp)
movq -0x58(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1130
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x5547
movq 0x19ad8(%rip), %rax     # 0x1eff0
movq (%rax), %rdi
movq -0x58(%rbp), %r8
shlq $0x3, %r8
leaq 0x130e4(%rip), %rsi     # 0x1860e
leaq 0x13337(%rip), %rdx     # 0x18868
movl $0x4d3, %ecx            # imm = 0x4D3
movb $0x0, %al
callq 0x1100
movl $0x1, %edi
callq 0x1170
jmp 0x5549
jmp 0x55ab
movq -0x58(%rbp), %rax
shlq %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
shlq $0x3, %rsi
callq 0x1150
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x55a7
movq 0x19a78(%rip), %rax     # 0x1eff0
movq (%rax), %rdi
movq -0x58(%rbp), %r8
shlq $0x3, %r8
leaq 0x1340b(%rip), %rsi     # 0x18995
leaq 0x132d7(%rip), %rdx     # 0x18868
movl $0x4d3, %ecx            # imm = 0x4D3
movb $0x0, %al
callq 0x1100
movl $0x1, %edi
callq 0x1170
jmp 0x55a9
jmp 0x55ab
jmp 0x55ad
movq -0x60(%rbp), %rdx
movq -0x48(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rcx
callq 0x8e20
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %r8
callq 0x9ce0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x5655
jmp 0x5630
jmp 0x5632
movq -0x30(%rbp), %rdi
callq 0x8be0
movq -0x28(%rbp), %rdi
callq 0xa030
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop