// File: /workspace/github_c_makefile_star2_O3/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
leaq 0x11375(%rip), %r14     # 0x183b0
movq %rdi, %rbx
movq %r14, %rdi
callq 0x23b0
leaq 0x1138b(%rip), %rdi     # 0x183d8
movq %rbx, %rsi
callq 0xc60f
movq %r14, %rdi
callq 0x2190
leaq 0x158(%rbx), %r14
movq %r14, %rdi
callq 0x23b0
movl 0x10(%rbx), %eax
testb $0x1, %al
je 0x7087
movq %r14, %rdi
callq 0x2190
movl $0x1, %edi
callq 0x2180
jmp 0x7064
orl $0x2, %eax
movq %r14, %rdi
movl %eax, 0x10(%rbx)
callq 0x2190
cmpl $0x0, 0x24(%rbx)
leaq 0x18(%rbx), %r15
je 0x70c1
xorl %ebp, %ebp
movq %rsp, %r12
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0xe7d4
movq (%rsp), %rdi
callq 0x22c0
incl %ebp
cmpl 0x24(%rbx), %ebp
jb 0x70a4
movq %r15, %rdi
callq 0xe580
leaq 0x60(%rbx), %rdi
callq 0xe580
cmpl $0x0, 0xb4(%rbx)
leaq 0xa8(%rbx), %r15
je 0x711c
xorl %ebp, %ebp
movq %rsp, %r12
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0xe7d4
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 0x70e7
movq %r15, %rdi
callq 0xe580
cmpl $0x0, 0x104(%rbx)
leaq 0xf8(%rbx), %r15
je 0x7159
xorl %ebp, %ebp
movq %rsp, %r12
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0xe7d4
movq (%rsp), %rdi
callq 0x7188
incl %ebp
cmpl 0x104(%rbx), %ebp
jb 0x7139
movq %r15, %rdi
callq 0xe580
movq %rbx, %rdi
callq 0x8a14
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