// File: /workspace/github_cpp_makefile_star2_O3/chahk0129[P]hash_bench/./index/extendible_hash.h
// Function: ExtendibleHash<char [32]>::Update(char (&) [32], long)
bool ExtendibleHash<Key_t>::Update(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:
    while(dir->sema < 0){
	asm("nop");
    }

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

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

    /* acquire segment shared 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;
    }

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

#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(memcmp(target->_[loc].key, key, sizeof(Key_t)) == 0){
		memcpy(&target->_[loc].value, &value, sizeof(Value_t));
		target->mutex.unlock();
		return true; 
	    }
	}
	else{
	    if(memcmp(&target->_[loc].key, &key, sizeof(Key_t)) == 0){
		memcpy(&target->_[loc].value, &value, sizeof(Value_t));
		target->mutex.unlock();
		return true; 
	    }
	}

    }
#endif

    target->mutex.unlock();
    return false; 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r13
movl $0x20, %esi
movl $0xc70697, %edx         # imm = 0xC70697
movq %r14, %rdi
callq 0x2050
movq %rax, %r15
movq 0x8(%r13), %rax
cmpq $0x0, 0x8(%rax)
jns 0x9a6c
movq 0x8(%r13), %rax
nop
cmpq $0x0, 0x8(%rax)
js 0x9a64
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r15, %rdx
shrq %cl, %rdx
movq (%rax), %rax
movq (%rax,%rdx,8), %rbx
testq %rbx, %rbx
je 0x9ab7
leaq 0xa008(%rbx), %r12
movq %r12, %rdi
callq 0x2360
cmpl $0x10, %eax
je 0x9ab7
movq 0x8(%r13), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r15, %rdx
shrq %cl, %rdx
movq (%rax), %rax
cmpq (%rax,%rdx,8), %rbx
je 0x9abe
movq %r12, %rdi
callq 0x2240
callq 0x2280
jmp 0x9a55
shlq $0x2, %r15
movl %r15d, %eax
andl $0x3fc, %eax            # imm = 0x3FC
leaq (%rax,%rax,4), %rcx
movdqu (%rbx,%rcx,8), %xmm0
movdqu 0x10(%rbx,%rcx,8), %xmm1
movdqu (%r14), %xmm2
pcmpeqb %xmm0, %xmm2
movdqu 0x10(%r14), %xmm0
pcmpeqb %xmm1, %xmm0
pand %xmm2, %xmm0
pmovmskb %xmm0, %ecx
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x9b5d
movq $-0x1, %rcx
cmpq $0x3fe, %rcx            # imm = 0x3FE
je 0x9b6e
leal (%r15,%rcx), %eax
addl $0x2, %eax
andl $0x3ff, %eax            # imm = 0x3FF
leaq (%rax,%rax,4), %rdx
movdqu (%rbx,%rdx,8), %xmm0
movdqu 0x10(%rbx,%rdx,8), %xmm1
movdqu (%r14), %xmm2
pcmpeqb %xmm0, %xmm2
movdqu 0x10(%r14), %xmm0
pcmpeqb %xmm1, %xmm0
pand %xmm2, %xmm0
pmovmskb %xmm0, %edx
incq %rcx
cmpl $0xffff, %edx           # imm = 0xFFFF
jne 0x9b03
movq %rbp, %rdx
cmpq $0x3ff, %rcx            # imm = 0x3FF
setb %bpl
jmp 0x9b63
movq %rbp, %rdx
movb $0x1, %bpl
leaq (%rax,%rax,4), %rax
movq %rdx, 0x20(%rbx,%rax,8)
jmp 0x9b70
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x2240
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop