// File: /workspace/github_c_makefile_star2_O3/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 0xc670
leaq 0xf0(%r14), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x7a70
movq %rax, %r15
leaq 0x4bd8(%r14), %rsi
movq %rax, %rdi
callq 0x30a0
movq %rbx, %rdi
callq 0x15914
movq %rbx, %rdi
movl $0x31, %esi
callq 0x1637d
jmp 0xc673
xorl %r15d, %r15d
movq %rbx, %rdi
movl $0x49, %esi
callq 0x16329
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 0x836a
testb %al, %al
je 0xc6fe
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x8154
testq %rax, %rax
je 0xc6f6
movq 0x308(%rax), %rcx
movb 0x310(%rax), %al
movl %eax, 0xc(%rsp)
testq %rcx, %rcx
jne 0xc72c
jmp 0xc6fe
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 0x849a
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 0x7d04
testq %r15, %r15
jne 0xc7a1
leaq 0xf0(%r14), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x7a70
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 0x7e80
testl %eax, %eax
js 0xc7d5
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x69d7c(%rip), %rsi     # 0x7654a
movq %r15, %rdx
xorl %eax, %eax
callq *(%rcx)
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x7df3
movl %eax, %ebp
testl %eax, %eax
jns 0xc8a4
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 0x15684
movq %rbx, %rdi
callq 0x15914
movq %r14, %rdi
callq 0xbad0
movl %eax, %ebp
movq %rbx, %rdi
callq 0x158f0
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 0xc8d0
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x69c9e(%rip), %rsi     # 0x76567
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 0xc905
movq 0x4218(%r14), %rcx
movq 0x20(%rcx), %rdi
leaq 0x69c4e(%rip), %rsi     # 0x7654a
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 0x15238
movq %r13, %rdi
callq 0x15914
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop