// File: /workspace/github_c_makefile_star2_O1/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 0x2e3dc
movl %esi, %r14d
movq %rdi, %r15
leaq 0x60(%rsp), %rdx
callq 0x4a78e
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x2e63f
callq 0x3132b
movl %eax, %ebp
testl %eax, %eax
js 0x2e3d5
movl %ebp, %edi
movl $0x1, %esi
callq 0x312af
testl %eax, %eax
js 0x2e638
leaq 0x60(%rsp), %rsi
movl %ebp, %edi
movl $0x10, %edx
callq 0x6580
testl %eax, %eax
js 0x2e3f4
movl %ebp, %edi
xorl %esi, %esi
callq 0x312af
testl %eax, %eax
js 0x2e638
movl %ebp, %edi
movl $0x78, %esi
callq 0x2e2d4
testl %eax, %eax
js 0x2e638
movl %ebp, %ebx
jmp 0x2e63f
leaq 0x64212(%rip), %rdi     # 0x925f5
xorl %eax, %eax
callq 0x268ca
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2e63f
callq 0x6060
movq %rax, 0x18(%rsp)
movl (%rax), %r12d
cmpl $0x73, %r12d
jne 0x2e61c
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 0x2e527
je 0x2e5af
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 0x2e5d8
movl 0x20(%rsp), %r12d
testl %r12d, %r12d
je 0x2e614
movl %r12d, %edi
callq 0x6230
leaq 0x6415a(%rip), %rdi     # 0x92669
movl %r12d, %esi
movq %rax, %rdx
movq %r15, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x268ca
jmp 0x2e5fc
movq 0x18(%rsp), %rax
movl (%rax), %r12d
cmpl $0x4, %r12d
jne 0x2e5c7
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 0x2e609
movl $0x2710, %edx           # imm = 0x2710
leaq 0x640ec(%rip), %rdi     # 0x926a7
movq %r15, %rsi
xorl %eax, %eax
callq 0x268ca
jmp 0x2e5fc
movl %r12d, %edi
callq 0x6230
leaq 0x6403b(%rip), %rdi     # 0x92611
jmp 0x2e5ef
movq 0x18(%rsp), %rax
movl (%rax), %r12d
movl %r12d, %edi
callq 0x6230
leaq 0x6405c(%rip), %rdi     # 0x9264b
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x268ca
movl %ebp, %edi
callq 0x6e80
movl $0x1, %r12d
testl %r12d, %r12d
je 0x2e447
jmp 0x2e653
movl $0x3, %r12d
jmp 0x2e609
movl %r12d, %edi
callq 0x6230
leaq 0x640d8(%rip), %rdi     # 0x92703
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x268ca
movl %ebp, %edi
callq 0x6e80
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3, %r12d
je 0x2e3b0
jmp 0x2e63f