// File: /workspace/github_c_makefile_star2_O1/sharpyfox[P]citrusleaf-client/src/cl_cluster.c
// Function: citrusleaf_cluster_get
cl_cluster *
citrusleaf_cluster_get(char const *url)
{
//	cf_debug(" cluster get: %s",url);

	// make sure it's a citrusleaf url
	char *urlx = strdup(url);
	char *proto = strchr(urlx, ':');
	if (!proto) {
		cf_error("warning: url %s illegal for citrusleaf connect", url);
		free(urlx);
		return(0);
	}
	*proto = 0;
	if (strcmp(proto, "citrusleaf") == 0) {
		cf_error("warning: url %s illegal for citrusleaf connect", url);
		free(urlx);
		return(0);
	}
	char *host = proto+3;
	char *port = strchr(host, ':');
	int port_i = 0;

	if (port) {
		*port = 0; // will terminate the host string properly
		port_i = atoi(port + 1);
	}
	else {
		port = strchr(host, '/');
		if (port) *port = 0;
	}
	if (port_i == 0) port_i = 3000; 

	// cf_debug(" cluster get: host %s port %d",host, port_i);

	// search the cluster list for matching url open names
	cl_cluster *asc = 0;
	pthread_mutex_lock(&cluster_ll_LOCK);
	cf_ll_element *e = cf_ll_get_head(&cluster_ll);
	while (e && asc == 0) {
		cl_cluster *cl_asc = (cl_cluster *) e;

		// cf_debug(" cluster get: comparing against %p",cl_asc);

		uint i;
		pthread_mutex_lock(&cl_asc->LOCK);
		for (i=0;i<cf_vector_size(&cl_asc->host_str_v);i++) {
			char *cl_host_str = cf_vector_pointer_get(&cl_asc->host_str_v, i);
			int   cl_port_i = cf_vector_integer_get(&cl_asc->host_port_v, i);

			// cf_debug(" cluster get: comparing against %s %d",cl_host_str, cl_port_i);

			if (strcmp(cl_host_str, host)!= 0)	continue;
			if (cl_port_i == port_i) {
				// found
				asc = cl_asc;
				break;
			}
			// cf_debug(" cluster get: comparing against %p",cl_asc);
		}
		pthread_mutex_unlock(&cl_asc->LOCK);

		e = cf_ll_get_next(e);
	}
	pthread_mutex_unlock(&cluster_ll_LOCK);

	if (asc) {
		// cf_debug(" cluster get: reusing cluster %p",asc);
		free(urlx);
		return(asc);
	}

	// doesn't exist yet? create a new one
	asc = citrusleaf_cluster_create();
	citrusleaf_cluster_add_host(asc, host, port_i, 0);

    // check to see if we actually got some initial node
	uint node_v_sz = cf_vector_size(&asc->node_v);
    if (node_v_sz==0) {
		cf_error("no node added in initial create");
        citrusleaf_cluster_destroy(asc);
    	free(urlx);
        return NULL;
    }
        
	// cf_debug(" cluster get: new cluster %p",asc);

	free(urlx);
	return(asc);	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r12
callq 0x24f0
movq %rax, %rbx
movq %rax, %rdi
movl $0x3a, %esi
callq 0x20d0
testq %rax, %rax
je 0x7c3c
movq %rax, %r14
movb $0x0, (%rax)
leaq 0x8da6(%rip), %rsi      # 0x109a6
movq %rax, %rdi
callq 0x2350
testl %eax, %eax
je 0x7c3c
addq $0x3, %r14
movq %r14, %rdi
movl $0x3a, %esi
callq 0x20d0
testq %rax, %rax
movq %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
je 0x7c6e
movb $0x0, (%rax)
incq %rax
movq %rax, %rdi
callq 0x2440
jmp 0x7c88
leaq 0xf685(%rip), %rax      # 0x172c8
cmpl $0x0, (%rax)
js 0x7dcb
leaq 0xf67d(%rip), %rax      # 0x172d0
movq (%rax), %rcx
leaq 0x8d1a(%rip), %rsi      # 0x10977
xorl %r15d, %r15d
xorl %edi, %edi
movq %r12, %rdx
xorl %eax, %eax
callq *%rcx
jmp 0x7dce
movq %r14, %rdi
movl $0x2f, %esi
callq 0x20d0
movq %rax, %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x7c88
movb $0x0, (%rcx)
testl %eax, %eax
movl $0xbb8, %ebp            # imm = 0xBB8
cmovnel %eax, %ebp
leaq 0x11717(%rip), %rdi     # 0x193b0
callq 0x23b0
leaq 0x11733(%rip), %rax     # 0x193d8
movq (%rax), %r14
testq %r14, %r14
je 0x7d62
leaq 0x158(%r14), %rdi
movq %rdi, 0x18(%rsp)
callq 0x23b0
cmpl $0x0, 0x24(%r14)
je 0x7d3a
leaq 0x18(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0x60(%r14), %rax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
movq 0x28(%rsp), %rdi
movl %r12d, %esi
leaq 0x30(%rsp), %rdx
callq 0xec6f
movq 0x30(%rsp), %r13
movq 0x20(%rsp), %rdi
movl %r12d, %esi
leaq 0x4(%rsp), %rdx
callq 0xec6f
movl 0x4(%rsp), %r15d
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x2350
movl %r15d, %ecx
xorl %ebp, %ecx
orl %eax, %ecx
cmoveq %r14, %rbx
testl %eax, %eax
jne 0x7d2f
cmpl %ebp, %r15d
je 0x7d3c
incl %r12d
cmpl 0x24(%r14), %r12d
jb 0x7ce0
jmp 0x7d3c
xorl %ebx, %ebx
movq 0x18(%rsp), %rdi
callq 0x2190
movq (%r14), %r14
movq %rbx, %r15
testq %rbx, %rbx
sete %bl
testq %r14, %r14
je 0x7d67
testq %r15, %r15
je 0x7cb1
jmp 0x7d67
movb $0x1, %bl
xorl %r15d, %r15d
leaq 0x11642(%rip), %rdi     # 0x193b0
callq 0x2190
testb %bl, %bl
movq 0x10(%rsp), %rbx
je 0x7dce
callq 0x6bbc
movq %rax, %r15
movswl %bp, %edx
movq %rax, %rdi
movq 0x8(%rsp), %rsi
xorl %ecx, %ecx
callq 0x6e9d
cmpl $0x0, 0x104(%r15)
jne 0x7dce
leaq 0xf521(%rip), %rax      # 0x172c8
cmpl $0x0, (%rax)
js 0x7dc3
leaq 0xf51d(%rip), %rax      # 0x172d0
movq (%rax), %rcx
leaq 0x8bf4(%rip), %rsi      # 0x109b1
xorl %edi, %edi
xorl %eax, %eax
callq *%rcx
movq %r15, %rdi
callq 0x7163
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x22c0
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq