// File: /workspace/github_c_makefile_star2_O1/atomlang[P]atomlang/src/pk_compiler.c
// Function: compilerSearchName
static NameSearchResult compilerSearchName(Compiler* compiler,
  const char* name, uint32_t length) {

  NameSearchResult result;
  result.type = NAME_NOT_DEFINED;

  for (int i = compiler->local_count - 1; i >= 0; i--) {
    Local* local = &compiler->locals[i];
    ASSERT(local->depth != DEPTH_GLOBAL, OOPS);

    // Literal functions are not closures and ignore it's outer function's
    // local variables.
    if (compiler->func->depth >= local->depth) {
      continue;
    }

    if (length == local->length) {
      if (strncmp(local->name, name, length) == 0) {
        result.type = NAME_LOCAL_VAR;
        result.index = i;
        return result;
      }
    }
  }

  int index; // For storing the search result below.

  // Search through globals.
  index = scriptGetGlobals(compiler->script, name, length);
  if (index != -1) {
    result.type = NAME_GLOBAL_VAR;
    result.index = index;
    return result;
  }

  // Search through classes.
  index = scriptGetClass(compiler->script, name, length);
  if (index != -1) {
    result.type = NAME_CLASS;
    result.index = index;
    return result;
  }

  // Search through functions.
  index = scriptGetFunc(compiler->script, name, length);
  if (index != -1) {
    result.type = NAME_FUNCTION;
    result.index = index;
    return result;
  }

  // Search through builtin functions.
  index = findBuiltinFunction(compiler->vm, name, length);
  if (index != -1) {
    result.type = NAME_BUILTIN;
    result.index = index;
    return result;
  }

  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebx
movq %rdi, %r15
movslq 0x18a8(%rdi), %r13
testq %r13, %r13
setle %bpl
jle 0x1178c
movl %ebx, %eax
movq %rax, 0x18(%rsp)
leaq (,%r13,2), %rax
addq %r13, %rax
leaq 0x9c(%r15,%rax,8), %r14
movl $0x0, 0xc(%rsp)
movq %rax, 0x10(%rsp)
movq %r15, 0x20(%rsp)
movl (%r14), %eax
cmpl $-0x1, %eax
je 0x11841
movq 0x18c0(%r15), %rcx
movl $0x4, %r12d
cmpl %eax, (%rcx)
jge 0x1176f
xorl %r12d, %r12d
cmpl %ebx, -0x4(%r14)
jne 0x1176f
movq -0xc(%r14), %rdi
movl %ebx, %r15d
movq %rsi, %rbx
movq 0x18(%rsp), %rdx
callq 0x2090
movq %rbx, %rsi
movl %r15d, %ebx
movq 0x20(%rsp), %r15
testl %eax, %eax
jne 0x1176f
leaq -0x1(%r13), %rax
movq %rax, 0x10(%rsp)
movl $0x1, %r12d
movl $0x1, 0xc(%rsp)
testb $0x3, %r12b
jne 0x11799
decq %r13
leaq 0x1(%r13), %rax
addq $-0x18, %r14
cmpq $0x2, %rax
setl %bpl
jge 0x1170f
jmp 0x11799
movl $0x0, 0xc(%rsp)
movq %rax, 0x10(%rsp)
testb $0x1, %bpl
je 0x117bc
movq 0x18b0(%r15), %rdi
movq %rsi, %r12
movl %ebx, %edx
callq 0xa5d9
cmpl $-0x1, %eax
je 0x117c7
movl $0x2, %edx
jmp 0x11804
movl 0xc(%rsp), %edx
movq 0x10(%rsp), %r14
jmp 0x11807
movq 0x18b0(%r15), %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0xa4a4
cmpl $-0x1, %eax
movq 0x10(%rsp), %r14
je 0x117e9
movl $0x4, %edx
jmp 0x11804
movq 0x18b0(%r15), %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0xa54c
cmpl $-0x1, %eax
je 0x1181f
movl $0x3, %edx
movl %eax, %r14d
shlq $0x20, %r14
movl %edx, %eax
orq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x13bf5
cmpl $-0x1, %eax
movl $0x5, %ecx
movl 0xc(%rsp), %edx
cmovnel %ecx, %edx
cmovnel %eax, %r14d
jmp 0x11807
callq 0x4ce9