// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movl %esi, %ebp
movq %rdi, %r14
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x20a0
cmpl $-0x1, %eax
je 0x7cb6
movl %eax, %ebx
testb %bpl, %bpl
je 0x7c01
pushq $0x3
popq %rsi
movl %ebx, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2450
movl $0x800, %ecx            # imm = 0x800
movl %eax, %edx
orl %ecx, %edx
cmpl $-0x1, %eax
cmovel %ecx, %edx
pushq $0x4
popq %rsi
movl %ebx, %edi
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
je 0x7ce9
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
pushq $0x4
popq %r8
movl %ebx, %edi
callq 0x24d0
leaq 0x20(%r14), %r15
xorl %r13d, %r13d
cmpl 0x2c(%r14), %r13d
jae 0x7ce9
movq %r15, %rdi
movl %r13d, %esi
callq 0xd9c7
movq %rax, %r12
callq 0x7ec1
movl %ebx, %edi
movq %r12, %rsi
pushq $0x10
popq %rdx
callq 0x21d0
testl %eax, %eax
je 0x7cbb
callq 0x2040
movl (%rax), %edx
cmpl $0x73, %edx
sete %al
testb %al, %bpl
jne 0x7cf3
leaq 0xd659(%rip), %rax      # 0x152c8
cmpl $0x6f, %edx
movl (%rax), %eax
jne 0x7c93
testl %eax, %eax
js 0x7cae
leaq 0xd64f(%rip), %rax      # 0x152d0
xorl %edi, %edi
leaq 0x6ddb(%rip), %rsi      # 0xea65
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0x7cae
testl %eax, %eax
js 0x7cae
leaq 0xd632(%rip), %rax      # 0x152d0
xorl %edi, %edi
leaq 0x6ddd(%rip), %rsi      # 0xea84
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
incl %r13d
jmp 0x7c24
pushq $-0x1
popq %rbx
jmp 0x7cf8
pushq $0x3
popq %rsi
movl %ebx, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2450
movl $0x800, %ecx            # imm = 0x800
movl %eax, %edx
orl %ecx, %edx
cmpl $-0x1, %eax
cmovel %ecx, %edx
pushq $0x4
popq %rsi
movl %ebx, %edi
xorl %eax, %eax
callq 0x2450
cmpl $-0x1, %eax
jne 0x7cf3
movl %ebx, %edi
callq 0x2550
pushq $-0x1
popq %rbx
callq 0x7ec1
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq