// File: /workspace/github_c_makefile_star2_O2/vtereshkov[P]umka-lang/src/umka_common.c
// Function: moduleAdd
int moduleAdd(Modules *modules, const char *path)
{
    if (modules->numModules >= MAX_MODULES)
        modules->error->handler(modules->error->context, "Too many modules");

    char folder[DEFAULT_STR_LEN + 1] = "";
    char name  [DEFAULT_STR_LEN + 1] = "";

    moduleNameFromPath(modules, path, folder, name, DEFAULT_STR_LEN + 1);

    for (int i = 0; name[i]; i++)
        if (name[i] == ' ' || name[i] == '\t')
            modules->error->handler(modules->error->context, "Module name cannot contain spaces or tabs");

    int res = moduleFind(modules, path);
    if (res >= 0)
        modules->error->handler(modules->error->context, "Duplicate module %s", path);

    Module *module = storageAdd(modules->storage, sizeof(Module));

    strncpy(module->path, path, DEFAULT_STR_LEN);
    module->path[DEFAULT_STR_LEN] = 0;

    strncpy(module->folder, folder, DEFAULT_STR_LEN);
    module->folder[DEFAULT_STR_LEN] = 0;

    strncpy(module->name, name, DEFAULT_STR_LEN);
    module->name[DEFAULT_STR_LEN] = 0;

    module->pathHash = hash(path);

    if (modules->implLibsEnabled)
    {
        char libPath[2 + 2 * DEFAULT_STR_LEN + 8 + 4 + 1];

        const char *pathPrefix = modulePathIsAbsolute(module->path) ? "" : "./";

        // First, search for an implementation library with an OS-specific suffix
        sprintf(libPath, "%s%s%s%s.umi", pathPrefix, module->folder, module->name, moduleImplLibSuffix());
        module->implLib = moduleLoadImplLib(libPath);

        // If not found, search for an implementation library without suffix
        if (!module->implLib)
        {
            sprintf(libPath, "%s%s%s.umi", pathPrefix, module->folder, module->name);
            module->implLib = moduleLoadImplLib(libPath);
        }
    }

    // Self-import
    module->importAlias[modules->numModules] = storageAdd(modules->storage, DEFAULT_STR_LEN + 1);
    strcpy(module->importAlias[modules->numModules], name);

    module->isCompiled = false;

    modules->module[modules->numModules] = module;
    return modules->numModules++;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x400, 0x4000(%rdi)    # imm = 0x400
jl 0x45c8
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x61d2a(%rip), %rsi     # 0x662ee
xorl %eax, %eax
callq *(%rcx)
leaq 0x100(%rsp), %r12
movl $0x100, %edx            # imm = 0x100
movq %r12, %rdi
xorl %esi, %esi
callq 0x3190
movq %rsp, %r15
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
xorl %esi, %esi
callq 0x3190
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0x43cc
leaq 0x61cf0(%rip), %r12     # 0x662ff
movzbl (%r15), %eax
cmpl $0x9, %eax
je 0x4623
cmpl $0x20, %eax
je 0x4623
testl %eax, %eax
jne 0x4635
jmp 0x463a
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
movq %r12, %rsi
xorl %eax, %eax
callq *(%rcx)
incq %r15
jmp 0x460f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x44b5
testl %eax, %eax
js 0x4662
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x61cce(%rip), %rsi     # 0x66329
movq %r14, %rdx
xorl %eax, %eax
callq *(%rcx)
movq 0x4110(%rbx), %rdi
movl $0x2318, %esi           # imm = 0x2318
callq 0x4144
movq %rax, %r15
movl $0xff, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x3080
xorl %ebp, %ebp
movb %bpl, 0xff(%r15)
leaq 0x100(%r15), %r12
leaq 0x100(%rsp), %rsi
movl $0xff, %edx
movq %r12, %rdi
callq 0x3080
movb %bpl, 0x1ff(%r15)
leaq 0x200(%r15), %r13
movq %rsp, %rsi
movl $0xff, %edx
movq %r13, %rdi
callq 0x3080
movb %bpl, 0x2ff(%r15)
movq %r14, %rdi
callq 0x4515
movl %eax, 0x300(%r15)
cmpb $0x1, 0x4108(%rbx)
jne 0x4750
movq %r15, %rdi
callq 0x47a9
leaq 0x66a1f(%rip), %r9      # 0x6b116
leaq 0x61c3f(%rip), %r14     # 0x6633d
testb %al, %al
cmovneq %r9, %r14
leaq 0x61c35(%rip), %rsi     # 0x66340
leaq 0x200(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x3380
andq $0x0, 0x308(%r15)
leaq 0x61c0d(%rip), %rsi     # 0x66342
movq %rbp, %rdi
movq %r14, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x3380
andq $0x0, 0x308(%r15)
movq 0x4110(%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x4144
movslq 0x4000(%rbx), %rcx
movq %rax, 0x310(%r15,%rcx,8)
movq %rsp, %rsi
movq %rax, %rdi
callq 0x30a0
movb $0x0, 0x2310(%r15)
movslq 0x4000(%rbx), %rax
movq %r15, (%rbx,%rax,8)
leal 0x1(%rax), %ecx
movl %ecx, 0x4000(%rbx)
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq