// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3450
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edi
movl $0x48, %esi
callq *(%rax)
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edi
movl $0x400, %esi            # imm = 0x400
callq *(%rax)
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edi
movl $0x800, %esi            # imm = 0x800
callq *(%rax)
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x2c(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x34(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x7bc9
movq -0x20(%rbp), %rax
movl 0x28(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x28(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x7ba7
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x7bf7
movq -0x20(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2c(%rax)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x7bd5
movq -0x20(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x7c7a
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x28(%rcx), %rcx
imulq $0x18, %rcx, %rcx
movl %ecx, %esi
xorl %ecx, %ecx
movl %ecx, %edi
callq *%rax
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x34(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x7c78
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdx
movl -0x34(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
cltq
imulq $0x18, %rax, %rax
addq %rax, %rdx
callq 0x7f60
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x7c3a
jmp 0x7c7a
movq -0x20(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x7ee6
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x2c(%rcx), %rcx
imulq $0x38, %rcx, %rcx
movl %ecx, %esi
xorl %ecx, %ecx
movl %ecx, %edi
callq *%rax
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x7e80
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x44(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x44(%rbp)
movslq %ecx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movb $0x0, -0xe0(%rbp)
leaq -0xe0(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rdi
callq 0xb370
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
leaq -0xe0(%rbp), %rdi
leaq 0x5a95(%rip), %rsi      # 0xd7c0
callq 0x3270
movb $0x0, -0xe1(%rbp)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0x7dbf
cmpb $0x0, -0xe1(%rbp)
je 0x7d6c
leaq -0xe0(%rbp), %rdi
leaq 0x5909(%rip), %rsi      # 0xd670
callq 0x3270
leaq -0xe0(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0xf0(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0xb370
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
movb -0xe1(%rbp), %al
addb $0x1, %al
movb %al, -0xe1(%rbp)
movq -0xf0(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xf0(%rbp)
jmp 0x7d46
leaq -0xe0(%rbp), %rdi
leaq 0x6347(%rip), %rsi      # 0xe114
callq 0x3270
movq -0x20(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0xae20
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x58(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x20(%rax)
movq -0x58(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x58(%rbp), %rax
movl $0x0, 0x30(%rax)
movb -0xe1(%rbp), %cl
movq -0x58(%rbp), %rax
movb %cl, 0x34(%rax)
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x7e55
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x118(%rbp)
jmp 0x7e60
xorl %eax, %eax
movq %rax, -0x118(%rbp)
jmp 0x7e60
movq -0x118(%rbp), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x7cc1
movl $0x0, -0x44(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, -0xf8(%rbp)
je 0x7ee4
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x44(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x44(%rbp)
cltq
imulq $0x38, %rax, %rax
addq %rax, %rdi
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0xf8(%rbp), %rcx
callq 0x8060
movq -0xf8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xf8(%rbp)
jmp 0x7e96
jmp 0x7ee6
movq -0x18(%rbp), %rdi
callq 0x3900
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rdi
leaq 0x576d(%rip), %rsi      # 0xd672
movl $0x6, %edx
callq 0xae20
movq %rax, -0x100(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x100(%rbp), %rdx
callq 0x8250
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x100(%rbp), %rsi
callq 0xb0b0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x7940
movq -0x20(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nop