// File: /workspace/github_c_makefile_star2_O3/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 0x17301
movq 0x8(%rsp), %rax
leaq 0x4e38(%rax), %r15
leaq 0x4220(%rax), %r12
leaq 0x4b98(%rax), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x100(%rax), %rbp
leaq 0x4e70(%rax), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x4e18(%rax), %r13
movl 0x110(%r14), %eax
movq 0x8(%rsp), %rcx
cmpl 0x4b88(%rcx), %eax
jne 0x172f1
cmpl $0x0, 0x11c(%r14)
js 0x172e6
leaq 0x4(%r14), %rbx
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x85da
testq %rax, %rax
je 0x171b1
movq %rax, %rcx
movq %rbx, 0x10(%rsp)
cmpb $0x1, 0x110(%rax)
jne 0x17151
movq %rcx, %rbx
movq 0x8(%rsp), %rcx
movq 0x5118(%rcx), %rdi
leaq 0x6054f(%rip), %rsi     # 0x77690
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq *0x50f8(%rcx)
movq %rbx, %rcx
movq 0x8(%rsp), %rax
cmpb $0x1, 0x4bb0(%rax)
jne 0x17176
cmpb $0x1, 0x111(%rcx)
jne 0x1719c
movq 0x8(%rsp), %rax
cmpb $0x0, 0x4bb1(%rax)
jne 0x1719c
movq %rcx, %rbx
movq 0x8(%rsp), %rcx
movq 0x5118(%rcx), %rdi
leaq 0x60524(%rip), %rsi     # 0x776b0
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 0x171cd
movq 0x8(%rsp), %rcx
movslq 0x4b88(%rcx), %rax
movq 0x100(%rcx,%rax,8), %rdi
movq %rbx, %rsi
callq 0x8367
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x171f5
movq 0x8(%rsp), %rcx
movq 0x5118(%rcx), %rdi
leaq 0x604e7(%rip), %rsi     # 0x776d1
movq %rbx, %rdx
xorl %eax, %eax
callq *0x50f8(%rcx)
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x850c
movl 0x11c(%r14), %esi
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x14570
movq %rbx, %rdi
callq 0x144cf
movq 0x108(%r14), %r8
cmpl $0x0, 0x28(%r8)
jle 0x17258
addq $0x28, %r8
xorl %ebx, %ebx
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movl %ebx, %r9d
callq 0x15468
incl %ebx
movq 0x108(%r14), %rax
leaq 0x28(%rax), %r8
cmpl 0x28(%rax), %ebx
jl 0x17232
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x13c6e
movq 0x8(%rsp), %rdi
movl 0x4b84(%rdi), %esi
callq 0x16e8c
movq %r12, %rdi
callq 0x85bf
movq %r15, %rdi
movl %eax, %esi
callq 0x1559f
movq %r12, %rdi
callq 0x85bf
movq %r15, %rdi
movl %eax, %esi
callq 0x14a1a
movq 0x108(%r14), %rsi
addq $0x28, %rsi
movq %r13, %rdi
callq 0x1acd0
movq %rax, 0x10(%rsp)
movq %r13, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x1ad8b
movq %rbx, %rdi
movq %rax, %rsi
callq 0x14510
movq 0x10(%rsp), %rax
movl 0x10(%rax), %esi
movq %rbx, %rdi
callq 0x13ebd
movq %r12, %rdi
callq 0x8584
movq %r15, %rdi
movq %r14, %rsi
callq 0x1554b
movq 0x140(%r14), %r14
testq %r14, %r14
jne 0x170da
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq