// File: /workspace/github_c_makefile_star2_O3/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, 0x4(%rsp)
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, 0x8(%rsp)
callq 0x23b0
cmpl $0x0, 0x24(%rbx)
leaq 0x18(%rbx), %rax
movq %rax, 0x10(%rsp)
movswl %bp, %eax
movl %eax, (%rsp)
je 0x6e15
leaq 0x60(%rbx), %rbp
xorl %r14d, %r14d
movq 0x10(%rsp), %rdi
movq %rbx, %r15
leaq 0x70(%rsp), %rbx
movl %r14d, %esi
movq %rbx, %rdx
callq 0xe7d4
movq 0x70(%rsp), %r12
movq %rbp, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0xe7d4
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 0x6e09
cmpl (%rsp), %ebx
je 0x6f90
incl %r14d
movq %r15, %rbx
cmpl 0x24(%r15), %r14d
jb 0x6db6
movq %r13, %rdi
callq 0x24f0
movq 0x8(%rsp), %r12
movq %rax, %r14
movq %r12, %rdi
callq 0x2190
leaq 0x28(%rsp), %r15
leaq 0x70(%rsp), %rdx
movl $0x10, %esi
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
movq %r15, %rdi
callq 0xe49a
movl (%rsp), %r13d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
movl %r13d, %edx
callq 0x8bfc
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x6fa3
movq %r12, %rdi
callq 0x23b0
movq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
movq %r14, (%r15)
callq 0xe723
leaq 0x60(%rbx), %rdi
leaq 0x18(%rsp), %rsi
movl %r13d, (%r15)
callq 0xe723
movq %r12, %rdi
callq 0x2190
movq %rbx, %rdi
movb $0x0, 0x16(%rbx)
callq 0x72f4
movl 0x4(%rsp), %eax
movl $0x64, %ebp
testl %eax, %eax
cmovnel %eax, %ebp
testl %ebp, %ebp
jle 0x6f4c
leaq 0x18(%rsp), %r14
movl $0x1, %edi
movq %r14, %rsi
callq 0x2230
movabsq $0x431bde82d7b634db, %r15 # imm = 0x431BDE82D7B634DB
movq (%r14), %r13
movl %ebp, %ebp
movq %r15, %rax
mulq 0x8(%r14)
movq %rdx, %r12
shrq $0x12, %r12
cmpb $0x0, 0x16(%rbx)
jne 0x6f03
movq %rbx, %rdi
callq 0x72f4
cmpb $0x0, 0x16(%rbx)
jne 0x6f13
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2360
cmpb $0x0, 0x16(%rbx)
jne 0x6f4c
movl $0x1, %edi
movq %r14, %rsi
callq 0x2230
movq %r15, %rax
movq 0x18(%rsp), %rcx
mulq 0x20(%rsp)
subq %r13, %rcx
shrq $0x12, %rdx
imulq $0x3e8, %rcx, %rax      # imm = 0x3E8
subq %r12, %rdx
addq %rdx, %rax
cmpq %rbp, %rax
jb 0x6ef5
cmpl $0x0, 0x34(%rsp)
je 0x6f9e
xorl %ebp, %ebp
leaq 0x28(%rsp), %r14
xorl %r15d, %r15d
movq %r14, %rdi
movl %ebp, %esi
callq 0xe834
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6a14
testq %rax, %rax
setne %al
incl %ebp
orb %al, %r15b
cmpl 0x34(%rsp), %ebp
jb 0x6f5d
movzbl %r15b, %eax
andl $0x1, %eax
leal -0x2(%rax,%rax), %ebp
jmp 0x6fa3
movq 0x8(%rsp), %rdi
callq 0x2190
xorl %ebp, %ebp
jmp 0x6fad
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
leaq 0x28(%rsp), %rdi
callq 0xe580
movl %ebp, %eax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq