// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movq 0x8(%rdi), %rdi
cmpl $0x2, (%rdi)
jne 0x42ee
leaq 0x8(%r15), %r14
callq 0x297c
movq (%r15), %rdi
movq %r14, %rsi
callq 0x1c9f
movq 0x8(%r15), %rdi
cmpl $0x1b, (%rdi)
jne 0x42f8
callq 0x297c
movq (%r15), %rdi
movq %r14, %rsi
callq 0x1c9f
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x464d
testl %eax, %eax
je 0x4314
movl %eax, %r12d
jmp 0x43ca
xorl %r14d, %r14d
pushq $0x1
popq %rsi
pushq $0x2
jmp 0x4300
xorl %r14d, %r14d
pushq $0x1
popq %rsi
pushq $0x1b
popq %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x3273
pushq $-0x1
popq %r12
jmp 0x43cd
movq (%r14), %rdi
cmpl $0x1c, (%rdi)
jne 0x43ac
callq 0x297c
movq (%r15), %rdi
movq %r14, %rsi
callq 0x1c9f
movq %rsp, %rsi
movq %r15, %rdi
callq 0x338d
movl %eax, %r12d
testl %eax, %eax
jne 0x43c0
movq (%r14), %rdi
cmpl $0x3, (%rdi)
jne 0x43df
callq 0x297c
movq (%r15), %rdi
movq %r14, %rsi
callq 0x1c9f
movq 0x8(%r15), %rax
movl (%rax), %eax
cmpl $0x2, %eax
je 0x4402
xorl %r12d, %r12d
cmpl $0x20, %eax
jne 0x4416
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x338d
testl %eax, %eax
jne 0x4413
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq 0x10(%rsp), %rdx
movq (%r14), %rax
movq 0x18(%rax), %rcx
movq 0x20(%rax), %r8
jmp 0x446e
pushq $0x1
popq %rsi
pushq $0x1c
popq %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x3273
pushq $-0x1
popq %r12
movq 0x8(%rsp), %rdi
callq 0x6424
xorl %r14d, %r14d
movq %r14, (%rbx)
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x8(%rsp), %rax
movq (%rsp), %rsi
movq 0x18(%rdi), %rcx
movq 0x20(%rdi), %r8
xorl %r12d, %r12d
movq %rax, %rdi
xorl %edx, %edx
callq 0x61de
movq %rax, %r14
jmp 0x43cd
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x428e
testl %eax, %eax
je 0x4421
movl %eax, %r12d
movq (%rsp), %rdi
callq 0x58c4
jmp 0x43c0
movq 0x10(%rsp), %rdi
pushq $0x3
popq %rsi
callq 0x5bb5
movq %rax, %r15
pushq $0x8
popq %rdi
callq 0x1130
testq %rax, %rax
je 0x447e
movq %r15, (%rax)
movq (%r14), %rcx
movq 0x18(%rcx), %rdx
movq 0x20(%rcx), %rcx
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x5a07
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq (%r14), %rdx
movq 0x18(%rdx), %rcx
movq 0x20(%rdx), %r8
movq %rax, %rdx
callq 0x61de
movq %rax, %r14
xorl %r12d, %r12d
jmp 0x43cd
movq 0x11b6b(%rip), %rax     # 0x15ff0
movq (%rax), %rdi
leaq 0xa12f(%rip), %rsi      # 0xe5be
leaq 0xa32e(%rip), %rdx      # 0xe7c4
pushq $0x8
popq %r8
movl $0x4d3, %ecx            # imm = 0x4D3
xorl %eax, %eax
callq 0x1100
pushq $0x1
popq %rdi
callq 0x1180