// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x3e10d(%rip), %rax     # 0x5a480
movq -0x18(%rbp), %rdi
movl $0x20, %esi
movl $0xc70697, %edx         # imm = 0xC70697
callq *%rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
andq $0xff, %rax
shlq $0x2, %rax
movq %rax, -0x30(%rbp)
jmp 0x1c39b
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
cmpq $0x0, 0x8(%rax)
jge 0x1c3ad
nop
jmp 0x1c39b
movq -0x60(%rbp), %rax
movq -0x28(%rbp), %rdx
movq 0x8(%rax), %rsi
movl $0x40, %ecx
subq 0x18(%rsi), %rcx
shrq %cl, %rdx
movq %rdx, %rcx
movq %rcx, -0x38(%rbp)
movq 0x8(%rax), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x1c3ed
callq 0x1cbf0
jmp 0x1c399
movq -0x40(%rbp), %rdi
addq $0xa008, %rdi           # imm = 0xA008
callq 0x1cc10
testb $0x1, %al
jne 0x1c408
callq 0x1cbf0
jmp 0x1c399
movq -0x60(%rbp), %rcx
movq -0x28(%rbp), %rax
movq 0x8(%rcx), %rdx
movl $0x40, %ecx
subq 0x18(%rdx), %rcx
shrq %cl, %rax
movq -0x60(%rbp), %rcx
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
movq -0x48(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
je 0x1c457
movq -0x40(%rbp), %rdi
addq $0xa008, %rdi           # imm = 0xA008
callq 0x1cc30
callq 0x1cbf0
jmp 0x1c399
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpq $0x400, %rax            # imm = 0x400
jae 0x1c4d3
movq -0x30(%rbp), %rax
movl -0x4c(%rbp), %ecx
addq %rcx, %rax
andq $0x3ff, %rax            # imm = 0x3FF
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
imulq $0x28, -0x58(%rbp), %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movl $0x20, %edx
callq 0x6190
cmpl $0x0, %eax
jne 0x1c4c6
movq -0x40(%rbp), %rax
imulq $0x28, -0x58(%rbp), %rcx
addq %rcx, %rax
movq -0x20(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rdi
addq $0xa008, %rdi           # imm = 0xA008
callq 0x1cc30
movb $0x1, -0x1(%rbp)
jmp 0x1c4e7
jmp 0x1c4c8
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1c45e
movq -0x40(%rbp), %rdi
addq $0xa008, %rdi           # imm = 0xA008
callq 0x1cc30
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)