// File: /workspace/github_c_makefile_star2_O3/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 0x6cb1
leaq 0x8(%r15), %r14
callq 0x53ac
movq (%r15), %rdi
movq %r14, %rsi
callq 0x45d6
movq 0x8(%r15), %rdi
cmpl $0x1b, (%rdi)
jne 0x6cc3
callq 0x53ac
movq (%r15), %rdi
movq %r14, %rsi
callq 0x45d6
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x6fc5
testl %eax, %eax
je 0x6ce5
movl %eax, %r12d
jmp 0x6da5
xorl %r14d, %r14d
movl $0x1, %esi
movq %r15, %rdi
movl $0x2, %edx
jmp 0x6cd3
xorl %r14d, %r14d
movl $0x1, %esi
movq %r15, %rdi
movl $0x1b, %edx
xorl %eax, %eax
callq 0x5b97
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x6da8
movq (%r14), %rdi
cmpl $0x1c, (%rdi)
jne 0x6d81
callq 0x53ac
movq (%r15), %rdi
movq %r14, %rsi
callq 0x45d6
movq %rsp, %rsi
movq %r15, %rdi
callq 0x5cc9
movl %eax, %r12d
testl %eax, %eax
jne 0x6d9b
movq (%r14), %rdi
cmpl $0x3, (%rdi)
jne 0x6dba
callq 0x53ac
movq (%r15), %rdi
movq %r14, %rsi
callq 0x45d6
movq 0x8(%r15), %rax
movl (%rax), %eax
cmpl $0x2, %eax
je 0x6ddd
xorl %r12d, %r12d
cmpl $0x20, %eax
jne 0x6df1
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x5cc9
testl %eax, %eax
jne 0x6dee
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq 0x10(%rsp), %rdx
movq (%r14), %rax
movq 0x18(%rax), %rcx
movq 0x20(%rax), %r8
jmp 0x6e4f
movl $0x1, %esi
movq %r15, %rdi
movl $0x1c, %edx
xorl %eax, %eax
callq 0x5b97
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rdi
callq 0x8bdc
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 0x89a6
movq %rax, %r14
jmp 0x6da8
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x6c51
testl %eax, %eax
je 0x6dfc
movl %eax, %r12d
movq (%rsp), %rdi
callq 0x80df
jmp 0x6d9b
movq 0x10(%rsp), %rdi
movl $0x3, %esi
callq 0x83c7
movq %rax, %r15
movl $0x8, %edi
callq 0x1130
testq %rax, %rax
je 0x6e5f
movq %r15, (%rax)
movq (%r14), %rcx
movq 0x18(%rcx), %rdx
movq 0x20(%rcx), %rcx
movl $0x1, %esi
movq %rax, %rdi
callq 0x8245
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq (%r14), %rdx
movq 0x18(%rdx), %rcx
movq 0x20(%rdx), %r8
movq %rax, %rdx
callq 0x89a6
movq %rax, %r14
xorl %r12d, %r12d
jmp 0x6da8
callq 0x184d