// File: /workspace/github_c_makefile_star2_O2/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 $0x1a78, %rsp           # imm = 0x1A78
movq %rdi, %rbx
callq 0x3450
movq %rax, %r12
movq %rax, -0x58(%rbp)
xorl %r15d, %r15d
pushq $0x48
popq %rsi
xorl %edi, %edi
callq *(%rbx)
movq %rax, %r14
addq $0x8, %rax
movq %rax, -0x48(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
xorl %edi, %edi
movl $0x400, %esi            # imm = 0x400
callq *(%rbx)
movq %rax, 0x18(%r14)
xorl %edi, %edi
movl $0x800, %esi            # imm = 0x800
callq *(%rbx)
movq %rax, 0x20(%r14)
leaq 0x28(%r14), %rcx
andq $0x0, 0x40(%r14)
movq %r12, %rsi
leaq 0x10(%r12), %r13
movq %r14, -0x30(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r14)
pushq $0x1
popq %rax
movq %r13, %rdx
movq (%rdx), %rdx
testq %rdx, %rdx
je 0x5adf
movl %eax, (%rcx)
addq $0x18, %rdx
incl %eax
addl $0x18, %r15d
jmp 0x5ac9
movq %rsi, %r14
addq $0x20, %r14
xorl %ecx, %ecx
movq %r14, %rdx
movq -0x30(%rbp), %rsi
movq (%rdx), %rdx
testq %rdx, %rdx
je 0x5b02
incl %ecx
movl %ecx, 0x2c(%rsi)
addq $0x28, %rdx
jmp 0x5aef
cmpl $0x1, %eax
movq %rbx, -0x40(%rbp)
movq %r14, -0x50(%rbp)
je 0x5bdf
xorl %r12d, %r12d
xorl %edi, %edi
movl %r15d, %esi
callq *(%rbx)
movq -0x48(%rbp), %rcx
movq %rax, (%rcx)
movq (%r13), %r13
testq %r13, %r13
je 0x5bd8
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
leaq 0x8(%r13), %r14
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %r14, %rax
movq (%rax), %rax
testq %rax, %rax
je 0x5b5c
incl %ebx
addq $0x10, %rax
addl $0x10, %r15d
jmp 0x5b48
movq %r12, -0x38(%rbp)
imulq $0x18, %r12, %r12
movq %r13, -0x60(%rbp)
movq (%r13), %rdi
callq 0x79dd
movq -0x68(%rbp), %r13
movq %rax, (%r13,%r12)
movl %ebx, 0x10(%r13,%r12)
xorl %edi, %edi
movl %r15d, %esi
movq -0x40(%rbp), %rbx
callq *(%rbx)
movq %rax, %r15
movq %rax, 0x8(%r13,%r12)
movq (%r14), %r14
testq %r14, %r14
je 0x5bc0
movq (%r14), %rdi
callq 0x79dd
movq %rax, (%r15)
movq 0x8(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x79dd
movq %rax, 0x8(%r15)
addq $0x10, %r14
addq $0x10, %r15
jmp 0x5b92
movq -0x38(%rbp), %r12
incq %r12
movq -0x60(%rbp), %r13
addq $0x18, %r13
movq -0x50(%rbp), %r14
jmp 0x5b24
movq -0x30(%rbp), %rax
movl 0x2c(%rax), %ecx
testl %ecx, %ecx
je 0x5e4b
imull $0x38, %ecx, %esi
movq $0x0, -0x38(%rbp)
xorl %edi, %edi
callq *(%rbx)
movq -0x30(%rbp), %rcx
movq %rax, 0x10(%rcx)
leaq -0x1aa0(%rbp), %r15
movq %r14, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x5d06
movq -0x30(%rbp), %rax
movq 0x10(%rax), %r14
movb $0x0, -0x1aa0(%rbp)
movq (%rbx), %rdi
callq 0x7b28
movq %r15, %rdi
movq %rax, %rsi
callq 0x3280
movq %r15, %rdi
callq 0x3100
movw $0x28, -0x1aa0(%rbp,%rax)
xorl %r13d, %r13d
movq %rbx, %r12
movq 0x10(%r12), %r12
testq %r12, %r12
je 0x5c8d
testb %r13b, %r13b
je 0x5c6f
movq %r15, %rdi
callq 0x3100
movw $0x2c, -0x1aa0(%rbp,%rax)
movq 0x8(%r12), %rax
movq 0x8(%rax), %rdi
callq 0x7b28
movq %r15, %rdi
movq %rax, %rsi
callq 0x3280
incb %r13b
jmp 0x5c4e
imulq $0x38, -0x38(%rbp), %r12
movq %r15, %rdi
callq 0x3100
movw $0x29, -0x1aa0(%rbp,%rax)
movq -0x30(%rbp), %rax
movq %rax, (%r14,%r12)
movq -0x40(%rbp), %rdi
movq %r15, %rsi
pushq $-0x1
popq %rdx
callq 0x781e
movq %rax, 0x10(%r14,%r12)
andq $0x0, 0x18(%r14,%r12)
movb $0x0, 0x20(%r14,%r12)
andq $0x0, 0x28(%r14,%r12)
andl $0x0, 0x30(%r14,%r12)
movb %r13b, 0x34(%r14,%r12)
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x5cec
movq 0x8(%rax), %rax
jmp 0x5cee
xorl %eax, %eax
incq -0x38(%rbp)
addq %r12, %r14
movq %rax, 0x8(%r14)
addq $0x28, %rbx
movq -0x50(%rbp), %r14
jmp 0x5c08
leaq -0x1970(%rbp), %r15
xorl %r13d, %r13d
movq -0x40(%rbp), %rbx
movq (%r14), %r14
testq %r14, %r14
je 0x5e4b
movq -0x30(%rbp), %rdx
movq 0x10(%rdx), %rax
imulq $0x38, %r13, %rcx
leaq (%rax,%rcx), %r12
movq %rdx, -0x1aa0(%rbp)
movq %r14, -0x1a98(%rbp)
andl $0x0, -0x70(%rbp)
movq 0x8(%rax,%rcx), %rdi
callq 0x79dd
movq %rax, -0x1a90(%rbp)
leaq -0x1a70(%rbp), %rax
movq %rax, -0x1a88(%rbp)
movq %rax, -0x1a80(%rbp)
movq %r15, -0x1a78(%rbp)
xorl %ecx, %ecx
movq %r14, %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x5dad
movzbl %cl, %edx
incl %ecx
movb %cl, -0x70(%rbp)
imulq $0x18, %rdx, %rsi
movq (%rax), %rdi
movq %rdi, (%r15,%rsi)
movq 0x8(%rax), %rdi
movq 0x8(%rdi), %rdi
movq %rdi, 0x8(%r15,%rsi)
movb %cl, -0x6e(%rbp)
movb %dl, 0x10(%r15,%rsi)
movb $0x1, 0x11(%r15,%rsi)
jmp 0x5d74
movq 0x18(%r14), %rdx
movq %rbx, %rdi
leaq -0x1aa0(%rbp), %rsi
callq 0x608c
movq -0x1a88(%rbp), %rsi
movq -0x1a80(%rbp), %rax
cmpq %rax, %rsi
je 0x5dd9
cmpb $0xb, -0x1(%rsi)
je 0x5df9
movq %rbx, %rdi
leaq -0x1a88(%rbp), %rsi
pushq $0xb
popq %rdx
callq 0x653a
movq -0x1a88(%rbp), %rsi
movq -0x1a80(%rbp), %rax
subl %eax, %esi
movl %esi, 0x30(%r12)
xorl %edi, %edi
callq *(%rbx)
movq %rax, 0x28(%r12)
movq -0x1a80(%rbp), %rsi
movslq 0x30(%r12), %rdx
movq %rax, %rdi
callq 0x31c0
movb -0x6f(%rbp), %al
subb -0x6e(%rbp), %al
movb %al, 0x35(%r12)
movq -0x1a80(%rbp), %rdi
leaq -0x1a70(%rbp), %rax
cmpq %rax, %rdi
je 0x5e3f
xorl %esi, %esi
callq *(%rbx)
incq %r13
addq $0x28, %r14
jmp 0x5d14
movq -0x58(%rbp), %rdi
callq 0x37ad
movq -0x30(%rbp), %r14
andq $0x0, (%r14)
leaq 0x35bd(%rip), %rsi      # 0x9420
pushq $0x6
popq %rdx
movq %rbx, %rdi
callq 0x781e
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x5eae
movq %rax, 0x38(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x79ab
movq %rbx, %rdi
movq %r14, %rsi
callq 0x593b
movq %r14, %rax
addq $0x1a78, %rsp           # imm = 0x1A78
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq