// File: /workspace/github_cpp_makefile_star2_O1/chahk0129[P]hash_bench/./index/extendible_hash.h
// Function: ExtendibleHash<char [32]>::Insert(char (&) [32], long)
void ExtendibleHash<Key_t>::Insert(Key_t& key, Value_t value) {
    size_t f_hash;
    if constexpr(sizeof(Key_t) > 8)
	f_hash = hash_funcs[0](key, sizeof(Key_t), f_seed);
    else
	f_hash = hash_funcs[0](&key, sizeof(Key_t), f_seed);
    auto f_idx = (f_hash & kMask) * kNumPairPerCacheLine;

RETRY:
    auto x = (f_hash >> (8*sizeof(f_hash) - dir->depth));
    auto target = dir->_[x];

    if(!target){
	std::this_thread::yield();
	goto RETRY;
    }

    /* acquire segment exclusive lock */
    if(!target->mutex.try_lock()){
	std::this_thread::yield();
	goto RETRY;
    }

    auto target_check = (f_hash >> (8*sizeof(f_hash) - dir->depth));
    if(target != dir->_[target_check]){
	target->mutex.unlock();
	std::this_thread::yield();
	goto RETRY;
    }

    auto target_local_depth = target->local_depth;
    auto pattern = (f_hash >> (8*sizeof(f_hash) - target->local_depth));
    for(unsigned i=0; i<kNumPairPerCacheLine * kNumCacheLine; ++i){
	auto loc = (f_idx + i) % Segment<Key_t>::kNumSlot;
	if constexpr(sizeof(Key_t) > 8){
	    if((((hash_funcs[0](target->_[loc].key, sizeof(Key_t), f_seed) >> (8*sizeof(f_hash)-target_local_depth)) != pattern) ||
			(memcmp(target->_[loc].key, INVALID<Key_t>, sizeof(Key_t)) == 0))){
		memcpy(target->_[loc].key, key, sizeof(Key_t));
		memcpy(&target->_[loc].value, &value, sizeof(Value_t));
		target->mutex.unlock();
		return;
	    }
	}
	else{
	    if((((hash_funcs[0](&target->_[loc].key, sizeof(Key_t), f_seed) >> (8*sizeof(f_hash)-target_local_depth)) != pattern) ||
			(memcmp(&target->_[loc].key, &INVALID<Key_t>, sizeof(Key_t)) == 0))){
		memcpy(&target->_[loc].key, &key, sizeof(Key_t));
		memcpy(&target->_[loc].value, &value, sizeof(Value_t));
		target->mutex.unlock();
		return;
	    }
	}

    }

#ifdef S_HASH
    size_t s_hash;
    if constexpr(sizeof(Key_t) > 8)
	s_hash = hash_funcs[2](key, sizeof(Key_t), s_seed);
    else
	s_hash = hash_funcs[2](&key, sizeof(Key_t), s_seed);
    auto s_idx = (s_hash & kMask) * kNumPairPerCacheLine;

    for(unsigned i=0; i<kNumPairPerCacheLine * kNumCacheLine; ++i){
	auto loc = (s_idx + i) % Segment<Key_t>::kNumSlot;
	if constexpr(sizeof(Key_t) > 8){
	    if((((hash_funcs[0](target->_[loc].key, sizeof(Key_t), f_seed) >> (8*sizeof(f_hash)-target_local_depth)) != pattern) ||
			(memcmp(target->_[loc].key, INVALID<Key_t>, sizeof(Key_t)) == 0))){
		memcpy(target->_[loc].key, key, sizeof(Key_t));
		memcpy(&target->_[loc].value, &value, sizeof(Value_t));
		target->mutex.unlock();
		return;
	    }
	}
	else{
	    if((((hash_funcs[0](&target->_[loc].key, sizeof(Key_t), f_seed) >> (8*sizeof(f_hash)-target_local_depth)) != pattern) ||
			(memcmp(&target->_[loc].key, &INVALID<Key_t>, sizeof(Key_t)) == 0))){
		memcpy(&target->_[loc].key, &key, sizeof(Key_t));
		memcpy(&target->_[loc].value, &value, sizeof(Value_t));
		target->mutex.unlock();
		return;
	    }
	}
    }
#endif

    // COLLISION!!
    /* need to split segment but release the exclusive lock first to avoid deadlock */

    /* need to check whether the target segment has been split */
#ifdef BREAKDOWN
    clock_gettime(CLOCK_MONOTONIC, &t_start);
#endif
    Segment<Key_t>** s = target->Split();

DIR_RETRY:
    /* need to double the directory */
    if(target_local_depth == dir->depth){
	if(!dir->suspend()){
	    std::this_thread::yield();
	    goto DIR_RETRY;
	}

	x = (f_hash >> (8*sizeof(f_hash) - dir->depth));
	auto dir_old = dir;
	auto d = dir->_;
	auto _dir = new Directory<Key_t>(dir->depth+1);
	for(unsigned i = 0; i < dir->capacity; ++i){
	    if (i == x){
		_dir->_[2*i] = s[0];
		_dir->_[2*i+1] = s[1];
	    }
	    else{
		_dir->_[2*i] = d[i];
		_dir->_[2*i+1] = d[i];
	    }
	}
	dir = _dir;
#ifdef INPLACE
	s[0]->local_depth++;
	s[0]->mutex.unlock();
#endif
    }
    else{ // normal segment split
	while(!dir->lock()){
	    asm("nop");
	}

	x = (f_hash >> (8 * sizeof(f_hash) - dir->depth));
	if(dir->depth == target_local_depth + 1){
	    if(x%2 == 0){
		dir->_[x+1] = s[1];
#ifndef INPLACE
		dir->_[x] = s[0];
#endif
	    }
	    else{
		dir->_[x] = s[1];
#ifndef INPLACE
		dir->_[x-1] = s[0];
#endif
	    }	    
	    dir->unlock();
#ifdef INPLACE
	    s[0]->local_depth++;
	    /* release target segment exclusive lock */
	    s[0]->mutex.unlock();
#endif
	}
	else{
	    int stride = pow(2, dir->depth - target_local_depth);
	    auto loc = x - (x%stride);
	    for(int i=0; i<stride/2; ++i){
		dir->_[loc+stride/2+i] = s[1];
	    }
#ifndef INPLACE
	    for(int i=0; i<stride/2; ++i){
		dir->_[loc+i] = s[0];
	    }
#endif
	    dir->unlock();
#ifdef INPLACE
	    s[0]->local_depth++;
	    /* release target segment exclusive lock */
	    s[0]->mutex.unlock();
#endif
	}
    }
#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
    std::this_thread::yield();
    goto RETRY;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, 0x28(%rsp)
movq %rsi, %rax
movq %rdi, %rbp
movl $0x20, %esi
movl $0xc70697, %edx         # imm = 0xC70697
movq %rax, 0x20(%rsp)
movq %rax, %rdi
callq 0x2050
movq %rax, %r14
leaq (,%rax,4), %rax
movq %rax, 0x30(%rsp)
movq %rbp, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x8(%rbp), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
movq (%rax,%rdx,8), %r12
testq %r12, %r12
je 0x885a
leaq 0xa008(%r12), %r13
movq %r13, %rdi
callq 0x2360
cmpl $0x10, %eax
je 0x885a
movq 0x8(%rbp), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
cmpq (%rax,%rdx,8), %r12
je 0x8861
movq %r13, %rdi
callq 0x2240
callq 0x2280
jmp 0x880a
movq 0xa000(%r12), %rcx
movl $0x40, %ebx
subq %rcx, %rbx
movq %rcx, 0x18(%rsp)
negq %rcx
movq %r14, %rbp
shrq %cl, %rbp
movq $-0x1, %r15
movq 0x30(%rsp), %rax
addl %r15d, %eax
incl %eax
andl $0x3ff, %eax            # imm = 0x3FF
leaq (%rax,%rax,4), %rax
leaq (%r12,%rax,8), %r14
movl $0x20, %esi
movl $0xc70697, %edx         # imm = 0xC70697
movq %r14, %rdi
callq 0x2050
movl %ebx, %ecx
shrq %cl, %rax
cmpq %rbp, %rax
jne 0x8c0a
movdqu (%r14), %xmm0
movdqu 0x10(%r14), %xmm1
pcmpeqb 0xba10(%rip), %xmm1     # 0x142e0
pcmpeqb 0xb9f8(%rip), %xmm0     # 0x142d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x8c0a
incq %r15
cmpq $0x3ff, %r15            # imm = 0x3FF
jne 0x8886
movl $0x1, %edi
leaq 0xb99d(%rip), %rsi      # 0x142a0
callq 0x21b0
movq %r12, %rdi
callq 0x9362
movq %rax, %r13
movq 0x10(%rsp), %rbp
movq 0x8(%rbp), %rdx
movq 0x18(%rsp), %r15
cmpq 0x18(%rdx), %r15
movq 0x8(%rsp), %r14
movq $-0x1, %rbx
jne 0x8977
movq 0x8(%rdx), %rcx
testq %rcx, %rcx
js 0x8965
movq %rcx, %rax
lock
cmpxchgq %rbx, 0x8(%rdx)
jne 0x8933
jmp 0x894a
nop
testq %rax, %rax
je 0x895c
movq 0x8(%rdx), %rsi
xorq %rax, %rsi
cmpq $-0x1, %rsi
jne 0x8949
testq %rcx, %rcx
jns 0x8abc
callq 0x2280
movq 0x8(%rbp), %rdx
cmpq 0x18(%rdx), %r15
je 0x8933
jmp 0x8977
nop
movq 0x8(%rbp), %rdx
movq 0x8(%rdx), %rcx
testq %rcx, %rcx
js 0x8976
leaq 0x1(%rcx), %rsi
movq %rcx, %rax
lock
cmpxchgq %rsi, 0x8(%rdx)
jne 0x897b
testq %rcx, %rcx
js 0x8976
movq 0x8(%rbp), %rax
movq 0x18(%rax), %rdx
movl %edx, %ecx
negb %cl
movq %r14, %r12
shrq %cl, %r12
leaq 0x1(%r15), %rcx
cmpq %rcx, %rdx
jne 0x89dd
movq 0x8(%r13), %rcx
movq (%rax), %rax
testb $0x1, %r12b
jne 0x8a8f
movq %rcx, 0x8(%rax,%r12,8)
movq (%r13), %rax
movq 0x8(%rbp), %rcx
movq (%rcx), %rcx
movq %rax, (%rcx,%r12,8)
jmp 0x8aa3
subq %r15, %rdx
movq %rdx, %xmm1
punpckldq 0x4623(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x462b(%rip), %xmm1     # 0xd020
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x2190
cvttsd2si %xmm0, %ecx
movslq %ecx, %rsi
movq %r12, %rax
xorl %edx, %edx
divq %rsi
subq %rdx, %r12
movl %esi, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %eax, %eax
cmpl $0x2, %esi
jl 0x8a50
movq %rax, %rdx
xorl %esi, %esi
movq 0x8(%r13), %rdi
movq 0x8(%rbp), %r8
leaq (,%r12,8), %r9
addq (%r8), %r9
leaq (%r9,%rax,8), %r8
movq %rdi, (%r8,%rsi,8)
incq %rsi
decq %rdx
jne 0x8a2d
cmpl $0x2, %ecx
jl 0x8a76
xorl %ecx, %ecx
movq 0x8(%rbp), %rdx
leaq (,%r12,8), %rsi
addq (%rdx), %rsi
movq (%r13), %rdx
movq %rdx, (%rsi,%rcx,8)
incq %rcx
decq %rax
jne 0x8a57
movq 0x8(%rbp), %rcx
movq 0x8(%rcx), %rax
leaq -0x1(%rax), %rdx
lock
cmpxchgq %rdx, 0x8(%rcx)
jne 0x8a7a
jmp 0x8bc0
movq %rcx, (%rax,%r12,8)
movq (%r13), %rax
movq 0x8(%rbp), %rcx
movq (%rcx), %rcx
movq %rax, -0x8(%rcx,%r12,8)
movq 0x8(%rbp), %rcx
movq 0x8(%rcx), %rax
leaq -0x1(%rax), %rdx
lock
cmpxchgq %rdx, 0x8(%rcx)
jne 0x8aa7
jmp 0x8bc0
movq 0x8(%rbp), %r15
xorl %ecx, %ecx
subb 0x18(%r15), %cl
movq %r14, %rbx
shrq %cl, %rbx
movq (%r15), %r14
movl $0x20, %edi
callq 0x21f0
movq %rax, %r12
movq 0x18(%r15), %r15
incq %r15
movq $0x0, 0x8(%rax)
movq %r15, %xmm1
punpckldq 0x4518(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4520(%rip), %xmm1     # 0xd020
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x2190
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x453b(%rip), %xmm0     # 0xd060
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, 0x10(%r12)
movq %r15, 0x18(%r12)
movq %rdx, %rax
shrq $0x3d, %rax
shlq $0x3, %rdx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rdx, %rdi
callq 0x2030
movq %rax, (%r12)
movq 0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x8bb7
leaq 0x8(%r13), %rax
movl $0x1, %ecx
movl $0x1, %edx
xorl %esi, %esi
cmpq %rsi, %rbx
leaq (%r14,%rsi,8), %rsi
movq %rsi, %rdi
cmoveq %r13, %rdi
cmoveq %rax, %rsi
movq (%rdi), %rdi
movq (%r12), %r8
leal -0x1(%rdx), %r9d
movq %rdi, (%r8,%r9,8)
movq (%rsi), %rsi
movq (%r12), %rdi
movl %edx, %r8d
movq %rsi, (%rdi,%r8,8)
movl %ecx, %esi
movq 0x8(%rbp), %rdi
addl $0x2, %edx
incl %ecx
cmpq %rsi, 0x10(%rdi)
ja 0x8b77
movq %r12, 0x8(%rbp)
movq 0x8(%rsp), %r14
movl $0x1, %edi
leaq 0xb6e4(%rip), %rsi      # 0x142b0
callq 0x21b0
movq 0xb6e0(%rip), %rax      # 0x142b8
subq 0xb6c9(%rip), %rax      # 0x142a8
movq 0xb6ca(%rip), %rcx      # 0x142b0
subq 0xb6b3(%rip), %rcx      # 0x142a0
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq 0xb695(%rip), %rax      # 0x14290
addq %rcx, %rax
movq %rax, 0xb68b(%rip)      # 0x14290
jmp 0x885a
movq 0x20(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%r14)
movups %xmm0, (%r14)
movq 0x28(%rsp), %rax
movq %rax, 0x20(%r14)
movq %r13, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2240
movq %rax, %rbx
movl $0x20, %esi
movq %r12, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x2330