// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/cl_cluster.c
// Function: citrusleaf_cluster_add_host
cl_rv
citrusleaf_cluster_add_host(cl_cluster *asc, char const *host_in, short port, int timeout_ms)
{
	int rv = CITRUSLEAF_OK;
#ifdef DEBUG	
	cf_debug("adding host %s:%d timeout %d",host_in, (int)port, timeout_ms);
#endif	
	// Find if the host has already been added on this cluster object
	char *hostp;
	int portp, found = 0;
	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_in,hostp,strlen(host_in)+1)==0) && (port == portp)) {
			// Return OK if host is already added in the list
			pthread_mutex_unlock(&asc->LOCK);
			#ifdef DEBUG
			cf_debug("host already added in this cluster object. Return OK");
			#endif
			return(CITRUSLEAF_OK);
		}
	}

	char *host = strdup(host_in);

	pthread_mutex_unlock(&asc->LOCK);
	// Lookup the address before adding to asc. If lookup fails
	// return CITRUSLEAF_FAIL_CLIENT
	// Resolve - error message need to change.
	cf_vector_define(sockaddr_in_v, sizeof( struct sockaddr_in ), 0);
 	if(cl_lookup(asc, host, port, &sockaddr_in_v) != 0) {
		rv = CITRUSLEAF_FAIL_CLIENT;
		goto cleanup;
	}
	
	// Host not found on this cluster object
	// Add the host and port to the lists of hosts to try when maintaining
	pthread_mutex_lock(&asc->LOCK);
	cf_vector_pointer_append(&asc->host_str_v, host);
	cf_vector_integer_append(&asc->host_port_v, (int) port);
	pthread_mutex_unlock(&asc->LOCK);
	asc->found_all = false; // Added a new item in the list. Mark the cluster not fully discovered.

	// Fire the normal tender function to speed up resolution
	cluster_tend(asc);

	if (timeout_ms == 0)	timeout_ms = 100;
	
	if (timeout_ms > 0) {
		int n_tends = 0;
		uint64_t start_ms = cf_getms();
		do {
			n_tends++;
			if (asc->found_all == false) { cluster_tend(asc); }
			if (asc->found_all == false) usleep(1000);
		} while ((asc->found_all == false) && ((cf_getms() - start_ms) < (unsigned int) timeout_ms) );
#ifdef DEBUG		
		cf_debug("add host: required %d tends %"PRIu64"ms to set right", n_tends, cf_getms() - start_ms);
#endif		
	}

	// The cluster may or many not be fully discovered (found_all is true/false). 
	// found_all flag only signifies if the full cluster is discovered or not.
	// It does not signify if the newly added node is reacheable or not.
	// We should check if the newly added node is in the list of reacheable nodes. 
	bool reacheable = false;
	for (uint i=0;i<cf_vector_size(&sockaddr_in_v);i++) {
		struct sockaddr_in *sin = cf_vector_getp(&sockaddr_in_v, i);
		if (0 != cl_cluster_node_get_byaddr(asc, sin)) {
			reacheable = true;
		}
	}
	if (!reacheable) {
		rv = CITRUSLEAF_FAIL_TIMEOUT;
		goto cleanup;
	}

cleanup:
	cf_vector_destroy(&sockaddr_in_v);
	return rv;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
movq %rdi, %rbx
addq $0x158, %rdi            # imm = 0x158
movl %ecx, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movq %rsi, %rbp
movq %rdi, 0x10(%rsp)
callq 0x23b0
leaq 0x60(%rbx), %rax
leaq 0x18(%rbx), %r13
xorl %r14d, %r14d
movq %rax, 0x18(%rsp)
cmpl 0x24(%rbx), %r14d
jae 0x6a01
movq %r13, %rdi
movl %r14d, %esi
callq 0x66c2
movq 0x18(%rsp), %rdi
movl %r14d, %esi
movq %rax, %r12
callq 0x694d
movq %rbp, %rdi
movl %eax, %r15d
callq 0x20e0
leaq 0x1(%rax), %rdx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x20f0
incl %r14d
testl %eax, %eax
jne 0x69a7
cmpl 0x8(%rsp), %r15d
jne 0x69a7
movq 0x10(%rsp), %rdi
callq 0x2190
xorl %ebx, %ebx
jmp 0x6b22
movq %rbp, %rdi
callq 0x24f0
movq 0x10(%rsp), %r12
movq %rax, %r14
movq %r12, %rdi
callq 0x2190
leaq 0x28(%rsp), %r15
pushq $0x10
popq %rsi
leaq 0x70(%rsp), %rdx
movq %r15, %rdi
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
callq 0xd641
movl 0x8(%rsp), %ebp
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
movswl %bp, %edx
callq 0x84a4
testl %eax, %eax
je 0x6a57
pushq $-0x1
popq %rbx
jmp 0x6b18
movq %r12, %rdi
callq 0x23b0
movq %r13, %rdi
movq %r14, %rsi
callq 0x6dff
movq 0x18(%rsp), %rdi
leaq 0x24(%rsp), %rsi
movl %ebp, (%rsi)
callq 0xd8b7
movq %r12, %rdi
callq 0x2190
movq %rbx, %rdi
movb $0x0, 0x16(%rbx)
callq 0x6e10
movl 0xc(%rsp), %eax
testl %eax, %eax
pushq $0x64
popq %rbp
cmovnel %eax, %ebp
testl %ebp, %ebp
jle 0x6adb
callq 0x7ec1
movq %rax, %r14
movl %ebp, %r15d
cmpb $0x0, 0x16(%rbx)
jne 0x6ab8
movq %rbx, %rdi
callq 0x6e10
cmpb $0x0, 0x16(%rbx)
jne 0x6ac8
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2360
cmpb $0x0, 0x16(%rbx)
jne 0x6adb
callq 0x7ec1
subq %r14, %rax
cmpq %r15, %rax
jb 0x6aaa
xorl %r15d, %r15d
leaq 0x28(%rsp), %r14
xorl %ebp, %ebp
cmpl 0x34(%rsp), %ebp
jae 0x6b0d
movq %r14, %rdi
movl %ebp, %esi
callq 0xd9c7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x663c
testq %rax, %rax
setne %al
incl %ebp
orb %al, %r15b
jmp 0x6ae5
movzbl %r15b, %eax
andl $0x1, %eax
leal -0x2(%rax,%rax), %ebx
leaq 0x28(%rsp), %rdi
callq 0xd724
movl %ebx, %eax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq