// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x4e38(%rax), %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x1d9b9
movq -0x10(%rbp), %rax
movl 0x110(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x4b88(%rcx), %eax
jne 0x1d9a3
movq -0x10(%rbp), %rax
cmpl $0x0, 0x11c(%rax)
jl 0x1d98f
movq -0x8(%rbp), %rdi
addq $0x4b98, %rdi           # imm = 0x4B98
movq -0x10(%rbp), %rsi
addq $0x4, %rsi
callq 0x6080
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x1d7a2
movq -0x18(%rbp), %rax
testb $0x1, 0x110(%rax)
je 0x1d736
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x4, %rdx
leaq 0x1bc296(%rip), %rsi    # 0x1d99c8
movb $0x0, %al
callq *%rcx
movq -0x8(%rbp), %rax
testb $0x1, 0x4bb0(%rax)
je 0x1d75d
movq -0x18(%rbp), %rax
testb $0x1, 0x111(%rax)
je 0x1d786
movq -0x8(%rbp), %rax
testb $0x1, 0x4bb1(%rax)
jne 0x1d786
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x4, %rdx
leaq 0x1bc266(%rip), %rsi    # 0x1d99e8
movb $0x0, %al
callq *%rcx
movq -0x18(%rbp), %rax
movq 0x108(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movb $0x1, 0x110(%rax)
jmp 0x1d7ca
movq -0x8(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq 0x4b88(%rcx), %rcx
movq 0x100(%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rsi
addq $0x4, %rsi
callq 0x5b00
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1d7fa
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x4, %rdx
leaq 0x1bc213(%rip), %rsi    # 0x1d9a09
movb $0x0, %al
callq *%rcx
movq -0x8(%rbp), %rdi
addq $0x4220, %rdi           # imm = 0x4220
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x5e90
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x10(%rbp), %rax
movl 0x11c(%rax), %esi
callq 0x18b90
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
callq 0x18b00
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x108(%rcx), %rcx
cmpl 0x28(%rcx), %eax
jge 0x1d8a3
movq -0x8(%rbp), %rdi
addq $0x4e38, %rdi           # imm = 0x4E38
movq -0x8(%rbp), %rsi
addq $0x4e18, %rsi           # imm = 0x4E18
movq -0x8(%rbp), %rdx
addq $0x100, %rdx            # imm = 0x100
movq -0x8(%rbp), %rcx
addq $0x4220, %rcx           # imm = 0x4220
movq -0x10(%rbp), %rax
movq 0x108(%rax), %r8
addq $0x28, %r8
movl -0x24(%rbp), %r9d
callq 0x1a610
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1d841
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x20(%rbp), %rsi
callq 0x17ff0
movq -0x8(%rbp), %rdi
callq 0x1d3a0
movq -0x8(%rbp), %rax
addq $0x4e38, %rax           # imm = 0x4E38
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4220, %rdi           # imm = 0x4220
callq 0x6020
movq -0x48(%rbp), %rdi
movl %eax, %esi
callq 0x1a7e0
movq -0x8(%rbp), %rax
addq $0x4e38, %rax           # imm = 0x4E38
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4220, %rdi           # imm = 0x4220
callq 0x6020
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x194d0
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rax
movq 0x108(%rax), %rsi
addq $0x28, %rsi
callq 0x239a0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
addq $0x4e70, %rax           # imm = 0x4E70
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0x23af0
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x18b30
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %esi
callq 0x18350
movq -0x8(%rbp), %rdi
addq $0x4220, %rdi           # imm = 0x4220
callq 0x5fa0
movq -0x8(%rbp), %rdi
addq $0x4e38, %rdi           # imm = 0x4E38
movq -0x10(%rbp), %rsi
callq 0x1a750
jmp 0x1d9a5
movq -0x10(%rbp), %rax
movq 0x140(%rax), %rax
movq %rax, -0x10(%rbp)
jmp 0x1d69b
addq $0x50, %rsp
popq %rbp
retq
nop