// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movq 0x8(%rdi), %rdi
cmpl $0x2, (%rdi)
jne 0x6f1f
leaq 0x8(%r15), %r14
callq 0x53b0
movq (%r15), %rdi
movq %r14, %rsi
callq 0x45b5
movq 0x8(%r15), %rdi
cmpl $0x1b, (%rdi)
jne 0x6f31
callq 0x53b0
movq (%r15), %rdi
movq %r14, %rsi
callq 0x45b5
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x723e
testl %eax, %eax
je 0x6f53
movl %eax, %r12d
jmp 0x7016
xorl %r14d, %r14d
movl $0x1, %esi
movq %r15, %rdi
movl $0x2, %edx
jmp 0x6f41
xorl %r14d, %r14d
movl $0x1, %esi
movq %r15, %rdi
movl $0x1b, %edx
xorl %eax, %eax
callq 0x5bec
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x7019
movq (%r14), %rdi
cmpl $0x1c, (%rdi)
jne 0x6ff2
callq 0x53b0
movq (%r15), %rdi
movq %r14, %rsi
callq 0x45b5
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x5d26
movl %eax, %r12d
testl %eax, %eax
jne 0x700c
movq (%r14), %rdi
cmpl $0x3, (%rdi)
jne 0x702b
callq 0x53b0
movq (%r15), %rdi
movq %r14, %rsi
callq 0x45b5
movq 0x8(%r15), %rax
movl (%rax), %eax
cmpl $0x2, %eax
je 0x704f
xorl %r12d, %r12d
cmpl $0x20, %eax
jne 0x7063
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x5d26
testl %eax, %eax
jne 0x7060
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq (%r14), %rax
movq 0x18(%rax), %rcx
movq 0x20(%rax), %r8
jmp 0x70c8
movl $0x1, %esi
movq %r15, %rdi
movl $0x1c, %edx
xorl %eax, %eax
callq 0x5bec
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rdi
callq 0x8f73
xorl %r14d, %r14d
movq %r14, (%rbx)
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rsi
movq 0x18(%rdi), %rcx
movq 0x20(%rdi), %r8
xorl %r12d, %r12d
movq %rax, %rdi
xorl %edx, %edx
callq 0x8d3d
movq %rax, %r14
jmp 0x7019
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x6ebf
testl %eax, %eax
je 0x706f
movl %eax, %r12d
movq 0x8(%rsp), %rdi
callq 0x8465
jmp 0x700c
movq 0x20(%rsp), %rdi
movl $0x3, %esi
callq 0x8756
movq %rax, %r15
movl $0x8, %edi
callq 0x1130
testq %rax, %rax
je 0x70d8
movq %r15, (%rax)
movq (%r14), %rcx
movq 0x18(%rcx), %rdx
movq 0x20(%rcx), %rcx
movl $0x1, %esi
movq %rax, %rdi
callq 0x85d4
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq (%r14), %rdx
movq 0x18(%rdx), %rcx
movq 0x20(%rdx), %r8
movq %rax, %rdx
callq 0x8d3d
movq %rax, %r14
xorl %r12d, %r12d
jmp 0x7019
callq 0x182f