// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/lib/net.c
// Function: netConnectWithTimeout
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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x41777
leaq 0x9ad39(%rip), %rdi     # 0xdc49d
movb $0x0, %al
callq 0x358e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
leaq -0x34(%rbp), %rdx
callq 0x6c410
cmpl $0x0, %eax
jne 0x41798
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
callq 0x45dc0
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
jge 0x417b0
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x41bd4
movl -0x24(%rbp), %edi
movl $0x1, %esi
callq 0x45c80
cmpl $0x0, %eax
jge 0x417d6
movl -0x24(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
movl -0x24(%rbp), %edi
leaq -0x34(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rsi
movl $0x10, %edx
callq 0x6580
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0x41b8b
callq 0x6060
cmpl $0x73, (%rax)
jne 0x41b48
leaq -0xd0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6c40
movq -0x20(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, -0xe0(%rbp)
movq -0x20(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
subq -0xe0(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
movq %rax, -0xd8(%rbp)
jmp 0x4185a
leaq -0xb8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl $0x0, -0xe4(%rbp)
movl -0xe4(%rbp), %eax
cmpq $0x10, %rax
jae 0x418a4
movq -0xf0(%rbp), %rax
movl -0xe4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x41872
jmp 0x418a6
movl -0x24(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x24(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xb8(%rbp,%rax,8), %rcx
movq %rcx, -0xb8(%rbp,%rax,8)
movq -0xe0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl -0x24(%rbp), %edi
addl $0x1, %edi
xorl %eax, %eax
movl %eax, %esi
leaq -0xb8(%rbp), %rcx
leaq -0x100(%rbp), %r8
movq %rcx, %rdx
callq 0x66b0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0x41a45
callq 0x6060
cmpl $0x4, (%rax)
jne 0x419fc
leaq -0x110(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6c40
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rsi
movq -0xd0(%rbp), %rdx
movq -0xc8(%rbp), %rcx
callq 0x45d50
movq %rax, -0x120(%rbp)
movq %rdx, -0x118(%rbp)
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0x120(%rbp), %rdx
movq -0x118(%rbp), %rcx
callq 0x45d50
movq %rax, -0x130(%rbp)
movq %rdx, -0x128(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xe0(%rbp)
jge 0x419de
movq $0x0, -0xe0(%rbp)
movq $0x0, -0xd8(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x41a40
callq 0x6060
movl (%rax), %eax
movl %eax, -0x13c(%rbp)
callq 0x6060
movl (%rax), %edi
callq 0x6220
movl -0x13c(%rbp), %esi
movq %rax, %rdx
leaq 0x9aa94(%rip), %rdi     # 0xdc4b9
movb $0x0, %al
callq 0x358e0
movl -0x24(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
jmp 0x41b41
cmpl $0x0, -0x38(%rbp)
jle 0x41b17
movl $0x4, -0x138(%rbp)
movl -0x24(%rbp), %edi
movl $0x1, %esi
movl $0x4, %edx
leaq -0x134(%rbp), %rcx
leaq -0x138(%rbp), %r8
callq 0x64d0
cmpl $0x0, %eax
jge 0x41ac2
callq 0x6060
movl (%rax), %eax
movl %eax, -0x140(%rbp)
callq 0x6060
movl (%rax), %edi
callq 0x6220
movl -0x140(%rbp), %esi
movq %rax, %rdx
leaq 0x9aa4c(%rip), %rdi     # 0xdc4f3
movb $0x0, %al
callq 0x358e0
movl -0x24(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
cmpl $0x0, -0x134(%rbp)
je 0x41b15
movl -0x134(%rbp), %eax
movl %eax, -0x144(%rbp)
movl -0x134(%rbp), %edi
callq 0x6220
movl -0x144(%rbp), %esi
movq %rax, %rdx
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %r8d
leaq 0x9aa17(%rip), %rdi     # 0xdc511
movb $0x0, %al
callq 0x358e0
movl -0x24(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
jmp 0x41b46
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x9aa29(%rip), %rdi     # 0xdc54f
movb $0x0, %al
callq 0x358e0
movl -0x24(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
jmp 0x41858
jmp 0x41b89
callq 0x6060
movl (%rax), %eax
movl %eax, -0x148(%rbp)
callq 0x6060
movl (%rax), %edi
callq 0x6220
movl -0x148(%rbp), %esi
movq %rax, %rdx
leaq 0x9aa3a(%rip), %rdi     # 0xdc5ab
movb $0x0, %al
callq 0x358e0
movl -0x24(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
jmp 0x41b8b
movl -0x24(%rbp), %edi
xorl %esi, %esi
callq 0x45c80
cmpl $0x0, %eax
jge 0x41bab
movl -0x24(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
movl -0x24(%rbp), %edi
movl $0x78, %esi
callq 0x41670
cmpl $0x0, %eax
jge 0x41bce
movl -0x24(%rbp), %edi
callq 0x6e90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x41bd4
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq