// File: /workspace/github_cpp_makefile_star2_O2/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, 0x18(%rsp)
movq %rsi, %rax
movq %rdi, %rbp
pushq $0x20
popq %rsi
movl $0xc70697, %edx         # imm = 0xC70697
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x2040
movq %rax, %r14
leaq (,%rax,4), %rax
movq %rax, 0x30(%rsp)
pushq $0x40
popq %r15
movq %rbp, 0x8(%rsp)
movq %r14, (%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), %rbx
testq %rbx, %rbx
je 0x795d
leaq 0xa008(%rbx), %r12
movq %r12, %rdi
callq 0x839e
testb %al, %al
je 0x795d
movq 0x8(%rbp), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
cmpq (%rax,%rdx,8), %rbx
je 0x7964
movq %r12, %rdi
callq 0x2240
callq 0x2290
jmp 0x790f
movq %r12, 0x28(%rsp)
movq 0xa000(%rbx), %rcx
movq %r15, %rbp
subq %rcx, %rbp
movq %rcx, 0x20(%rsp)
negq %rcx
shrq %cl, %r14
xorl %r15d, %r15d
cmpq $0x400, %r15            # imm = 0x400
je 0x79e1
movq 0x30(%rsp), %rax
addl %r15d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
imulq $0x28, %rax, %r12
addq %rbx, %r12
movl $0xc70697, %edx         # imm = 0xC70697
movq %r12, %rdi
pushq $0x20
popq %r13
movq %r13, %rsi
callq 0x2040
movl %ebp, %ecx
shrq %cl, %rax
cmpq %r14, %rax
jne 0x7c05
movq %r12, %rdi
leaq 0xc913(%rip), %rsi      # 0x142e0
movq %r13, %rdx
callq 0x2200
incq %r15
testl %eax, %eax
jne 0x7984
jmp 0x7c05
pushq $0x1
popq %rdi
leaq 0xc8c5(%rip), %rsi      # 0x142b0
callq 0x21b0
movq %rbx, %rdi
callq 0x814e
movq %rax, %rbx
movq 0x8(%rsp), %rbp
pushq $0x20
popq %r13
movq (%rsp), %r14
movq 0x20(%rsp), %r15
movq 0x8(%rbp), %rdi
cmpq 0x18(%rdi), %r15
jne 0x7a2c
callq 0x828c
testb %al, %al
jne 0x7a7d
callq 0x2290
jmp 0x7a0d
nop
movq 0x8(%rbp), %rdi
callq 0x82bc
testb %al, %al
je 0x7a27
movq 0x8(%rbp), %rax
movq 0x18(%rax), %rdx
movl %edx, %ecx
negb %cl
movq %r14, %r13
shrq %cl, %r13
leaq 0x1(%r15), %rcx
cmpq %rcx, %rdx
jne 0x7aff
movq 0x8(%rbx), %rcx
movq (%rax), %rax
testb $0x1, %r13b
jne 0x7b9d
movq %rcx, 0x8(%rax,%r13,8)
movq (%rbx), %rax
movq 0x8(%rbp), %rcx
movq (%rcx), %rcx
movq %rax, (%rcx,%r13,8)
jmp 0x7bb0
movq 0x8(%rbp), %r15
xorl %ecx, %ecx
subb 0x18(%r15), %cl
shrq %cl, %r14
movq (%r15), %r12
movq %r13, %rdi
callq 0x21e0
movq %rax, %r13
movq 0x18(%r15), %rsi
incq %rsi
movq %rax, %rdi
callq 0x7800
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %ecx
movq 0x8(%rbp), %rdx
cmpq %rcx, 0x10(%rdx)
jbe 0x7af2
movl %eax, %esi
cmpq %rcx, %r14
jne 0x7ad1
movq (%rbx), %rdx
movq (%r13), %rdi
movq %rdx, (%rdi,%rsi,8)
movq 0x8(%rbx), %rdx
movl %eax, %edi
jmp 0x7ae0
movq (%r12,%rcx,8), %rdx
movq (%r13), %r8
leal (%rcx,%rcx), %edi
movq %rdx, (%r8,%rsi,8)
movq (%r13), %rsi
movl %edi, %edi
movq %rdx, 0x8(%rsi,%rdi,8)
incl %ecx
addl $0x2, %eax
jmp 0x7aab
movq %r13, 0x8(%rbp)
movq (%rsp), %r14
jmp 0x7bb9
subq %r15, %rdx
movq %rdx, %xmm1
punpckldq 0x4501(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4509(%rip), %xmm1     # 0xc020
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x2190
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
movq %r13, %rax
xorl %edx, %edx
divq %rcx
subq %rdx, %r13
movl %ecx, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movslq %eax, %rcx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movq %rax, %rdx
xorl %esi, %esi
subq $0x1, %rdx
jb 0x7b7a
movq 0x8(%rbp), %rdi
leaq (,%r13,8), %r8
addq (%rdi), %r8
movq 0x8(%rbx), %rdi
leaq (%r8,%rcx,8), %r8
movq %rdi, (%r8,%rsi,8)
incq %rsi
jmp 0x7b54
xorl %ecx, %ecx
subq $0x1, %rax
jb 0x7bb0
movq (%rbx), %rdx
movq 0x8(%rbp), %rsi
leaq (,%r13,8), %rdi
addq (%rsi), %rdi
movq %rdx, (%rdi,%rcx,8)
incq %rcx
jmp 0x7b7c
movq %rcx, (%rax,%r13,8)
movq (%rbx), %rax
movq 0x8(%rbp), %rcx
movq (%rcx), %rcx
movq %rax, -0x8(%rcx,%r13,8)
movq 0x8(%rbp), %rdi
callq 0x82dc
pushq $0x40
popq %r15
pushq $0x1
popq %rdi
leaq 0xc6f9(%rip), %rsi      # 0x142c0
callq 0x21b0
movq 0xc6f5(%rip), %rax      # 0x142c8
subq 0xc6de(%rip), %rax      # 0x142b8
movq 0xc6df(%rip), %rcx      # 0x142c0
subq 0xc6c8(%rip), %rcx      # 0x142b0
imulq $0x3b9aca00, %rcx, %rcx # imm = 0x3B9ACA00
addq 0xc6aa(%rip), %rax      # 0x142a0
addq %rcx, %rax
movq %rax, 0xc6a0(%rip)      # 0x142a0
jmp 0x795d
movq 0x10(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%r12)
movups %xmm0, (%r12)
movq 0x18(%rsp), %rax
movq %rax, 0x20(%r12)
movq 0x28(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2240
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r13, %rdi
callq 0x21f0
movq %rbx, %rdi
callq 0x2340