// File: 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
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x30(%rax), %rcx
movq %rcx, 0xb8(%rax)
movq 0x38(%rax), %rcx
movq %rcx, 0xc0(%rax)
movl 0xd8(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x10(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x60(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0xd8(%rcx), %eax
jge 0x202b8
movl $0x10, %edi
callq 0x6420
movq %rax, %rdi
movq -0x60(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x68(%rbp)
movq 0xd0(%rax), %rsi
movslq -0x1c(%rbp), %rax
imulq $0x38, %rax, %rax
addq %rax, %rsi
callq 0x1dd90
jmp 0x2027e
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rdx
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2023e
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x10, %esi
callq 0x6430
jmp 0x206bd
movq -0x60(%rbp), %rax
movq 0xd0(%rax), %rcx
movq %rcx, -0x38(%rbp)
movl 0xd8(%rax), %eax
movl %eax, -0x3c(%rbp)
movb $0x1, -0x3d(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x60(%rbp), %rcx
movb $0x1, -0x3d(%rbp)
movq 0x38(%rcx), %rax
cmpq 0xc0(%rcx), %rax
je 0x2030e
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x2030c
movq -0x70(%rbp), %rdi
callq 0x6540
jmp 0x2030e
movq -0x60(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x78(%rbp)
testq %rax, %rax
movss %xmm0, -0x74(%rbp)
js 0x2034b
movss -0x78(%rbp), %xmm0
movss %xmm0, -0x74(%rbp)
movq -0x60(%rbp), %rax
movss -0x74(%rbp), %xmm0
movss 0x20(%rax), %xmm1
mulss %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttss2si %xmm1, %rcx
movq %rcx, %rsi
sarq $0x3f, %rsi
movss 0x21ce8(%rip), %xmm1    # 0x4205c
subss %xmm1, %xmm0
cvttss2si %xmm0, %rdx
andq %rsi, %rdx
orq %rdx, %rcx
movq %rcx, 0x30(%rax)
movq 0x30(%rax), %rax
movq %rax, -0x88(%rbp)
movl $0x28, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0x90(%rbp)
callq 0x6030
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, -0x80(%rbp)
cmpq $0x0, %rax
je 0x20424
movq -0x80(%rbp), %rax
movq -0x88(%rbp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0xa0(%rbp)
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x14ce0
jmp 0x20406
movq -0xa0(%rbp), %rcx
movq -0xa8(%rbp), %rax
addq $0x28, %rax
cmpq %rcx, %rax
movq %rax, -0x98(%rbp)
jne 0x203f1
movq -0x60(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, 0x38(%rax)
cmpq $0x0, 0x38(%rax)
jne 0x20484
movq 0x39baa(%rip), %rdi     # 0x59fe8
leaq 0x21fb6(%rip), %rsi     # 0x423fb
callq 0x6400
movq %rax, %rdi
movq 0x39b4c(%rip), %rsi     # 0x59fa0
callq 0x6460
movl $0x1, %edi
callq 0x6500
movq -0x90(%rbp), %rsi
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x6830
jmp 0x206bd
movl $0x0, -0x4c(%rbp)
movq -0x60(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpq 0xb8(%rcx), %rax
jae 0x20511
movq -0x60(%rbp), %rax
movq 0xc0(%rax), %rdi
movl -0x4c(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x3a7e9(%rip), %rsi     # 0x5aca0
movl $0x20, %edx
callq 0x6190
cmpl $0x0, %eax
je 0x20501
movq -0x60(%rbp), %rdi
movq 0xc0(%rdi), %rsi
movl -0x4c(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq 0xc0(%rdi), %rax
movl -0x4c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rdx
callq 0x23f20
testb $0x1, %al
jne 0x204ff
movb $0x0, -0x3d(%rbp)
jmp 0x20511
jmp 0x20501
jmp 0x20503
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x2048b
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
xorl %eax, %eax
testb $0x1, -0x3d(%rbp)
movb %al, -0xa9(%rbp)
jne 0x20540
movq -0x60(%rbp), %rcx
movq -0x48(%rbp), %rax
cmpq 0x28(%rcx), %rax
setb %al
movb %al, -0xa9(%rbp)
movb -0xa9(%rbp), %al
testb $0x1, %al
jne 0x202dc
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x205a0
movq -0x58(%rbp), %rax
movslq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
je 0x20593
movq -0xb8(%rbp), %rdi
callq 0x1ddd0
movq -0xb8(%rbp), %rdi
movl $0x10, %esi
callq 0x6430
jmp 0x20595
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x20555
testb $0x1, -0x3d(%rbp)
je 0x206a2
movq -0x60(%rbp), %rcx
movq 0x30(%rcx), %rax
movslq 0xdc(%rcx), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq -0x60(%rbp), %rax
addq $0x1, %rcx
movl %ecx, 0xd8(%rax)
movslq 0xd8(%rax), %rax
movq %rax, -0xc8(%rbp)
movl $0x38, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x6030
movq %rax, %rcx
movq -0xc8(%rbp), %rax
movq %rcx, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x2066c
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rcx
imulq $0x38, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0xd8(%rbp)
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x1cb90
movq -0xe0(%rbp), %rax
movq -0xd8(%rbp), %rcx
addq $0x38, %rax
cmpq %rcx, %rax
movq %rax, -0xd0(%rbp)
jne 0x2063b
movq -0x60(%rbp), %rax
movq -0xc0(%rbp), %rcx
movq %rcx, 0xd0(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
je 0x206a0
movq -0xe8(%rbp), %rdi
movl $0x38, %esi
callq 0x6430
jmp 0x206ac
movl $0x1, %edi
callq 0x6500
movb -0x3d(%rbp), %al
movq -0x10(%rbp), %rcx
movq %rcx, %rsp
andb $0x1, %al
movq %rbp, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x6780
nopw %cs:(%rax,%rax)