// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movb %sil, %al
movq %rdi, 0x48(%rsp)
andb $0x1, %al
movb %al, 0x47(%rsp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20c0
movl %eax, %ecx
movl %ecx, 0x40(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0xbf2f
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0xc150
movb 0x47(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xbfa3
movl 0x40(%rsp), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x2470
movl %eax, %ecx
movl %ecx, 0x2c(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0xbf66
movl $0x0, 0x2c(%rsp)
movl 0x40(%rsp), %edi
movl 0x2c(%rsp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x2470
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0xbfa1
movl 0x40(%rsp), %edi
callq 0x2570
movl $0xffffffff, 0x40(%rsp) # imm = 0xFFFFFFFF
jmp 0xc13e
jmp 0xbfa3
movl $0x1, 0x28(%rsp)
movl 0x40(%rsp), %edi
movl $0x6, %esi
movl $0x1, %edx
leaq 0x28(%rsp), %rcx
movl $0x4, %r8d
callq 0x24f0
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x48(%rsp), %rdi
addq $0x20, %rdi
callq 0x9da0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jae 0xc12d
movq 0x48(%rsp), %rdi
addq $0x20, %rdi
movl 0x24(%rsp), %esi
callq 0x17340
movq %rax, 0x18(%rsp)
callq 0xc500
movq %rax, 0x10(%rsp)
movl 0x40(%rsp), %edi
movq 0x18(%rsp), %rsi
movl $0x10, %edx
callq 0x2200
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xc09a
movl 0x40(%rsp), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x2470
movl %eax, %ecx
movl %ecx, 0xc(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0xc05a
movl $0x0, 0xc(%rsp)
movl 0x40(%rsp), %edi
movl 0xc(%rsp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x2470
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0xc095
movl 0x40(%rsp), %edi
callq 0x2570
movl $0xffffffff, 0x40(%rsp) # imm = 0xFFFFFFFF
jmp 0xc13e
jmp 0xc13e
testb $0x1, 0x47(%rsp)
je 0xc0b0
callq 0x2040
cmpl $0x73, (%rax)
jne 0xc0b0
jmp 0xc13e
callq 0x2040
cmpl $0x6f, (%rax)
jne 0xc0e2
leaq 0x12217(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xc0e0
leaq 0x12210(%rip), %rax     # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0xc9d5(%rip), %rsi      # 0x18ab1
movb $0x0, %al
callq *%rcx
jmp 0xc119
leaq 0x121ef(%rip), %rax     # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0xc117
leaq 0x121e8(%rip), %rax     # 0x1e2e0
movq (%rax), %rax
movq %rax, (%rsp)
callq 0x2040
movq (%rsp), %rcx
movl (%rax), %edx
xorl %edi, %edi
leaq 0xc9bd(%rip), %rsi      # 0x18ad0
movb $0x0, %al
callq *%rcx
jmp 0xc119
jmp 0xc11b
jmp 0xc11d
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xbfd1
movl 0x40(%rsp), %edi
callq 0x2570
movl $0xffffffff, 0x40(%rsp) # imm = 0xFFFFFFFF
callq 0xc500
movq %rax, 0x30(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax)