// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
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 %r12, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x8(%r12), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
movq (%rax,%rdx,8), %r13
testq %r13, %r13
je 0x962f
leaq 0xa008(%r13), %rbp
movq %rbp, %rdi
callq 0x2360
cmpl $0x10, %eax
je 0x962f
movq 0x8(%r12), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
cmpq (%rax,%rdx,8), %r13
je 0x9636
movq %rbp, %rdi
callq 0x2240
callq 0x2280
jmp 0x95de
movq 0xa000(%r13), %rcx
movl $0x40, %ebx
subq %rcx, %rbx
movq %rcx, 0x18(%rsp)
negq %rcx
movq %r14, %r12
shrq %cl, %r12
xorl %r15d, %r15d
movq 0x30(%rsp), %rax
addl %r15d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
leaq (%rax,%rax,4), %rax
leaq (,%rax,8), %r14
addq %r13, %r14
movl $0x20, %esi
movl $0xc70697, %edx         # imm = 0xC70697
movq %r14, %rdi
callq 0x2050
movl %ebx, %ecx
shrq %cl, %rax
cmpq %r12, %rax
jne 0x99df
movdqu (%r14), %xmm0
movdqu 0x10(%r14), %xmm1
pcmpeqb 0xbc3b(%rip), %xmm1     # 0x152e0
pcmpeqb 0xbc23(%rip), %xmm0     # 0x152d0
pand %xmm1, %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x99df
incq %r15
cmpq $0x400, %r15            # imm = 0x400
jne 0x9656
movl $0x1, %edi
leaq 0xbbc8(%rip), %rsi      # 0x152a0
callq 0x21b0
movq %r13, %rdi
callq 0xa05a
movq %rax, %r13
movq 0x10(%rsp), %r12
movq 0x8(%r12), %rcx
movq 0x18(%rsp), %rbx
cmpq 0x18(%rcx), %rbx
movq 0x8(%rsp), %r14
jne 0x9734
movq $-0x1, %rdx
movq 0x8(%rcx), %rax
testq %rax, %rax
js 0x971c
lock
cmpxchgq %rdx, 0x8(%rcx)
jne 0x9709
jmp 0x9794
callq 0x2280
movq 0x8(%r12), %rcx
cmpq 0x18(%rcx), %rbx
je 0x9702
jmp 0x9734
nop
movq 0x8(%r12), %rcx
movq 0x8(%rcx), %rax
testq %rax, %rax
js 0x972e
leaq 0x1(%rax), %rdx
lock
cmpxchgq %rdx, 0x8(%rcx)
jne 0x9734
movq 0x8(%r12), %rax
movq 0x18(%rax), %rdx
movl %edx, %ecx
negb %cl
movq %r14, %rbp
shrq %cl, %rbp
leaq 0x1(%rbx), %rcx
cmpq %rcx, %rdx
jne 0x98ba
movq 0x8(%r13), %rcx
movq (%rax), %rax
testb $0x1, %bpl
jne 0x996b
movq %rcx, 0x8(%rax,%rbp,8)
movq (%r13), %rax
movq 0x8(%r12), %rcx
movq (%rcx), %rcx
movq %rax, (%rcx,%rbp,8)
jmp 0x9980
testq %rax, %rax
je 0x97a9
movq 0x8(%rcx), %rdx
xorq %rax, %rdx
cmpq $-0x1, %rdx
je 0x97a9
nop
jmp 0x9799
movq 0x8(%r12), %r15
xorl %ecx, %ecx
subb 0x18(%r15), %cl
movq %r14, %rbx
shrq %cl, %rbx
movq (%r15), %r14
movl $0x20, %edi
callq 0x21f0
movq %rax, %rbp
movq 0x18(%r15), %r15
incq %r15
movq $0x0, 0x8(%rax)
movq %r15, %xmm1
punpckldq 0x485a(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4862(%rip), %xmm1     # 0xe050
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 0x488d(%rip), %xmm0     # 0xe0a0
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, 0x10(%rbp)
movq %r15, 0x18(%rbp)
movq %rdx, %rax
shrq $0x3d, %rax
shlq $0x3, %rdx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rdx, %rdi
callq 0x2030
movq %rax, (%rbp)
movq 0x8(%r12), %rax
cmpq $0x0, 0x10(%rax)
je 0x98ab
movl $0x1, %eax
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl %ecx, %edi
cmpq %rsi, %rbx
jne 0x987c
movq (%r13), %rsi
movq (%rbp), %r9
leal (%rdx,%rdx), %r8d
movq %rsi, (%r9,%rdi,8)
movq 0x8(%r13), %rsi
jmp 0x988b
movq (%r14,%rsi,8), %rsi
movq (%rbp), %r8
movq %rsi, (%r8,%rdi,8)
movl %ecx, %r8d
movq (%rbp), %rdi
movl %r8d, %r8d
movq %rsi, 0x8(%rdi,%r8,8)
incl %edx
movl %eax, %esi
movq 0x8(%r12), %rdi
incl %eax
addl $0x2, %ecx
cmpq %rsi, 0x10(%rdi)
ja 0x985f
movq %rbp, 0x8(%r12)
movq 0x8(%rsp), %r14
jmp 0x9995
subq %rbx, %rdx
movq %rdx, %xmm1
punpckldq 0x4776(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x477e(%rip), %xmm1     # 0xe050
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x2190
cvttsd2si %xmm0, %ecx
movslq %ecx, %rsi
movq %rbp, %rax
xorl %edx, %edx
divq %rsi
cmpl $0x2, %ecx
jl 0x9954
subq %rdx, %rbp
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %eax, %eax
movq 0x8(%r13), %rsi
movq %rax, %rdi
xorl %r8d, %r8d
movq 0x8(%r12), %r9
leaq (,%rbp,8), %rdx
movq (%r9), %r9
addq %rdx, %r9
leaq (%r9,%rax,8), %r9
movq %rsi, (%r9,%r8,8)
incq %r8
decq %rdi
jne 0x990f
cmpl $0x2, %ecx
jl 0x9954
movq (%r13), %rcx
xorl %esi, %esi
movq 0x8(%r12), %rdi
movq (%rdi), %rdi
addq %rdx, %rdi
movq %rcx, (%rdi,%rsi,8)
incq %rsi
decq %rax
jne 0x993d
movq 0x8(%r12), %rcx
movq 0x8(%rcx), %rax
leaq -0x1(%rax), %rdx
lock
cmpxchgq %rdx, 0x8(%rcx)
jne 0x9959
jmp 0x9995
movq %rcx, (%rax,%rbp,8)
movq (%r13), %rax
movq 0x8(%r12), %rcx
movq (%rcx), %rcx
movq %rax, -0x8(%rcx,%rbp,8)
movq 0x8(%r12), %rcx
movq 0x8(%rcx), %rax
leaq -0x1(%rax), %rdx
lock
cmpxchgq %rdx, 0x8(%rcx)
jne 0x9985
movl $0x1, %edi
leaq 0xb90f(%rip), %rsi      # 0x152b0
callq 0x21b0
movq 0xb90b(%rip), %rax      # 0x152b8
subq 0xb8f4(%rip), %rax      # 0x152a8
movq 0xb8f5(%rip), %rcx      # 0x152b0
subq 0xb8de(%rip), %rcx      # 0x152a0
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq 0xb8c0(%rip), %rax      # 0x15290
addq %rcx, %rax
movq %rax, 0xb8b6(%rip)      # 0x15290
jmp 0x962f
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 %rbp, %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 %rbp, %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x2330
nop