#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef double uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
scalar_t__ EINPROGRESS;
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int POLLERR;
int POLLHUP;
int POLLOUT;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
double monotonic_seconds();
scalar_t__ poll(struct pollfd *, int, double);
int /*<<< orphan*/ sockaddrnumfill(struct sockaddr_in *, double,
                                   int /*<<< orphan*/);
int sockgetstatus(int);
scalar_t__ socknonblock(int);
int tcpnumtoconnect(int sock, uint32_t ip, uint16_t port, uint32_t msecto) {
  struct sockaddr_in sa;
  if (socknonblock(sock) < 0) {
    return -1;
  }
  sockaddrnumfill(&sa, ip, port);
  if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) >= 0) {
    return 0;
  }
  if (errno == EINPROGRESS) {
    double s, c;
    uint32_t msecpassed;
    struct pollfd pfd;
    s = monotonic_seconds();
    msecpassed = 0;
    while (1) {
      pfd.fd = sock;
      pfd.events = POLLOUT;
      pfd.revents = 0;
      if (poll(&pfd, 1, msecto - msecpassed) >= 0) {
        break;
      }
      if (errno == EINTR) {
        c = monotonic_seconds();
        msecpassed = (c - s) * 1000.0;
        if (msecpassed >= msecto) {
          errno = ETIMEDOUT;
          return -1;
        }
        continue;
      } else {
        return -1;
      }
    }
    if (pfd.revents & (POLLHUP | POLLERR)) {
      return -1;
    }
    if (pfd.revents & POLLOUT) {
      return sockgetstatus(sock);
    }
    errno = ETIMEDOUT;
  }
  return -1;
}


x86 assembly (clang, -O0):
<tcpnumtoconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movsd  %xmm1,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <socknonblock@plt>
cmp    $0x0,%rax
jge    1196 <tcpnumtoconnect+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <tcpnumtoconnect+0x1ca>
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1040 <sockaddrnumfill@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jl     11ce <tcpnumtoconnect+0x6e>
movl   $0x0,-0x4(%rbp)
jmp    132a <tcpnumtoconnect+0x1ca>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
jne    1323 <tcpnumtoconnect+0x1c3>
mov    $0x0,%al
call   1070 <monotonic_seconds@plt>
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
movl   $0x0,-0x48(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
lea    -0x50(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
cmp    $0x0,%rax
jl     123c <tcpnumtoconnect+0xdc>
jmp    12be <tcpnumtoconnect+0x15e>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
jne    12b2 <tcpnumtoconnect+0x152>
mov    $0x0,%al
call   1070 <monotonic_seconds@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  0xd8c(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     12ad <tcpnumtoconnect+0x14d>
mov    0x2d24(%rip),%rax
mov    (%rax),%rcx
mov    0x2d22(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <tcpnumtoconnect+0x1ca>
jmp    11fc <tcpnumtoconnect+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <tcpnumtoconnect+0x1ca>
mov    -0x48(%rbp),%eax
mov    0x2d18(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2cff(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     12ea <tcpnumtoconnect+0x18a>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <tcpnumtoconnect+0x1ca>
mov    -0x48(%rbp),%eax
mov    0x2d04(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     130f <tcpnumtoconnect+0x1af>
mov    -0x8(%rbp),%edi
call   1060 <sockgetstatus@plt>
mov    %eax,-0x4(%rbp)
jmp    132a <tcpnumtoconnect+0x1ca>
mov    0x2ca2(%rip),%rax
mov    (%rax),%rcx
mov    0x2ca0(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpnumtoconnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movsd  %xmm1,0x18(%rsp)
mov    %esi,%ebx
movsd  %xmm0,(%rsp)
mov    %edi,%r15d
call   1030 <socknonblock@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     12bc <tcpnumtoconnect+0x15c>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
movsd  (%rsp),%xmm0
mov    %ebx,%esi
call   1040 <sockaddrnumfill@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
xor    %ebp,%ebp
test   %rax,%rax
jns    12bc <tcpnumtoconnect+0x15c>
mov    0x2df9(%rip),%r12
mov    (%r12),%rax
mov    0x2e06(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    12bc <tcpnumtoconnect+0x15c>
xor    %eax,%eax
call   1070 <monotonic_seconds@plt>
movsd  %xmm0,(%rsp)
xorpd  %xmm0,%xmm0
mov    0x2e01(%rip),%r13
lea    0x8(%rsp),%r14
mov    0x2dad(%rip),%rbx
movsd  0x18(%rsp),%xmm1
mov    $0xffffffff,%ebp
xchg   %ax,%ax
mov    %r15d,0x8(%rsp)
mov    0x0(%r13),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
subsd  %xmm0,%xmm1
mov    %r14,%rdi
mov    $0x1,%esi
movapd %xmm1,%xmm0
call   1050 <poll@plt>
test   %rax,%rax
jns    1278 <tcpnumtoconnect+0x118>
mov    (%r12),%rax
cmp    (%rbx),%rax
jne    12bc <tcpnumtoconnect+0x15c>
xor    %eax,%eax
call   1070 <monotonic_seconds@plt>
subsd  (%rsp),%xmm0
mulsd  0xda4(%rip),%xmm0
movsd  0x18(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jb     1210 <tcpnumtoconnect+0xb0>
mov    0x2d49(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
jmp    12bc <tcpnumtoconnect+0x15c>
mov    0x10(%rsp),%eax
mov    0x2d5d(%rip),%rcx
mov    0x2d46(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    $0xffffffff,%ebp
test   %eax,%edx
jne    12bc <tcpnumtoconnect+0x15c>
test   %eax,0x0(%r13)
je     12a9 <tcpnumtoconnect+0x149>
mov    %r15d,%edi
call   1060 <sockgetstatus@plt>
mov    %eax,%ebp
jmp    12bc <tcpnumtoconnect+0x15c>
mov    0x2d08(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@POLLOUT = global i32 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
define i32 @tcpnumtoconnect(i32 %0, double %1, i32 %2, double %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca i32
%9 = alloca double
%10 = alloca %struct.sockaddr_in
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca %struct.pollfd
store i32 %0, i32* %6
store double %1, double* %7
store i32 %2, i32* %8
store double %3, double* %9
%15 = load i32, i32* %6
%16 = call i64 @socknonblock(i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %84
19: ; preds = %4
%20 = load double, double* %7
%21 = load i32, i32* %8
%22 = call i32 @sockaddrnumfill(%struct.sockaddr_in* %10, double %20, i32 %21)
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 4)
%26 = icmp sge i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
store i32 0, i32* %5
br label %84
28: ; preds = %19
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINPROGRESS
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %83
32: ; preds = %28
%33 = call double (...) @monotonic_seconds()
store double %33, double* %11
store double 0.000000e+00, double* %13
br label %34
34: ; preds = %32, %61
%35 = load i32, i32* %6
%36 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* @POLLOUT
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 2
store i32 0, i32* %39
%40 = load double, double* %9
%41 = load double, double* %13
%42 = fsub double %40, %41
%43 = call i64 @poll(%struct.pollfd* %14, i32 1, double %42)
%44 = icmp sge i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %34
br label %63
46: ; preds = %34
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINTR
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %62
50: ; preds = %46
%51 = call double (...) @monotonic_seconds()
store double %51, double* %12
%52 = load double, double* %12
%53 = load double, double* %11
%54 = fsub double %52, %53
%55 = fmul double %54, 1.000000e+03
store double %55, double* %13
%56 = load double, double* %13
%57 = load double, double* %9
%58 = fcmp oge double %56, %57
br i1 %58, label %59, label %61
59: ; preds = %50
%60 = load i64, i64* @ETIMEDOUT
store i64 %60, i64* @errno
store i32 -1, i32* %5
br label %84
61: ; preds = %50
br label %34
62: ; preds = %46
store i32 -1, i32* %5
br label %84
63: ; preds = %45
%64 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 2
%65 = load i32, i32* %64
%66 = load i32, i32* @POLLHUP
%67 = load i32, i32* @POLLERR
%68 = or i32 %66, %67
%69 = and i32 %65, %68
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %63
store i32 -1, i32* %5
br label %84
72: ; preds = %63
%73 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 2
%74 = load i32, i32* %73
%75 = load i32, i32* @POLLOUT
%76 = and i32 %74, %75
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %72
%79 = load i32, i32* %6
%80 = call i32 @sockgetstatus(i32 %79)
store i32 %80, i32* %5
br label %84
81: ; preds = %72
%82 = load i64, i64* @ETIMEDOUT
store i64 %82, i64* @errno
br label %83
83: ; preds = %81, %28
store i32 -1, i32* %5
br label %84
84: ; preds = %83, %78, %71, %62, %59, %27, %18
%85 = load i32, i32* %5
ret i32 %85
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@POLLOUT = global i32 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
define i32 @tcpnumtoconnect(i32 %0, double %1, i32 %2, double %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.pollfd
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = tail call i64 @socknonblock(i32 %0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %59, label %10
10: ; preds = %4
%11 = call i32 @sockaddrnumfill(%struct.sockaddr_in* %5, double %1, i32 %2)
%12 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%13 = call i64 @connect(i32 %0, %struct.sockaddr* %12, i32 4)
%14 = icmp sgt i64 %13, -1
br i1 %14, label %59, label %15
15: ; preds = %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINPROGRESS
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %59
19: ; preds = %15
%20 = bitcast %struct.pollfd* %6 to i8*
%21 = call double (...) @monotonic_seconds()
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 0
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 1
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i64 0, i32 2
br label %25
25: ; preds = %35, %19
%26 = phi double [ 0.000000e+00, %19 ], [ %38, %35 ]
store i32 %0, i32* %22
%27 = load i32, i32* @POLLOUT
store i32 %27, i32* %23
store i32 0, i32* %24
%28 = fsub double %3, %26
%29 = call i64 @poll(%struct.pollfd* %6, i32 1, double %28)
%30 = icmp sgt i64 %29, -1
br i1 %30, label %42, label %31
31: ; preds = %25
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %55
35: ; preds = %31
%36 = call double (...) @monotonic_seconds()
%37 = fsub double %36, %21
%38 = fmul double %37, 1.000000e+03
%39 = fcmp ult double %38, %3
br i1 %39, label %25, label %40
40: ; preds = %35
%41 = load i64, i64* @ETIMEDOUT
store i64 %41, i64* @errno
br label %55
42: ; preds = %25
%43 = load i32, i32* %24
%44 = load i32, i32* @POLLHUP
%45 = load i32, i32* @POLLERR
%46 = or i32 %45, %44
%47 = and i32 %46, %43
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %42
%50 = load i32, i32* @POLLOUT
%51 = and i32 %50, %43
%52 = icmp eq i32 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %49
%54 = call i32 @sockgetstatus(i32 %0)
br label %55
55: ; preds = %31, %53, %40, %42
%56 = phi i32 [ -1, %42 ], [ -1, %40 ], [ %54, %53 ], [ -1, %31 ]
br label %59
57: ; preds = %49
%58 = load i64, i64* @ETIMEDOUT
store i64 %58, i64* @errno
br label %59
59: ; preds = %15, %57, %55, %10, %4
%60 = phi i32 [ -1, %4 ], [ 0, %10 ], [ %56, %55 ], [ -1, %57 ], [ -1, %15 ]
ret i32 %60
}