// File: /workspace/github_cpp_makefile_star2_O2/chahk0129[P]hash_bench/./index/cuckoo_hash.h
// Function: CuckooHash<char [32]>::resize()
bool CuckooHash<Key_t>::resize(void) {
  old_cap = capacity;
  old_tab = table;

  std::unique_lock<std::shared_mutex> *lock[nlocks];
  for(int i=0;i<nlocks;i++){
    lock[i] = new std::unique_lock<std::shared_mutex>(mutex[i]);
  }
  std::shared_mutex* old_mutex = mutex;

  int prev_nlocks = nlocks;

  bool success = true;
  size_t num_grows = 0;

  do {
    success = true;
    if (table != old_tab) delete [] table;
    capacity = capacity * kResizingFactor;
    table = new Pair<Key_t>[capacity];
    if (table == nullptr) {
      cerr << "error: memory allocation failed." << endl;
      exit(1);
    }

    for (unsigned i = 0; i < old_cap; ++i) {
	    if constexpr(sizeof(Key_t) > 8){
		    if(memcmp(old_tab[i].key, INVALID<Key_t>, sizeof(Key_t)) != 0){
			    if(!insert4resize(old_tab[i].key, old_tab[i].value)){
				    success = false;
				    break;
			    }
		    }
	    }
	    else{
		    if(memcmp(&old_tab[i].key, &INVALID<Key_t>, sizeof(Key_t)) != 0){
			    if(!insert4resize(old_tab[i].key, old_tab[i].value)){
				    success = false;
				    break;
			    }
		    }
	    }
    }
    ++num_grows;
  } while (!success && num_grows < kMaxGrows);

  for (int i = 0; i < prev_nlocks; ++i) {
    delete lock[i];
  }

  if (success) {
    nlocks = capacity/locksize+1;
    mutex = new std::shared_mutex[nlocks];
    delete old_mutex;
  } else {
    exit(1);
  }
  return success;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movups 0x30(%rdi), %xmm0
movups %xmm0, 0xb8(%rdi)
movl 0xd8(%rdi), %edx
movq %rsp, %rcx
leaq 0xf(,%rdx,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
xorl %r12d, %r12d
pushq $0x10
popq %r14
xorl %r13d, %r13d
movslq %edx, %rax
cmpq %rax, %r13
jge 0x9c3c
movq %r14, %rdi
callq 0x21e0
movq %rax, %r15
movq 0xd0(%rbx), %rsi
addq %r12, %rsi
movq %rax, %rdi
callq 0x8a0c
movq -0x30(%rbp), %rax
movq %r15, (%rax,%r13,8)
incq %r13
movl 0xd8(%rbx), %edx
addq $0x38, %r12
jmp 0x9c00
movq %rdx, -0x48(%rbp)
movq 0xd0(%rbx), %rax
movq %rax, -0x40(%rbp)
xorl %r15d, %r15d
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x9c65
cmpq 0xc0(%rbx), %rdi
je 0x9c65
callq 0x2220
movq 0x30(%rbx), %rax
testq %rax, %rax
js 0x9c75
cvtsi2ss %rax, %xmm0
jmp 0x9c8a
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
mulss 0x20(%rbx), %xmm0
cvttss2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x23f5(%rip), %xmm0     # 0xc098
cvttss2si %xmm0, %r14
andq %rcx, %r14
orq %rax, %r14
movq %r14, %rax
pushq $0x28
popq %rcx
mulq %rcx
movq %r14, 0x30(%rbx)
pushq $-0x1
popq %rcx
cmovoq %rcx, %rax
movq %rax, %rdi
callq 0x2030
testq %r14, %r14
xorps %xmm0, %xmm0
je 0x9cea
imulq $0x28, %r14, %rcx
xorl %edx, %edx
movups %xmm0, 0x10(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0x9cd8
movq %rax, 0x38(%rbx)
xorl %r13d, %r13d
movl %r13d, %r13d
movq 0xb8(%rbx), %r12
cmpq %r13, %r12
jbe 0x9d4c
imulq $0x28, %r13, %r14
addq 0xc0(%rbx), %r14
movq %r14, %rdi
leaq 0xa5cb(%rip), %rsi      # 0x142e0
pushq $0x20
popq %rdx
callq 0x2200
testl %eax, %eax
je 0x9d34
movq 0x20(%r14), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaaaa
testb %al, %al
je 0x9d39
incl %r13d
jmp 0x9cf1
movq %r15, %rax
incq %rax
movq %rax, %r15
cmpq 0x28(%rbx), %rax
jb 0x9c4e
movq %rbx, -0x38(%rbp)
xorl %r15d, %r15d
movq -0x48(%rbp), %rbx
testl %ebx, %ebx
cmovlel %r15d, %ebx
cmpq %r15, %rbx
je 0x9d87
movq -0x30(%rbp), %rax
movq (%rax,%r15,8), %r14
testq %r14, %r14
je 0x9d77
movq %r14, %rdi
callq 0x8a22
movq %r14, %rdi
pushq $0x10
popq %rsi
callq 0x21f0
incq %r15
jmp 0x9d5d
cmpq %r13, %r12
ja 0x9e1a
movq -0x38(%rbp), %rbx
movq 0x30(%rbx), %rax
movslq 0xdc(%rbx), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %r12
leal 0x1(%rax), %ecx
movslq %ecx, %r14
pushq $0x38
popq %rdx
movq %r14, %rax
mulq %rdx
movl %ecx, 0xd8(%rbx)
pushq $-0x1
popq %rdi
cmovnoq %rax, %rdi
callq 0x2030
incl %r12d
je 0x9df6
imulq $0x38, %r14, %rdx
movq %rax, %rcx
addq %rdx, %rcx
xorps %xmm0, %xmm0
movq %rax, %rdx
movups %xmm0, 0x20(%rdx)
andq $0x0, 0x30(%rdx)
movups %xmm0, 0x10(%rdx)
movups %xmm0, (%rdx)
addq $0x38, %rdx
cmpq %rcx, %rdx
jne 0x9ddd
movq %rax, 0xd0(%rbx)
pushq $0x38
popq %rsi
movq -0x40(%rbp), %rdi
callq 0x21f0
movb $0x1, %al
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x2210
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x21f0
movq %rbx, %rdi
callq 0x2340