// File: /workspace/github_c_makefile_star2_O0/wuxb45[P]SLB/lib1.c
// Function: kvmap1_split
static void
kvmap1_split(struct kvmap1 * const map, const u64 i, const u64 j)
{
  debug_assert(i < 64);
  debug_assert(j < (UINT64_C(1) << i));
  const u64 lid1 = i + 1;
  struct __kvmap1_level * const level0 = &(map->levels[i]);
  struct __kvmap1_level * const level1 = &(map->levels[lid1]);
  debug_assert(level0->table);
  debug_assert(level0->bm);

  // create next level
  if (level1->table == NULL) {
    struct bitmap * const bm1 = bitmap_create(KVMAP_SLOTS_OF(lid1));
    u64 alloc_size = 0;
    struct __kvmap_slot * const table1 = pages_alloc_best(KVMAP_SIZE_OF_LEVEL(lid1), true, &alloc_size);
    debug_assert(bm1);
    debug_assert(table1);
    bool allocs_used = false;

    spinlock_lock(&map->split_lock);
    if (level1->table == NULL) {
      allocs_used = true;
      level1->bm = bm1;
      level1->table = table1;
      level1->alloc_size = alloc_size;
      atomic_thread_fence(memory_order_seq_cst);
      if (map->max_level < lid1) {
        debug_assert((map->max_level + 1) == lid1);
        map->max_level++;
      }
      atomic_thread_fence(memory_order_seq_cst);
    }
    spinlock_unlock(&map->split_lock);

    if (allocs_used == false) {
      free(bm1);
      pages_unmap(table1, alloc_size);
    }
  }

  const u64 j2 = j | (UINT64_C(1) << i);
  bitmap_set1(level1->bm, j);
  bitmap_set1(level1->bm, j2);

  debug_assert(map->max_level >= lid1);

  // mark next level slots inuse
  for (u64 k = 0; k < 8; k++) {
    struct kv * const kv = __u64_to_ptr(level0->table[j].e[k].ptr);
    if (kv) {
      const u64 hash = bits_rotl_u64(kv->hash, map->hash_rotate);
      const u64 jj = hash & ((UINT64_C(1) << (lid1)) - 1);
      level1->table[jj].e[k] = level0->table[j].e[k];
      level0->table[j].e[k].ptr = 0;
      level0->table[j].e[k].pkey = 0;
      atomic_fetch_add(&(level1->nr_kv), 1);
      atomic_fetch_sub(&(level0->nr_kv), 1);
    }
  }

  struct __kvmap_slot * table0 = NULL;

  spinlock_lock(&map->split_lock);
  if (map->first_check == i && (level1->nr_kv >= (level0->nr_kv << 1))) {
    map->first_check ++;
  }
  bitmap_set0(level0->bm, j);
  if (bitmap_test_all0(level0->bm) && (map->min_level == i)) {
    table0 = level0->table;
    level0->table = NULL;
    // TODO: start bg thread to recycle it.
    // Don't free bm. Some getter may check it.
    //free(level0->bm);
    //level0->bm = NULL;
    map->min_level++;
  }
  spinlock_unlock(&map->split_lock);
  if(table0) pages_unmap(table0, level0->alloc_size);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
cmpq $0x40, -0x10(%rbp)
setb %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x7640
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
setb %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x7640
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
addq $0x10128, %rax          # imm = 0x10128
movq -0x10(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
addq $0x10128, %rax          # imm = 0x10128
movq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, (%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x7640
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x7640
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x188dc
movq -0x20(%rbp), %rcx
movl $0x1, %edi
shlq %cl, %rdi
callq 0x7c70
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x20(%rbp), %rcx
subq $0x6, %rcx
movl $0x1, %edi
shlq %cl, %rdi
shlq $0xc, %rdi
movl $0x1, %esi
leaq -0x40(%rbp), %rdx
callq 0x8d40
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x38(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x7640
cmpq $0x0, -0x48(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x7640
movb $0x0, -0x49(%rbp)
movq -0x8(%rbp), %rdi
addq $0x100e8, %rdi          # imm = 0x100E8
callq 0x6e50
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x188a7
movb $0x1, -0x49(%rbp)
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
mfence
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
cmpq -0x20(%rbp), %rax
jae 0x188a4
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
addq $0x1, %rax
cmpq -0x20(%rbp), %rax
sete %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x7640
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x58(%rax)
mfence
movq -0x8(%rbp), %rdi
addq $0x100e8, %rdi          # imm = 0x100E8
callq 0x6ed0
movb -0x49(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x188da
movq -0x38(%rbp), %rdi
callq 0x6060
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x8c10
jmp 0x188dc
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x7db0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x7db0
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
cmpq -0x20(%rbp), %rax
setae %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x7640
movq $0x0, -0x60(%rbp)
cmpq $0x8, -0x60(%rbp)
jae 0x18a6c
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
shrq $0x10, %rdi
callq 0xf220
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x18a59
movq -0x68(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rsi
callq 0x7b70
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movb -0x20(%rbp), %cl
movb %cl, %dl
movl %edx, %ecx
bzhiq %rcx, %rax, %rax
movq %rax, -0x78(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq -0x28(%rbp), %rdx
movq (%rdx), %rdx
movq -0x18(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movzwl (%rax,%rcx,8), %edx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
andq $-0x10000, %rdx         # imm = 0xFFFF0000
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rcx
movq $0x1, -0x80(%rbp)
movq -0x80(%rbp), %rax
lock
xaddq %rax, 0x18(%rcx)
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rcx
movq $0x1, -0x90(%rbp)
movq -0x90(%rbp), %rax
negq %rax
lock
xaddq %rax, 0x18(%rcx)
movq %rax, -0x98(%rbp)
jmp 0x18a5b
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1893a
movq $0x0, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
addq $0x100e8, %rdi          # imm = 0x100E8
callq 0x6e50
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
cmpq -0x10(%rbp), %rax
jne 0x18abd
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rcx
shlq %rcx
cmpq %rcx, %rax
jb 0x18abd
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x68(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x7e30
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7d80
testb $0x1, %al
jne 0x18ae1
jmp 0x18b18
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rax
cmpq -0x10(%rbp), %rax
jne 0x18b18
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x60(%rax)
movq -0x8(%rbp), %rdi
addq $0x100e8, %rdi          # imm = 0x100E8
callq 0x6ed0
cmpq $0x0, -0xa0(%rbp)
je 0x18b46
movq -0xa0(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x8c10
addq $0xa0, %rsp
popq %rbp
retq
nop