// File: /workspace/github_c_makefile_star2_O2/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, 0x20(%rsp)
movq %rsi, %r12
callq *0x10(%rbx)
xorl %edx, %edx
divl 0x18(%rbx)
movl 0xc(%rbx), %eax
testb $0x4, %al
movl %edx, %r13d
jne 0x3edb
testb $0x8, %al
je 0x3ef1
movq 0x50(%rbx), %r15
testq %r15, %r15
je 0x3ef1
imulq $0x28, %r13, %rax
addq %rax, %r15
jmp 0x3edf
leaq 0x28(%rbx), %r15
movq %r15, %rdi
callq 0x23b0
movl $0x0, 0xc(%rsp)
jmp 0x3efa
movb $0x1, %al
xorl %r15d, %r15d
movl %eax, 0xc(%rsp)
movl 0x8(%rbx), %ecx
movl 0x4(%rbx), %r14d
movq 0x20(%rbx), %rax
movq %rcx, 0x18(%rsp)
leaq 0x10(%r14,%rcx), %rcx
imulq %rcx, %r13
cmpb $0x0, 0x8(%rax,%r13)
je 0x3f52
movq %r15, 0x10(%rsp)
addq %r13, %rax
xorl %r15d, %r15d
testq %rax, %rax
je 0x3f4d
leaq 0x9(%rax), %rbp
movq %r12, %rsi
movq %r14, %rdx
movq %rax, %r13
movq %rbp, %rdi
callq 0x2260
testl %eax, %eax
je 0x3f5b
movq (%r13), %rax
movq %r13, %r15
jmp 0x3f26
movq 0x10(%rsp), %r15
movl 0xc(%rsp), %eax
pushq $-0x3
popq %rbx
jmp 0x3fb1
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rdx
addq %r14, %rbp
movq %rbp, %rsi
callq 0x21b0
movq (%r13), %r14
testq %r15, %r15
je 0x3f81
movq %r14, (%r15)
movq %r13, %rdi
jmp 0x3f9f
testq %r14, %r14
je 0x3fce
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
movq %r13, %rdi
movq %r14, %rsi
leaq 0x10(%rax,%rcx), %rdx
callq 0x21b0
movq %r14, %rdi
callq 0x22c0
movq 0x10(%rsp), %r15
movl 0xc(%rsp), %eax
decl (%rbx)
xorl %ebx, %ebx
testb %al, %al
jne 0x3fbd
movq %r15, %rdi
callq 0x2190
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, 0x8(%r13)
jmp 0x3fa4