// File: /workspace/github_c_makefile_star2_O3/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
subq $0x28, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq 0x8(%rdi), %rsi
movl 0x8(%r12), %eax
movl 0xc(%r12), %ecx
leaq (%rax,%rcx), %r13
addq $0x10, %r13
movq %r13, %rdi
callq *(%rbx)
testq %rax, %rax
je 0xfb3d
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x6340
movq (%r15), %r12
movq %r12, %r14
shrq $0x6, %r14
movl %r14d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
shll $0x6, %eax
leaq (%rbx,%rax), %rdi
addq $0x138, %rdi            # imm = 0x138
movq %rdi, -0x30(%rbp)
callq 0x6050
cmpl $0x23, %eax
je 0xfadd
movb 0xd0(%rbx), %al
bzhiq %rax, %r14, %rcx
movq %rbx, -0x38(%rbp)
movq 0xe8(%rbx), %rax
movq %rcx, -0x48(%rbp)
movq (%rax,%rcx,8), %r14
testq %r14, %r14
je 0xfa3b
movq %r12, %rax
shrq $0x20, %rax
xorq %r12, %rax
movq %rax, %rbx
shrq $0x10, %rbx
xorq %rax, %rbx
leaq 0x10(%r15), %rax
movq %rax, -0x40(%rbp)
movq (%r14), %r13
movl %ebx, %eax
xorw %r13w, %ax
jne 0xfa32
cmpq $0x10000, %r13          # imm = 0x10000
jb 0xfa32
movq %r13, %r12
shrq $0x10, %r12
movq (%r15), %rax
cmpq (%r12), %rax
jne 0xfa32
movl 0x8(%r15), %edx
cmpl 0x8(%r12), %edx
jne 0xfa32
leaq 0x10(%r12), %rsi
movq -0x40(%rbp), %rdi
callq 0x63d0
testl %eax, %eax
je 0xfb54
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0xf9ed
movl $0x1, %ebx
movl $0x1, %edi
movl $0x10, %esi
callq 0x62d0
testq %rax, %rax
je 0xfae4
movq %r15, %rcx
shlq $0x10, %rcx
movq (%r15), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
xorl %edx, %esi
movl %esi, %edx
shrl $0x10, %edx
xorl %esi, %edx
movzwl %dx, %edx
orq %rcx, %rdx
movq %rdx, (%rax)
movq -0x38(%rbp), %r12
lock
xaddq %rbx, 0x88(%r12)
movq 0xe8(%r12), %rcx
movq -0x48(%rbp), %rsi
movq (%rcx,%rsi,8), %rdx
movq %rdx, 0x8(%rax)
movq %rax, (%rcx,%rsi,8)
movq -0x30(%rbp), %rdi
callq 0x6320
movb $0x1, %r14b
cmpq 0xd8(%r12), %rbx
jbe 0xfbc5
leaq 0xf8(%r12), %r15
movq %r15, %rdi
callq 0x6280
cmpl $0xb, %eax
je 0xfac3
testl %eax, %eax
je 0xfb0b
cmpl $0x10, %eax
je 0xfbc5
xorl %edi, %edi
callq 0x6530
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rax
testq %rax, %rax
je 0xfafa
movq 0x18(%rcx), %rsi
movq %r15, %rdi
callq *%rax
movq -0x30(%rbp), %rdi
callq 0x6320
xorl %r14d, %r14d
jmp 0xfbc5
leaq 0xe0(%r12), %rdi
leaq 0x3dc0(%rip), %rdx      # 0x138da
xorl %esi, %esi
movq %r12, %rcx
callq 0x6430
movq 0xe0(%r12), %rdi
leaq 0x4ca4(%rip), %rsi      # 0x147d7
callq 0x61d0
jmp 0xfbc5
callq 0x66ef
xorl %r14d, %r14d
xorl %edi, %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0x6340
jmp 0xfbc5
movq -0x38(%rbp), %rcx
movq 0x40(%rcx), %rax
testq %rax, %rax
je 0xfb73
movq 0x48(%rcx), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq *%rax
movq -0x30(%rbp), %rbx
jmp 0xfb89
movq 0x10(%rcx), %rax
testq %rax, %rax
movq -0x30(%rbp), %rbx
je 0xfb8d
movq 0x18(%rcx), %rsi
movq %r12, %rdi
callq *%rax
movzwl (%r14), %r13d
movq %r15, %rax
shlq $0x10, %rax
movzwl %r13w, %ecx
orq %rax, %rcx
movq %rcx, (%r14)
movq (%r15), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
xorl %ecx, %edx
movl %edx, %ecx
shrl $0x10, %ecx
xorl %edx, %ecx
movzwl %cx, %ecx
orq %rax, %rcx
movq %rcx, (%r14)
movq %rbx, %rdi
callq 0x6320
movb $0x1, %r14b
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq