// File: /workspace/github_c_makefile_star2_O3/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, %rbx
callq 0x24f0
movq %rax, %rdi
movl $0x3a, %esi
movq %rax, %r14
callq 0x20d0
testq %rax, %rax
je 0x7ac9
leaq 0x7f3f(%rip), %rsi      # 0xf9c4
movq %rax, %rdi
movq %rax, %r13
movb $0x0, (%rax)
callq 0x2350
testl %eax, %eax
je 0x7ac9
addq $0x3, %r13
movl $0x3a, %esi
movq %r13, %rdi
callq 0x20d0
testq %rax, %rax
movq %r14, 0x8(%rsp)
je 0x7afe
movb $0x0, (%rax)
incq %rax
movq %rax, %rdi
callq 0x2440
testl %eax, %eax
movl %eax, (%rsp)
jne 0x7b1a
jmp 0x7b13
leaq 0xe7f8(%rip), %rax      # 0x162c8
cmpl $0x0, (%rax)
js 0x7aef
leaq 0xe7f4(%rip), %rax      # 0x162d0
leaq 0x7eb2(%rip), %rsi      # 0xf995
xorl %edi, %edi
movq %rbx, %rdx
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
movq %r14, %rdi
callq 0x22c0
xorl %ebx, %ebx
jmp 0x7c26
movq %r13, %rdi
movl $0x2f, %esi
callq 0x20d0
testq %rax, %rax
je 0x7b13
movb $0x0, (%rax)
movl $0xbb8, (%rsp)          # imm = 0xBB8
leaq 0x1088f(%rip), %rdi     # 0x183b0
callq 0x23b0
leaq 0x108ab(%rip), %rax     # 0x183d8
movq (%rax), %r15
testq %r15, %r15
je 0x7beb
leaq 0x158(%r15), %r14
movq %r14, %rdi
callq 0x23b0
cmpl $0x0, 0x24(%r15)
je 0x7bae
leaq 0x18(%r15), %rax
leaq 0x60(%r15), %rbx
xorl %ebp, %ebp
movq %r14, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movl %ebp, %esi
leaq 0x20(%rsp), %rdx
callq 0xe7d4
movq 0x20(%rsp), %r12
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x4(%rsp), %rdx
callq 0xe7d4
movl 0x4(%rsp), %r14d
movq %r12, %rdi
movq %r13, %rsi
callq 0x2350
testl %eax, %eax
jne 0x7ba2
cmpl (%rsp), %r14d
je 0x7bb2
incl %ebp
cmpl 0x24(%r15), %ebp
jb 0x7b63
xorl %ebx, %ebx
jmp 0x7bb5
xorl %ebx, %ebx
jmp 0x7bba
movq %r15, %rbx
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2190
movq (%r15), %r15
testq %r15, %r15
je 0x7bd3
testq %rbx, %rbx
je 0x7b39
leaq 0x107d6(%rip), %rdi     # 0x183b0
callq 0x2190
testq %rbx, %rbx
je 0x7bf7
movq 0x8(%rsp), %rdi
jmp 0x7c21
leaq 0x107be(%rip), %rdi     # 0x183b0
callq 0x2190
callq 0x6aae
movswl (%rsp), %edx
movq %rax, %rdi
movq %r13, %rsi
xorl %ecx, %ecx
movq %rax, %rbx
callq 0x6d6c
cmpl $0x0, 0x104(%rbx)
movq 0x8(%rsp), %r14
je 0x7c38
movq %r14, %rdi
callq 0x22c0
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe689(%rip), %rax      # 0x162c8
cmpl $0x0, (%rax)
js 0x7c5b
leaq 0xe685(%rip), %rax      # 0x162d0
leaq 0x7d7d(%rip), %rsi      # 0xf9cf
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
movq %rbx, %rdi
callq 0x7029
jmp 0x7aef