// File: /workspace/github_c_makefile_star2_O2/wuxb45[P]SLB/lib1.c
// Function: chainmap_set
bool
chainmap_set(struct chainmap * const map, const struct kv * const kv0)
{
  const struct kv * const kv = kv_alloc_dup(kv0, &(map->mm));
  if (kv == NULL) return false;

  const u64 hash = kv->hash;
  const u64 lidx = kvmap_hlocks_index(hash);
  const u64 pkey = kvmap_pkey(hash);
  spinlock_lock(&(map->hlocks[lidx]));
  const u64 cid = (hash >> KVMAP_BASE_LEVEL) & ((UINT64_C(1) << map->power) - UINT64_C(1));
  struct chainnode * iter = map->table[cid];
  while (iter) {
    if (iter->e.pkey == pkey) {
      const struct kv * const curr = __u64_to_ptr(iter->e.ptr);
      if (kv_keymatch(kv, curr)) { // update
        kvmap_put_entry(&(map->mm), &(iter->e), kv);
        spinlock_unlock(&(map->hlocks[lidx]));
        return true;
      }
    }
    iter = iter->next;
  }
  // insert
  struct chainnode * const node = (typeof(node))malloc(sizeof(*node));
  if (node == NULL) {
    if (map->mm.rf) map->mm.rf((struct kv *)kv, map->mm.rp);
    spinlock_unlock(&(map->hlocks[lidx]));
    return false;
  }
  memset(node, 0, sizeof(*node));
  //chainmap_put_entry(map, &(node->e), kv);
  kvmap_put_entry(&(map->mm), &(node->e), kv);
  const u64 nr_kv = atomic_fetch_add(&(map->nr_kv), 1);
  node->next = map->table[cid];
  map->table[cid] = node;
  spinlock_unlock(&(map->hlocks[lidx]));
  if (nr_kv > map->trigger) {
    chainmap_expand(map);
  }
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq %rsi, %rdi
movq %rbx, %rsi
callq 0xba29
testq %rax, %rax
je 0xdc74
movq %rax, %r14
movq (%rax), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorq %rax, %rcx
movq %rax, %r15
shrq $0x6, %r15
movl %r15d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
movq %rcx, %r13
shrq $0x10, %r13
xorq %rcx, %r13
shll $0x6, %eax
leaq (%rbx,%rax), %rdi
addq $0x138, %rdi            # imm = 0x138
movq %rdi, -0x30(%rbp)
callq 0x6afb
movb 0xd0(%rbx), %al
bzhiq %rax, %r15, %r15
leaq (,%r15,8), %r12
addq 0xe8(%rbx), %r12
movq (%r12), %r12
testq %r12, %r12
je 0xdbc6
movq (%r12), %rsi
movl %r13d, %eax
xorw %si, %ax
jne 0xdbc0
shrq $0x10, %rsi
movq %r14, %rdi
callq 0xab0c
testb %al, %al
jne 0xdc79
addq $0x8, %r12
jmp 0xdb97
pushq $0x1
popq %r12
pushq $0x10
popq %rsi
movq %r12, %rdi
callq 0x62d0
testq %rax, %rax
je 0xdc59
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xccb2
lock
xaddq %r12, 0x88(%rbx)
movq 0xe8(%rbx), %rax
movq (%rax,%r15,8), %rcx
movq %rcx, 0x8(%r13)
movq %r13, (%rax,%r15,8)
movq -0x30(%rbp), %rdi
callq 0x6320
movb $0x1, %r14b
cmpq 0xd8(%rbx), %r12
jbe 0xdc93
leaq 0xf8(%rbx), %rdi
callq 0x6b6e
testb %al, %al
je 0xdc93
leaq 0xe0(%rbx), %rdi
leaq 0x3052(%rip), %rdx      # 0x10c8c
xorl %esi, %esi
movq %rbx, %rcx
callq 0x6430
movq 0xe0(%rbx), %rdi
leaq 0x4b85(%rip), %rsi      # 0x127d7
callq 0x61d0
jmp 0xdc93
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0xdc6b
movq 0x18(%rbx), %rsi
movq %r14, %rdi
callq *%rax
movq -0x30(%rbp), %rdi
callq 0x6320
xorl %r14d, %r14d
jmp 0xdc93
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xccb2
movq -0x30(%rbp), %rdi
callq 0x6320
movb $0x1, %r14b
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq