// File: /workspace/github_c_makefile_star2_O1/vtereshkov[P]umka-lang/src/umka_stmt.c
// Function: doResolveExtern
void doResolveExtern(Compiler *comp)
{
    for (const Ident *ident = comp->idents.first; ident; ident = ident->next)
        if (ident->module == comp->blocks.module)
        {
            if (ident->prototypeOffset >= 0)
            {
                External *external = externalFind(&comp->externals, ident->name);

                // Try to find the function in the external function list or in an external implementation library
                void *fn = NULL;
                if (external)
                {
                    if (external->resolved)
                        comp->error.handler(comp->error.context, "External %s is already resolved", ident->name);

                    if (!comp->lex.hasSourceString || (external->resolveInTrusted && !comp->lex.trusted))
                        comp->error.handler(comp->error.context, "Cannot resolve %s in this module", ident->name);

                    fn = external->entry;
                    external->resolved = true;
                }
                else
                    fn = moduleGetImplLibFunc(comp->modules.module[comp->blocks.module], ident->name);

                if (!fn)
                    comp->error.handler(comp->error.context, "Unresolved prototype of %s", ident->name);

                blocksEnterFn(&comp->blocks, ident, false);
                genEntryPoint(&comp->gen, ident->prototypeOffset);
                genEnterFrameStub(&comp->gen);

                // All parameters must be declared since they may require garbage collection
                for (int i = 0; i < ident->type->sig.numParams; i++)
                    identAllocParam(&comp->idents, &comp->types, &comp->modules, &comp->blocks, &ident->type->sig, i);

                genCallExtern(&comp->gen, fn);

                doGarbageCollection(comp);
                identWarnIfUnusedAll(&comp->idents, blocksCurrent(&comp->blocks));
                identFree(&comp->idents, blocksCurrent(&comp->blocks));

                const ParamLayout *paramLayout = typeMakeParamLayout(&comp->types, &ident->type->sig);

                genLeaveFrameFixup(&comp->gen, typeMakeParamAndLocalVarLayout(&comp->types, paramLayout, 0));
                genReturn(&comp->gen, paramLayout->numParamSlots);

                blocksLeave(&comp->blocks);
            }

            identWarnIfUnused(&comp->idents, ident);
        }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, 0x8(%rsp)
movq 0x4e38(%rdi), %r14
testq %r14, %r14
je 0x1c376
movq 0x8(%rsp), %rax
leaq 0x4e38(%rax), %r15
leaq 0x4220(%rax), %r12
leaq 0x4b98(%rax), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x4e70(%rax), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x4e18(%rax), %r13
leaq 0x100(%rax), %rbp
movl 0x110(%r14), %eax
movq 0x8(%rsp), %rcx
cmpl 0x4b88(%rcx), %eax
jne 0x1c366
cmpl $0x0, 0x11c(%r14)
js 0x1c35b
leaq 0x4(%r14), %rbx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0xd83f
testq %rax, %rax
je 0x1c226
movq %rax, %rcx
movq %rbx, 0x10(%rsp)
cmpb $0x1, 0x110(%rax)
jne 0x1c1c6
movq %rcx, %rbx
movq 0x8(%rsp), %rcx
movq 0x5118(%rcx), %rdi
leaq 0x5c4b2(%rip), %rsi     # 0x78668
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq *0x50f8(%rcx)
movq %rbx, %rcx
movq 0x8(%rsp), %rax
cmpb $0x1, 0x4bb0(%rax)
jne 0x1c1eb
cmpb $0x1, 0x111(%rcx)
jne 0x1c211
movq 0x8(%rsp), %rax
cmpb $0x0, 0x4bb1(%rax)
jne 0x1c211
movq %rcx, %rbx
movq 0x8(%rsp), %rcx
movq 0x5118(%rcx), %rdi
leaq 0x5c487(%rip), %rsi     # 0x78688
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq *0x50f8(%rcx)
movq %rbx, %rcx
movq 0x108(%rcx), %rax
movb $0x1, 0x110(%rcx)
movq 0x10(%rsp), %rbx
jmp 0x1c242
movq 0x8(%rsp), %rcx
movslq 0x4b88(%rcx), %rax
movq 0x100(%rcx,%rax,8), %rdi
movq %rbx, %rsi
callq 0xd5c8
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x1c26a
movq 0x8(%rsp), %rcx
movq 0x5118(%rcx), %rdi
leaq 0x5c44a(%rip), %rsi     # 0x786a9
movq %rbx, %rdx
xorl %eax, %eax
callq *0x50f8(%rcx)
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xd77d
movl 0x11c(%r14), %esi
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x195ac
movq %rbx, %rdi
callq 0x1950b
movq 0x108(%r14), %r8
cmpl $0x0, 0x28(%r8)
jle 0x1c2cd
addq $0x28, %r8
xorl %ebx, %ebx
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movl %ebx, %r9d
callq 0x1a4d7
incl %ebx
movq 0x108(%r14), %rax
leaq 0x28(%rax), %r8
cmpl 0x28(%rax), %ebx
jl 0x1c2a7
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x18c8d
movq 0x8(%rsp), %rdi
movl 0x4b84(%rdi), %esi
callq 0x1bf00
movq %r12, %rdi
callq 0xd824
movq %r15, %rdi
movl %eax, %esi
callq 0x1a60e
movq %r12, %rdi
callq 0xd824
movq %r15, %rdi
movl %eax, %esi
callq 0x19a46
movq 0x108(%r14), %rsi
addq $0x28, %rsi
movq %r13, %rdi
callq 0x1fd62
movq %rax, 0x10(%rsp)
movq %r13, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x1fe1d
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1954c
movq 0x10(%rsp), %rax
movl 0x10(%rax), %esi
movq %rbx, %rdi
callq 0x18f05
movq %r12, %rdi
callq 0xd7f2
movq %r15, %rdi
movq %r14, %rsi
callq 0x1a5ba
movq 0x140(%r14), %r14
testq %r14, %r14
jne 0x1c14f
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq