// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movq %rdx, -0x60(%rbp)
movq %rsi, %rbx
movq %rdi, %r12
movq 0x8(%rdi), %rdx
pushq $0x20
popq %r14
movq %rsi, %rdi
movq %r14, %rsi
callq 0x2040
movq %rax, -0xd8(%rbp)
movq 0x8(%r12), %rdx
movq %rbx, -0x58(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x82f2
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %r15
leaq 0xb4f7(%rip), %r14      # 0x142e0
movq %r12, -0x38(%rbp)
cmpl $0x1, 0xc8(%r12)
jne 0x8dfb
nop
jmp 0x8ded
movq 0x30(%r12), %rcx
movq -0xd8(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, %rbx
movq -0xd0(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rdx, -0x70(%rbp)
movslq 0xdc(%r12), %rcx
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
imulq $0x38, %rax, %rsi
addq 0xd0(%r12), %rsi
movq %r15, %rdi
callq 0x8a0c
movq %rbx, -0xe0(%rbp)
imulq $0x28, %rbx, %rbx
movq %r15, %r13
movq 0x38(%r12), %r15
addq %rbx, %r15
movq %r15, %rdi
movq %r14, %rsi
pushq $0x20
popq %r14
movq %r14, %rdx
callq 0x2200
testl %eax, %eax
je 0x92dc
movq %r13, %rdi
callq 0x8a22
movslq 0xdc(%r12), %rcx
movq -0x70(%rbp), %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
imulq $0x38, %rax, %rsi
addq 0xd0(%r12), %rsi
movq %r13, %rdi
callq 0x8a0c
imulq $0x28, %rbx, %rbx
movq 0x38(%r12), %r15
addq %rbx, %r15
movq %r15, %rdi
leaq 0xb427(%rip), %rsi      # 0x142e0
movq %r14, %rdx
callq 0x2200
testl %eax, %eax
je 0x92dc
movq %r13, %rdi
callq 0x8a22
xorl %eax, %eax
pushq $0x1
popq %rcx
lock
cmpxchgl %ecx, 0xc8(%r12)
movq %r13, %r15
leaq 0xb3f6(%rip), %r14      # 0x142e0
leaq -0x88(%rbp), %r13
jne 0x8ded
pushq $0x1
popq %rdi
leaq 0xb3af(%rip), %rsi      # 0x142b0
callq 0x21b0
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq -0xe0(%rbp), %rdx
pushq $0x1
popq %rbx
movl %ebx, %ecx
callq 0x97d6
movq %r13, %rdi
movq %r12, %rsi
movq -0x70(%rbp), %rdx
movl %ebx, %ecx
callq 0x97d6
movq -0xa0(%rbp), %r13
movq -0x98(%rbp), %rbx
movq %rbx, %rax
movq -0x88(%rbp), %r14
movq -0x80(%rbp), %r15
subq %r13, %rax
je 0x8f94
movq %r15, %rcx
subq %r14, %rcx
je 0x8faf
cmpq %rax, %rcx
jb 0x8f85
movq 0x8(%r13), %rdi
leaq 0xb372(%rip), %rsi      # 0x142e0
pushq $0x20
popq %rdx
callq 0x2200
leaq -0xa0(%rbp), %rcx
movq %rcx, -0x30(%rbp)
testl %eax, %eax
jne 0x8fba
movq %r15, %rbx
movq %r14, %r13
leaq -0x88(%rbp), %rax
jmp 0x8fb6
movq %r15, %rbx
movq %r14, %r13
leaq -0x88(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq %r14, %r15
jne 0x8fba
jmp 0x925d
leaq -0xa0(%rbp), %rax
movq %rax, -0x30(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
leaq -0xc0(%rbp), %r14
leaq -0xc8(%rbp), %r15
cmpq %rbx, %r13
je 0x901d
movq %r15, %rdi
movq %r13, %rsi
callq 0x9aee
movq -0xc8(%rbp), %rax
movslq 0xdc(%r12), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, -0x100(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xa41a
movq %r14, %rdi
callq 0x20a0
addq $0x28, %r13
jmp 0x8fd4
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xa5a9
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xaa1e
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0x9b02
movq %rsp, -0xe8(%rbp)
movq 0x10(%r12), %rax
movq %rsp, %r14
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq %r14, -0xf0(%rbp)
xorl %r12d, %r12d
cmpq -0x68(%rbp), %rbx
je 0x90c0
movq (%rbx), %r13
pushq $0x10
popq %rdi
callq 0x21e0
movq %rax, %r15
imulq $0x38, %r13, %rsi
movq -0x38(%rbp), %rax
addq 0xd0(%rax), %rsi
movq %r15, %rdi
callq 0x8a0c
movq %r15, (%r14)
addq $0x8, %rbx
decq %r12
addq $0x8, %r14
leaq -0xc8(%rbp), %r15
jmp 0x907e
movq -0x30(%rbp), %rax
movq (%rax), %r13
movq 0x8(%rax), %rax
movq %rax, -0xf8(%rbp)
movq -0xf0(%rbp), %r14
cmpq -0xf8(%rbp), %r13
je 0x916d
movq %r15, %rdi
movq %r13, %rsi
callq 0x9aee
imulq $0x28, -0xc8(%rbp), %rdi
movq -0x38(%rbp), %rax
addq 0x38(%rax), %rdi
movq -0xc0(%rbp), %rsi
callq 0x2260
movl %eax, -0x68(%rbp)
testl %eax, %eax
je 0x914c
xorl %r15d, %r15d
movq %r12, %rax
addq %r15, %rax
je 0x9140
movq (%r14,%r15,8), %rbx
testq %rbx, %rbx
je 0x9130
movq %rbx, %rdi
callq 0x8a22
movq %rbx, %rdi
pushq $0x10
popq %rsi
callq 0x21f0
incq %r15
jmp 0x9117
pushq $0x5
popq %rbx
leaq -0xc8(%rbp), %r15
jmp 0x914e
xorl %ebx, %ebx
leaq -0xc0(%rbp), %rdi
callq 0x20a0
addq $0x28, %r13
cmpl $0x0, -0x68(%rbp)
je 0x90d9
jmp 0x9205
movq -0x38(%rbp), %rdi
andl $0x0, 0xc8(%rdi)
movq -0x30(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
pushq $0x1
popq %r8
callq 0x9b08
xorl %r15d, %r15d
movq %r12, %rax
addq %r15, %rax
je 0x91b9
movq (%r14,%r15,8), %rbx
testq %rbx, %rbx
je 0x91a9
movq %rbx, %rdi
callq 0x8a22
movq %rbx, %rdi
pushq $0x10
popq %rsi
callq 0x21f0
incq %r15
jmp 0x9190
pushq $0x1
popq %rbx
movl %ebx, %edi
leaq 0xb0fb(%rip), %rsi      # 0x142c0
callq 0x21b0
movq 0xb0f7(%rip), %rax      # 0x142c8
subq 0xb0e0(%rip), %rax      # 0x142b8
movq 0xb0e1(%rip), %rcx      # 0x142c0
subq 0xb0ca(%rip), %rcx      # 0x142b0
addq 0xb0bb(%rip), %rax      # 0x142a8
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq %rcx, %rax
movq %rax, 0xb0aa(%rip)      # 0x142a8
leaq -0xc8(%rbp), %r15
movq -0xe8(%rbp), %rsp
leaq -0x50(%rbp), %rdi
callq 0xaa8c
leaq -0x88(%rbp), %r13
movq %r13, %rdi
callq 0x9e38
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
callq 0x9e38
cmpl $0x5, %ebx
movq -0x38(%rbp), %r12
je 0x8f0d
testl %ebx, %ebx
leaq 0xb097(%rip), %r14      # 0x142e0
je 0x8ded
testb $0x5, %bl
je 0x8ded
jmp 0x930a
movq %r12, %rdi
callq 0x9bb8
andl $0x0, 0xc8(%r12)
pushq $0x1
popq %rdi
leaq 0xb048(%rip), %rsi      # 0x142c0
callq 0x21b0
movq 0xb044(%rip), %rax      # 0x142c8
subq 0xb02d(%rip), %rax      # 0x142b8
movq 0xb02e(%rip), %rcx      # 0x142c0
subq 0xb017(%rip), %rcx      # 0x142b0
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq 0xaff9(%rip), %rax      # 0x142a0
addq %rcx, %rax
movq %rax, 0xafef(%rip)      # 0x142a0
leaq -0x88(%rbp), %rdi
callq 0x9e38
leaq -0xa0(%rbp), %rdi
callq 0x9e38
leaq -0xc8(%rbp), %r15
leaq 0xb009(%rip), %r14      # 0x142e0
jmp 0x8ded
movq -0x58(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%r15)
movups %xmm0, (%r15)
movq 0x38(%r12), %rax
movq -0x60(%rbp), %rcx
movq %rcx, 0x20(%rax,%rbx)
leaq -0xc8(%rbp), %rdi
callq 0x8a22
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x9356
jmp 0x933d
jmp 0x933d
jmp 0x933d
movq %rax, %rbx
jmp 0x9362
jmp 0x933d
jmp 0x933d
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x21f0
jmp 0x934d
movq %rax, %rbx
jmp 0x934d
movq %rax, %rbx
movq %r14, %rdi
callq 0x20a0
leaq -0x50(%rbp), %rdi
callq 0xaa8c
leaq -0x88(%rbp), %rdi
callq 0x9e38
leaq -0xa0(%rbp), %rdi
callq 0x9e38
movq %rbx, %rdi
callq 0x2340