// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x4d8, %rsp            # imm = 0x4D8
movw %dx, %ax
movq %rdi, 0x4c8(%rsp)
movq %rsi, 0x4c0(%rsp)
movw %ax, 0x4be(%rsp)
movl %ecx, 0x4b8(%rsp)
movl $0x0, 0x4b4(%rsp)
movl $0x0, 0x4a0(%rsp)
movq 0x4c8(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x23e0
movl $0x0, 0x49c(%rsp)
movl 0x49c(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x4c8(%rsp), %rdi
addq $0x18, %rdi
callq 0x9da0
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jae 0xa3b6
movq 0x4c8(%rsp), %rdi
addq $0x18, %rdi
movl 0x49c(%rsp), %esi
callq 0x9db0
movq %rax, 0x4a8(%rsp)
movq 0x4c8(%rsp), %rdi
addq $0x60, %rdi
movl 0x49c(%rsp), %esi
callq 0xa220
movl %eax, 0x4a4(%rsp)
movq 0x4c0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x4a8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x4c0(%rsp), %rdi
callq 0x2100
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x2120
cmpl $0x0, %eax
jne 0xa39e
movswl 0x4be(%rsp), %eax
cmpl 0x4a4(%rsp), %eax
jne 0xa39e
movq 0x4c8(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x21c0
movl $0x0, 0x4d4(%rsp)
jmp 0xa609
jmp 0xa3a0
movl 0x49c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x49c(%rsp)
jmp 0xa2be
movq 0x4c0(%rsp), %rdi
callq 0x2520
movq %rax, 0x490(%rsp)
movq 0x4c8(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x21c0
leaq 0x90(%rsp), %rdx
leaq 0x48(%rsp), %rdi
movl $0x10, %esi
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
callq 0x16c50
movq 0x4c8(%rsp), %rdi
movq 0x490(%rsp), %rsi
leaq 0x48(%rsp), %rcx
movswl 0x4be(%rsp), %edx
callq 0xd460
cmpl $0x0, %eax
je 0xa435
movl $0xffffffff, 0x4b4(%rsp) # imm = 0xFFFFFFFF
jmp 0xa5f1
movq 0x4c8(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x23e0
movq 0x4c8(%rsp), %rdi
addq $0x18, %rdi
movq 0x490(%rsp), %rsi
callq 0xaae0
movq 0x4c8(%rsp), %rdi
addq $0x60, %rdi
movswl 0x4be(%rsp), %esi
callq 0xab10
movq 0x4c8(%rsp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x21c0
movq 0x4c8(%rsp), %rax
movb $0x0, 0x16(%rax)
movq 0x4c8(%rsp), %rdi
callq 0xab40
cmpl $0x0, 0x4b8(%rsp)
jne 0xa4bd
movl $0x64, 0x4b8(%rsp)
cmpl $0x0, 0x4b8(%rsp)
jle 0xa56f
movl $0x0, 0x44(%rsp)
callq 0xc500
movq %rax, 0x38(%rsp)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
movq 0x4c8(%rsp), %rax
movb 0x16(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xa50a
movq 0x4c8(%rsp), %rdi
callq 0xab40
movq 0x4c8(%rsp), %rax
movb 0x16(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0xa529
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2390
jmp 0xa52b
movq 0x4c8(%rsp), %rax
movb 0x16(%rax), %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0xf(%rsp)
jne 0xa561
callq 0xc500
subq 0x38(%rsp), %rax
movl 0x4b8(%rsp), %ecx
cmpq %rcx, %rax
setb %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xa4dd
jmp 0xa56f
movb $0x0, 0x37(%rsp)
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x9da0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jae 0xa5db
movl 0x30(%rsp), %esi
leaq 0x48(%rsp), %rdi
callq 0x17340
movq %rax, 0x28(%rsp)
movq 0x4c8(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x9ca0
movq %rax, %rcx
xorl %eax, %eax
cmpq %rcx, %rax
je 0xa5cc
movb $0x1, 0x37(%rsp)
jmp 0xa5ce
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0xa57c
testb $0x1, 0x37(%rsp)
jne 0xa5ef
movl $0xfffffffe, 0x4b4(%rsp) # imm = 0xFFFFFFFE
jmp 0xa5f1
jmp 0xa5f1
leaq 0x48(%rsp), %rdi
callq 0x16e10
movl 0x4b4(%rsp), %eax
movl %eax, 0x4d4(%rsp)
movl 0x4d4(%rsp), %eax
addq $0x4d8, %rsp            # imm = 0x4D8
retq
nopl (%rax,%rax)