// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x38(%rsp), %rdi
callq *%rax
movl %eax, 0x2c(%rsp)
movq 0x40(%rsp), %rax
movl 0x18(%rax), %ecx
movl 0x2c(%rsp), %eax
xorl %edx, %edx
divl %ecx
movl %edx, 0x2c(%rsp)
movl $0xffffffff, 0x28(%rsp) # imm = 0xFFFFFFFF
movq $0x0, 0x20(%rsp)
movq 0x40(%rsp), %rax
movl 0xc(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x4d7c
movq 0x40(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x20(%rsp)
jmp 0x4da7
movq 0x40(%rsp), %rax
movl 0xc(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x4da5
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
jmp 0x4da7
cmpq $0x0, 0x20(%rsp)
je 0x4db9
movq 0x20(%rsp), %rdi
callq 0x23e0
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x4(%rcx), %ecx
addq $0x10, %rcx
movq 0x40(%rsp), %rdx
movl 0x8(%rdx), %edx
addq %rdx, %rcx
movl 0x2c(%rsp), %edx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movb 0x8(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4e08
movl $0xfffffffd, 0x28(%rsp) # imm = 0xFFFFFFFD
jmp 0x4f24
movq $0x0, 0x10(%rsp)
cmpq $0x0, 0x18(%rsp)
je 0x4f1c
movq 0x18(%rsp), %rdi
addq $0x9, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
callq 0x2110
cmpl $0x0, %eax
jne 0x4f00
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
addq $0x9, %rsi
movq 0x40(%rsp), %rax
movl 0x4(%rax), %eax
addq %rax, %rsi
movq 0x40(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x21e0
cmpq $0x0, 0x10(%rsp)
je 0x4e8f
movq 0x18(%rsp), %rax
movq (%rax), %rcx
movq 0x10(%rsp), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rdi
callq 0x22e0
jmp 0x4eea
movq 0x18(%rsp), %rcx
xorl %eax, %eax
cmpq (%rcx), %rax
jne 0x4ea6
movq 0x18(%rsp), %rax
movb $0x0, 0x8(%rax)
jmp 0x4ee8
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x40(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
addq $0x10, %rdx
movq 0x40(%rsp), %rax
movl 0x8(%rax), %eax
addq %rax, %rdx
callq 0x21e0
movq 0x8(%rsp), %rdi
callq 0x22e0
jmp 0x4eea
movq 0x40(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movl $0x0, 0x28(%rsp)
jmp 0x4f24
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x4e11
movl $0xfffffffd, 0x28(%rsp) # imm = 0xFFFFFFFD
cmpq $0x0, 0x20(%rsp)
je 0x4f36
movq 0x20(%rsp), %rdi
callq 0x21c0
movl 0x28(%rsp), %eax
addq $0x48, %rsp
retq
nop