// File: /workspace/github_c_makefile_star2_O3/naleksiev[P]fin/src/fin_ast.c
// Function: fin_ast_parse
fin_ast_module_t* fin_ast_parse(fin_ctx_t* ctx, const char* str) {
    fin_lex_t* lex = fin_lex_create(ctx->alloc, str);
    fin_str_t* name = fin_str_create(ctx, "<noname>", -1);

/*
    if (fin_lex_match(&lex, fin_lex_type_module)) {
    }
    while (fin_lex_match(lex, fin_lex_type_import)) {
    }
*/

    fin_ast_func_t*  funcs = NULL;
    fin_ast_func_t** func_tail = &funcs;

    fin_ast_enum_t*  enums = NULL;
    fin_ast_enum_t** enum_tail = &enums;

    fin_ast_type_t*  types = NULL;
    fin_ast_type_t** type_tail = &types;

    while (fin_lex_get_type(lex) != fin_lex_type_eof) {
        if (fin_lex_get_type(lex) == fin_lex_type_struct) {
            *type_tail = fin_ast_parse_type(ctx, lex);
            type_tail = &(*type_tail)->next;
        }
        else  if (fin_lex_get_type(lex) == fin_lex_type_enum) {
            *enum_tail = fin_ast_parse_enum(ctx, lex);
            enum_tail = &(*enum_tail)->next;
        }
        else {
            *func_tail = fin_ast_parse_func(ctx, lex);
            func_tail = &(*func_tail)->next;
        }
    }

    fin_lex_destroy(ctx->alloc, lex);

    fin_ast_module_t* module = (fin_ast_module_t*)ctx->alloc(NULL, sizeof(fin_ast_module_t));
    module->ctx = ctx;
    module->name = name;
    module->types = types;
    module->enums = enums;
    module->funcs = funcs;
    return module;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x519c
movq %rax, %r15
leaq 0x6c42(%rip), %rsi      # 0xa0b8
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x8040
movq %rax, -0x70(%rbp)
xorl %r14d, %r14d
movq %r14, -0x68(%rbp)
movq %r14, -0x60(%rbp)
movq %r14, -0x58(%rbp)
movq %r15, %rdi
callq 0x57fc
movl $0x0, %r12d
movl $0x0, %r13d
cmpl $0x41, %eax
je 0x382b
leaq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq -0x68(%rbp), %rax
movq %rax, -0x40(%rbp)
movq %r15, %rdi
callq 0x57fc
cmpl $0x3e, %eax
jne 0x35d1
xorl %edi, %edi
movl $0x20, %esi
callq *(%rbx)
movq %rax, %r14
movq %r15, %rdi
movl $0x3e, %esi
callq 0x3992
movq %r15, %rdi
callq 0x589c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8040
movq %rax, (%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x39d4
movq %rax, 0x10(%r14)
movq %r15, %rdi
movl $0x32, %esi
callq 0x3992
movq %r14, -0x30(%rbp)
movq $0x0, 0x8(%r14)
movq %r15, %rdi
movl $0x33, %esi
callq 0x57da
testb %al, %al
jne 0x35ae
movq -0x30(%rbp), %rax
leaq 0x8(%rax), %r14
xorl %edi, %edi
movl $0x18, %esi
callq *(%rbx)
movq %rax, %r13
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3a5e
movq %rax, 0x8(%r13)
movq %r15, %rdi
callq 0x589c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8040
movq %rax, (%r13)
movq %r15, %rdi
movl $0xc, %esi
callq 0x3992
leaq 0x10(%r13), %r12
movq $0x0, 0x10(%r13)
movq %r13, (%r14)
movq %r15, %rdi
movl $0x33, %esi
callq 0x57da
movq %r12, %r14
testb %al, %al
je 0x354c
movq -0x30(%rbp), %rcx
movq $0x0, 0x18(%rcx)
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq %rcx, %rax
addq $0x18, %rax
movq %rax, -0x50(%rbp)
jmp 0x380e
movq %r15, %rdi
callq 0x57fc
movq (%rbx), %rcx
xorl %edi, %edi
cmpl $0x3f, %eax
jne 0x3700
movl $0x18, %esi
callq *%rcx
movq %rax, %r14
movq %r15, %rdi
movl $0x3f, %esi
callq 0x3992
movq %r15, %rdi
callq 0x589c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8040
movq %rax, (%r14)
movq %r15, %rdi
movl $0x32, %esi
callq 0x3992
movq %r14, -0x38(%rbp)
movq $0x0, 0x8(%r14)
movq %r15, %rdi
movl $0x33, %esi
callq 0x57da
testb %al, %al
jne 0x36dd
movq -0x38(%rbp), %rax
leaq 0x8(%rax), %r14
movq %r14, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x3665
movq %r15, %rdi
movl $0x9, %esi
callq 0x3992
xorl %edi, %edi
movl $0x18, %esi
callq *(%rbx)
movq %rax, %r12
movq %r15, %rdi
callq 0x589c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8040
movq %rax, (%r12)
movq %r15, %rdi
movl $0x10, %esi
callq 0x57da
movl %eax, %ecx
movl $0x0, %eax
testb %cl, %cl
je 0x36ad
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3ad6
movq %rax, 0x8(%r12)
movq %r12, %r13
addq $0x10, %r13
movq $0x0, 0x10(%r12)
movq %r12, (%r14)
movq %r15, %rdi
movl $0x33, %esi
callq 0x57da
movq %r13, %r14
testb %al, %al
je 0x364e
movq -0x38(%rbp), %rcx
movq $0x0, 0x10(%rcx)
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq %rcx, %rax
addq $0x10, %rax
movq %rax, -0x48(%rbp)
jmp 0x380e
movl $0x30, %esi
callq *%rcx
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3a5e
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x589c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8040
movq %rax, (%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x39d4
movq %rax, 0x20(%r14)
movq %r15, %rdi
movl $0x2e, %esi
callq 0x3992
movq %r14, -0x38(%rbp)
movq $0x0, 0x10(%r14)
movq %r15, %rdi
movl $0x2f, %esi
callq 0x57da
testb %al, %al
jne 0x37e1
movq -0x38(%rbp), %rax
leaq 0x10(%rax), %r12
movq %r12, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x378b
movq %r15, %rdi
movl $0x9, %esi
callq 0x3992
xorl %edi, %edi
movl $0x18, %esi
callq *(%rbx)
movq %rax, %r13
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3a5e
movq %rax, 0x8(%r13)
movq %r15, %rdi
callq 0x589c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8040
movq %rax, (%r13)
leaq 0x10(%r13), %r14
movq $0x0, 0x10(%r13)
movq %r13, (%r12)
movq %r15, %rdi
movl $0x2f, %esi
callq 0x57da
movq %r14, %r12
testb %al, %al
je 0x3774
movq %rbx, %rdi
movq %r15, %rsi
callq 0x46b4
movq -0x38(%rbp), %rcx
movq %rax, 0x18(%rcx)
movq $0x0, 0x28(%rcx)
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq %rcx, %rax
addq $0x28, %rax
movq %rax, -0x40(%rbp)
movq %r15, %rdi
callq 0x57fc
cmpl $0x41, %eax
jne 0x34cb
movq -0x58(%rbp), %r13
movq -0x60(%rbp), %r12
movq -0x68(%rbp), %r14
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x57cb
xorl %edi, %edi
movl $0x28, %esi
callq *(%rbx)
movq %rbx, (%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq %r13, 0x10(%rax)
movq %r12, 0x18(%rax)
movq %r14, 0x20(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq