// File: /workspace/github_c_makefile_star2_O3/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 0x7f16
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x6a3dc(%rip), %rsi     # 0x722ee
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 0x7d04
leaq 0x6a3a2(%rip), %r12     # 0x722ff
movzbl (%r15), %eax
cmpl $0x9, %eax
je 0x7f71
cmpl $0x20, %eax
je 0x7f71
testl %eax, %eax
jne 0x7f83
jmp 0x7f88
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
movq %r12, %rsi
xorl %eax, %eax
callq *(%rcx)
incq %r15
jmp 0x7f5d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7df3
testl %eax, %eax
js 0x7fb0
movq 0x4118(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x6a380(%rip), %rsi     # 0x72329
movq %r14, %rdx
xorl %eax, %eax
callq *(%rcx)
movq 0x4110(%rbx), %rdi
movl $0x2318, %esi           # imm = 0x2318
callq 0x7a70
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 0x8045
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 0x802d
jmp 0x804a
movl $0x1505, %eax           # imm = 0x1505
movl %eax, 0x300(%r15)
cmpb $0x1, 0x4108(%rbx)
jne 0x80d9
movq %r15, %rax
movzbl (%rax), %ecx
cmpl $0x9, %ecx
je 0x806a
cmpl $0x20, %ecx
jne 0x806f
incq %rax
jmp 0x805d
cmpl $0x2f, %ecx
jne 0x807d
leaq 0x6f0fb(%rip), %r14     # 0x77176
jmp 0x8084
leaq 0x6a2b9(%rip), %r14     # 0x7233d
leaq 0x6a2b5(%rip), %rsi     # 0x72340
leaq 0x6f0e4(%rip), %r9      # 0x77176
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 0x6a285(%rip), %rsi     # 0x72342
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 0x7a70
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