#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 int /*<<< orphan*/ val;
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};
typedef int socklen_t;

/* Variables and functions */
int EINPROGRESS;
int EINTR;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ POLLOUT;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
int poll(struct pollfd *, int, int);
int connect_timeout(int s, struct sockaddr *saddrp, int len, int time_secs) {
  struct pollfd pfds;
  int ret, oldfl, err, val;
  socklen_t val_len;
  oldfl = fcntl(s, F_GETFL, 0);
  if (!(oldfl & O_NONBLOCK))
    fcntl(s, F_SETFL, oldfl | O_NONBLOCK);
connect_timeout_connect_restart:
  ret = connect(s, saddrp, len);
  if (ret < 0) {
    if (errno == EINTR)
      goto connect_timeout_connect_restart;
    if (errno != EINPROGRESS)
      goto connect_timeout_err;
    pfds.fd = s;
    pfds.events = POLLOUT;
  connect_timeout_poll_restart:
    ret = poll(&pfds, 1, time_secs * 1000);
    if (ret < 0) {
      if (errno == EINTR)
        goto connect_timeout_poll_restart;
      else
        goto connect_timeout_err;
    }
    if (ret == 0) {
      err = errno;
      close(s);
      errno = err;
      goto connect_timeout_err;
    }
  }
  val_len = sizeof(val);
  ret = getsockopt(s, SOL_SOCKET, SO_ERROR, &val, &val_len);
  if (val != 0) {
    errno = val;
    goto connect_timeout_err;
  }
  fcntl(s, F_SETFL, oldfl & (~O_NONBLOCK));
  return 0;
connect_timeout_err:
  err = errno;
  fcntl(s, F_SETFL, oldfl);
  errno = err;
  return -1;
}


