// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movl %edx, 0x24(%rsp)
movl $0x0, 0x3c(%rsp)
movq 0x30(%rsp), %rax
movl 0x18a8(%rax), %eax
subl $0x1, %eax
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x20(%rsp)
jl 0x16ed8
movq 0x30(%rsp), %rax
addq $0xa8, %rax
movslq 0x20(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
cmpl $-0x1, 0xc(%rax)
jne 0x16e68
movq 0x1c1c3(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x13311(%rip), %rsi     # 0x2a150
leaq 0x1341e(%rip), %rdx     # 0x2a264
leaq 0x14313(%rip), %rcx     # 0x2b160
leaq 0x14183(%rip), %r8      # 0x2afd7
movl $0x40f, %r9d            # imm = 0x40F
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x16e6a
movq 0x30(%rsp), %rax
movq 0x18c0(%rax), %rax
movl (%rax), %eax
movq 0x18(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jl 0x16e84
jmp 0x16ec8
movl 0x24(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x16ec6
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %eax
movl %eax, %edx
callq 0x2080
cmpl $0x0, %eax
jne 0x16ec4
movl $0x1, 0x3c(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x16fb5
jmp 0x16ec6
jmp 0x16ec8
movl 0x20(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x16dfc
movq 0x30(%rsp), %rax
movq 0x18b0(%rax), %rdi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %edx
callq 0x6a40
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
je 0x16f12
movl $0x2, 0x3c(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x16fb5
movq 0x30(%rsp), %rax
movq 0x18b0(%rax), %rdi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %edx
callq 0x68a0
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
je 0x16f49
movl $0x4, 0x3c(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x16fb5
movq 0x30(%rsp), %rax
movq 0x18b0(%rax), %rdi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %edx
callq 0x69a0
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
je 0x16f80
movl $0x3, 0x3c(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x16fb5
movq 0x30(%rsp), %rax
movq (%rax), %rdi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %edx
callq 0x1c620
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
je 0x16fb3
movl $0x5, 0x3c(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x16fb5
jmp 0x16fb5
movl 0x44(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x3c(%rsp), %rax
movq %rax, (%rsp)
movq (%rsp), %rax
movl 0x8(%rsp), %edx
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)