// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_var.c
// Function: mapFindEntry
static bool _mapFindEntry(Map* self, Var key, MapEntry** result) {

  // An empty map won't contain the key.
  if (self->capacity == 0) return false;

  // The [start_index] is where the entry supposed to be if there wasn't any
  // collision occurred. It'll be the start index for the linear probing.
  uint32_t start_index = varHashValue(key) % self->capacity;
  uint32_t index = start_index;

  // Keep track of the first tombstone after the [start_index] if we don't find
  // the key anywhere. The tombstone would be the entry at where we will have
  // to insert the key/value pair.
  MapEntry* tombstone = NULL;

  do {
    MapEntry* entry = &self->entries[index];

    if (IS_UNDEF(entry->key)) {
      ASSERT(IS_BOOL(entry->value), OOPS);

      if (IS_TRUE(entry->value)) {

        // We've found a tombstone, if we haven't found one [tombstone] should
        // be updated. We still need to keep search for if the key exists.
        if (tombstone == NULL) tombstone = entry;

      } else {
        // We've found a new empty slot and the key isn't found. If we've
        // found a tombstone along the sequence we could use that entry
        // otherwise the entry at the current index.

        *result = (tombstone != NULL) ? tombstone : entry;
        return false;
      }

    } else if (isValuesEqual(entry->key, key)) {
      // We've found the key.
      *result = entry;
      return true;
    }

    index = (index + 1) % self->capacity;

  } while (index != start_index);

  // If we reach here means the map is filled with tombstone. Set the first
  // tombstone as result for the next insertion and return false.
  ASSERT(tombstone != NULL, OOPS);
  *result = tombstone;
  return false;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq 0x38(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x5fc8
movb $0x0, 0x47(%rsp)
jmp 0x6193
movq 0x30(%rsp), %rdi
callq 0x5d90
movq 0x38(%rsp), %rcx
xorl %edx, %edx
divl 0x10(%rcx)
movl %edx, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x20(%rsp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movabsq $0x7ffd000000000000, %rcx # imm = 0x7FFD000000000000
cmpq %rcx, (%rax)
jne 0x60ea
jmp 0x6024
movq 0x10(%rsp), %rax
movabsq $0x7ffd000000000003, %rcx # imm = 0x7FFD000000000003
cmpq %rcx, 0x8(%rax)
je 0x6088
movq 0x10(%rsp), %rax
movabsq $0x7ffd000000000002, %rcx # imm = 0x7FFD000000000002
cmpq %rcx, 0x8(%rax)
je 0x6088
movq 0x2cfa3(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x240f1(%rip), %rsi     # 0x2a150
leaq 0x241fe(%rip), %rdx     # 0x2a264
leaq 0x24363(%rip), %rcx     # 0x2a3d0
leaq 0x24128(%rip), %r8      # 0x2a19c
movl $0x359, %r9d            # imm = 0x359
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x608a
movq 0x10(%rsp), %rax
movabsq $0x7ffd000000000003, %rcx # imm = 0x7FFD000000000003
cmpq %rcx, 0x8(%rax)
jne 0x60b3
cmpq $0x0, 0x18(%rsp)
jne 0x60b1
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x60e8
cmpq $0x0, 0x18(%rsp)
je 0x60c7
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x60d1
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, (%rax)
movb $0x0, 0x47(%rsp)
jmp 0x6193
jmp 0x6118
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x30(%rsp), %rsi
callq 0x76d0
testb $0x1, %al
jne 0x6102
jmp 0x6116
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, (%rax)
movb $0x1, 0x47(%rsp)
jmp 0x6193
jmp 0x6118
movl 0x20(%rsp), %eax
addl $0x1, %eax
movq 0x38(%rsp), %rcx
xorl %edx, %edx
divl 0x10(%rcx)
movl %edx, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x24(%rsp), %eax
jne 0x5ff1
jmp 0x613d
cmpq $0x0, 0x18(%rsp)
jne 0x617f
movq 0x2ceac(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x23ffa(%rip), %rsi     # 0x2a150
leaq 0x24107(%rip), %rdx     # 0x2a264
leaq 0x2426c(%rip), %rcx     # 0x2a3d0
leaq 0x24031(%rip), %r8      # 0x2a19c
movl $0x376, %r9d            # imm = 0x376
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x6181
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, (%rax)
movb $0x0, 0x47(%rsp)
movb 0x47(%rsp), %al
andb $0x1, %al
addq $0x48, %rsp
retq
nop