// File: /workspace/github_c_makefile_star2_O3/sharpyfox[P]citrusleaf-client/tests/loop_c/src/shash.c
// Function: shash_get_and_delete
int
shash_get_and_delete(shash *h, void *key, void *value)
{
	// Calculate hash
	uint hash = h->h_fn(key);
	hash %= h->table_len;
	int rv = SHASH_ERR;

	pthread_mutex_t		*l = 0;
	if (h->flags & SHASH_CR_MT_BIGLOCK) {
		l = &h->biglock;
	}
	else if (h->flags & SHASH_CR_MT_MANYLOCK) {
		l = & (h->lock_table[hash]);
	}
	if (l)     pthread_mutex_lock( l );
		
	shash_elem *e = (shash_elem *) ( ((uint8_t *)h->table) + (SHASH_ELEM_SZ(h) * hash));	

	// If bucket empty, def can't delete
	if ( e->in_use == false ) {
		rv = SHASH_ERR_NOTFOUND;
		goto Out;
	}

	shash_elem *e_prev = 0;

	// Look for teh element and destroy if found
	while (e) {
		if ( memcmp(SHASH_ELEM_KEY_PTR(h, e), key, h->key_len) == 0) {
		
			// Found it - check length of buffer and copy to destination
			memcpy(value, SHASH_ELEM_VALUE_PTR(h, e), h->value_len);

			
			// patchup pointers & free element if not head
			if (e_prev) {
				e_prev->next = e->next;
				free (e);
			}
			// am at head - more complicated
			else {
				// at head with no next - easy peasy!
				if (0 == e->next) {
					e->in_use = false;
				}
				// at head with a next - more complicated
				else {
					shash_elem *_t = e->next;
					memcpy(e, e->next, SHASH_ELEM_SZ(h) );
					free(_t);
				}
			}
			h->elements--;
			rv = SHASH_OK;
			goto Out;

		}
		e_prev = e;
		e = e->next;
	}
	rv = SHASH_ERR_NOTFOUND;

Out:
	if (l)
		pthread_mutex_unlock( l );
		
	return(rv);	
	

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq %rsi, %rdi
movq %rdx, %r14
movq %rsi, %r12
callq *0x10(%rbx)
xorl %edx, %edx
divl 0x18(%rbx)
movl 0xc(%rbx), %eax
movq %r14, 0x20(%rsp)
testb $0x4, %al
movl %edx, %ebp
jne 0x41f4
movb $0x1, %dl
testb $0x8, %al
je 0x4204
movq 0x50(%rbx), %rax
testq %rax, %rax
je 0x4204
leaq (%rbp,%rbp,4), %rcx
leaq (%rax,%rcx,8), %r13
jmp 0x41f8
leaq 0x28(%rbx), %r13
movq %r13, %rdi
callq 0x23b0
xorl %edx, %edx
jmp 0x4207
xorl %r13d, %r13d
movl 0x4(%rbx), %r15d
movl 0x8(%rbx), %esi
movq 0x20(%rbx), %r14
leaq 0x10(%r15,%rsi), %rax
imulq %rax, %rbp
cmpb $0x0, 0x8(%r14,%rbp)
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jne 0x42fa
addq %rbp, %r14
movq %rsi, 0x18(%rsp)
movl %edx, 0xc(%rsp)
movq %r12, %rsi
movq %r15, %rdx
movq %r13, 0x10(%rsp)
leaq 0x9(%r14), %rdi
callq 0x2260
testl %eax, %eax
je 0x4282
movq %r14, %r13
movq (%r13), %r14
testq %r14, %r14
je 0x42b3
leaq 0x9(%r14), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2260
movq %r13, %rbp
testl %eax, %eax
movq %r14, %r13
jne 0x425e
jmp 0x4284
xorl %ebp, %ebp
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rdx
leaq 0x9(%r14,%r15), %rsi
callq 0x21b0
movq (%r14), %r15
testq %rbp, %rbp
je 0x42c3
movq %r14, %rdi
movq %r15, (%rbp)
callq 0x22c0
movq 0x10(%rsp), %r13
jmp 0x42f2
movq 0x10(%rsp), %r13
movl 0xc(%rsp), %edx
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x42fa
movq 0x10(%rsp), %r13
testq %r15, %r15
je 0x42ed
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
movq %r14, %rdi
movq %r15, %rsi
leaq 0x10(%rax,%rcx), %rdx
callq 0x21b0
movq %r15, %rdi
callq 0x22c0
jmp 0x42f2
movb $0x0, 0x8(%r14)
decl (%rbx)
movl 0xc(%rsp), %edx
xorl %eax, %eax
testb %dl, %dl
jne 0x430a
movq %r13, %rdi
movl %eax, %ebx
callq 0x2190
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq