// File: /workspace/github_c_makefile_star2_O1/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 0x518c
movq %rax, %r15
leaq 0x6c42(%rip), %rsi      # 0xa0b8
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x80e4
movq %rax, -0x70(%rbp)
xorl %eax, %eax
movq %rax, -0x68(%rbp)
movq %rax, -0x60(%rbp)
movq %rax, -0x58(%rbp)
movq %r15, %rdi
callq 0x57a4
cmpl $0x41, %eax
je 0x3812
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 0x57a4
cmpl $0x3e, %eax
jne 0x35c4
xorl %edi, %edi
movl $0x20, %esi
callq *(%rbx)
movq %rax, %r14
movq %r15, %rdi
movl $0x3e, %esi
callq 0x3985
movq %r15, %rdi
callq 0x5844
movq %rbx, %rdi
movq %rax, %rsi
callq 0x80e4
movq %rax, (%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x39c7
movq %rax, 0x10(%r14)
movq %r15, %rdi
movl $0x32, %esi
callq 0x3985
movq %r14, -0x30(%rbp)
movq $0x0, 0x8(%r14)
movq %r15, %rdi
movl $0x33, %esi
callq 0x5782
testb %al, %al
jne 0x35a1
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 0x3a51
movq %rax, 0x8(%r13)
movq %r15, %rdi
callq 0x5844
movq %rbx, %rdi
movq %rax, %rsi
callq 0x80e4
movq %rax, (%r13)
movq %r15, %rdi
movl $0xc, %esi
callq 0x3985
leaq 0x10(%r13), %r12
movq $0x0, 0x10(%r13)
movq %r13, (%r14)
movq %r15, %rdi
movl $0x33, %esi
callq 0x5782
movq %r12, %r14
testb %al, %al
je 0x353f
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 0x3801
movq %r15, %rdi
callq 0x57a4
movq (%rbx), %rcx
xorl %edi, %edi
cmpl $0x3f, %eax
jne 0x36f3
movl $0x18, %esi
callq *%rcx
movq %rax, %r14
movq %r15, %rdi
movl $0x3f, %esi
callq 0x3985
movq %r15, %rdi
callq 0x5844
movq %rbx, %rdi
movq %rax, %rsi
callq 0x80e4
movq %rax, (%r14)
movq %r15, %rdi
movl $0x32, %esi
callq 0x3985
movq %r14, -0x38(%rbp)
movq $0x0, 0x8(%r14)
movq %r15, %rdi
movl $0x33, %esi
callq 0x5782
testb %al, %al
jne 0x36d0
movq -0x38(%rbp), %rax
leaq 0x8(%rax), %r14
movq %r14, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x3658
movq %r15, %rdi
movl $0x9, %esi
callq 0x3985
xorl %edi, %edi
movl $0x18, %esi
callq *(%rbx)
movq %rax, %r12
movq %r15, %rdi
callq 0x5844
movq %rbx, %rdi
movq %rax, %rsi
callq 0x80e4
movq %rax, (%r12)
movq %r15, %rdi
movl $0x10, %esi
callq 0x5782
movl %eax, %ecx
movl $0x0, %eax
testb %cl, %cl
je 0x36a0
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3ac9
movq %rax, 0x8(%r12)
movq %r12, %r13
addq $0x10, %r13
movq $0x0, 0x10(%r12)
movq %r12, (%r14)
movq %r15, %rdi
movl $0x33, %esi
callq 0x5782
movq %r13, %r14
testb %al, %al
je 0x3641
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 0x3801
movl $0x30, %esi
callq *%rcx
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3a51
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x5844
movq %rbx, %rdi
movq %rax, %rsi
callq 0x80e4
movq %rax, (%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x39c7
movq %rax, 0x20(%r14)
movq %r15, %rdi
movl $0x2e, %esi
callq 0x3985
movq %r14, -0x38(%rbp)
movq $0x0, 0x10(%r14)
movq %r15, %rdi
movl $0x2f, %esi
callq 0x5782
testb %al, %al
jne 0x37d4
movq -0x38(%rbp), %rax
leaq 0x10(%rax), %r12
movq %r12, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x377e
movq %r15, %rdi
movl $0x9, %esi
callq 0x3985
xorl %edi, %edi
movl $0x18, %esi
callq *(%rbx)
movq %rax, %r13
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3a51
movq %rax, 0x8(%r13)
movq %r15, %rdi
callq 0x5844
movq %rbx, %rdi
movq %rax, %rsi
callq 0x80e4
movq %rax, (%r13)
leaq 0x10(%r13), %r14
movq $0x0, 0x10(%r13)
movq %r13, (%r12)
movq %r15, %rdi
movl $0x2f, %esi
callq 0x5782
movq %r14, %r12
testb %al, %al
je 0x3767
movq %rbx, %rdi
movq %r15, %rsi
callq 0x46a4
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 0x57a4
cmpl $0x41, %eax
jne 0x34be
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x5773
xorl %edi, %edi
movl $0x28, %esi
callq *(%rbx)
movq %rbx, (%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x68(%rbp), %rcx
movq %rcx, 0x20(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq