// File: /workspace/github_cpp_makefile_star2_O3/chahk0129[P]hash_bench/./index/extendible_hash.h
// Function: ExtendibleHash<char [32]>::Delete(char (&) [32])
bool ExtendibleHash<Key_t>::Delete(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()){
	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].key, INVALID<Key_t>, sizeof(Key_t));
		target->mutex.unlock();
		return true;
	    }
	}
	else{
	    if(memcmp(&target->_[loc].key, &key, sizeof(Key_t)) == 0){
		    memcpy(&target->_[loc].key, &INVALID<Key_t>, sizeof(Key_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].key, INVALID<Key_t>, sizeof(Key_t));
		target->mutex.unlock();
		return true; 
	    }
	}
	else{
	    if(memcmp(&target->_[loc].key, &key, sizeof(Key_t)) == 0){
		    memcpy(&target->_[loc].key, &INVALID<Key_t>, sizeof(Key_t));
		target->mutex.unlock();
		return true; 
	    }
	}

    }
#endif

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

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %rbx
movq %rdi, %r12
movl $0x20, %esi
movl $0xc70697, %edx         # imm = 0xC70697
movq %rbx, %rdi
callq 0x2050
movq %rax, %r14
movq 0x8(%r12), %rax
cmpq $0x0, 0x8(%rax)
jns 0x9bc7
movq 0x8(%r12), %rax
nop
cmpq $0x0, 0x8(%rax)
js 0x9bbf
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
movq (%rax,%rdx,8), %r13
testq %r13, %r13
je 0x9c13
leaq 0xa008(%r13), %r15
movq %r15, %rdi
callq 0x2360
cmpl $0x10, %eax
je 0x9c13
movq 0x8(%r12), %rax
xorl %ecx, %ecx
subb 0x18(%rax), %cl
movq %r14, %rdx
shrq %cl, %rdx
movq (%rax), %rax
cmpq (%rax,%rdx,8), %r13
je 0x9c1a
movq %r15, %rdi
callq 0x2240
callq 0x2280
jmp 0x9bae
shlq $0x2, %r14
movl %r14d, %eax
andl $0x3fc, %eax            # imm = 0x3FC
leaq (%rax,%rax,4), %rax
movdqu (%r13,%rax,8), %xmm0
movdqu 0x10(%r13,%rax,8), %xmm1
movdqu (%rbx), %xmm2
pcmpeqb %xmm0, %xmm2
movdqu 0x10(%rbx), %xmm0
pcmpeqb %xmm1, %xmm0
pand %xmm2, %xmm0
pmovmskb %xmm0, %ecx
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x9cc1
movq $-0x1, %rax
cmpq $0x3fe, %rax            # imm = 0x3FE
je 0x9ce7
leal (%r14,%rax), %ecx
addl $0x2, %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
leaq (%rcx,%rcx,4), %rcx
movdqu (%r13,%rcx,8), %xmm0
movdqu 0x10(%r13,%rcx,8), %xmm1
movdqu (%rbx), %xmm2
pcmpeqb %xmm0, %xmm2
movdqu 0x10(%rbx), %xmm0
pcmpeqb %xmm1, %xmm0
pand %xmm2, %xmm0
pmovmskb %xmm0, %edx
incq %rax
cmpl $0xffff, %edx           # imm = 0xFFFF
jne 0x9c60
leaq (,%rcx,8), %rcx
addq %r13, %rcx
cmpq $0x3ff, %rax            # imm = 0x3FF
setb %bl
jmp 0x9cce
leaq (,%rax,8), %rcx
addq %r13, %rcx
movb $0x1, %bl
movaps 0xb60b(%rip), %xmm0     # 0x152e0
movups %xmm0, 0x10(%rcx)
movdqa 0xb5ef(%rip), %xmm0     # 0x152d0
movdqu %xmm0, (%rcx)
jmp 0x9ce9
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x2240
movl %ebx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop