// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, %rdi
callq *0x10(%rbx)
xorl %edx, %edx
divl 0x18(%rbx)
movl 0xc(%rbx), %eax
testb $0x4, %al
movl %edx, %ebp
movq %r14, 0x10(%rsp)
jne 0x41f0
testb $0x8, %al
jne 0x41f6
xorl %r13d, %r13d
jmp 0x4203
leaq 0x28(%rbx), %r13
jmp 0x4203
leaq (%rbp,%rbp,4), %r13
shlq $0x3, %r13
addq 0x50(%rbx), %r13
testq %r13, %r13
je 0x4210
movq %r13, %rdi
callq 0x23b0
movq 0x20(%rbx), %r14
movl 0x4(%rbx), %r15d
movl 0x8(%rbx), %edx
leaq 0x10(%r15,%rdx), %rax
imulq %rax, %rbp
cmpb $0x0, 0x8(%r14,%rbp)
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x42b1
movq %rdx, 0x8(%rsp)
movq %r13, (%rsp)
addq %rbp, %r14
leaq 0x9(%r14), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2260
testl %eax, %eax
je 0x427d
movq %r14, %r13
movq (%r13), %r14
testq %r14, %r14
je 0x42ad
leaq 0x9(%r14), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2260
testl %eax, %eax
movq %r13, %rbp
movq %r14, %r13
jne 0x4259
jmp 0x427f
xorl %ebp, %ebp
leaq 0x9(%r14,%r15), %rsi
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rdx
callq 0x21b0
testq %rbp, %rbp
movq (%r14), %r15
je 0x42b8
movq %r15, (%rbp)
movq %r14, %rdi
callq 0x22c0
movq (%rsp), %r13
jmp 0x42e6
movq (%rsp), %r13
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
jmp 0x42ea
testq %r15, %r15
movq (%rsp), %r13
je 0x42e1
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
leaq 0x10(%rax,%rcx), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x21b0
movq %r15, %rdi
callq 0x22c0
jmp 0x42e6
movb $0x0, 0x8(%r14)
decl (%rbx)
xorl %ebx, %ebx
testq %r13, %r13
je 0x42f7
movq %r13, %rdi
callq 0x2190
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq