// File: /workspace/github_cpp_makefile_star2_O1/chahk0129[P]hash_bench/./index/cuckoo_hash.h
// Function: CuckooHash<char [32]>::Insert(char (&) [32], long)
void CuckooHash<Key_t>::Insert(Key_t& key, Value_t value) {
  size_t f_hash, s_hash;
  if constexpr(sizeof(Key_t) > 8){
      f_hash = hash_funcs[0](key, sizeof(Key_t), _seed);
      s_hash = hash_funcs[1](key, sizeof(Key_t), _seed);
  }
  else{
      f_hash = hash_funcs[0](&key, sizeof(Key_t), _seed);
      s_hash = hash_funcs[1](&key, sizeof(Key_t), _seed);
  }


RETRY:
  while (resizing_lock == 1) {
    asm("nop");
  }
  auto f_idx = f_hash % capacity;
  auto s_idx = s_hash % capacity;


  {
    unique_lock<shared_mutex> f_lock(mutex[f_idx/locksize]);
    if constexpr(sizeof(Key_t) > 8){
	if(memcmp(table[f_idx].key, INVALID<Key_t>, sizeof(Key_t)) == 0){
	    memcpy(table[f_idx].key, key, sizeof(Key_t));
	    memcpy(&table[f_idx].value, &value, sizeof(Value_t));
	    return;
	}
    }
    else{
	if(memcmp(&table[f_idx].key, &INVALID<Key_t>, sizeof(Key_t)) == 0){
	    memcpy(&table[f_idx].key, &key, sizeof(Key_t));
	    memcpy(&table[f_idx].value, &value, sizeof(Value_t));
	    return;
	}
    }
  }
  {
    unique_lock<shared_mutex> s_lock(mutex[s_idx/locksize]);
    if constexpr(sizeof(Key_t) > 8){
	if(memcmp(table[s_idx].key, INVALID<Key_t>, sizeof(Key_t)) == 0){
	    memcpy(table[s_idx].key, key, sizeof(Key_t));
	    memcpy(&table[s_idx].value, &value, sizeof(Value_t));
	    return;
	}
    }
    else{
	if(memcmp(&table[s_idx].key, &INVALID<Key_t>, sizeof(Key_t)) == 0){
	    memcpy(&table[s_idx].key, &key, sizeof(Key_t));
	    memcpy(&table[s_idx].value, &value, sizeof(Value_t));
	    return;
	}
    }

  }

  { // Failed to insert... Doing Cuckooing...
    int unlocked = 0;
    if (CAS(&resizing_lock, &unlocked, 1)) {
#ifdef BREAKDOWN
	clock_gettime(CLOCK_MONOTONIC, &t_start);
#endif
PATH_RETRY:
	    if constexpr(sizeof(Key_t) > 8){
		    auto path1 = find_path(f_idx, true);
		    auto path2 = find_path(s_idx, true);
		    if (path1.size() != 0 || path2.size() != 0) {
			    auto path = &path1;
			    if(path1.size() == 0
					    || (path2.size() != 0 && path2.size() < path1.size())
					    || (path2.size() != 0 && (memcmp(path1[0].second.c_str(), INVALID<Key_t>, sizeof(Key_t)) == 0))){
				    path = &path2;
			    }

			    auto id = 0;
			    vector<size_t> lock_loc;
			    for (auto p: *path) {
				    lock_loc.push_back(p.first/locksize);
			    }
			    sort(begin(lock_loc), end(lock_loc));
			    lock_loc.erase( unique( lock_loc.begin(), lock_loc.end() ), lock_loc.end() );
			    unique_lock<shared_mutex> *lock[kCuckooThreshold];
			    for (auto i :lock_loc) {
				    lock[id++] = new unique_lock<shared_mutex>(mutex[i]);
			    }
			    for (auto p : *path) {
				    if(strcmp(table[p.first].key, p.second.c_str()) != 0){
					    for(int i=0; i<id; i++)
						    delete lock[i];
					    goto PATH_RETRY;
				    }

			    }
			    resizing_lock = 0;
			    execute_path(*path, key, value, true);
			    for (int i = 0; i < id; ++i) {
				    delete lock[i];
			    }
#ifdef BREAKDOWN
			    clock_gettime(CLOCK_MONOTONIC, &t_end);
			    cuckoo_time += t_end.tv_nsec - t_start.tv_nsec + (t_end.tv_sec - t_start.tv_sec) * 1000000000;
#endif
			    return;
		    } else {
			    resize();
			    resizing_lock = 0;
#ifdef BREAKDOWN
			    clock_gettime(CLOCK_MONOTONIC, &t_end);
			    split_time += t_end.tv_nsec - t_start.tv_nsec + (t_end.tv_sec - t_start.tv_sec) * 1000000000;
#endif
		    }
	    }
	    else{

		    auto path1 = find_path(f_idx);
		    auto path2 = find_path(s_idx);
		    if (path1.size() != 0 || path2.size() != 0) {
			    auto path = &path1;
			    if(path1.size() == 0
					    || (path2.size() != 0 && path2.size() < path1.size())
					    || (path2.size() != 0 && memcmp(&path1[0].second, &INVALID<Key_t>, sizeof(Key_t)) == 0)){
				    path = &path2;
			    }

			    auto id = 0;
			    vector<size_t> lock_loc;
			    for (auto p: *path) {
				    lock_loc.push_back(p.first/locksize);
			    }
			    sort(begin(lock_loc), end(lock_loc));
			    lock_loc.erase( unique( lock_loc.begin(), lock_loc.end() ), lock_loc.end() );
			    unique_lock<shared_mutex> *lock[kCuckooThreshold];
			    for (auto i :lock_loc) {
				    lock[id++] = new unique_lock<shared_mutex>(mutex[i]);
			    }
			    for (auto p : *path) {
				    if constexpr(sizeof(Key_t) > 8){
					    if(memcmp(table[p.first].key, p.second, sizeof(Key_t)) != 0){
						    for(int i=0; i<id; i++)
							    delete lock[i];
						    goto PATH_RETRY;
					    }
				    }
				    else{
					    if(memcmp(&table[p.first].key, &p.second, sizeof(Key_t)) != 0){
						    for(int i=0; i<id; i++)
							    delete lock[i];
						    goto PATH_RETRY;
					    }
				    }

			    }
			    resizing_lock = 0;
			    execute_path(*path, key, value);
			    for (int i = 0; i < id; ++i) {
				    delete lock[i];
			    }
#ifdef BREAKDOWN
			    clock_gettime(CLOCK_MONOTONIC, &t_end);
			    cuckoo_time += t_end.tv_nsec - t_start.tv_nsec + (t_end.tv_sec - t_start.tv_sec) * 1000000000;
#endif

			    return;
		    } else {
			    resize();
			    resizing_lock = 0;
#ifdef BREAKDOWN
			    clock_gettime(CLOCK_MONOTONIC, &t_end);
			    split_time += t_end.tv_nsec - t_start.tv_nsec + (t_end.tv_sec - t_start.tv_sec) * 1000000000;
#endif
		    }
	    }
    }
  }
  goto RETRY;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdx, -0x90(%rbp)
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rdi), %rdx
movl $0x20, %esi
movq %r14, %rdi
callq 0x2050
movq %rax, -0xd0(%rbp)
movq %rbx, -0x30(%rbp)
movl 0x8(%rbx), %eax
xorl $0x20, %eax
xorl %ecx, %ecx
imull $0x5bd1e995, (%r14,%rcx), %edx # imm = 0x5BD1E995
movl %edx, %esi
shrl $0x18, %esi
xorl %edx, %esi
imull $0x5bd1e995, %esi, %edx # imm = 0x5BD1E995
imull $0x5bd1e995, %eax, %eax # imm = 0x5BD1E995
xorl %edx, %eax
addq $0x4, %rcx
cmpq $0x20, %rcx
jne 0xa17f
movq %r14, -0x88(%rbp)
movl %eax, %ecx
shrl $0xd, %ecx
xorl %eax, %ecx
imull $0x5bd1e995, %ecx, %eax # imm = 0x5BD1E995
movl %eax, %ecx
shrl $0xf, %ecx
xorl %eax, %ecx
movq %rcx, -0xc8(%rbp)
leaq -0x108(%rbp), %r13
movq -0x30(%rbp), %rbx
cmpl $0x1, 0xc8(%rbx)
jne 0xa1e8
movl 0xc8(%rbx), %eax
nop
cmpl $0x1, %eax
je 0xa1e2
movq 0x30(%rbx), %rcx
movq -0xd0(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %r15
movq -0xc8(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %r12
movslq 0xdc(%rbx), %rcx
movq %r15, %rax
xorl %edx, %edx
divq %rcx
imulq $0x38, %rax, %rax
addq 0xd0(%rbx), %rax
movq %rax, -0x78(%rbp)
movb $0x0, -0x70(%rbp)
leaq -0x78(%rbp), %r14
movq %r14, %rdi
callq 0xa0ba
movb $0x1, -0x70(%rbp)
movq 0x38(%rbx), %rcx
movq %r15, -0xe0(%rbp)
leaq (%r15,%r15,4), %rax
movdqu (%rcx,%rax,8), %xmm0
movdqu 0x10(%rcx,%rax,8), %xmm1
pcmpeqb 0xa082(%rip), %xmm1     # 0x142e0
pcmpeqb 0xa06a(%rip), %xmm0     # 0x142d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %edx
cmpl $0xffff, %edx           # imm = 0xFFFF
je 0xa835
movq %r14, %rdi
callq 0x9de6
movslq 0xdc(%rbx), %rcx
movq %r12, %rax
xorl %edx, %edx
divq %rcx
imulq $0x38, %rax, %rax
addq 0xd0(%rbx), %rax
movq %rax, -0x78(%rbp)
movb $0x0, -0x70(%rbp)
movq %r14, %rdi
callq 0xa0ba
movb $0x1, -0x70(%rbp)
movq 0x38(%rbx), %rcx
leaq (%r12,%r12,4), %rax
movdqu (%rcx,%rax,8), %xmm0
movdqu 0x10(%rcx,%rax,8), %xmm1
pcmpeqb 0xa015(%rip), %xmm1     # 0x142e0
pcmpeqb 0x9ffd(%rip), %xmm0     # 0x142d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %edx
cmpl $0xffff, %edx           # imm = 0xFFFF
je 0xa835
movq %r14, %rdi
callq 0x9de6
xorl %eax, %eax
movl $0x1, %ecx
lock
cmpxchgl %ecx, 0xc8(%rbx)
jne 0xa1cf
movq %r12, -0xd8(%rbp)
movl $0x1, %edi
leaq 0x9f89(%rip), %rsi      # 0x142a0
callq 0x21b0
leaq -0xc0(%rbp), %rbx
movq %r13, %rdi
movq -0x30(%rbp), %r15
movq %r15, %rsi
movq -0xe0(%rbp), %rdx
movl $0x1, %ecx
callq 0xaf04
movq %rbx, %rdi
movq %r15, %rsi
movq -0xd8(%rbp), %rdx
movl $0x1, %ecx
callq 0xaf04
movq -0x108(%rbp), %rax
movq -0x100(%rbp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
je 0xa589
movq -0xc0(%rbp), %rdx
movq -0xb8(%rbp), %rsi
movq %rsi, %rdi
subq %rdx, %rdi
je 0xa38f
cmpq %rcx, %rdi
jae 0xa38f
movq %rbx, %rcx
jmp 0xa3c6
movq %r13, %rcx
cmpq %rdx, %rsi
je 0xa3c6
movq 0x8(%rax), %rax
movdqu (%rax), %xmm0
movdqu 0x10(%rax), %xmm1
pcmpeqb 0x9f34(%rip), %xmm1     # 0x142e0
pcmpeqb 0x9f1c(%rip), %xmm0     # 0x142d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %eax
movq %r13, %rcx
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0xa38a
pxor %xmm0, %xmm0
movdqa %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq (%rcx), %r14
movq %rcx, -0xa8(%rbp)
movq 0x8(%rcx), %rbx
cmpq %rbx, %r14
je 0xa496
movq (%r14), %rax
movq %rax, -0x78(%rbp)
leaq 0x8(%r14), %r13
leaq -0xa0(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x70cc
leaq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq %r12, %rdi
callq 0xb9aa
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0xbbec
movq -0x78(%rbp), %rax
movslq 0xdc(%r15), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, -0xa0(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0xa45d
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, -0x48(%rbp)
jmp 0xa46d
leaq -0x50(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0xbeea
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0xa486
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
addq $0x20, %r13
movq %r13, %r14
cmpq %rbx, %r13
jne 0xa3ee
movq -0x50(%rbp), %r13
movq -0x48(%rbp), %r12
cmpq %r12, %r13
je 0xa4d0
movq %r12, %rax
subq %r13, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0xc025
movq %r13, %rdi
movq %r12, %rsi
callq 0xc10e
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xc3a3
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rcx
cmpq %rcx, %rax
je 0xa500
movq -0x48(%rbp), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
addq %rsi, %rax
cmpq %rax, %rdx
je 0xa500
movq %rax, -0x48(%rbp)
movq %rsp, -0xe8(%rbp)
movq 0x10(%rdi), %rax
movq %rsp, %r15
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq -0x50(%rbp), %r14
movq -0x48(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq %rax, %r14
movq %r15, -0x80(%rbp)
je 0xa5ab
xorl %ebx, %ebx
movq (%r14), %r13
movl $0x10, %edi
callq 0x21f0
movq %rax, %r12
imulq $0x38, %r13, %rax
movq -0x30(%rbp), %rcx
addq 0xd0(%rcx), %rax
movq %rax, (%r12)
movb $0x0, 0x8(%r12)
movq %r12, %rdi
callq 0xa0ba
movb $0x1, 0x8(%r12)
movq %r12, (%r15)
addq $0x8, %r14
addq $0x8, %r15
incl %ebx
cmpq -0x98(%rbp), %r14
jne 0xa53a
jmp 0xa5ad
movq -0xb8(%rbp), %rsi
cmpq -0xc0(%rbp), %rsi
je 0xa7c3
cmpq %rax, %rdx
jne 0xa36f
jmp 0xa38a
xorl %ebx, %ebx
movq -0xa8(%rbp), %rax
movq (%rax), %r14
movq 0x8(%rax), %rax
movq %rax, -0xf0(%rbp)
cmpq %rax, %r14
je 0xa6bc
movl %ebx, %r13d
movq -0x30(%rbp), %r12
movq (%r14), %rax
movq %rax, -0x78(%rbp)
leaq 0x8(%r14), %rsi
leaq -0xa0(%rbp), %r15
movq %r15, %rdi
callq 0x70cc
leaq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq %r15, %rdi
callq 0xb9aa
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0xbbec
testl %ebx, %ebx
sete %r15b
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rsi
leaq (%rax,%rax,4), %rdi
shlq $0x3, %rdi
addq 0x38(%r12), %rdi
callq 0x2250
xorl %esi, %esi
movl %eax, -0x98(%rbp)
testl %eax, %eax
setne %cl
sete %dl
orb %r15b, %dl
je 0xa64e
movb %cl, %sil
leal (%rsi,%rsi,4), %r12d
jmp 0xa685
xorl %r15d, %r15d
movq -0x80(%rbp), %rax
movq (%rax,%r15,8), %r12
testq %r12, %r12
je 0xa677
movq %r12, %rdi
callq 0x9de6
movl $0x10, %esi
movq %r12, %rdi
callq 0x2200
movq -0x80(%rbp), %rax
incq %r15
cmpq %r15, %r13
jne 0xa655
movl $0x5, %r12d
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0xa69e
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
cmpl $0x0, -0x98(%rbp)
jne 0xa6c2
addq $0x28, %r14
cmpq -0xf0(%rbp), %r14
movq -0x30(%rbp), %r12
jne 0xa5d2
movl $0xa, %r12d
cmpl $0xa, %r12d
leaq -0x108(%rbp), %r13
jne 0xa784
movq -0x30(%rbp), %rdi
movl $0x0, 0xc8(%rdi)
movq -0xa8(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq -0x90(%rbp), %rcx
movl $0x1, %r8d
callq 0xb2e0
testl %ebx, %ebx
movq -0x80(%rbp), %rax
je 0xa739
movl %ebx, %r14d
xorl %r15d, %r15d
movq (%rax,%r15,8), %rbx
testq %rbx, %rbx
je 0xa731
movq %rbx, %rdi
callq 0x9de6
movl $0x10, %esi
movq %rbx, %rdi
callq 0x2200
movq -0x80(%rbp), %rax
incq %r15
cmpq %r15, %r14
jne 0xa70f
movl $0x1, %r12d
movl $0x1, %edi
leaq 0x9b65(%rip), %rsi      # 0x142b0
callq 0x21b0
movq 0x9b61(%rip), %rax      # 0x142b8
subq 0x9b4a(%rip), %rax      # 0x142a8
movq 0x9b4b(%rip), %rcx      # 0x142b0
subq 0x9b34(%rip), %rcx      # 0x142a0
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq 0x9b1e(%rip), %rax      # 0x14298
addq %rcx, %rax
movq %rax, 0x9b14(%rip)      # 0x14298
movq -0xe8(%rbp), %rsp
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xa7a0
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq -0xc0(%rbp), %rbx
movq %rbx, %rdi
callq 0xb694
movq %r13, %rdi
callq 0xb694
cmpl $0x5, %r12d
je 0xa323
jmp 0xa820
movq %r15, %rdi
callq 0xb3c6
movl $0x0, 0xc8(%r15)
movl $0x1, %edi
leaq 0x9ace(%rip), %rsi      # 0x142b0
callq 0x21b0
movq 0x9aca(%rip), %rax      # 0x142b8
subq 0x9ab3(%rip), %rax      # 0x142a8
movq 0x9ab4(%rip), %rcx      # 0x142b0
subq 0x9a9d(%rip), %rcx      # 0x142a0
addq 0x9a86(%rip), %rax      # 0x14290
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq %rcx, %rax
movq %rax, 0x9a75(%rip)      # 0x14290
xorl %r12d, %r12d
jmp 0xa7a7
testl %r12d, %r12d
je 0xa1cf
testb $0xd, %r12b
je 0xa1cf
jmp 0xa86b
leaq (%rcx,%rax,8), %rcx
movq -0x88(%rbp), %rdx
movdqu (%rdx), %xmm0
movdqu 0x10(%rdx), %xmm1
movdqu %xmm1, 0x10(%rcx)
movdqu %xmm0, (%rcx)
movq 0x38(%rbx), %rcx
movq -0x90(%rbp), %rdx
movq %rdx, 0x20(%rcx,%rax,8)
leaq -0x78(%rbp), %rdi
callq 0x9de6
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xa8cf
jmp 0xa8b7
movq %rax, %rbx
jmp 0xa8db
jmp 0xa8b7
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0xa8ba
movq -0x60(%rbp), %rsi
incq %rsi
jmp 0xa8b0
jmp 0xa8b7
jmp 0xa8b7
movq %rax, %rbx
movl $0x10, %esi
movq %r12, %rdi
callq 0x2200
jmp 0xa8ba
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0xa8cf
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq -0xc0(%rbp), %rdi
callq 0xb694
leaq -0x108(%rbp), %rdi
callq 0xb694
movq %rbx, %rdi
callq 0x2330
nop