// File: /workspace/github_cpp_makefile_star2_O3/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, -0x98(%rbp)
movq %rsi, %rbx
movq %rdi, %r12
movq 0x8(%rdi), %rdx
movl $0x20, %esi
movq %rbx, %rdi
callq 0x2050
movq %rax, -0xd8(%rbp)
movl 0x8(%r12), %eax
xorl $0x20, %eax
xorl %ecx, %ecx
imull $0x5bd1e995, (%rbx,%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 0xadff
movq %rbx, -0x90(%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, -0xd0(%rbp)
leaq -0x60(%rbp), %r15
leaq -0x108(%rbp), %r13
leaq -0xc8(%rbp), %rbx
movq %r12, -0x88(%rbp)
cmpl $0x1, 0xc8(%r12)
jne 0xae79
movl 0xc8(%r12), %eax
nop
cmpl $0x1, %eax
je 0xae73
movq 0x30(%r12), %rcx
movq -0xd8(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %r14
movq -0xd0(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, -0xb0(%rbp)
movslq 0xdc(%r12), %rcx
movq %r14, %rax
xorl %edx, %edx
divq %rcx
imulq $0x38, %rax, %rax
addq 0xd0(%r12), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r15, %rdi
callq 0xad3c
movb $0x1, -0x58(%rbp)
movq 0x38(%r12), %rcx
movq %r14, -0xe0(%rbp)
leaq (%r14,%r14,4), %rax
movdqu (%rcx,%rax,8), %xmm0
movdqu 0x10(%rcx,%rax,8), %xmm1
pcmpeqb 0xa3ed(%rip), %xmm1     # 0x152e0
pcmpeqb 0xa3d5(%rip), %xmm0     # 0x152d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %edx
cmpl $0xffff, %edx           # imm = 0xFFFF
je 0xb4f8
movq %r15, %rdi
callq 0xaa52
movslq 0xdc(%r12), %rcx
movq -0xb0(%rbp), %r14
movq %r14, %rax
xorl %edx, %edx
divq %rcx
imulq $0x38, %rax, %rax
addq 0xd0(%r12), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r15, %rdi
callq 0xad3c
movb $0x1, -0x58(%rbp)
movq 0x38(%r12), %rcx
leaq (%r14,%r14,4), %rax
movdqu (%rcx,%rax,8), %xmm0
movdqu 0x10(%rcx,%rax,8), %xmm1
pcmpeqb 0xa376(%rip), %xmm1     # 0x152e0
pcmpeqb 0xa35e(%rip), %xmm0     # 0x152d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %edx
cmpl $0xffff, %edx           # imm = 0xFFFF
je 0xb4f8
movq %r15, %rdi
callq 0xaa52
xorl %eax, %eax
movl $0x1, %ecx
lock
cmpxchgl %ecx, 0xc8(%r12)
jne 0xae60
movl $0x1, %edi
leaq 0xa2ef(%rip), %rsi      # 0x152a0
callq 0x21b0
movq %r13, %rdi
movq %r12, %rsi
movq -0xe0(%rbp), %rdx
movl $0x1, %ecx
callq 0xbb62
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0xbb62
movq -0x108(%rbp), %r14
movq -0x100(%rbp), %rbx
movq %rbx, %rdx
movq -0xc8(%rbp), %rax
movq -0xc0(%rbp), %rcx
subq %r14, %rdx
je 0xb055
movq %rcx, %rsi
subq %rax, %rsi
je 0xb070
cmpq %rdx, %rsi
jb 0xb042
movq 0x8(%r14), %rdx
movdqu (%rdx), %xmm0
movdqu 0x10(%rdx), %xmm1
pcmpeqb 0xa2ba(%rip), %xmm1     # 0x152e0
pcmpeqb 0xa2a2(%rip), %xmm0     # 0x152d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %edx
movq %r13, -0x38(%rbp)
cmpl $0xffff, %edx           # imm = 0xFFFF
jne 0xb074
movq %rcx, %rbx
movq %rax, %r14
leaq -0xc8(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xb074
movq %rcx, %rbx
movq %rax, %r14
leaq -0xc8(%rbp), %rdx
movq %rdx, -0x38(%rbp)
cmpq %rax, %rcx
jne 0xb074
jmp 0xb483
movq %r13, -0x38(%rbp)
pxor %xmm0, %xmm0
movdqa %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
cmpq %rbx, %r14
je 0xb1c6
movq (%r14), %rax
movq %rax, -0x60(%rbp)
leaq 0x8(%r14), %r13
leaq -0xa8(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x7e9c
leaq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq %r15, %rdi
callq 0xc5f8
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
leaq -0x58(%rbp), %rdi
callq 0xc814
movq -0x60(%rbp), %rax
movslq 0xdc(%r12), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, -0xa8(%rbp)
movq -0x78(%rbp), %rsi
cmpq -0x70(%rbp), %rsi
je 0xb0fe
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, -0x78(%rbp)
jmp 0xb10e
leaq -0x80(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0xcb20
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xb127
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x2200
addq $0x20, %r13
movq %r13, %r14
cmpq %rbx, %r13
jne 0xb08e
movq -0x80(%rbp), %r15
movq -0x78(%rbp), %r13
cmpq %r13, %r15
je 0xb179
movq %r13, %rax
subq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0xcc5b
movq %r15, %rdi
movq %r13, %rsi
callq 0xcd44
movq -0x80(%rbp), %r15
movq -0x78(%rbp), %r13
cmpq %r13, %r15
je 0xb1c6
addq $0x8, %r15
cmpq %r13, %r15
je 0xb1c6
movq -0x8(%r15), %rcx
leaq 0x8(%r15), %rdx
cmpq (%r15), %rcx
movq %rdx, %r15
jne 0xb182
leaq -0x10(%rdx), %rax
cmpq %r13, %rdx
je 0xb1b9
movq %rcx, %rsi
movq (%rdx), %rcx
cmpq %rcx, %rsi
je 0xb1b3
movq %rcx, 0x8(%rax)
addq $0x8, %rax
addq $0x8, %rdx
jmp 0xb19b
addq $0x8, %rax
cmpq %r13, %rax
je 0xb1c6
movq %rax, -0x78(%rbp)
movq %rsp, -0xe8(%rbp)
movq 0x10(%r12), %rax
movq %rsp, %rbx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq %rax, %r14
je 0xb256
movl $0x0, -0x2c(%rbp)
movq %rbx, %r12
movq (%r14), %r13
movl $0x10, %edi
callq 0x21f0
movq %rax, %r15
imulq $0x38, %r13, %rax
movq -0x88(%rbp), %rcx
addq 0xd0(%rcx), %rax
movq %rax, (%r15)
movb $0x0, 0x8(%r15)
movq %r15, %rdi
callq 0xad3c
movb $0x1, 0x8(%r15)
movq %r15, (%r12)
addq $0x8, %r14
addq $0x8, %r12
incl -0x2c(%rbp)
cmpq -0xa0(%rbp), %r14
jne 0xb205
jmp 0xb25d
movl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %r13
movq 0x8(%rax), %rax
movl -0x2c(%rbp), %r14d
movq %rax, -0xf0(%rbp)
cmpq %rax, %r13
je 0xb36d
movq (%r13), %rax
movq %rax, -0x60(%rbp)
leaq 0x8(%r13), %rsi
leaq -0xa8(%rbp), %r15
movq %r15, %rdi
callq 0x7e9c
leaq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq %r15, %rdi
callq 0xc5f8
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
addq %rsi, %rdx
leaq -0x58(%rbp), %rdi
callq 0xc814
cmpl $0x0, -0x2c(%rbp)
sete %r12b
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %r15
leaq (%rax,%rax,4), %rdi
shlq $0x3, %rdi
movq -0x88(%rbp), %rax
addq 0x38(%rax), %rdi
movq %r15, %rsi
callq 0x2250
xorl %esi, %esi
movl %eax, -0xa0(%rbp)
testl %eax, %eax
setne %cl
sete %dl
orb %r12b, %dl
je 0xb304
movb %cl, %sil
leal (%rsi,%rsi,4), %r12d
jmp 0xb337
xorl %r12d, %r12d
movq (%rbx,%r12,8), %r15
testq %r15, %r15
je 0xb318
movq %r15, %rdi
callq 0xaa52
movl $0x10, %esi
movq %r15, %rdi
callq 0x2200
incq %r12
cmpq %r12, %r14
jne 0xb307
movl $0x5, %r12d
movq -0x58(%rbp), %r15
leaq -0x48(%rbp), %rax
cmpq %rax, %r15
je 0xb34f
movq -0x48(%rbp), %rsi
incq %rsi
movq %r15, %rdi
callq 0x2200
cmpl $0x0, -0xa0(%rbp)
jne 0xb415
addq $0x28, %r13
cmpq -0xf0(%rbp), %r13
jne 0xb27c
movq -0x88(%rbp), %rdi
movl $0x0, 0xc8(%rdi)
movq -0x38(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x98(%rbp), %rcx
movl $0x1, %r8d
callq 0xbf1c
cmpl $0x0, -0x2c(%rbp)
je 0xb3ca
xorl %r12d, %r12d
movq (%rbx,%r12,8), %r15
testq %r15, %r15
je 0xb3b5
movq %r15, %rdi
callq 0xaa52
movl $0x10, %esi
movq %r15, %rdi
callq 0x2200
incq %r12
cmpq %r12, %r14
jne 0xb3a4
movl $0x1, %r12d
movl $0x1, %edi
leaq 0x9ed4(%rip), %rsi      # 0x152b0
callq 0x21b0
movq 0x9ed0(%rip), %rax      # 0x152b8
subq 0x9eb9(%rip), %rax      # 0x152a8
movq 0x9eba(%rip), %rcx      # 0x152b0
subq 0x9ea3(%rip), %rcx      # 0x152a0
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq 0x9e8d(%rip), %rax      # 0x15298
addq %rcx, %rax
movq %rax, 0x9e83(%rip)      # 0x15298
movq -0xe8(%rbp), %rsp
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0xb431
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq -0xc8(%rbp), %rbx
movq %rbx, %rdi
callq 0xc2d2
leaq -0x108(%rbp), %r13
movq %r13, %rdi
callq 0xc2d2
cmpl $0x5, %r12d
movl %r12d, %eax
movq -0x88(%rbp), %r12
leaq -0x60(%rbp), %r15
movq -0xb0(%rbp), %r14
je 0xafb6
testl %eax, %eax
je 0xae60
testb $0x5, %al
je 0xae60
jmp 0xb52f
movq %r12, %rdi
callq 0xc004
movl $0x0, 0xc8(%r12)
movl $0x1, %edi
leaq 0x9e0d(%rip), %rsi      # 0x152b0
callq 0x21b0
movq 0x9e09(%rip), %rax      # 0x152b8
subq 0x9df2(%rip), %rax      # 0x152a8
movq 0x9df3(%rip), %rcx      # 0x152b0
subq 0x9ddc(%rip), %rcx      # 0x152a0
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq 0x9dbe(%rip), %rax      # 0x15290
addq %rcx, %rax
movq %rax, 0x9db4(%rip)      # 0x15290
leaq -0xc8(%rbp), %rbx
movq %rbx, %rdi
callq 0xc2d2
movq %r13, %rdi
callq 0xc2d2
jmp 0xae60
leaq (%rcx,%rax,8), %rcx
movq -0x90(%rbp), %rdx
movdqu (%rdx), %xmm0
movdqu 0x10(%rdx), %xmm1
movdqu %xmm1, 0x10(%rcx)
movdqu %xmm0, (%rcx)
movq 0x38(%r12), %rcx
movq -0x98(%rbp), %rdx
movq %rdx, 0x20(%rcx,%rax,8)
leaq -0x60(%rbp), %rdi
callq 0xaa52
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xb591
jmp 0xb579
movq %rax, %rbx
jmp 0xb59d
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xb57c
movq -0x48(%rbp), %rsi
incq %rsi
jmp 0xb572
jmp 0xb579
jmp 0xb579
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x2200
jmp 0xb57c
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0xb591
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq -0xc8(%rbp), %rdi
callq 0xc2d2
leaq -0x108(%rbp), %rdi
callq 0xc2d2
movq %rbx, %rdi
callq 0x2330
nop