// File: /workspace/github_cpp_makefile_star2_O1/chahk0129[P]hash_bench/./index/extendible_hash.h
// Function: ExtendibleHash<char [32]>::Get(char (&) [32])
char* ExtendibleHash<Key_t>::Get(Key_t& key) {
    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_shared()){
	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){
		Value_t v = target->_[loc].value;
		target->mutex.unlock();
		return (char*)v;
	    }
	}
	else{
	    if(memcmp(&target->_[loc].key, &key, sizeof(Key_t)) == 0){
		Value_t v = target->_[loc].value;
		target->mutex.unlock();
		return (char*)v;
	    }
	}
    }

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

    }
#endif

    target->mutex.unlock();
    return (char*)NONE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rax
movq %rdi, %rbx
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 %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x8(%rbx), %rax
cmpq $0x0, 0x8(%rax)
jns 0x8fe2
movq 0x8(%rbx), %rax
nop
cmpq $0x0, 0x8(%rax)
js 0x8fda
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
movq (%rax,%rdx,8), %r15
testq %r15, %r15
je 0x9036
leaq 0xa008(%r15), %r13
movq %r13, %rdi
callq 0x2160
cmpl $0x10, %eax
je 0x9036
cmpl $0xb, %eax
je 0x9036
movq 0x8(%rbx), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
cmpq (%rax,%rdx,8), %r15
setne %r12b
je 0x9048
movq %r13, %rdi
callq 0x2240
callq 0x2280
movb $0x1, %r12b
testb %r12b, %r12b
jne 0x8fcb
jmp 0x9104
movq %r13, 0x28(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x20(%rsp), %rdx
movq 0x30(%rsp), %rax
addl %ebp, %eax
andl $0x3ff, %eax            # imm = 0x3FF
leaq (%rax,%rax,4), %rax
movdqu (%r15,%rax,8), %xmm0
movdqu 0x10(%r15,%rax,8), %xmm1
movdqu (%rdx), %xmm2
pcmpeqb %xmm0, %xmm2
movdqu 0x10(%rdx), %xmm0
pcmpeqb %xmm1, %xmm0
pand %xmm2, %xmm0
pmovmskb %xmm0, %ecx
xorl %ebx, %ebx
cmpl $0xffff, %ecx           # imm = 0xFFFF
setne %r13b
jne 0x90b7
leaq (%r15,%rax,8), %rax
movq 0x20(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rdi
callq 0x2240
movq 0x20(%rsp), %rdx
movb %r13b, %bl
testl %ebx, %ebx
je 0x90dc
cmpq $0x3ff, %rbp            # imm = 0x3FF
leaq 0x1(%rbp), %rax
setae %r14b
movq %rax, %rbp
cmpq $0x400, %rax            # imm = 0x400
jne 0x9057
testb $0x1, %r14b
je 0x90f5
movq 0x28(%rsp), %rdi
callq 0x2240
movq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
jmp 0x903e
movq 0x18(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq