// File: /workspace/github_c_makefile_star2_O1/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 0xd170
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x66182(%rip), %rsi     # 0x732ee
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 0xcf4a
leaq 0x66148(%rip), %r12     # 0x732ff
movzbl (%r15), %eax
cmpl $0x9, %eax
je 0xd1cb
cmpl $0x20, %eax
je 0xd1cb
testl %eax, %eax
jne 0xd1dd
jmp 0xd1e2
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
movq %r12, %rsi
xorl %eax, %eax
callq *(%rcx)
incq %r15
jmp 0xd1b7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd039
testl %eax, %eax
js 0xd20a
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x66126(%rip), %rsi     # 0x73329
movq %r14, %rdx
xorl %eax, %eax
callq *(%rcx)
movq 0x4110(%rbx), %rdi
movl $0x2318, %esi           # imm = 0x2318
callq 0xcca8
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)
movb (%r14), %cl
testb %cl, %cl
je 0xd29f
incq %r14
movl $0x1505, %eax           # imm = 0x1505
movl %eax, %edx
shll $0x5, %edx
addl %eax, %edx
movsbl %cl, %eax
addl %edx, %eax
movb (%r14), %cl
incq %r14
testb %cl, %cl
jne 0xd287
jmp 0xd2a4
movl $0x1505, %eax           # imm = 0x1505
movl %eax, 0x300(%r15)
cmpb $0x1, 0x4108(%rbx)
jne 0xd33a
leaq 0x6607e(%rip), %r14     # 0x7333d
testq %r15, %r15
je 0xd2e5
movq %r15, %rax
movzbl (%rax), %ecx
cmpl $0x9, %ecx
je 0xd2d4
cmpl $0x20, %ecx
jne 0xd2d9
incq %rax
jmp 0xd2c7
cmpl $0x2f, %ecx
jne 0xd2e5
leaq 0x6ae91(%rip), %r14     # 0x78176
leaq 0x66054(%rip), %rsi     # 0x73340
leaq 0x6ae83(%rip), %r9      # 0x78176
leaq 0x200(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x3380
xorl %eax, %eax
movq %rax, 0x308(%r15)
leaq 0x66024(%rip), %rsi     # 0x73342
movq %rbp, %rdi
movq %r14, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x3380
xorl %eax, %eax
movq %rax, 0x308(%r15)
movq 0x4110(%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0xcca8
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