x86 assembly (clang, -O0):
<connect_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2e53(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11ad <connect_timeout+0x5d>
mov    -0x8(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
jmp    11b2 <connect_timeout+0x62>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1292 <connect_timeout+0x142>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
jne    11eb <connect_timeout+0x9b>
jmp    11b2 <connect_timeout+0x62>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%eax
je     1208 <connect_timeout+0xb8>
jmp    1307 <connect_timeout+0x1b7>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
imul   $0x3e8,-0x18(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1060 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    125e <connect_timeout+0x10e>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%eax
jne    1259 <connect_timeout+0x109>
jmp    121a <connect_timeout+0xca>
jmp    1307 <connect_timeout+0x1b7>
cmpl   $0x0,-0x24(%rbp)
jne    128d <connect_timeout+0x13d>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2d32(%rip),%rax
mov    %ecx,(%rax)
jmp    1307 <connect_timeout+0x1b7>
jmp    1292 <connect_timeout+0x142>
movl   $0x4,-0x34(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%r8
call   1050 <getsockopt@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     12d9 <connect_timeout+0x189>
mov    -0x30(%rbp),%ecx
mov    0x2ce6(%rip),%rax
mov    %ecx,(%rax)
jmp    1307 <connect_timeout+0x1b7>
mov    -0x8(%rbp),%edi
mov    0x2cdd(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    0x2ce9(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
movl   $0x0,-0x4(%rbp)
jmp    133a <connect_timeout+0x1ea>
mov    0x2caa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ca3(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
call   1030 <fcntl@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2c87(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x4(%rsp)
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r12d
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%r14d
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
test   %r14d,%edx
jne    119f <connect_timeout+0x4f>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
or     %r14d,%edx
mov    %r12d,%edi
call   1030 <fcntl@plt>
mov    %r14d,(%rsp)
mov    0x2e0e(%rip),%r13
mov    0x2def(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <connect@plt>
test   %eax,%eax
jns    1234 <connect_timeout+0xe4>
mov    0x0(%r13),%r15d
cmp    (%r14),%r15d
je     11c0 <connect_timeout+0x70>
mov    0x2def(%rip),%rax
cmp    (%rax),%r15d
jne    126e <connect_timeout+0x11e>
mov    %r12d,0x10(%rsp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
imul   $0x3e8,0x4(%rsp),%ebx
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1060 <poll@plt>
test   %eax,%eax
jns    122e <connect_timeout+0xde>
mov    0x0(%r13),%r15d
cmp    (%r14),%r15d
je     1210 <connect_timeout+0xc0>
jmp    126e <connect_timeout+0x11e>
je     12bf <connect_timeout+0x16f>
movl   $0x4,0x8(%rsp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %r12d,%edi
call   1050 <getsockopt@plt>
mov    0xc(%rsp),%r15d
test   %r15d,%r15d
je     128d <connect_timeout+0x13d>
mov    %r15d,0x0(%r13)
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    (%rsp),%edx
call   1030 <fcntl@plt>
mov    %r15d,0x0(%r13)
mov    $0xffffffff,%eax
jmp    12b0 <connect_timeout+0x160>
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
not    %eax
mov    (%rsp),%edx
and    %eax,%edx
mov    %r12d,%edi
call   1030 <fcntl@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%r13),%r15d
mov    %r12d,%edi
call   1040 <close@plt>
jmp    126a <connect_timeout+0x11a>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.pollfd = type { i32, i32 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EINPROGRESS = global i32 0
@POLLOUT = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @connect_timeout(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.pollfd
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store %struct.sockaddr* %1, %struct.sockaddr** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%16 = load i32, i32* %6
%17 = load i32, i32* @F_GETFL
%18 = call i32 @fcntl(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = load i32, i32* @O_NONBLOCK
%21 = and i32 %19, %20
%22 = icmp ne i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %4
%24 = load i32, i32* %6
%25 = load i32, i32* @F_SETFL
%26 = load i32, i32* %12
%27 = load i32, i32* @O_NONBLOCK
%28 = or i32 %26, %27
%29 = call i32 @fcntl(i32 %24, i32 %25, i32 %28)
br label %30
30: ; preds = %23, %4
br label %31
31: ; preds = %42, %30
%32 = load i32, i32* %6
%33 = load %struct.sockaddr*, %struct.sockaddr** %7
%34 = load i32, i32* %8
%35 = call i32 @connect(i32 %32, %struct.sockaddr* %33, i32 %34)
store i32 %35, i32* %11
%36 = load i32, i32* %11
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %74
38: ; preds = %31
%39 = load i32, i32* @errno
%40 = load i32, i32* @EINTR
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %38
br label %31
43: ; preds = %38
%44 = load i32, i32* @errno
%45 = load i32, i32* @EINPROGRESS
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
br label %91
48: ; preds = %43
%49 = load i32, i32* %6
%50 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
store i32 %49, i32* %50
%51 = load i32, i32* @POLLOUT
%52 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
store i32 %51, i32* %52
br label %53
53: ; preds = %63, %48
%54 = load i32, i32* %9
%55 = mul nsw i32 %54, 1000
%56 = call i32 @poll(%struct.pollfd* %10, i32 1, i32 %55)
store i32 %56, i32* %11
%57 = load i32, i32* %11
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %53
%60 = load i32, i32* @errno
%61 = load i32, i32* @EINTR
%62 = icmp eq i32 %60, %61
br i1 %62, label %63, label %64
63: ; preds = %59
br label %53
64: ; preds = %59
br label %91
65: ; preds = %53
%66 = load i32, i32* %11
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %65
%69 = load i32, i32* @errno
store i32 %69, i32* %13
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %13
store i32 %72, i32* @errno
br label %91
73: ; preds = %65
br label %74
74: ; preds = %73, %31
store i32 4, i32* %15
%75 = load i32, i32* %6
%76 = load i32, i32* @SOL_SOCKET
%77 = load i32, i32* @SO_ERROR
%78 = call i32 @getsockopt(i32 %75, i32 %76, i32 %77, i32* %14, i32* %15)
store i32 %78, i32* %11
%79 = load i32, i32* %14
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %74
%82 = load i32, i32* %14
store i32 %82, i32* @errno
br label %91
83: ; preds = %74
%84 = load i32, i32* %6
%85 = load i32, i32* @F_SETFL
%86 = load i32, i32* %12
%87 = load i32, i32* @O_NONBLOCK
%88 = xor i32 %87, -1
%89 = and i32 %86, %88
%90 = call i32 @fcntl(i32 %84, i32 %85, i32 %89)
store i32 0, i32* %5
br label %98
91: ; preds = %81, %68, %64, %47
%92 = load i32, i32* @errno
store i32 %92, i32* %13
%93 = load i32, i32* %6
%94 = load i32, i32* @F_SETFL
%95 = load i32, i32* %12
%96 = call i32 @fcntl(i32 %93, i32 %94, i32 %95)
%97 = load i32, i32* %13
store i32 %97, i32* @errno
store i32 -1, i32* %5
br label %98
98: ; preds = %91, %83
%99 = load i32, i32* %5
ret i32 %99
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.pollfd = type { i32, i32 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EINPROGRESS = global i32 0
@POLLOUT = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @connect_timeout(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca %struct.pollfd
%6 = alloca i32
%7 = alloca i32
%8 = bitcast %struct.pollfd* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 @fcntl(i32 %0, i32 %11, i32 0)
%13 = load i32, i32* @O_NONBLOCK
%14 = and i32 %13, %12
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %4
%17 = load i32, i32* @F_SETFL
%18 = or i32 %13, %12
%19 = tail call i32 @fcntl(i32 %0, i32 %17, i32 %18)
br label %20
20: ; preds = %16, %4
br label %21
21: ; preds = %20, %24
%22 = tail call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %48
24: ; preds = %21
%25 = load i32, i32* @errno
%26 = load i32, i32* @EINTR
%27 = icmp eq i32 %25, %26
br i1 %27, label %21, label %28
28: ; preds = %24
%29 = load i32, i32* @EINPROGRESS
%30 = icmp eq i32 %25, %29
br i1 %30, label %31, label %62
31: ; preds = %28
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %0, i32* %32
%33 = load i32, i32* @POLLOUT
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %33, i32* %34
%35 = mul nsw i32 %3, 1000
br label %36
36: ; preds = %39, %31
%37 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %35)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i32, i32* @errno
%41 = load i32, i32* @EINTR
%42 = icmp eq i32 %40, %41
br i1 %42, label %36, label %62
43: ; preds = %36
%44 = icmp eq i32 %37, 0
br i1 %44, label %45, label %48
45: ; preds = %43
%46 = load i32, i32* @errno
%47 = call i32 @close(i32 %0)
br label %60
48: ; preds = %21, %43
store i32 4, i32* %7
%49 = load i32, i32* @SOL_SOCKET
%50 = load i32, i32* @SO_ERROR
%51 = call i32 @getsockopt(i32 %0, i32 %49, i32 %50, i32* %6, i32* %7)
%52 = load i32, i32* %6
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %48
%55 = load i32, i32* @F_SETFL
%56 = load i32, i32* @O_NONBLOCK
%57 = xor i32 %56, -1
%58 = and i32 %12, %57
%59 = call i32 @fcntl(i32 %0, i32 %55, i32 %58)
br label %66
60: ; preds = %48, %45
%61 = phi i32 [ %46, %45 ], [ %52, %48 ]
store i32 %61, i32* @errno
br label %62
62: ; preds = %39, %60, %28
%63 = phi i32 [ %25, %28 ], [ %61, %60 ], [ %40, %39 ]
%64 = load i32, i32* @F_SETFL
%65 = call i32 @fcntl(i32 %0, i32 %64, i32 %12)
store i32 %63, i32* @errno
br label %66
66: ; preds = %62, %54
%67 = phi i32 [ -1, %62 ], [ 0, %54 ]
ret i32 %67
}