// File: /workspace/github_c_makefile_star2_O1/sharpyfox[P]citrusleaf-client/src/cl_cluster.c
// Function: citrusleaf_cluster_destroy
void
citrusleaf_cluster_destroy(cl_cluster *asc)
{
	// First remove the entry from global linked list of clusters
	// so that the tender function will not look into it.
	// As there is no destruction function specified in cf_ll_init()
	// the element will not freed in cf_ll_delete. It will just be 
	// removed from the linked list. So we need to free it later in
	// this function
	pthread_mutex_lock(&cluster_ll_LOCK);
	cf_ll_delete( &cluster_ll , (cf_ll_element *) asc);
	pthread_mutex_unlock(&cluster_ll_LOCK);

retry:
	pthread_mutex_lock(&asc->LOCK);
	if (asc->state & CLS_TENDER_RUNNING) {
		//If tender process is active, we cannot destroy at the moment
		pthread_mutex_unlock(&asc->LOCK);
		sleep(1);
		goto retry;
	}
	asc->state |= CLS_FREED;
	pthread_mutex_unlock(&asc->LOCK);

	for (uint i=0;i<cf_vector_size(&asc->host_str_v);i++) {
		char * host_str = cf_vector_pointer_get(&asc->host_str_v, i);
		free(host_str);
	}
	cf_vector_destroy(&asc->host_str_v);
	cf_vector_destroy(&asc->host_port_v);
	
	for (uint i=0;i<cf_vector_size(&asc->host_addr_map_v);i++) {
		cl_addrmap * addr_map_str = cf_vector_pointer_get(&asc->host_addr_map_v, i);
		free(addr_map_str->orig);
		free(addr_map_str->alt);
		free(addr_map_str);
	}
	cf_vector_destroy(&asc->host_addr_map_v);

	for (uint i=0;i<cf_vector_size(&asc->node_v);i++) {
		cl_cluster_node *cn = cf_vector_pointer_get(&asc->node_v, i);
		cl_cluster_node_release(cn);
	}
	cf_vector_destroy(&asc->node_v);
	
	cl_partition_table_destroy_all(asc);
	
	pthread_mutex_destroy(&asc->LOCK);
	free(asc);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x12238(%rip), %r14     # 0x193b0
movq %r14, %rdi
callq 0x23b0
leaq 0x12251(%rip), %rdi     # 0x193d8
movq %rbx, %rsi
callq 0xca05
movq %r14, %rdi
callq 0x2190
leaq 0x158(%rbx), %r14
movq %r14, %rdi
callq 0x23b0
movl 0x10(%rbx), %eax
testb $0x1, %al
je 0x71c1
movq %r14, %rdi
callq 0x2190
movl $0x1, %edi
callq 0x2180
jmp 0x719e
orl $0x2, %eax
movl %eax, 0x10(%rbx)
movq %r14, %rdi
callq 0x2190
leaq 0x18(%rbx), %r15
cmpl $0x0, 0x24(%rbx)
je 0x71fb
xorl %ebp, %ebp
movq %rsp, %r12
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0xec6f
movq (%rsp), %rdi
callq 0x22c0
incl %ebp
cmpl 0x24(%rbx), %ebp
jb 0x71de
movq %r15, %rdi
callq 0xea0d
leaq 0x60(%rbx), %rdi
callq 0xea0d
leaq 0xa8(%rbx), %r15
cmpl $0x0, 0xb4(%rbx)
je 0x7256
xorl %ebp, %ebp
movq %rsp, %r12
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0xec6f
movq (%rsp), %r13
movq (%r13), %rdi
callq 0x22c0
movq 0x8(%r13), %rdi
callq 0x22c0
movq %r13, %rdi
callq 0x22c0
incl %ebp
cmpl 0xb4(%rbx), %ebp
jb 0x7221
movq %r15, %rdi
callq 0xea0d
leaq 0xf8(%rbx), %r15
cmpl $0x0, 0x104(%rbx)
je 0x7293
xorl %ebp, %ebp
movq %rsp, %r12
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0xec6f
movq (%rsp), %rdi
callq 0x72c2
incl %ebp
cmpl 0x104(%rbx), %ebp
jb 0x7273
movq %r15, %rdi
callq 0xea0d
movq %rbx, %rdi
callq 0x8c6e
movq %r14, %rdi
callq 0x22b0
movq %rbx, %rdi
callq 0x22c0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq