// File: /workspace/github_c_makefile_star2_O1/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, %r15
xorl %edi, %edi
movl $0x48, %esi
callq *(%rbx)
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
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), %rax
movq $0x0, 0x40(%r14)
movq %r14, -0x30(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r14)
movq %r15, -0x40(%rbp)
movq 0x10(%r15), %rcx
testq %rcx, %rcx
je 0x5e83
movl (%rax), %edx
incl %edx
movq 0x18(%rcx), %rcx
testq %rcx, %rcx
jne 0x5e76
movl %edx, (%rax)
movq %rbx, -0x38(%rbp)
movq -0x40(%rbp), %rcx
movq 0x20(%rcx), %rcx
testq %rcx, %rcx
je 0x5ead
movq -0x30(%rbp), %rdx
movl 0x2c(%rdx), %edx
incl %edx
movq 0x28(%rcx), %rcx
testq %rcx, %rcx
jne 0x5e9b
movq -0x30(%rbp), %rcx
movl %edx, 0x2c(%rcx)
movl (%rax), %eax
testl %eax, %eax
je 0x5f78
movq -0x30(%rbp), %rbx
addq $0x8, %rbx
shll $0x3, %eax
leal (%rax,%rax,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 0x5f78
xorl %r15d, %r15d
movq 0x8(%r14), %rax
movl $0x0, %r12d
testq %rax, %rax
je 0x5f02
xorl %r12d, %r12d
incl %r12d
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x5ef6
leaq (%r15,%r15,2), %r13
shlq $0x3, %r13
addq (%rbx), %r13
movq (%r14), %rdi
callq 0x8322
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 0x5f68
movq %rax, %r12
movq (%r13), %rdi
callq 0x8322
movq %rax, (%r12)
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x8322
movq %rax, 0x8(%r12)
addq $0x10, %r12
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x5f3c
incq %r15
movq 0x18(%r14), %r14
testq %r14, %r14
jne 0x5ee4
movq -0x30(%rbp), %rax
movl 0x2c(%rax), %eax
testl %eax, %eax
movq -0x38(%rbp), %r15
je 0x629b
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 0x60b0
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 0x8477
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 0x6039
subb $0x1, %r14b
jb 0x6015
movq %r12, %rdi
callq 0x3100
movw $0x2c, -0x1a80(%rbp,%rax)
movq 0x8(%r15), %rax
movq 0x8(%rax), %rdi
callq 0x8477
movq %r12, %rdi
movq %rax, %rsi
callq 0x3280
movq 0x10(%r15), %r15
testq %r15, %r15
jne 0x5ffd
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 0x80e4
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 0x6098
movq 0x8(%rcx), %rax
movq -0x48(%rbp), %rcx
incq %rcx
movq %rax, 0x8(%r13)
movq 0x28(%rbx), %rbx
testq %rbx, %rbx
jne 0x5fb5
movq -0x40(%rbp), %rax
movq 0x20(%rax), %r12
testq %r12, %r12
je 0x629b
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 0x8322
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 0x6165
movb -0x50(%rbp), %cl
movb -0x4e(%rbp), %dl
movzbl %cl, %esi
incb %cl
leaq (%rsi,%rsi,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)
incb %dl
movb $0x1, 0x11(%r14,%rsi,8)
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x612c
movb %cl, -0x50(%rbp)
movb %dl, -0x4e(%rbp)
movq 0x18(%r12), %rdx
movq %r15, %rdi
leaq -0x1a80(%rbp), %rsi
callq 0x6526
movq -0x1a68(%rbp), %rax
movq -0x1a60(%rbp), %rdi
cmpq %rdi, %rax
je 0x6196
cmpb $0xb, -0x1(%rax)
je 0x6238
incq %rax
movq -0x1a58(%rbp), %r15
cmpq %r15, %rax
jb 0x621f
leaq -0x1a50(%rbp), %rax
cmpq %rax, %rdi
je 0x61c5
subl %edi, %r15d
addl %r15d, %r15d
movl %r15d, %esi
movq -0x38(%rbp), %rax
callq *(%rax)
movslq %r15d, %rdx
jmp 0x61f6
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, %edx            # imm = 0x200
movq -0x1a68(%rbp), %rcx
subq -0x1a60(%rbp), %rcx
addq %rax, %rcx
movq %rcx, -0x1a68(%rbp)
movq %rax, -0x1a60(%rbp)
addq %rax, %rdx
movq %rdx, -0x1a58(%rbp)
movq -0x1a68(%rbp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, -0x1a68(%rbp)
movb $0xb, (%rax)
movq -0x38(%rbp), %r15
addq -0x48(%rbp), %r13
movl -0x1a68(%rbp), %esi
subl -0x1a60(%rbp), %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 0x628a
xorl %esi, %esi
callq *(%r15)
incq %rbx
movq 0x28(%r12), %r12
testq %r12, %r12
jne 0x60ca
movq -0x40(%rbp), %rdi
callq 0x3858
movq -0x30(%rbp), %r14
movq $0x0, (%r14)
leaq 0x416a(%rip), %rsi      # 0xa420
movq %r15, %rdi
movl $0x6, %edx
callq 0x80e4
movq %r15, %rbx
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x6306
movq %rax, 0x38(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x82ed
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5d03
movq %r14, %rax
addq $0x1a58, %rsp           # imm = 0x1A58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq