#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;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int POLLERR;
int POLLHUP;
int POLLOUT;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
double monotonic_seconds();
scalar_t__ poll(struct pollfd *, int, double);
scalar_t__ sockaddrfill(struct sockaddr_in *, char const *, char const *,
                        int /*<<< orphan*/, int /*<<< orphan*/,
                        int /*<<< orphan*/);
int sockgetstatus(int);
scalar_t__ socknonblock(int);
int tcpstrtoconnect(int sock, const char *hostname, const char *service,
                    uint32_t msecto) {
  struct sockaddr_in sa;
  if (socknonblock(sock) < 0) {
    return -1;
  }
  if (sockaddrfill(&sa, hostname, service, AF_INET, SOCK_STREAM, 0) < 0) {
    return -1;
  }
  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):
<tcpstrtoconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <socknonblock@plt>
cmp    $0x0,%rax
jge    1196 <tcpstrtoconnect+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <tcpstrtoconnect+0x1f6>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0a(%rip),%rax
mov    (%rax),%r8d
lea    -0x28(%rbp),%rdi
xor    %r9d,%r9d
call   1030 <sockaddrfill@plt>
cmp    $0x0,%rax
jge    11d3 <tcpstrtoconnect+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <tcpstrtoconnect+0x1f6>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jl     11fa <tcpstrtoconnect+0x9a>
movl   $0x0,-0x4(%rbp)
jmp    1356 <tcpstrtoconnect+0x1f6>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    134f <tcpstrtoconnect+0x1ef>
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    0x2dc3(%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     1268 <tcpstrtoconnect+0x108>
jmp    12ea <tcpstrtoconnect+0x18a>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    0x2d27(%rip),%rcx
cmp    (%rcx),%rax
jne    12de <tcpstrtoconnect+0x17e>
mov    $0x0,%al
call   1070 <monotonic_seconds@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  0xd60(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     12d9 <tcpstrtoconnect+0x179>
mov    0x2ce8(%rip),%rax
mov    (%rax),%rcx
mov    0x2ce6(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <tcpstrtoconnect+0x1f6>
jmp    1228 <tcpstrtoconnect+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <tcpstrtoconnect+0x1f6>
mov    -0x48(%rbp),%eax
mov    0x2cec(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ccb(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1316 <tcpstrtoconnect+0x1b6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1356 <tcpstrtoconnect+0x1f6>
mov    -0x48(%rbp),%eax
mov    0x2cd8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     133b <tcpstrtoconnect+0x1db>
mov    -0x8(%rbp),%edi
call   1060 <sockgetstatus@plt>
mov    %eax,-0x4(%rbp)
jmp    1356 <tcpstrtoconnect+0x1f6>
mov    0x2c66(%rip),%rax
mov    (%rax),%rcx
mov    0x2c64(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpstrtoconnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x18(%rsp)
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
call   1040 <socknonblock@plt>
test   %rax,%rax
js     12d8 <tcpstrtoconnect+0x178>
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1d(%rip),%rax
mov    (%rax),%r8d
mov    %rsp,%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %r9d,%r9d
call   1030 <sockaddrfill@plt>
test   %rax,%rax
js     12d8 <tcpstrtoconnect+0x178>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    12dd <tcpstrtoconnect+0x17d>
mov    0x2dd6(%rip),%r12
mov    (%r12),%rax
mov    0x2deb(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    12dd <tcpstrtoconnect+0x17d>
xor    %eax,%eax
call   1070 <monotonic_seconds@plt>
movsd  %xmm0,0x20(%rsp)
xorpd  %xmm0,%xmm0
mov    0x2ded(%rip),%r13
lea    0x8(%rsp),%r14
mov    0x2d89(%rip),%rbx
movsd  0x18(%rsp),%xmm1
mov    $0xffffffff,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
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    1299 <tcpstrtoconnect+0x139>
mov    (%r12),%rax
cmp    (%rbx),%rax
jne    12dd <tcpstrtoconnect+0x17d>
xor    %eax,%eax
call   1070 <monotonic_seconds@plt>
subsd  0x20(%rsp),%xmm0
mulsd  0xd83(%rip),%xmm0
movsd  0x18(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jb     1230 <tcpstrtoconnect+0xd0>
mov    0x2d18(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
jmp    12dd <tcpstrtoconnect+0x17d>
mov    0x10(%rsp),%eax
mov    0x2d3c(%rip),%rcx
mov    0x2d1d(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    $0xffffffff,%ebp
test   %eax,%edx
jne    12dd <tcpstrtoconnect+0x17d>
test   %eax,0x0(%r13)
je     12ca <tcpstrtoconnect+0x16a>
mov    %r15d,%edi
call   1060 <sockgetstatus@plt>
mov    %eax,%ebp
jmp    12dd <tcpstrtoconnect+0x17d>
mov    0x2cd7(%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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@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 @tcpstrtoconnect(i32 %0, i8* %1, i8* %2, double %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%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 i8* %1, i8** %7
store i8* %2, i8** %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 %89
19: ; preds = %4
%20 = load i8*, i8** %7
%21 = load i8*, i8** %8
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i64 @sockaddrfill(%struct.sockaddr_in* %10, i8* %20, i8* %21, i32 %22, i32 %23, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -1, i32* %5
br label %89
27: ; preds = %19
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 4)
%31 = icmp sge i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 0, i32* %5
br label %89
33: ; preds = %27
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINPROGRESS
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %88
37: ; preds = %33
%38 = call double (...) @monotonic_seconds()
store double %38, double* %11
store double 0.000000e+00, double* %13
br label %39
39: ; preds = %37, %66
%40 = load i32, i32* %6
%41 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* @POLLOUT
%43 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 1
store i32 %42, i32* %43
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 2
store i32 0, i32* %44
%45 = load double, double* %9
%46 = load double, double* %13
%47 = fsub double %45, %46
%48 = call i64 @poll(%struct.pollfd* %14, i32 1, double %47)
%49 = icmp sge i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %39
br label %68
51: ; preds = %39
%52 = load i64, i64* @errno
%53 = load i64, i64* @EINTR
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %67
55: ; preds = %51
%56 = call double (...) @monotonic_seconds()
store double %56, double* %12
%57 = load double, double* %12
%58 = load double, double* %11
%59 = fsub double %57, %58
%60 = fmul double %59, 1.000000e+03
store double %60, double* %13
%61 = load double, double* %13
%62 = load double, double* %9
%63 = fcmp oge double %61, %62
br i1 %63, label %64, label %66
64: ; preds = %55
%65 = load i64, i64* @ETIMEDOUT
store i64 %65, i64* @errno
store i32 -1, i32* %5
br label %89
66: ; preds = %55
br label %39
67: ; preds = %51
store i32 -1, i32* %5
br label %89
68: ; preds = %50
%69 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 2
%70 = load i32, i32* %69
%71 = load i32, i32* @POLLHUP
%72 = load i32, i32* @POLLERR
%73 = or i32 %71, %72
%74 = and i32 %70, %73
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %77
76: ; preds = %68
store i32 -1, i32* %5
br label %89
77: ; preds = %68
%78 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 2
%79 = load i32, i32* %78
%80 = load i32, i32* @POLLOUT
%81 = and i32 %79, %80
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %77
%84 = load i32, i32* %6
%85 = call i32 @sockgetstatus(i32 %84)
store i32 %85, i32* %5
br label %89
86: ; preds = %77
%87 = load i64, i64* @ETIMEDOUT
store i64 %87, i64* @errno
br label %88
88: ; preds = %86, %33
store i32 -1, i32* %5
br label %89
89: ; preds = %88, %83, %76, %67, %64, %32, %26, %18
%90 = load i32, i32* %5
ret i32 %90
}

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