// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/cl_cluster.c
// Function: citrusleaf_cluster_get_or_create
cl_cluster *
citrusleaf_cluster_get_or_create(char *host, short port, int timeout_ms)
{
	if (! g_clust_initialized )    return(0);
#ifdef DEBUG
        cf_debug("get or create for host %s:%d",host, (int)port);
#endif
	cl_cluster *asc = NULL;

	// Check if the host and port exists in the linked list of hosts
	pthread_mutex_lock(&cluster_ll_LOCK);
	cf_ll_element *cur_element = cf_ll_get_head(&cluster_ll);
	char *hostp;
	short portp;
	while(cur_element) {
		asc = (cl_cluster *) cur_element;
		pthread_mutex_lock(&asc->LOCK);
		for (uint32_t i=0; i<cf_vector_size(&asc->host_str_v); i++) {
			hostp = (char*) cf_vector_pointer_get(&asc->host_str_v, i);
			portp = (int ) cf_vector_integer_get(&asc->host_port_v, i);
			if ((strncmp(host,hostp,strlen(host)+1) == 0) && (port == portp)) {
				// Found the cluster object.
				// Increment the reference count
				#ifdef DEBUG
				cf_debug("host already added on a cluster object. Increment ref_count (%d) and returning pointer - %p", asc->ref_count, asc);
				#endif
				asc->ref_count++;
				pthread_mutex_unlock(&asc->LOCK);
				pthread_mutex_unlock(&cluster_ll_LOCK);
				return(asc);
			}
		}
		pthread_mutex_unlock(&asc->LOCK);
		cur_element = cf_ll_get_next(cur_element);
	}
	pthread_mutex_unlock(&cluster_ll_LOCK);

	// Cluster object for this host does not exist. Create new.
	asc = citrusleaf_cluster_create();
	if (!asc) {
		cf_error("get_or_create - could not create cluster");
		return(0);
	}

	// Add the host to the created cluster object	
	int ret = citrusleaf_cluster_add_host(asc, host, port, timeout_ms);
	if (0 != ret) {
		cf_error("get_or_create - add_host failed with error %d", ret);
		citrusleaf_cluster_release_or_destroy(&asc);
		return(0);
	}

	return(asc);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
leaq 0x10bd7(%rip), %rax     # 0x173a8
cmpl $0x0, (%rax)
je 0x6938
movq %rdi, %r14
leaq 0x10bcc(%rip), %rdi     # 0x173b0
movl %esi, %ebx
movl %edx, 0xc(%rsp)
callq 0x23b0
leaq 0x10be2(%rip), %r13     # 0x173d8
movswl %bx, %eax
movl %eax, 0x8(%rsp)
movq (%r13), %r13
testq %r13, %r13
je 0x68aa
leaq 0x158(%r13), %rdi
movq %rdi, 0x10(%rsp)
callq 0x23b0
leaq 0x18(%r13), %rax
leaq 0x60(%r13), %rbp
xorl %r12d, %r12d
movq %rax, 0x18(%rsp)
cmpl 0x24(%r13), %r12d
jae 0x6879
movq 0x18(%rsp), %rdi
movl %r12d, %esi
callq 0x66c2
movq %rbp, %rdi
movl %r12d, %esi
movq %rax, %rbx
callq 0x694d
movq %r14, %rdi
movl %eax, %r15d
callq 0x20e0
leaq 0x1(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x20f0
testl %eax, %eax
jne 0x6874
movswl %r15w, %eax
cmpl 0x8(%rsp), %eax
je 0x6888
incl %r12d
jmp 0x682b
movq 0x10(%rsp), %rdi
callq 0x2190
jmp 0x67fd
movq 0x10(%rsp), %rdi
incl 0x150(%r13)
callq 0x2190
leaq 0x10b10(%rip), %rdi     # 0x173b0
callq 0x2190
jmp 0x693b
leaq 0x10aff(%rip), %rdi     # 0x173b0
callq 0x2190
callq 0x66dd
testq %rax, %rax
movq %rax, 0x20(%rsp)
je 0x6910
movl 0x8(%rsp), %edx
movl 0xc(%rsp), %ecx
movq %rax, %rdi
movq %r14, %rsi
movq %rax, %r13
callq 0x6967
testl %eax, %eax
je 0x693b
leaq 0xe9e2(%rip), %rcx      # 0x152c8
cmpl $0x0, (%rcx)
js 0x6904
leaq 0xe9de(%rip), %rcx      # 0x152d0
movl %eax, %edx
leaq 0x806c(%rip), %rsi      # 0xe967
xorl %edi, %edi
xorl %eax, %eax
movq (%rcx), %rcx
callq *%rcx
leaq 0x20(%rsp), %rdi
callq 0x6b36
jmp 0x6938
leaq 0xe9b1(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
js 0x6938
leaq 0xe9ad(%rip), %rax      # 0x152d0
leaq 0x8014(%rip), %rsi      # 0xe93e
xorl %edi, %edi
xorl %r13d, %r13d
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0x693b
xorl %r13d, %r13d
movq %r13, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq