// File: /workspace/github_cpp_makefile_star2_O1/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
subq $0x38, %rsp
movq %rdx, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movl $0x20, %esi
movl $0xc70697, %edx         # imm = 0xC70697
movq %r14, %rdi
callq 0x2050
movq %rax, %r15
leaq (,%rax,4), %rax
movq %rax, 0x30(%rsp)
movl %eax, 0xc(%rsp)
movq %rbx, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq 0x8(%rbx), %rax
cmpq $0x0, 0x8(%rax)
jns 0x8cb6
movq 0x8(%rbx), %rax
nop
cmpq $0x0, 0x8(%rax)
js 0x8cae
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r15, %rdx
shrq %cl, %rdx
movq (%rax), %rax
movq (%rax,%rdx,8), %r12
testq %r12, %r12
je 0x8d09
leaq 0xa008(%r12), %rdi
movq %rdi, 0x10(%rsp)
callq 0x2360
cmpl $0x10, %eax
je 0x8d09
movq 0x8(%rbx), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r15, %rdx
shrq %cl, %rdx
movq (%rax), %rax
cmpq (%rax,%rdx,8), %r12
setne %al
je 0x8d19
movq 0x10(%rsp), %rdi
callq 0x2240
callq 0x2280
movb $0x1, %al
testb %al, %al
jne 0x8c9f
jmp 0x8dd4
movb %al, 0xb(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x30(%rsp), %rax
addl %r15d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
leaq (%rax,%rax,4), %rax
movdqu (%r12,%rax,8), %xmm0
movdqu 0x10(%r12,%rax,8), %xmm1
movdqu (%r14), %xmm2
pcmpeqb %xmm0, %xmm2
movdqu 0x10(%r14), %xmm0
pcmpeqb %xmm1, %xmm0
pand %xmm2, %xmm0
pmovmskb %xmm0, %ecx
xorl %ebp, %ebp
cmpl $0xffff, %ecx           # imm = 0xFFFF
setne %r13b
jne 0x8d86
leaq (%r12,%rax,8), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x10(%rsp), %rdi
callq 0x2240
movb $0x1, %al
movl %eax, 0xc(%rsp)
movb %r13b, %bpl
testl %ebp, %ebp
je 0x8daa
cmpq $0x3ff, %r15            # imm = 0x3FF
leaq 0x1(%r15), %rax
setae %bl
movq %rax, %r15
cmpq $0x400, %rax            # imm = 0x400
jne 0x8d22
testb $0x1, %bl
je 0x8dc1
movq 0x10(%rsp), %rdi
callq 0x2240
movl $0x0, 0xc(%rsp)
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r15
movb 0xb(%rsp), %al
jmp 0x8d10
movl 0xc(%rsp), %eax
andb $0x1, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop