// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rdi, %r12
callq 0x24f0
movq %rax, %r14
pushq $0x3a
popq %rsi
movq %rax, %rdi
callq 0x20d0
testq %rax, %rax
je 0x74fa
leaq 0x750d(%rip), %rsi      # 0xe9c4
movq %rax, %rdi
movq %rax, %rbp
movb $0x0, (%rax)
callq 0x2350
testl %eax, %eax
je 0x74fa
addq $0x3, %rbp
pushq $0x3a
popq %rsi
movq %rbp, %rdi
callq 0x20d0
testq %rax, %rax
movq %r14, 0x10(%rsp)
je 0x752b
movb $0x0, (%rax)
incq %rax
movq %rax, %rdi
callq 0x2440
testl %eax, %eax
movl %eax, 0xc(%rsp)
jne 0x7546
jmp 0x753e
leaq 0xddc7(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
js 0x763f
leaq 0xddbf(%rip), %rax      # 0x152d0
leaq 0x747d(%rip), %rsi      # 0xe995
xorl %edi, %edi
movq %r12, %rdx
xorl %ebx, %ebx
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0x7641
pushq $0x2f
popq %rsi
movq %rbp, %rdi
callq 0x20d0
testq %rax, %rax
je 0x753e
movb $0x0, (%rax)
movl $0xbb8, 0xc(%rsp)       # imm = 0xBB8
leaq 0xfe63(%rip), %rdi      # 0x173b0
callq 0x23b0
leaq 0xfe7f(%rip), %r15      # 0x173d8
xorl %ebx, %ebx
movq (%r15), %r15
testq %r15, %r15
je 0x75db
testq %rbx, %rbx
jne 0x75db
leaq 0x158(%r15), %rdi
movq %rdi, 0x18(%rsp)
callq 0x23b0
leaq 0x18(%r15), %rax
leaq 0x60(%r15), %r13
xorl %ebx, %ebx
movq %rax, 0x20(%rsp)
cmpl 0x24(%r15), %ebx
jae 0x75cd
movq 0x20(%rsp), %rdi
movl %ebx, %esi
callq 0x66c2
movq %r13, %rdi
movl %ebx, %esi
movq %rbp, %r14
movq %rax, %rbp
callq 0x694d
movq %rbp, %rdi
movq %r14, %rsi
movl %eax, %r12d
movq %r14, %rbp
callq 0x2350
incl %ebx
testl %eax, %eax
jne 0x7588
cmpl 0xc(%rsp), %r12d
jne 0x7588
movq %r15, %rbx
jmp 0x75cf
xorl %ebx, %ebx
movq 0x18(%rsp), %rdi
callq 0x2190
jmp 0x755b
leaq 0xfdce(%rip), %rdi      # 0x173b0
callq 0x2190
movq 0x10(%rsp), %r14
testq %rbx, %rbx
jne 0x7641
callq 0x66dd
movswl 0xc(%rsp), %edx
movq %rax, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
movq %rax, %rbx
callq 0x6967
cmpl $0x0, 0x104(%rbx)
jne 0x7641
leaq 0xdcad(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
js 0x7637
leaq 0xdca9(%rip), %rax      # 0x152d0
leaq 0x73a1(%rip), %rsi      # 0xe9cf
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
movq %rbx, %rdi
callq 0x6b9b
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x22c0
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq