// File: /workspace/github_c_makefile_star2_O0/sharpyfox[P]citrusleaf-client/tests/loop_c/src/shash.c
// Function: shash_delete
int
shash_delete(shash *h, void *key)
{

	// 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
			// 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 $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x28(%rsp), %rdi
callq *%rax
movl %eax, 0x24(%rsp)
movq 0x30(%rsp), %rax
movl 0x18(%rax), %ecx
movl 0x24(%rsp), %eax
xorl %edx, %edx
divl %ecx
movl %edx, 0x24(%rsp)
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
movq $0x0, 0x18(%rsp)
movq 0x30(%rsp), %rax
movl 0xc(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x49f7
movq 0x30(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x18(%rsp)
jmp 0x4a22
movq 0x30(%rsp), %rax
movl 0xc(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x4a20
movq 0x30(%rsp), %rax
movq 0x50(%rax), %rax
movl 0x24(%rsp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
jmp 0x4a22
cmpq $0x0, 0x18(%rsp)
je 0x4a34
movq 0x18(%rsp), %rdi
callq 0x23e0
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x30(%rsp), %rcx
movl 0x4(%rcx), %ecx
addq $0x10, %rcx
movq 0x30(%rsp), %rdx
movl 0x8(%rdx), %edx
addq %rdx, %rcx
movl 0x24(%rsp), %edx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movb 0x8(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4a83
movl $0xfffffffd, 0x20(%rsp) # imm = 0xFFFFFFFD
jmp 0x4b75
movq $0x0, 0x8(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x4b6d
movq 0x10(%rsp), %rdi
addq $0x9, %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
callq 0x2110
cmpl $0x0, %eax
jne 0x4b51
cmpq $0x0, 0x8(%rsp)
je 0x4ae2
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rsp), %rdi
callq 0x22e0
jmp 0x4b3b
movq 0x10(%rsp), %rcx
xorl %eax, %eax
cmpq (%rcx), %rax
jne 0x4af9
movq 0x10(%rsp), %rax
movb $0x0, 0x8(%rax)
jmp 0x4b39
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
movq 0x10(%rsp), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x30(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
addq $0x10, %rdx
movq 0x30(%rsp), %rax
movl 0x8(%rax), %eax
addq %rax, %rdx
callq 0x21e0
movq (%rsp), %rdi
callq 0x22e0
jmp 0x4b3b
movq 0x30(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movl $0x0, 0x20(%rsp)
jmp 0x4b75
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x4a8c
movl $0xfffffffd, 0x20(%rsp) # imm = 0xFFFFFFFD
cmpq $0x0, 0x18(%rsp)
je 0x4b87
movq 0x18(%rsp), %rdi
callq 0x21c0
movl 0x20(%rsp), %eax
addq $0x38, %rsp
retq