// File: /workspace/github_c_makefile_star2_O1/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
movl %ecx, 0x4(%rsp)
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %rbx
addq $0x158, %rdi            # imm = 0x158
movq %rdi, 0x8(%rsp)
callq 0x23b0
leaq 0x18(%rbx), %rax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x24(%rbx)
movswl %bp, %eax
movl %eax, (%rsp)
je 0x6f46
leaq 0x60(%rbx), %rbp
xorl %r14d, %r14d
movq %rbx, %r15
movq 0x10(%rsp), %rdi
movl %r14d, %esi
leaq 0x70(%rsp), %rbx
movq %rbx, %rdx
callq 0xec6f
movq 0x70(%rsp), %r12
movq %rbp, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0xec6f
movl 0x70(%rsp), %ebx
movq %r13, %rdi
callq 0x20e0
leaq 0x1(%rax), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x20f0
testl %eax, %eax
jne 0x6f3a
cmpl (%rsp), %ebx
je 0x70c9
incl %r14d
movq %r15, %rbx
cmpl 0x24(%r15), %r14d
jb 0x6ee7
movq %r13, %rdi
callq 0x24f0
movq %rax, %r14
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x2190
leaq 0x28(%rsp), %r15
leaq 0x70(%rsp), %rdx
movq %r15, %rdi
movl $0x10, %esi
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
callq 0xe924
movq %rbx, %rdi
movq %r14, %rsi
movl (%rsp), %r13d
movl %r13d, %edx
movq %r15, %rcx
callq 0x8e68
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x70dc
movq %r12, %rdi
callq 0x23b0
leaq 0x18(%rsp), %r15
movq %r14, (%r15)
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0xeb9c
leaq 0x60(%rbx), %rdi
movl %r13d, (%r15)
leaq 0x18(%rsp), %rsi
callq 0xeb9c
movq %r12, %rdi
callq 0x2190
movb $0x0, 0x16(%rbx)
movq %rbx, %rdi
callq 0x742e
movl 0x4(%rsp), %eax
testl %eax, %eax
movl $0x64, %ebp
cmovnel %eax, %ebp
testl %ebp, %ebp
jle 0x707d
leaq 0x18(%rsp), %r14
movl $0x1, %edi
movq %r14, %rsi
callq 0x2230
movabsq $0x431bde82d7b634db, %r15 # imm = 0x431BDE82D7B634DB
movq %r15, %rax
mulq 0x8(%r14)
movq %rdx, %r12
shrq $0x12, %r12
movq (%r14), %r13
movl %ebp, %ebp
cmpb $0x0, 0x16(%rbx)
jne 0x7034
movq %rbx, %rdi
callq 0x742e
cmpb $0x0, 0x16(%rbx)
jne 0x7044
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2360
cmpb $0x0, 0x16(%rbx)
jne 0x707d
movl $0x1, %edi
movq %r14, %rsi
callq 0x2230
movq %r15, %rax
mulq 0x20(%rsp)
shrq $0x12, %rdx
subq %r12, %rdx
movq 0x18(%rsp), %rax
subq %r13, %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
addq %rdx, %rax
cmpq %rbp, %rax
jb 0x7026
cmpl $0x0, 0x34(%rsp)
je 0x70d7
xorl %ebp, %ebp
leaq 0x28(%rsp), %r14
movl $0x1, %r15d
xorl %r12d, %r12d
movq %r14, %rdi
movl %ebp, %esi
callq 0xeccf
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6ad0
testq %rax, %rax
movzbl %r12b, %r12d
cmovnel %r15d, %r12d
incl %ebp
cmpl 0x34(%rsp), %ebp
jb 0x7094
movzbl %r12b, %eax
andl $0x1, %eax
leal -0x2(%rax,%rax), %ebp
jmp 0x70dc
movq 0x8(%rsp), %rdi
callq 0x2190
xorl %ebp, %ebp
jmp 0x70e6
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
leaq 0x28(%rsp), %rdi
callq 0xea0d
movl %ebp, %eax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq