// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %r9, 0x18(%rsp)
movq %r8, %r14
movq %rdx, 0x10(%rsp)
movb (%r8), %al
testb %al, %al
je 0x19af9
leaq 0x1(%r14), %r8
movl $0x1505, %r12d          # imm = 0x1505
movl %r12d, %edx
shll $0x5, %edx
addl %r12d, %edx
movsbl %al, %r12d
addl %edx, %r12d
movb (%r8), %al
incq %r8
testb %al, %al
jne 0x19add
jmp 0x19aff
movl $0x1505, %r12d          # imm = 0x1505
movq 0x10(%rsp), %rax
movslq 0x964(%rax), %rbx
testq %rbx, %rbx
js 0x19cb3
movq %rsi, 0x28(%rsp)
movl %ecx, 0xc(%rsp)
movq (%rdi), %r13
testq %r13, %r13
sete 0xb(%rsp)
movq %r13, 0x30(%rsp)
testq %r13, %r13
je 0x19c93
leaq (%rbx,%rbx,2), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rbp
movb 0xb(%rsp), %r15b
cmpl %r12d, 0x104(%r13)
jne 0x19c7d
leaq 0x4(%r13), %rdi
movq %r14, %rsi
callq 0x3210
testl %eax, %eax
jne 0x19c7d
movl 0x114(%r13), %eax
cmpl (%rbp), %eax
jne 0x19c7d
cmpl $0x4, (%r13)
setne %al
cmpb 0x78(%rsp), %al
je 0x19c7d
movslq 0x110(%r13), %rcx
testq %rcx, %rcx
jne 0x19ba8
movl 0xc(%rsp), %eax
movq 0x10(%rsp), %rdx
cmpl %eax, 0x968(%rdx)
je 0x19bf4
movb $0x1, %al
movl 0xc(%rsp), %esi
cmpl %esi, %ecx
jne 0x19c79
movq 0x10(%rsp), %rdx
movslq 0x968(%rdx), %rdx
cmpl %esi, %edx
je 0x19bf4
cmpb $0x1, 0x118(%r13)
jne 0x19c79
cmpq $0x0, 0x18(%rsp)
jne 0x19bf4
movq 0x28(%rsp), %rsi
movq (%rsi,%rdx,8), %rdx
cmpq $0x0, 0x310(%rdx,%rcx,8)
je 0x19c79
movq 0x108(%r13), %rcx
movb $0x1, %dl
cmpl $0x1a, (%rcx)
jne 0x19c08
movb 0x30(%rcx), %dl
xorb $0x1, %dl
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
sete %al
andb %dl, %al
testq %rsi, %rsi
je 0x19c4f
testb %dl, %dl
jne 0x19c4f
movq 0x38(%rcx), %rax
movq 0x108(%rax), %rax
cmpl $0x10, (%rax)
jne 0x19c4d
movq 0x18(%rsp), %rcx
cmpl $0x10, (%rcx)
jne 0x19c4d
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq 0x8(%rcx), %rcx
cmpq 0x18(%rcx), %rax
sete %al
jmp 0x19c4f
xorl %eax, %eax
movl %eax, %ecx
andb 0x70(%rsp), %cl
testb %al, %al
movq 0x20(%rsp), %rdx
cmovneq %r13, %rdx
movq %rdx, 0x20(%rsp)
cmpb $0x1, %cl
jne 0x19c77
movb $0x1, 0x11a(%r13)
movq %r13, 0x20(%rsp)
xorb $0x1, %al
testb %al, %al
je 0x19c98
movq 0x140(%r13), %r13
testq %r13, %r13
sete %r15b
jne 0x19b48
jmp 0x19c98
movb 0xb(%rsp), %r15b
testb $0x1, %r15b
je 0x19cb7
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
movq 0x30(%rsp), %r13
jg 0x19b2d
xorl %eax, %eax
jmp 0x19cbc
movq 0x20(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq