// File: /workspace/github_c_makefile_star2_O1/vtereshkov[P]umka-lang/src/umka_decl.c
// Function: parseImportItem
static void parseImportItem(Compiler *comp)
{
    char *alias = NULL;
    if (comp->lex.tok.kind == TOK_IDENT)
    {
        alias = storageAdd(&comp->storage, DEFAULT_STR_LEN + 1);
        strcpy(alias, comp->lex.tok.name);
        lexNext(&comp->lex);
        lexEat(&comp->lex, TOK_EQ);
    }

    lexCheck(&comp->lex, TOK_STRLITERAL);

    char path[DEFAULT_STR_LEN + 1] = "";

    // Module source strings, if any, have precedence over files
    const char *sourceString = NULL;
    bool sourceTrusted = false;

    if (moduleRegularizePath(&comp->modules, comp->lex.tok.strVal, comp->modules.curFolder, path, DEFAULT_STR_LEN + 1))
    {
        const ModuleSource *sourceDesc = moduleFindSource(&comp->modules, path);
        if (sourceDesc)
        {
            sourceString = sourceDesc->source;
            sourceTrusted = sourceDesc->trusted;
        }
    }

    if (!sourceString)
        moduleAssertRegularizePath(&comp->modules, comp->lex.tok.strVal, comp->modules.module[comp->blocks.module]->folder, path, DEFAULT_STR_LEN + 1);

    char folder[DEFAULT_STR_LEN + 1] = "";
    char name  [DEFAULT_STR_LEN + 1] = "";

    moduleNameFromPath(&comp->modules, path, folder, name, DEFAULT_STR_LEN + 1);

    if (!alias)
    {
        alias = storageAdd(&comp->storage, DEFAULT_STR_LEN + 1);
        strcpy(alias, name);
    }

    if (moduleFindImported(&comp->modules, &comp->blocks, alias) >= 0)
        comp->modules.error->handler(comp->modules.error->context, "Duplicate imported module %s", alias);

    int importedModule = moduleFind(&comp->modules, path);
    if (importedModule < 0)
    {
        // Save context
        int currentModule       = comp->blocks.module;
        DebugInfo currentDebug  = comp->debug;
        Lexer currentLex        = comp->lex;
        lexInit(&comp->lex, &comp->storage, &comp->debug, path, sourceString, sourceTrusted, &comp->error);

        lexNext(&comp->lex);
        importedModule = parseModule(comp);

        // Restore context
        lexFree(&comp->lex);
        comp->lex               = currentLex;
        comp->debug             = currentDebug;
        comp->blocks.module     = currentModule;
    }

    // Imported module is registered but its body has not been compiled yet - this is only possible if it's imported in a cycle
    if (!comp->modules.module[importedModule]->isCompiled)
        comp->modules.error->handler(comp->modules.error->context, "Cyclic import of module %s", alias);

    // Module is imported iff it has an import alias (which may coincide with the module name if not specified explicitly)
    char **importAlias = &comp->modules.module[comp->blocks.module]->importAlias[importedModule];
    if (*importAlias)
         comp->modules.error->handler(comp->modules.error->context, "Duplicate imported module %s", path);
    *importAlias = alias;

    identAddModule(&comp->idents, &comp->modules, &comp->blocks, alias, comp->voidType, importedModule);

    lexNext(&comp->lex);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5b8, %rsp            # imm = 0x5B8
movq %rdi, %r14
leaq 0x4ba8(%rdi), %rbx
cmpl $0x45, 0x4bd0(%rdi)
jne 0x117d4
leaq 0xf0(%r14), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0xcca8
movq %rax, %r15
leaq 0x4bd8(%r14), %rsi
movq %rax, %rdi
callq 0x30a0
movq %rbx, %rdi
callq 0x1a984
movq %rbx, %rdi
movl $0x31, %esi
callq 0x1b41f
jmp 0x117d7
xorl %r15d, %r15d
movq %rbx, %rdi
movl $0x49, %esi
callq 0x1b3c8
leaq 0x40(%rsp), %r13
xorl %ebp, %ebp
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
xorl %esi, %esi
callq 0x3190
leaq 0x100(%r14), %r12
movq 0x4bd8(%r14), %rsi
leaq 0x4108(%r14), %rdx
movq %r12, %rdi
movq %r13, %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0xd5cb
testb %al, %al
je 0x11848
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0xd3b5
testq %rax, %rax
je 0x11846
movq 0x308(%rax), %rcx
movb 0x310(%rax), %bpl
jmp 0x1184a
xorl %ebp, %ebp
xorl %ecx, %ecx
movq %rbp, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
testq %rcx, %rcx
jne 0x11887
movq 0x4bd8(%r14), %rsi
movslq 0x4b88(%r14), %rax
movl $0x100, %edx            # imm = 0x100
addq 0x100(%r14,%rax,8), %rdx
leaq 0x40(%rsp), %rcx
movq %r12, %rdi
movl $0x100, %r8d            # imm = 0x100
callq 0xd70b
leaq 0x240(%rsp), %r13
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
xorl %esi, %esi
callq 0x3190
leaq 0x140(%rsp), %rbp
movl $0x100, %edx            # imm = 0x100
movq %rbp, %rdi
xorl %esi, %esi
callq 0x3190
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0xcf4a
testq %r15, %r15
jne 0x118f7
leaq 0xf0(%r14), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0xcca8
movq %rax, %r15
leaq 0x140(%rsp), %rsi
movq %rax, %rdi
callq 0x30a0
leaq 0x4220(%r14), %rsi
movq %r12, %rdi
movq %rsi, 0x18(%rsp)
movq %r15, %rdx
callq 0xd0c6
testl %eax, %eax
js 0x1192b
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x65c26(%rip), %rsi     # 0x7754a
movq %r15, %rdx
xorl %eax, %eax
callq *(%rcx)
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0xd039
movl %eax, %ebp
testl %eax, %eax
jns 0x119fa
movl 0x4b88(%r14), %r13d
leaq 0x50e0(%r14), %rbp
movq 0x50f0(%r14), %rax
movq %rax, 0x30(%rsp)
movups 0x50e0(%r14), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x348(%rsp), %rdi
movl $0x270, %edx            # imm = 0x270
movq %rbx, %rsi
callq 0x3260
leaq 0xf0(%r14), %rsi
leaq 0x50f8(%r14), %rax
movq %rax, (%rsp)
movzbl 0x10(%rsp), %r9d
leaq 0x40(%rsp), %rcx
movq %rbx, %rdi
movq %rbp, %rdx
movq 0x8(%rsp), %r8
callq 0x1a6f4
movq %rbx, %rdi
callq 0x1a984
movq %r14, %rdi
callq 0x10c82
movl %eax, %ebp
movq %rbx, %rdi
callq 0x1a960
movl $0x270, %edx            # imm = 0x270
movq %rbx, %rdi
leaq 0x348(%rsp), %rsi
callq 0x3260
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0x50e0(%r14)
movq 0x30(%rsp), %rax
movq %rax, 0x50f0(%r14)
movl %r13d, 0x4b88(%r14)
movq %rbx, %r13
movslq %ebp, %rbp
movq (%r12,%rbp,8), %rax
cmpb $0x0, 0x2310(%rax)
jne 0x11a26
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x65b48(%rip), %rsi     # 0x77567
movq %r15, %rdx
xorl %eax, %eax
callq *(%rcx)
movslq 0x4b88(%r14), %rax
movq 0x100(%r14,%rax,8), %rbx
cmpq $0x0, 0x310(%rbx,%rbp,8)
je 0x11a5b
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x65af8(%rip), %rsi     # 0x7754a
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq *(%rcx)
movq %r15, 0x310(%rbx,%rbp,8)
movq 0x5218(%r14), %r8
addq $0x4e38, %r14           # imm = 0x4E38
movq %r14, %rdi
movq %r12, %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rcx
movl %ebp, %r9d
callq 0x1a2a2
movq %r13, %rdi
callq 0x1a984
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)