// File: /workspace/github_c_makefile_star2_O3/vtereshkov[P]umka-lang/src/umka_ident.c
// Function: identFindEx
static const Ident *identFindEx(const Idents *idents, const Modules *modules, const Blocks *blocks, int module, const char *name, const Type *rcvType, bool markAsUsed, bool isModule)
{
    const unsigned int nameHash = hash(name);

    for (int i = blocks->top; i >= 0; i--)
    {
        for (const Ident *ident = idents->first; ident; ident = ident->next)
            if (ident->hash == nameHash && strcmp(ident->name, name) == 0 && ident->block == blocks->item[i].block && (ident->kind == IDENT_MODULE) == isModule)
            {
                // What we found has correct name and block scope, check module scope
                const bool identModuleValid = (ident->module == 0 && blocks->module == module) ||                                                // Universe module
                                              (ident->module == module && (blocks->module == module ||                                           // Current module
                                              (ident->exported && (rcvType || modules->module[blocks->module]->importAlias[ident->module]))));   // Imported module

                if (identModuleValid)
                {
                    // Method names need not be unique in the given scope - check the receiver type to see if we found the right name
                    const bool methodFound = ident->type->kind == TYPE_FN && ident->type->sig.isMethod;

                    const bool found = (!rcvType && !methodFound) || (rcvType && methodFound && typeCompatibleRcv(ident->type->sig.param[0]->type, rcvType));
                    if (found)
                    {
                        if (markAsUsed)
                            identSetUsed(ident);
                        return ident;
                    }
                }
            }
    }

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x8(%rsp)
movq %r8, %r14
movl %ecx, 0x4(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, 0x20(%rsp)
movb (%r8), %al
testb %al, %al
je 0x14ad5
leaq 0x1(%r14), %rcx
movl $0x1505, %r12d          # imm = 0x1505
movl %r12d, %edx
shll $0x5, %edx
addl %r12d, %edx
movsbl %al, %r12d
addl %edx, %r12d
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x14aba
jmp 0x14adb
movl $0x1505, %r12d          # imm = 0x1505
movq 0x10(%rsp), %rax
movslq 0x964(%rax), %rbx
testq %rbx, %rbx
js 0x14c58
movq (%rdi), %r15
cmpq $0x0, 0x8(%rsp)
sete 0x3(%rsp)
movslq 0x4(%rsp), %rax
movq %rax, 0x18(%rsp)
testq %r15, %r15
je 0x14c48
leaq (%rbx,%rbx,2), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rbp
movq %r15, %r13
cmpl %r12d, 0x104(%r13)
jne 0x14c38
leaq 0x4(%r13), %rdi
movq %r14, %rsi
callq 0x3210
testl %eax, %eax
jne 0x14c38
movl 0x114(%r13), %eax
cmpl (%rbp), %eax
jne 0x14c38
cmpl $0x4, (%r13)
setne %al
cmpb 0x68(%rsp), %al
je 0x14c38
movl 0x110(%r13), %eax
testl %eax, %eax
jne 0x14b80
movl 0x4(%rsp), %ecx
movq 0x10(%rsp), %rdx
cmpl %ecx, 0x968(%rdx)
je 0x14bcb
cmpl 0x4(%rsp), %eax
jne 0x14c38
movq 0x10(%rsp), %rax
movslq 0x968(%rax), %rax
cmpl 0x4(%rsp), %eax
je 0x14bcb
cmpb $0x1, 0x118(%r13)
jne 0x14c38
cmpq $0x0, 0x8(%rsp)
jne 0x14bcb
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq 0x18(%rsp), %rcx
cmpq $0x0, 0x310(%rax,%rcx,8)
je 0x14c38
movq 0x108(%r13), %rcx
cmpl $0x1a, (%rcx)
movb 0x3(%rsp), %al
jne 0x14c29
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
sete %al
movb 0x30(%rcx), %dl
xorb $0x1, %dl
andb %dl, %al
testq %rsi, %rsi
je 0x14c29
testb %dl, %dl
jne 0x14c29
movq 0x38(%rcx), %rax
movq 0x108(%rax), %rax
cmpl $0x10, (%rax)
jne 0x14c38
movq 0x8(%rsp), %rcx
cmpl $0x10, (%rcx)
jne 0x14c38
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
cmpq 0x18(%rcx), %rax
sete %al
movl %eax, %ecx
andb 0x60(%rsp), %cl
cmpb $0x1, %cl
je 0x14c6d
testb $0x1, %al
jne 0x14c5b
movq 0x140(%r13), %r13
testq %r13, %r13
jne 0x14b21
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0x14b08
xorl %r13d, %r13d
movq %r13, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x11a(%r13)
jmp 0x14c5b