// File: /workspace/github_c_makefile_star2_O3/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 0x117e7(%rip), %rax     # 0x183a8
movl %esi, 0x4(%rsp)
cmpl $0x0, (%rax)
je 0x6d57
movq %rdi, %r14
leaq 0x117d8(%rip), %rdi     # 0x183b0
movl %edx, 0x10(%rsp)
callq 0x23b0
leaq 0x117f0(%rip), %rax     # 0x183d8
movq (%rax), %rax
testq %rax, %rax
je 0x6ca7
movswl 0x4(%rsp), %ecx
leaq 0x18(%rsp), %r12
movl %ecx, 0x14(%rsp)
leaq 0x158(%rax), %rdi
movq %rax, 0x8(%rsp)
movq %rax, 0x20(%rsp)
callq 0x23b0
movq 0x8(%rsp), %r15
cmpl $0x0, 0x24(%r15)
je 0x6c87
xorl %r13d, %r13d
addq $0x18, %r15
movl %r13d, %esi
movq %r12, %rdx
movq %r15, %rdi
callq 0xe7d4
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %rbx
movl %r13d, %esi
movq %r12, %rdx
leaq 0x60(%r15), %rdi
callq 0xe7d4
movl 0x18(%rsp), %ebp
movq %r14, %rdi
callq 0x20e0
leaq 0x1(%rax), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x20f0
testl %eax, %eax
jne 0x6c7e
movswl %bp, %eax
cmpl 0x14(%rsp), %eax
je 0x6d0e
incl %r13d
cmpl 0x24(%r15), %r13d
jb 0x6c27
addq $0x158, %r15            # imm = 0x158
movq %r15, %rdi
callq 0x2190
movq 0x20(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
jne 0x6c02
leaq 0x11702(%rip), %rdi     # 0x183b0
callq 0x2190
callq 0x6aae
testq %rax, %rax
movq %rax, 0x8(%rsp)
je 0x6d2f
movswl 0x4(%rsp), %edx
movl 0x10(%rsp), %ecx
movq %rax, %rdi
movq %r14, %rsi
movq %rax, %r15
callq 0x6d6c
testl %eax, %eax
je 0x6d5a
leaq 0xf5e4(%rip), %rcx      # 0x162c8
cmpl $0x0, (%rcx)
js 0x6d02
leaq 0xf5e0(%rip), %rcx      # 0x162d0
movl %eax, %edx
leaq 0x8c6e(%rip), %rsi      # 0xf967
xorl %edi, %edi
xorl %eax, %eax
movq (%rcx), %rcx
callq *%rcx
leaq 0x8(%rsp), %rdi
callq 0x6fc1
jmp 0x6d57
incl 0x150(%r15)
leaq 0x158(%r15), %rdi
callq 0x2190
leaq 0x11688(%rip), %rdi     # 0x183b0
callq 0x2190
jmp 0x6d5a
leaq 0xf592(%rip), %rax      # 0x162c8
cmpl $0x0, (%rax)
js 0x6d57
leaq 0xf58e(%rip), %rax      # 0x162d0
leaq 0x8bf5(%rip), %rsi      # 0xf93e
xorl %edi, %edi
xorl %r15d, %r15d
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0x6d5a
xorl %r15d, %r15d
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq