// File: /workspace/github_c_makefile_star2_O3/sharpyfox[P]citrusleaf-client/src/cl_cluster.c
// Function: cl_cluster_node_fd_create
int
cl_cluster_node_fd_create(cl_cluster_node *cn, bool nonblocking)
{
	int fd;
	uint64_t starttime, endtime;
		
	// allocate a new file descriptor
	if (-1 == (fd = socket ( AF_INET, SOCK_STREAM, 0))) {
#ifdef DEBUG			
		cf_debug("could not allocate a socket, serious problem");
#endif			
		return(-1);
	}
#ifdef DEBUG_VERBOSE		
	else {
		cf_debug("new socket: fd %d node %s",fd, cn->name);
	}
#endif

	if (nonblocking == true) {
		int flags;
		if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
			flags = 0;
		if (-1 == fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
			close(fd); 
			fd = -1;
			goto Done;
		}
	}
	
	int f = 1;
	setsockopt(fd, SOL_TCP, TCP_NODELAY, &f, sizeof(f));

	// loop over all known IP addresses for the server
	for (uint i=0;i< cf_vector_size(&cn->sockaddr_in_v);i++) {
		struct sockaddr_in *sa_in = cf_vector_getp(&cn->sockaddr_in_v, i);
	
		uint64_t starttime = cf_getms();
		//dump_sockaddr_in("Connecting to ", sa_in);
		if (0 == connect(fd, (struct sockaddr *) sa_in, sizeof(struct sockaddr_in) ) )
		{
			// set nonblocking 
			int flags;
			if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
				flags = 0;
			if (-1 == fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
				close(fd); fd = -1; goto Done;
			}
			
			goto Done;
		}
		else {
			//In case of a non-blocking connect, the connection may not be established immediately.
			//in-progress is a valid return value. We can do select later and use the socket.
			if (nonblocking && (errno == EINPROGRESS))
			{
#if XDS //Hack for XDS
				cf_print_sockaddr_in("Connecting to ", sa_in);
				cf_debug("Non-blocking connect returned EINPROGRESS as expected");
#endif
				goto Done;
			}
			// todo: remove this sockaddr from the list, or dun the node?
			if (errno == ECONNREFUSED) {
				cf_error("a host is refusing connections");
			}
			else {
				cf_error("connect fail: errno %d", errno);
			}
		}
	}
	close(fd);
	fd = -1;
		
Done:
	endtime = cf_getms();
	//cf_debug("Time taken to open a new connection = %"PRIu64, (endtime - starttime));
	return(fd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebp
movq %rdi, %r14
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x838e
movl %eax, %ebx
testb %bpl, %bpl
je 0x82bf
movl %ebx, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2450
movl $0x800, %ecx            # imm = 0x800
movl %eax, %edx
movl %ebx, %edi
movl $0x4, %esi
orl %ecx, %edx
cmpl $-0x1, %eax
cmovel %ecx, %edx
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
je 0x83c7
leaq 0x4(%rsp), %rcx
movl %ebx, %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
movl $0x1, (%rcx)
callq 0x24d0
cmpl $0x0, 0x2c(%r14)
je 0x83c7
leaq 0x20(%r14), %r15
xorl %r13d, %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0xe834
movl $0x1, %edi
leaq 0x8(%rsp), %rsi
movq %rax, %r12
callq 0x2230
movl %ebx, %edi
movq %r12, %rsi
movl $0x10, %edx
callq 0x21d0
testl %eax, %eax
je 0x8395
callq 0x2040
movl (%rax), %edx
cmpl $0x73, %edx
sete %al
testb %al, %bpl
jne 0x83d3
leaq 0xdf88(%rip), %rax      # 0x162c8
cmpl $0x6f, %edx
movl (%rax), %eax
jne 0x8364
testl %eax, %eax
js 0x837f
leaq 0xdf7e(%rip), %rax      # 0x162d0
xorl %edi, %edi
leaq 0x770a(%rip), %rsi      # 0xfa65
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0x837f
testl %eax, %eax
js 0x837f
leaq 0xdf61(%rip), %rax      # 0x162d0
xorl %edi, %edi
leaq 0x770c(%rip), %rsi      # 0xfa84
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
incl %r13d
cmpl 0x2c(%r14), %r13d
jb 0x82f3
jmp 0x83c7
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x83e2
movl %ebx, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2450
movl $0x800, %ecx            # imm = 0x800
movl %eax, %edx
movl %ebx, %edi
movl $0x4, %esi
orl %ecx, %edx
cmpl $-0x1, %eax
cmovel %ecx, %edx
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
jne 0x83d3
movl %ebx, %edi
callq 0x2550
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %rsi
movl $0x1, %edi
callq 0x2230
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq