// File: /workspace/github_c_makefile_star2_O3/icebert[P]pblat/lib/net.c
// Function: netConnect
static int netConnectWithTimeout(char *hostName, int port, long msTimeout)
/* In order to avoid a very long default timeout (several minutes) for hosts that will
 * not answer the port, we are forced to connect non-blocking.
 * After the connection has been established, we return to blocking mode.
 * Also closes sd if error. */
{
int sd;
struct sockaddr_in sai;		/* Some system socket info. */
int res;
fd_set mySet;

if (hostName == NULL)
    {
    warn("NULL hostName in netConnect");
    return -1;
    }
if (!internetFillInAddress(hostName, port, &sai))
    return -1;
if ((sd = netStreamSocket()) < 0)
    return sd;

// Set socket to nonblocking so we can manage our own timeout time.
if (setSocketNonBlocking(sd, TRUE) < 0)
    {
    close(sd);
    return -1;
    }

// Trying to connect with timeout
res = connect(sd, (struct sockaddr*) &sai, sizeof(sai));
if (res < 0)
    {
    if (errno == EINPROGRESS)
	{
	struct timeval startTime;
	gettimeofday(&startTime, NULL);
	struct timeval remainingTime;
	remainingTime.tv_sec = (long) (msTimeout/1000);
	remainingTime.tv_usec = (long) (((msTimeout/1000)-remainingTime.tv_sec)*1000000);
	while (1) 
	    {
	    FD_ZERO(&mySet);
	    FD_SET(sd, &mySet);
	    // use tempTime (instead of using remainingTime directly) because on some platforms select() may modify the time val.
	    struct timeval tempTime = remainingTime;
	    res = select(sd+1, NULL, &mySet, &mySet, &tempTime);  
	    if (res < 0) 
		{
		if (errno == EINTR)  // Ignore the interrupt 
		    {
                    // Subtract the elapsed time from remaining time since some platforms need this.
		    struct timeval newTime;
		    gettimeofday(&newTime, NULL);
                    struct timeval elapsedTime = tvMinus(newTime, startTime);
		    remainingTime = tvMinus(remainingTime, elapsedTime);
		    if (remainingTime.tv_sec < 0)  // means our timeout has more than expired
			{
			remainingTime.tv_sec = 0;
			remainingTime.tv_usec = 0;
			}
		    startTime = newTime;
		    }
		else
		    {
		    warn("Error in select() during TCP non-blocking connect %d - %s", errno, strerror(errno));
		    close(sd);
		    return -1;
		    }
		}
	    else if (res > 0)
		{
		// Socket selected for write when it is ready
		int valOpt;
		socklen_t lon;
                // But check the socket for any errors
                lon = sizeof(valOpt);
                if (getsockopt(sd, SOL_SOCKET, SO_ERROR, (void*) (&valOpt), &lon) < 0)
                    {
                    warn("Error in getsockopt() %d - %s", errno, strerror(errno));
                    close(sd);
                    return -1;
                    }
                // Check the value returned...
                if (valOpt)
                    {
                    warn("Error in TCP non-blocking connect() %d - %s. Host %s port %d.", valOpt, strerror(valOpt), hostName, port);
                    close(sd);
                    return -1;
                    }
		break;
		}
	    else
		{
		warn("TCP non-blocking connect() to %s timed-out in select() after %ld milliseconds - Cancelling!", hostName, msTimeout);
		close(sd);
		return -1;
		}
	    }
	}
    else
	{
	warn("TCP non-blocking connect() error %d - %s", errno, strerror(errno));
	close(sd);
	return -1;
	}
    }

// Set to blocking mode again
if (setSocketNonBlocking(sd, FALSE) < 0)
    {
    close(sd);
    return -1;
    }

if (setReadWriteTimeouts(sd, DEFAULTREADWRITETTIMEOUTSEC) < 0)
    {
    close(sd);
    return -1;
    }

return sd;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
testq %rdi, %rdi
je 0x2e738
movl %esi, %r15d
movq %rdi, %r14
leaq 0x60(%rsp), %rdx
callq 0x4a9ce
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x2e97c
callq 0x31626
movl %eax, %ebp
testl %eax, %eax
js 0x2e731
movl %ebp, %edi
movl $0x1, %esi
callq 0x315aa
testl %eax, %eax
js 0x2e975
leaq 0x60(%rsp), %rsi
movl %ebp, %edi
movl $0x10, %edx
callq 0x6580
testl %eax, %eax
js 0x2e750
movl %ebp, %edi
xorl %esi, %esi
callq 0x315aa
testl %eax, %eax
js 0x2e975
movl %ebp, %edi
movl $0x78, %esi
callq 0x2e630
testl %eax, %eax
js 0x2e975
movl %ebp, %ebx
jmp 0x2e97c
leaq 0x63fa6(%rip), %rdi     # 0x926e5
xorl %eax, %eax
callq 0x26bea
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2e97c
callq 0x6060
movq %rax, 0x18(%rsp)
movl (%rax), %r12d
cmpl $0x73, %r12d
jne 0x2e959
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x6c40
movl $0x1, %eax
movl %ebp, %ecx
shlq %cl, %rax
movq %rax, 0x48(%rsp)
movl %ebp, %eax
shrl $0x6, %eax
movq %rax, 0x40(%rsp)
movl %ebp, %eax
incl %eax
movl %eax, 0x10(%rsp)
movl $0xa, %r13d
movq $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
orq %rax, 0x70(%rsp,%rcx,8)
movq %r13, 0x50(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x58(%rsp)
movl 0x10(%rsp), %edi
xorl %esi, %esi
leaq 0x70(%rsp), %rcx
movq %rcx, %rdx
leaq 0x50(%rsp), %r8
callq 0x66b0
testl %eax, %eax
js 0x2e883
je 0x2e990
movl $0x4, 0x14(%rsp)
movl %ebp, %edi
movl $0x1, %esi
movl $0x4, %edx
leaq 0x20(%rsp), %rcx
leaq 0x14(%rsp), %r8
callq 0x64e0
testl %eax, %eax
js 0x2e90b
movl 0x20(%rsp), %r12d
testl %r12d, %r12d
je 0x2e93e
movl %r12d, %edi
callq 0x6230
leaq 0x63eee(%rip), %rdi     # 0x92759
movl %r12d, %esi
movq %rax, %rdx
movq %r14, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x26bea
jmp 0x2e92f
movq 0x18(%rsp), %rax
movl (%rax), %r12d
cmpl $0x4, %r12d
jne 0x2e9a8
xorl %r12d, %r12d
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x6c40
movq 0x28(%rsp), %rax
movq 0x38(%rsp), %rcx
movl $0xf4240, %edi          # imm = 0xF4240
leaq (%rax,%rdi), %rdx
xorl %esi, %esi
cmpq %rcx, %rax
cmovgeq %rax, %rdx
setl %sil
subq %rcx, %rdx
movq 0x8(%rsp), %r8
leaq (%r8,%rdi), %rax
xorl %ecx, %ecx
cmpq %rdx, %r8
setl %cl
cmovgeq %r8, %rax
subq 0x20(%rsp), %r13
addq 0x30(%rsp), %r13
subq %rdx, %rax
addq %rsi, %r13
subq %rcx, %r13
testq %r13, %r13
cmovleq %r12, %r13
cmovsq %r12, %rax
movups 0x20(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq %rax, 0x8(%rsp)
jmp 0x2e944
movq 0x18(%rsp), %rax
movl (%rax), %r12d
movl %r12d, %edi
callq 0x6230
leaq 0x63e19(%rip), %rdi     # 0x9273b
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x26bea
movl %ebp, %edi
callq 0x6e80
movl $0x1, %r12d
jmp 0x2e944
movl $0x3, %r12d
testl %r12d, %r12d
je 0x2e7a3
cmpl $0x3, %r12d
je 0x2e70c
jmp 0x2e97c
movl %r12d, %edi
callq 0x6230
leaq 0x63e8b(%rip), %rdi     # 0x927f3
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x26bea
movl %ebp, %edi
callq 0x6e80
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x63e00(%rip), %rdi     # 0x92797
movl $0x2710, %edx           # imm = 0x2710
movq %r14, %rsi
xorl %eax, %eax
callq 0x26bea
jmp 0x2e975
movl %r12d, %edi
callq 0x6230
leaq 0x63d4a(%rip), %rdi     # 0x92701
jmp 0x2e968