// File: /workspace/github_c_makefile_star2_O2/wuxb45[P]SLB/lib1.c
// Function: kvmap2_set
bool
kvmap2_set(struct kvmap2 * const map2, const struct kv * const kv0)
{
  u64 i = UINT64_MAX;
  u64 j = UINT64_MAX;
  u64 k = UINT64_MAX;
  u64 hmax[2];
  u64 hi[2];
  //u64 hj[2];
  u64 hk[2];

  const struct kv * const kv = kv_alloc_dup(kv0, &(map2->mm));
  if (kv == NULL) return false;

  for (u64 x = 0; x < 2; x++) {
    kvmap1_lock_hash(map2->maps[x], kv);
  }
  for (u64 x = 0; x < 2; x++) {
    struct kvmap1 * const map = map2->maps[x];
    if (kvmap1_locate_key(map, kv, &i, &j, &k)) {
      kvmap1_unlock_hash(map2->maps[1 - x], kv);
      struct __kvmap_entry * const e = &(map->levels[i].table[j].e[k]);
      kvmap1_put_entry(map, e, kv);
      kvmap1_unlock_hash(map2->maps[x], kv);
      return true;
    }
    hi[x] = i;
    hk[x] = k;
    hmax[x] = map->max_level;
    const u64 bar0 = hmax[x] + (x * map2->sz_shift);
    if (bar0 < hmax[0]) {
      hmax[x] += (hmax[0] - bar0);
    }
  }

  // insert with no split
  for (u64 x = 0; x < 2; x++) {
    if (hk[x] < 8) {
      kvmap1_unlock_hash(map2->maps[1 - x], kv);
      const bool ret = kvmap1_insert(map2->maps[x], kv, hi[x]);
      (void)ret;
      kvmap1_unlock_hash(map2->maps[x], kv);
      debug_assert(ret);
      return true;
    }
  }

  // insert with limited number of splits
  for (u64 x = 0; x < 2; x++) {
    if (hmax[x] > hi[x]) {
      const bool ret = kvmap1_insert(map2->maps[x], kv, hmax[x]);
      if (ret) {
        for (u64 y = 0; y < 2; y++) {
          kvmap1_unlock_hash(map2->maps[y], kv);
        }
        return true;
      }
    }
  }

  // cuckoo
  for (u64 x = 0; x < 2; x++) {
    if (kvmap2_cuckoo(map2, x, kv)) {
      kvmap1_unlock_hash(map2->maps[1 - x], kv);
      const bool ret = kvmap1_insert(map2->maps[x], kv, hmax[x]);
      (void)ret;
      kvmap1_unlock_hash(map2->maps[x], kv);
      debug_assert(ret);
      return true;
    }
  }

  // bad luck: expand
  kvmap1_unlock_hash(map2->maps[1], kv);
  const bool ret = kvmap1_insert(map2->maps[0], kv, 64);
  (void)ret;
  debug_assert(ret);
  kvmap1_unlock_hash(map2->maps[0], kv);
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r14
orq $-0x1, -0x40(%rbp)
orq $-0x1, -0x68(%rbp)
orq $-0x1, -0x38(%rbp)
movq %rsi, %rdi
movq %r14, %rsi
callq 0xba29
movq %rax, %rbx
testq %rax, %rax
je 0xb9e4
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0xb732
movq 0x58(%r14,%r15,8), %rdi
movq %rbx, %rsi
callq 0xb505
incq %r15
jmp 0xb71a
leaq 0x60(%r14), %r13
xorl %r12d, %r12d
cmpq $0x2, %r12
je 0xb7a5
movq 0x58(%r14,%r12,8), %r15
movq %r15, %rdi
movq %rbx, %rsi
leaq -0x40(%rbp), %rdx
leaq -0x68(%rbp), %rcx
leaq -0x38(%rbp), %r8
callq 0xb527
testb %al, %al
jne 0xb7ca
movq -0x40(%rbp), %rax
movq %rax, -0xa0(%rbp,%r12,8)
movq -0x38(%rbp), %rax
movq %rax, -0xb0(%rbp,%r12,8)
movq 0x58(%r15), %rdx
movq %rdx, -0x80(%rbp,%r12,8)
movq 0x50(%r14), %rax
imulq %r12, %rax
addq %rax, %rdx
movq -0x80(%rbp), %rcx
cmpq %rcx, %rdx
jae 0xb79c
subq %rax, %rcx
movq %rcx, -0x80(%rbp,%r12,8)
incq %r12
addq $-0x8, %r13
jmp 0xb739
leaq 0x60(%r14), %rax
xorl %r12d, %r12d
cmpq $0x2, %r12
je 0xb813
cmpq $0x7, -0xb0(%rbp,%r12,8)
jbe 0xb845
incq %r12
addq $-0x8, %rax
jmp 0xb7ac
movq (%r13), %rdi
movq %rbx, %rsi
callq 0xb5da
movq -0x40(%rbp), %rax
shlq $0x5, %rax
movq -0x68(%rbp), %rcx
shlq $0x6, %rcx
addq 0x10128(%r15,%rax), %rcx
movq -0x38(%rbp), %rax
leaq (%rcx,%rax,8), %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0xba75
movq 0x58(%r14,%r12,8), %rdi
movq %rbx, %rsi
callq 0xb5da
jmp 0xb9e4
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0xb862
movq -0x80(%rbp,%r15,8), %rdx
cmpq -0xa0(%rbp,%r15,8), %rdx
jbe 0xb840
movq 0x58(%r14,%r15,8), %rdi
movq %rbx, %rsi
callq 0xbaf1
testb %al, %al
jne 0xb95d
incq %r15
jmp 0xb816
movq (%rax), %rdi
movq %rbx, %rsi
callq 0xb5da
movq 0x58(%r14,%r12,8), %rdi
movq -0xa0(%rbp,%r12,8), %rdx
jmp 0xb9c2
xorl %r15d, %r15d
leaq -0x50(%rbp), %rdx
leaq -0x48(%rbp), %rcx
leaq -0x70(%rbp), %r8
pushq $0x1
popq %r12
cmpq $0x2, %r15
je 0xb9fc
orq $-0x1, -0x50(%rbp)
orq $-0x1, -0x48(%rbp)
orq $-0x1, -0x70(%rbp)
movq 0x58(%r14,%r15,8), %rdi
movq %rbx, %rsi
callq 0xb527
movq 0x58(%r14,%r15,8), %rax
movq -0x50(%rbp), %rcx
shlq $0x5, %rcx
movq -0x48(%rbp), %rdx
shlq $0x6, %rdx
movq %rcx, -0x88(%rbp)
addq 0x10128(%rax,%rcx), %rdx
movq %rdx, -0x58(%rbp)
movq %r15, -0x30(%rbp)
subq %r15, %r12
movq %r12, -0x60(%rbp)
xorl %r13d, %r13d
cmpq $0x8, %r13
je 0xb951
movq -0x58(%rbp), %rax
movq (%rax,%r13,8), %r15
cmpq $0x10000, %r15          # imm = 0x10000
jb 0xb9a3
movq -0x60(%rbp), %rax
movq 0x58(%r14,%rax,8), %r12
movq -0x30(%rbp), %rax
movq 0x58(%r14,%rax,8), %rax
movq %rax, -0x90(%rbp)
shrq $0x10, %r15
movq (%r15), %rax
movb 0x50(%r12), %cl
rolq %cl, %rax
andl $0xffc0, %eax           # imm = 0xFFC0
leaq (%r12,%rax), %rdi
addq $0xe8, %rdi
callq 0x6b12
testb %al, %al
je 0xb94c
movq 0x58(%r12), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0xbaf1
testb %al, %al
jne 0xb978
movq %r12, %rdi
movq %r15, %rsi
callq 0xb5da
incq %r13
jmp 0xb8d1
movq -0x30(%rbp), %r15
incq %r15
jmp 0xb865
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0xb9e4
movq 0x58(%r14,%r15,8), %rdi
movq %rbx, %rsi
callq 0xb5da
incq %r15
jmp 0xb960
movq -0x58(%rbp), %rax
andq $0x0, (%rax,%r13,8)
movq -0x88(%rbp), %rax
movq -0x90(%rbp), %rcx
lock
decq 0x10140(%rcx,%rax)
movq %r12, %rdi
movq %r15, %rsi
callq 0xb5da
movq -0x60(%rbp), %rax
movq 0x58(%r14,%rax,8), %rdi
movq %rbx, %rsi
callq 0xb5da
movq -0x30(%rbp), %r12
movq 0x58(%r14,%r12,8), %rdi
movq -0x80(%rbp,%r12,8), %rdx
movq %rbx, %rsi
callq 0xbaf1
movl %eax, %r15d
movq 0x58(%r14,%r12,8), %rdi
movq %rbx, %rsi
callq 0xb5da
testb %r15b, %r15b
jne 0xb9e4
callq 0x6e43
testq %rbx, %rbx
setne %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x60(%r14), %rdi
movq %rbx, %rsi
callq 0xb5da
movq 0x58(%r14), %rdi
pushq $0x40
popq %rdx
movq %rbx, %rsi
callq 0xbaf1
testb %al, %al
jne 0xba20
callq 0x6e43
movq 0x58(%r14), %rdi
jmp 0xb806