// File: /workspace/github_c_makefile_star2_O3/naleksiev[P]fin/src/fin_mod.c
// Function: fin_mod_compile
fin_mod_t* fin_mod_compile(fin_ctx_t* ctx, const char* cstr) {
    fin_ast_module_t* module = fin_ast_parse(ctx, cstr);

    fin_mod_t* mod = (fin_mod_t*)ctx->alloc(NULL, sizeof(fin_mod_t));
    mod->types = NULL;
    mod->funcs = NULL;
    mod->consts = (fin_val_t*)ctx->alloc(NULL, sizeof(fin_val_t) * 128);
    mod->binds = (fin_mod_func_bind_t*)ctx->alloc(NULL, sizeof(fin_mod_func_bind_t) * 128);
    mod->types_count = 0;
    mod->funcs_count = 0;
    mod->consts_count = 0;
    mod->binds_count = 0;

    mod->next = NULL;

    for (fin_ast_type_t* type = module->types; type; type = type->next)
        mod->types_count++;
    for (fin_ast_func_t* func = module->funcs; func; func = func->next)
        mod->funcs_count++;

    if (mod->types_count) {
        mod->types = (fin_mod_type_t*)ctx->alloc(NULL, sizeof(fin_mod_type_t) * mod->types_count);

        int32_t idx = 0;
        for (fin_ast_type_t* type = module->types; type; type = type->next)
            fin_mod_compile_type(ctx, type, &mod->types[idx++]);
    }

    if (mod->funcs_count) {
        mod->funcs = (fin_mod_func_t*)ctx->alloc(NULL, sizeof(fin_mod_func_t) * mod->funcs_count);

        int32_t idx = 0;
        for (fin_ast_func_t* func = module->funcs; func; func = func->next) {
            fin_mod_func_t* f = &mod->funcs[idx++];

            char signature[128];
            signature[0] = '\0';
            //strcat(signature, fin_str_cstr(func->ret->name));
            //strcat(signature, " ");
            strcat(signature, fin_str_cstr(func->name));
            strcat(signature, "(");
            uint8_t args = 0;
            for (fin_ast_param_t* param = func->params; param; param = param->next) {
                if (args)
                    strcat(signature, ",");
                strcat(signature, fin_str_cstr(param->type->name));
                args++;
            }
            strcat(signature, ")");

            f->mod = mod;
            f->sign = fin_str_create(ctx, signature, -1);
            f->func = NULL;
            f->is_native = false;
            f->code = NULL;
            f->code_length = 0;
            f->args = args;
            f->ret_type = func->ret ? func->ret->name : NULL;
        }

        idx = 0;
        for (fin_ast_func_t* func = module->funcs; func; func = func->next)
            fin_mod_compile_func(&mod->funcs[idx++], ctx, mod, func);
    }

    fin_ast_destroy(module);

    mod->name = NULL;
    fin_str_t* entry_name = fin_str_create(ctx, "Main()", 6);
    mod->entry = fin_mod_find_func(ctx, mod, entry_name);
    fin_str_destroy(ctx, entry_name);
    fin_mod_register(ctx, mod);
    return mod;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a58, %rsp           # imm = 0x1A58
movq %rdi, %rbx
callq 0x3450
movq %rax, %r12
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x48, %esi
callq *(%rbx)
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
xorl %edi, %edi
movl $0x400, %esi            # imm = 0x400
callq *(%rbx)
movq %rax, 0x18(%r15)
xorl %edi, %edi
movl $0x800, %esi            # imm = 0x800
callq *(%rbx)
movq %rax, 0x20(%r15)
movq $0x0, 0x40(%r15)
movq %r15, -0x30(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r15)
movq %r12, -0x40(%rbp)
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x5ea4
movq -0x30(%rbp), %rcx
addq $0x28, %rcx
xorl %r14d, %r14d
incl %r14d
movq 0x18(%rax), %rax
testq %rax, %rax
jne 0x5e95
movl %r14d, (%rcx)
movq %rbx, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
testq %rax, %rax
je 0x5ec9
xorl %ecx, %ecx
incl %ecx
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x5eb7
movq -0x30(%rbp), %rax
movl %ecx, 0x2c(%rax)
testl %r14d, %r14d
je 0x5f95
movq -0x30(%rbp), %rbx
addq $0x8, %rbx
shll $0x3, %r14d
leal (%r14,%r14,2), %esi
xorl %edi, %edi
movq -0x38(%rbp), %rax
callq *(%rax)
movq %rax, (%rbx)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %r14
testq %r14, %r14
je 0x5f95
xorl %r15d, %r15d
movq 0x8(%r14), %rax
movl $0x0, %r12d
testq %rax, %rax
je 0x5f1f
xorl %r12d, %r12d
incl %r12d
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x5f13
leaq (%r15,%r15,2), %r13
shlq $0x3, %r13
addq (%rbx), %r13
movq (%r14), %rdi
callq 0x824c
movq %rax, (%r13)
movl %r12d, 0x10(%r13)
shll $0x4, %r12d
xorl %edi, %edi
movl %r12d, %esi
movq -0x38(%rbp), %rax
callq *(%rax)
movq %rax, 0x8(%r13)
movq 0x8(%r14), %r13
testq %r13, %r13
je 0x5f85
movq %rax, %r12
movq (%r13), %rdi
callq 0x824c
movq %rax, (%r12)
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x824c
movq %rax, 0x8(%r12)
addq $0x10, %r12
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x5f59
incq %r15
movq 0x18(%r14), %r14
testq %r14, %r14
jne 0x5f01
movq -0x30(%rbp), %rax
movl 0x2c(%rax), %eax
testl %eax, %eax
movq -0x38(%rbp), %r15
je 0x62a9
imull $0x38, %eax, %esi
xorl %edi, %edi
callq *(%r15)
movq -0x30(%rbp), %rcx
movq %rax, 0x10(%rcx)
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rbx
testq %rbx, %rbx
je 0x62a9
leaq -0x1a80(%rbp), %r12
xorl %ecx, %ecx
movq %rcx, -0x48(%rbp)
imulq $0x38, %rcx, %r13
movq -0x30(%rbp), %rax
addq 0x10(%rax), %r13
movb $0x0, -0x1a80(%rbp)
movq (%rbx), %rdi
callq 0x83b1
movq %r12, %rdi
movq %rax, %rsi
callq 0x3280
movq %r12, %rdi
callq 0x3100
movw $0x28, -0x1a80(%rbp,%rax)
movq 0x10(%rbx), %r15
xorl %r14d, %r14d
testq %r15, %r15
je 0x6056
subb $0x1, %r14b
jb 0x6032
movq %r12, %rdi
callq 0x3100
movw $0x2c, -0x1a80(%rbp,%rax)
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x83b1
movq %r12, %rdi
movq %rax, %rsi
callq 0x3280
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0x601a
negb %r14b
movq %r12, %rdi
callq 0x3100
movw $0x29, -0x1a80(%rbp,%rax)
movq -0x30(%rbp), %rax
movq %rax, (%r13)
movq -0x38(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x8040
movq %rax, 0x10(%r13)
xorl %eax, %eax
movq %rax, 0x18(%r13)
movb $0x0, 0x20(%r13)
movq %rax, 0x28(%r13)
movl $0x0, 0x30(%r13)
movb %r14b, 0x34(%r13)
movq 0x8(%rbx), %rcx
movl $0x0, %eax
testq %rcx, %rcx
je 0x60b5
movq 0x8(%rcx), %rax
movq -0x48(%rbp), %rcx
incq %rcx
movq %rax, 0x8(%r13)
movq 0x28(%rbx), %rbx
testq %rbx, %rbx
jne 0x5fd2
movq -0x40(%rbp), %rax
movq 0x20(%rax), %r12
testq %r12, %r12
je 0x62a9
leaq -0x1950(%rbp), %r14
xorl %ebx, %ebx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %r13
imulq $0x38, %rbx, %rcx
movq %rax, -0x1a80(%rbp)
movq %r12, -0x1a78(%rbp)
movl $0x0, -0x50(%rbp)
movq %rcx, -0x48(%rbp)
movq 0x8(%r13,%rcx), %rdi
callq 0x824c
movq %rax, -0x1a70(%rbp)
leaq -0x1a50(%rbp), %rax
movq %rax, -0x1a68(%rbp)
movq %rax, -0x1a60(%rbp)
movq %r14, -0x1a58(%rbp)
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x617c
xorl %ecx, %ecx
movzbl %cl, %edx
incb %cl
leaq (%rdx,%rdx,2), %rsi
movq (%rax), %rdi
movq %rdi, (%r14,%rsi,8)
movq 0x8(%rax), %rdi
movq 0x8(%rdi), %rdi
movq %rdi, 0x8(%r14,%rsi,8)
movb %dl, 0x10(%r14,%rsi,8)
movb $0x1, 0x11(%r14,%rsi,8)
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x6145
movb %cl, -0x50(%rbp)
movb %cl, -0x4e(%rbp)
movq 0x18(%r12), %rdx
movq %r15, %rdi
leaq -0x1a80(%rbp), %rsi
callq 0x64d4
movq -0x1a68(%rbp), %rsi
movq -0x1a60(%rbp), %rdi
cmpq %rdi, %rsi
je 0x61ad
cmpb $0xb, -0x1(%rsi)
je 0x6250
leaq 0x1(%rsi), %rax
movq -0x1a58(%rbp), %r15
cmpq %r15, %rax
jb 0x6230
leaq -0x1a50(%rbp), %rax
cmpq %rax, %rdi
je 0x61dd
subl %edi, %r15d
addl %r15d, %r15d
movl %r15d, %esi
movq -0x38(%rbp), %rax
callq *(%rax)
movslq %r15d, %rcx
jmp 0x620e
xorl %edi, %edi
movl $0x200, %esi            # imm = 0x200
movq -0x38(%rbp), %rax
callq *(%rax)
movq %rax, %r15
movq -0x1a68(%rbp), %rdx
movq -0x1a60(%rbp), %rsi
subq %rsi, %rdx
movq %rax, %rdi
callq 0x31c0
movq %r15, %rax
movl $0x200, %ecx            # imm = 0x200
movq -0x1a68(%rbp), %rsi
subq -0x1a60(%rbp), %rsi
addq %rax, %rsi
movq %rax, -0x1a60(%rbp)
addq %rax, %rcx
movq %rcx, -0x1a58(%rbp)
leaq 0x1(%rsi), %rax
movq %rax, -0x1a68(%rbp)
movb $0xb, (%rsi)
movq -0x1a68(%rbp), %rsi
movq -0x1a60(%rbp), %rdi
movq -0x38(%rbp), %r15
addq -0x48(%rbp), %r13
subl %edi, %esi
movl %esi, 0x30(%r13)
xorl %edi, %edi
callq *(%r15)
movq %rax, 0x28(%r13)
movq -0x1a60(%rbp), %rsi
movslq 0x30(%r13), %rdx
movq %rax, %rdi
callq 0x31c0
movb -0x4f(%rbp), %al
subb -0x4e(%rbp), %al
movb %al, 0x35(%r13)
movq -0x1a60(%rbp), %rdi
leaq -0x1a50(%rbp), %rax
cmpq %rax, %rdi
je 0x6298
xorl %esi, %esi
callq *(%r15)
incq %rbx
movq 0x28(%r12), %r12
testq %r12, %r12
jne 0x60e7
movq -0x40(%rbp), %rdi
callq 0x3865
movq -0x30(%rbp), %r14
movq $0x0, (%r14)
leaq 0x415c(%rip), %rsi      # 0xa420
movq %r15, %rdi
movl $0x6, %edx
callq 0x8040
movq %r15, %rbx
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x6314
movq %rax, 0x38(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8217
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5d17
movq %r14, %rax
addq $0x1a58, %rsp           # imm = 0x1A58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq