// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq 0x68(%rsp), %rdi
callq 0x2520
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
movl $0x3a, %esi
callq 0x20f0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0xb277
leaq 0x1309d(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xb25f
leaq 0x13096(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
movq 0x68(%rsp), %rdx
xorl %edi, %edi
leaq 0xd786(%rip), %rsi      # 0x189e1
movb $0x0, %al
callq *%rcx
movq 0x60(%rsp), %rdi
callq 0x22e0
movq $0x0, 0x70(%rsp)
jmp 0xb540
movq 0x58(%rsp), %rax
movb $0x0, (%rax)
movq 0x58(%rsp), %rdi
leaq 0xd785(%rip), %rsi      # 0x18a10
callq 0x2380
cmpl $0x0, %eax
jne 0xb2d8
leaq 0x1303c(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xb2c0
leaq 0x13035(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
movq 0x68(%rsp), %rdx
xorl %edi, %edi
leaq 0xd725(%rip), %rsi      # 0x189e1
movb $0x0, %al
callq *%rcx
movq 0x60(%rsp), %rdi
callq 0x22e0
movq $0x0, 0x70(%rsp)
jmp 0xb540
movq 0x58(%rsp), %rax
addq $0x3, %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
movl $0x3a, %esi
callq 0x20f0
movq %rax, 0x48(%rsp)
movl $0x0, 0x44(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0xb326
movq 0x48(%rsp), %rax
movb $0x0, (%rax)
movq 0x48(%rsp), %rdi
addq $0x1, %rdi
callq 0x2460
movl %eax, 0x44(%rsp)
jmp 0xb34c
movq 0x50(%rsp), %rdi
movl $0x2f, %esi
callq 0x20f0
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0xb34a
movq 0x48(%rsp), %rax
movb $0x0, (%rax)
jmp 0xb34c
cmpl $0x0, 0x44(%rsp)
jne 0xb35b
movl $0xbb8, 0x44(%rsp)      # imm = 0xBB8
movq $0x0, 0x38(%rsp)
leaq 0x15055(%rip), %rdi     # 0x203c0
callq 0x23e0
leaq 0x15071(%rip), %rdi     # 0x203e8
callq 0xa210
movq %rax, 0x30(%rsp)
xorl %eax, %eax
cmpq $0x0, 0x30(%rsp)
movb %al, 0xf(%rsp)
je 0xb39c
cmpq $0x0, 0x38(%rsp)
sete %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xb3a9
jmp 0xb47c
movq 0x30(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x23e0
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x28(%rsp), %rdi
addq $0x18, %rdi
callq 0x9da0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jae 0xb457
movq 0x28(%rsp), %rdi
addq $0x18, %rdi
movl 0x24(%rsp), %esi
callq 0x9db0
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %rdi
addq $0x60, %rdi
movl 0x24(%rsp), %esi
callq 0xa220
movl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x2380
cmpl $0x0, %eax
je 0xb42f
jmp 0xb447
movl 0x14(%rsp), %eax
cmpl 0x44(%rsp), %eax
jne 0xb445
movq 0x28(%rsp), %rax
movq %rax, 0x38(%rsp)
jmp 0xb457
jmp 0xb447
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xb3cc
movq 0x28(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x21c0
movq 0x30(%rsp), %rdi
callq 0xa250
movq %rax, 0x30(%rsp)
jmp 0xb381
leaq 0x14f3d(%rip), %rdi     # 0x203c0
callq 0x21c0
cmpq $0x0, 0x38(%rsp)
je 0xb4a9
movq 0x60(%rsp), %rdi
callq 0x22e0
movq 0x38(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0xb540
callq 0x9de0
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq 0x50(%rsp), %rsi
movl 0x44(%rsp), %eax
xorl %ecx, %ecx
movswl %ax, %edx
callq 0xa260
movq 0x38(%rsp), %rdi
addq $0xf8, %rdi
callq 0x9da0
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jne 0xb52c
leaq 0x12dea(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xb50d
leaq 0x12de3(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0xd512(%rip), %rsi      # 0x18a1b
movb $0x0, %al
callq *%rcx
movq 0x38(%rsp), %rdi
callq 0xa6d0
movq 0x60(%rsp), %rdi
callq 0x22e0
movq $0x0, 0x70(%rsp)
jmp 0xb540
movq 0x60(%rsp), %rdi
callq 0x22e0
movq 0x38(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
addq $0x78, %rsp
retq
nopw (%rax,%rax)