// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
leaq 0x15ce0(%rip), %rdi     # 0x203c0
callq 0x23e0
movq 0x40(%rsp), %rsi
leaq 0x15cf7(%rip), %rdi     # 0x203e8
callq 0x13580
leaq 0x15cc3(%rip), %rdi     # 0x203c0
callq 0x21c0
movq 0x40(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x23e0
movq 0x40(%rsp), %rax
movl 0x10(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xa740
movq 0x40(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x21c0
movl $0x1, %edi
callq 0x21b0
jmp 0xa702
movq 0x40(%rsp), %rax
movl 0x10(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0x10(%rax)
movq 0x40(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x21c0
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
callq 0x9da0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jae 0xa7b5
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movl 0x3c(%rsp), %esi
callq 0x9db0
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
callq 0x22e0
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0xa767
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
callq 0x16e10
movq 0x40(%rsp), %rdi
addq $0x60, %rdi
callq 0x16e10
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x40(%rsp), %rdi
addq $0xa8, %rdi
callq 0x9da0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jae 0xa848
movq 0x40(%rsp), %rdi
addq $0xa8, %rdi
movl 0x2c(%rsp), %esi
callq 0x9db0
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x22e0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x22e0
movq 0x20(%rsp), %rdi
callq 0x22e0
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xa7d9
movq 0x40(%rsp), %rdi
addq $0xa8, %rdi
callq 0x16e10
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x40(%rsp), %rdi
addq $0xf8, %rdi
callq 0x9da0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jae 0xa8b5
movq 0x40(%rsp), %rdi
addq $0xf8, %rdi
movl 0x1c(%rsp), %esi
callq 0x9db0
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
callq 0xa8f0
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xa861
movq 0x40(%rsp), %rdi
addq $0xf8, %rdi
callq 0x16e10
movq 0x40(%rsp), %rdi
callq 0xd100
movq 0x40(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x22d0
movq 0x40(%rsp), %rdi
callq 0x22e0
addq $0x48, %rsp
retq