// File: /workspace/github_c_makefile_star2_O0/masyagin1998[P]GCI/src/parser/parser.c
// Function: function_decl_ast_read
static enum PARSER_CODES function_decl_ast_read(struct PARSER*parser, struct FUNCTION_DECL_AST**function)
{
    enum PARSER_CODES r;
    struct IDENT_AST*function_name;
    struct FORMAL_PARAMETERS_LIST_AST*formal_parameters_list = NULL;
    struct BODY_AST*body;

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

    r = ident_ast_read(parser, &function_name);
    if (r != PARSER_OK) {
        goto err0;
    }

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

    if ((parser->tok)->token_type != TOKEN_TYPE_RPAREN) {
        r = formal_parameters_list_ast_read(parser, &formal_parameters_list);
        if (r != PARSER_OK) {
            goto err1;
        }
    }

    if ((parser->tok)->token_type != TOKEN_TYPE_RPAREN) {
        r = PARSER_INVALID_TOKEN;
        set_parser_error(parser, 1, TOKEN_TYPE_RPAREN);
        goto err2;
    }
    token_free((parser->tok));
    lexer_next_token(parser->lexer, &(parser->tok));
    
    r = body_ast_read(parser, &body);
    if (r != PARSER_OK) {
        goto err2;
    }

    (*function) = create_function_decl_ast(function_name, formal_parameters_list, body,
                                           (parser->tok)->frag.starting.line,
                                           (parser->tok)->frag.starting.pos);

    return PARSER_OK;

 err2:
    if (formal_parameters_list != NULL) {
        formal_parameters_list_ast_free(formal_parameters_list);
    }
 err1:
    ident_ast_free(function_name);
 err0:
    (*function) = NULL;
    return r;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x0, (%rax)
je 0x3ea3
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x4030
jmp 0x4013
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 0x4190
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x3edf
jmp 0x4013
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1b, (%rax)
je 0x3f0d
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x1b, %edx
movb $0x0, %al
callq 0x4030
jmp 0x400a
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 $0x1c, (%rax)
je 0x3f58
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4250
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x3f56
jmp 0x400a
jmp 0x3f58
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1c, (%rax)
je 0x3f83
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x1c, %edx
movb $0x0, %al
callq 0x4030
jmp 0x3ff8
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 -0x38(%rbp), %rsi
callq 0x4560
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x3fbc
jmp 0x3ff8
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 0x88d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x4024
cmpq $0x0, -0x30(%rbp)
je 0x4008
movq -0x30(%rbp), %rdi
callq 0x8b60
jmp 0x400a
movq -0x28(%rbp), %rdi
callq 0x8b30
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)