// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movq %rdi, %rbx
movdqu 0x30(%rdi), %xmm0
movdqu %xmm0, 0xb8(%rdi)
movl 0xd8(%rdi), %r13d
movq %rsp, %rcx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
testl %r13d, %r13d
jle 0xc097
xorl %r15d, %r15d
xorl %r12d, %r12d
movl $0x10, %edi
callq 0x21f0
movq %rax, %r14
movq 0xd0(%rbx), %rax
addq %r15, %rax
movq %rax, (%r14)
movb $0x0, 0x8(%r14)
movq %r14, %rdi
callq 0xad3c
movb $0x1, 0x8(%r14)
movq -0x30(%rbp), %rax
movq %r14, (%rax,%r12,8)
incq %r12
movslq 0xd8(%rbx), %r13
addq $0x38, %r15
cmpq %r13, %r12
jl 0xc050
movq 0xd0(%rbx), %rax
movq %rax, -0x38(%rbp)
xorl %r12d, %r12d
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0xc0bc
cmpq 0xc0(%rbx), %rdi
je 0xc0bc
callq 0x2230
movq 0x30(%rbx), %rax
testq %rax, %rax
js 0xc0cf
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0xc0e7
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
mulss 0x20(%rbx), %xmm0
cvttss2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x1fd0(%rip), %xmm0     # 0xe0d0
cvttss2si %xmm0, %r15
andq %rcx, %r15
orq %rax, %r15
movq %r15, %rax
movl $0x28, %ecx
mulq %rcx
movq %r15, 0x30(%rbx)
movq $-0x1, %rcx
cmovoq %rcx, %rax
movq %rax, %rdi
callq 0x2030
testq %r15, %r15
xorps %xmm0, %xmm0
je 0xc151
shlq $0x3, %r15
leaq (%r15,%r15,4), %rcx
xorl %edx, %edx
movups %xmm0, 0x10(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0xc13f
movq %rax, 0x38(%rbx)
movq 0xb8(%rbx), %rax
testq %rax, %rax
setne %r14b
je 0xc1da
movl $0x1, %r15d
xorl %edx, %edx
movq 0xc0(%rbx), %rcx
leaq (%rdx,%rdx,4), %rdx
movdqu (%rcx,%rdx,8), %xmm0
movdqu 0x10(%rcx,%rdx,8), %xmm1
pcmpeqb 0x9155(%rip), %xmm1     # 0x152e0
pcmpeqb 0x913d(%rip), %xmm0     # 0x152d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %esi
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0xc1be
leaq (%rcx,%rdx,8), %rsi
movq 0x20(%rsi), %rdx
movq %rbx, %rdi
callq 0xcfb6
testb %al, %al
je 0xc1cb
movq 0xb8(%rbx), %rax
movl %r15d, %edx
incl %r15d
cmpq %rdx, %rax
ja 0xc16d
jmp 0xc1da
incq %r12
cmpq 0x28(%rbx), %r12
jb 0xc0a5
jmp 0xc1dd
xorl %r14d, %r14d
testl %r13d, %r13d
jle 0xc212
movl %r13d, %r12d
xorl %r13d, %r13d
movq -0x30(%rbp), %rax
movq (%rax,%r13,8), %r15
testq %r15, %r15
je 0xc1fd
movq %r15, %rdi
callq 0xaa52
movl $0x10, %esi
movq %r15, %rdi
callq 0x2200
incq %r13
cmpq %r13, %r12
jne 0xc1e8
testb %r14b, %r14b
jne 0xc2af
movq 0x30(%rbx), %rax
movslq 0xdc(%rbx), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %r12
incl %eax
movl %eax, 0xd8(%rbx)
movslq %eax, %r15
movl $0x38, %ecx
movq %r15, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
callq 0x2030
incl %r12d
je 0xc289
imulq $0x38, %r15, %rdx
movq %rax, %rcx
addq %rdx, %rcx
pxor %xmm0, %xmm0
movq %rax, %rdx
movdqu %xmm0, 0x20(%rdx)
movdqu %xmm0, 0x10(%rdx)
movdqu %xmm0, (%rdx)
movq $0x0, 0x30(%rdx)
addq $0x38, %rdx
cmpq %rcx, %rdx
jne 0xc26a
movq %rax, 0xd0(%rbx)
movl $0x38, %esi
movq -0x38(%rbp), %rdi
callq 0x2200
movb $0x1, %al
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x2220
movq %rax, %rbx
movl $0x10, %esi
movq %r14, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x2330
nop