// File: /workspace/github_c_makefile_star2_O2/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 0x8928
leaq 0xf0(%r14), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x4144
movq %rax, %r15
leaq 0x4bd8(%r14), %rsi
movq %rax, %rdi
callq 0x30a0
movq %rbx, %rdi
callq 0x10f6e
pushq $0x31
popq %rsi
movq %rbx, %rdi
callq 0x118d3
jmp 0x892b
xorl %r15d, %r15d
pushq $0x49
popq %rsi
movq %rbx, %rdi
callq 0x1187f
leaq 0x40(%rsp), %rbp
movl $0x0, 0xc(%rsp)
movl $0x100, %r13d           # imm = 0x100
movl $0x100, %edx            # imm = 0x100
movq %rbp, %rdi
xorl %esi, %esi
callq 0x3190
leaq 0x100(%r14), %r12
movq 0x4bd8(%r14), %rsi
leaq 0x4108(%r14), %rdx
movq %r12, %rdi
movq %rbp, %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0x4999
testb %al, %al
je 0x89b4
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x47cb
testq %rax, %rax
je 0x89ac
movq 0x308(%rax), %rcx
movb 0x310(%rax), %al
movl %eax, 0xc(%rsp)
testq %rcx, %rcx
jne 0x89e2
jmp 0x89b4
movl $0x0, 0xc(%rsp)
movq 0x4bd8(%r14), %rsi
movslq 0x4b88(%r14), %rax
addq 0x100(%r14,%rax,8), %r13
leaq 0x40(%rsp), %rcx
movq %r12, %rdi
movq %r13, %rdx
movl $0x100, %r8d            # imm = 0x100
callq 0x4ab1
xorl %ecx, %ecx
movq %rcx, 0x10(%rsp)
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 0x43cc
testq %r15, %r15
jne 0x8a57
leaq 0xf0(%r14), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x4144
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 0x452c
testl %eax, %eax
js 0x8a8b
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x61a6a(%rip), %rsi     # 0x6a4ee
movq %r15, %rdx
xorl %eax, %eax
callq *(%rcx)
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x44b5
movl %eax, %ebp
testl %eax, %eax
jns 0x8b5a
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 0xc(%rsp), %r9d
leaq 0x40(%rsp), %rcx
movq %rbx, %rdi
movq %rbp, %rdx
movq 0x10(%rsp), %r8
callq 0x10d0c
movq %rbx, %rdi
callq 0x10f6e
movq %r14, %rdi
callq 0x7e2f
movl %eax, %ebp
movq %rbx, %rdi
callq 0x10f4a
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 0x8b86
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x6198c(%rip), %rsi     # 0x6a50b
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 0x8bbb
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x6193c(%rip), %rsi     # 0x6a4ee
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 0x1096c
movq %r13, %rdi
callq 0x10f6e
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)