// File: /workspace/github_c_makefile_star2_O1/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 0x8639
movl %eax, %ebx
testb %bpl, %bpl
je 0x84d3
movl %ebx, %edi
movl $0x3, %esi
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
movl %ebx, %edi
movl $0x4, %esi
xorl %eax, %eax
callq 0x2450
movl %eax, %r15d
cmpl $-0x1, %eax
jne 0x84c9
movl %ebx, %edi
callq 0x2550
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, %r15d
je 0x8628
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
movl %ebx, %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x24d0
cmpl $0x0, 0x2c(%r14)
je 0x860d
movl %ebp, (%rsp)
leaq 0x20(%r14), %r15
xorl %r12d, %r12d
movq %r15, %rdi
movl %r12d, %esi
callq 0xeccf
movq %rax, %r13
movl $0x1, %edi
leaq 0x8(%rsp), %rsi
callq 0x2230
movl %ebx, %edi
movq %r13, %rsi
movl $0x10, %edx
callq 0x21d0
testl %eax, %eax
je 0x858f
cmpb $0x0, (%rsp)
je 0x8554
callq 0x2040
cmpl $0x73, (%rax)
jne 0x8554
movl $0x2, %ebp
jmp 0x85d5
callq 0x2040
movl (%rax), %edx
cmpl $0x6f, %edx
leaq 0xed63(%rip), %rax      # 0x172c8
movl (%rax), %eax
movb $0x1, %r13b
jne 0x85da
testl %eax, %eax
js 0x85f9
leaq 0xed55(%rip), %rax      # 0x172d0
movq (%rax), %rcx
xorl %ebp, %ebp
xorl %edi, %edi
leaq 0x84be(%rip), %rsi      # 0x10a47
xorl %eax, %eax
callq *%rcx
jmp 0x85fb
xorl %r13d, %r13d
movl %ebx, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2450
movl %eax, %edx
movl $0x800, %ecx            # imm = 0x800
orl %ecx, %edx
cmpl $-0x1, %eax
cmovel %ecx, %edx
movl %ebx, %edi
movl $0x4, %esi
xorl %eax, %eax
callq 0x2450
movl $0x2, %ebp
cmpl $-0x1, %eax
jne 0x85fb
movl %ebx, %edi
callq 0x2550
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
jmp 0x85fb
testl %eax, %eax
js 0x85f9
leaq 0xeceb(%rip), %rax      # 0x172d0
movq (%rax), %rcx
xorl %ebp, %ebp
xorl %edi, %edi
leaq 0x8473(%rip), %rsi      # 0x10a66
xorl %eax, %eax
callq *%rcx
jmp 0x85fb
xorl %ebp, %ebp
testb %r13b, %r13b
je 0x8612
incl %r12d
cmpl 0x2c(%r14), %r12d
jb 0x850a
movl $0x3, %ebp
cmpl $0x2, %ebp
je 0x8628
cmpl $0x3, %ebp
jne 0x863e
movl %ebx, %edi
callq 0x2550
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %rsi
movl $0x1, %edi
callq 0x2230
jmp 0x863e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq