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

/* Variables and functions */
int EINPROGRESS;
int EINTR;
int ETIMEDOUT;
int /*<<< orphan*/ POLLOUT;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int connect(int, struct sockaddr *, int);
int errno;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ setnonblocking(int);
int connect_timeout(int fd, struct sockaddr_in *addr, int timeout) {
  int ret = 0;
  setnonblocking(fd);
  socklen_t len = sizeof(struct sockaddr_in);
  struct pollfd fds[1];
  fds[0].fd = fd;
  fds[0].events = POLLOUT;
  fds[0].revents = 0;
  ret = connect(fd, (struct sockaddr *)addr, len);
  if (ret < 0 && errno == EINPROGRESS) {
    do {
      ret = poll(fds, 1, timeout);
    } while (ret < 0 && errno == EINTR);
    if (ret == 0) {
      errno = ETIMEDOUT;
      return -1;
    } else if (ret < 0)
      return -1;
    else if (ret == 1) {
      int err;
      socklen_t socklen = sizeof(err);
      int sockoptret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &socklen);
      if (sockoptret == -1) {
        return -1;
      }
      if (err == 0)
        return 0;
      else {
        errno = err;
        return -1;
      }
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<connect_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <setnonblocking@plt>
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12c8 <connect_timeout+0x188>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%eax
jne    12c8 <connect_timeout+0x188>
jmp    11bb <connect_timeout+0x7b>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x4d(%rbp)
jge    11f6 <connect_timeout+0xb6>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x4d(%rbp)
mov    -0x4d(%rbp),%al
test   $0x1,%al
jne    11bb <connect_timeout+0x7b>
cmpl   $0x0,-0x18(%rbp)
jne    1229 <connect_timeout+0xe9>
mov    0x2da6(%rip),%rax
mov    (%rax),%ecx
mov    0x2dad(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <connect_timeout+0x18e>
cmpl   $0x0,-0x18(%rbp)
jge    123f <connect_timeout+0xff>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <connect_timeout+0x18e>
cmpl   $0x1,-0x18(%rbp)
jne    12b9 <connect_timeout+0x179>
movl   $0x4,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1030 <getsockopt@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    128b <connect_timeout+0x14b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <connect_timeout+0x18e>
cmpl   $0x0,-0x44(%rbp)
jne    12a1 <connect_timeout+0x161>
movl   $0x0,-0x4(%rbp)
jmp    12ce <connect_timeout+0x18e>
mov    -0x44(%rbp),%ecx
mov    0x2d1d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <connect_timeout+0x18e>
jmp    12be <connect_timeout+0x17e>
jmp    12c3 <connect_timeout+0x183>
jmp    12c8 <connect_timeout+0x188>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
call   1050 <setnonblocking@plt>
mov    %r14d,0x10(%rsp)
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %eax,%eax
jns    123b <connect_timeout+0xfb>
mov    0x2e35(%rip),%r15
mov    (%r15),%ecx
mov    0x2e3b(%rip),%rdx
cmp    (%rdx),%ecx
jne    123b <connect_timeout+0xfb>
lea    0x10(%rsp),%r12
mov    0x2dff(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1040 <poll@plt>
test   %eax,%eax
jns    11da <connect_timeout+0x9a>
mov    (%rbx),%ecx
cmp    %ecx,(%r15)
je     11c0 <connect_timeout+0x80>
test   %eax,%eax
je     122a <connect_timeout+0xea>
js     1236 <connect_timeout+0xf6>
cmp    $0x1,%eax
jne    123b <connect_timeout+0xfb>
movl   $0x4,0x8(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %r14d,%edi
call   1030 <getsockopt@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     123b <connect_timeout+0xfb>
mov    0xc(%rsp),%ecx
test   %ecx,%ecx
je     1248 <connect_timeout+0x108>
mov    %ecx,(%r15)
jmp    123b <connect_timeout+0xfb>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r15)
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    123b <connect_timeout+0xfb>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.pollfd = type { i32, i64, i32 }
%struct.sockaddr = type { i32 }
@POLLOUT = global i32 0
@errno = global i32 0
@EINPROGRESS = global i32 0
@EINTR = global i32 0
@ETIMEDOUT = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @connect_timeout(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1 x %struct.pollfd]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%14 = load i32, i32* %5
%15 = call i32 @setnonblocking(i32 %14)
store i32 4, i32* %9
%16 = load i32, i32* %5
%17 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %10, i64 0, i64 0
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* @POLLOUT
%20 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %10, i64 0, i64 0
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %20, i32 0, i32 2
store i32 %19, i32* %21
%22 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %10, i64 0, i64 0
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %22, i32 0, i32 1
store i64 0, i64* %23
%24 = load i32, i32* %5
%25 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%26 = bitcast %struct.sockaddr_in* %25 to %struct.sockaddr*
%27 = load i32, i32* %9
%28 = call i32 @connect(i32 %24, %struct.sockaddr* %26, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %78
31: ; preds = %3
%32 = load i32, i32* @errno
%33 = load i32, i32* @EINPROGRESS
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %78
35: ; preds = %31
br label %36
36: ; preds = %47, %35
%37 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %10, i64 0, i64 0
%38 = load i32, i32* %7
%39 = call i32 @poll(%struct.pollfd* %37, i32 1, i32 %38)
store i32 %39, i32* %8
br label %40
40: ; preds = %36
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* @errno
%45 = load i32, i32* @EINTR
%46 = icmp eq i32 %44, %45
br label %47
47: ; preds = %43, %40
%48 = phi i1 [ false, %40 ], [ %46, %43 ]
br i1 %48, label %36, label %49
49: ; preds = %47
%50 = load i32, i32* %8
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = load i32, i32* @ETIMEDOUT
store i32 %53, i32* @errno
store i32 -1, i32* %4
br label %80
54: ; preds = %49
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 -1, i32* %4
br label %80
58: ; preds = %54
%59 = load i32, i32* %8
%60 = icmp eq i32 %59, 1
br i1 %60, label %61, label %75
61: ; preds = %58
store i32 4, i32* %12
%62 = load i32, i32* %5
%63 = load i32, i32* @SOL_SOCKET
%64 = load i32, i32* @SO_ERROR
%65 = call i32 @getsockopt(i32 %62, i32 %63, i32 %64, i32* %11, i32* %12)
store i32 %65, i32* %13
%66 = load i32, i32* %13
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %69
68: ; preds = %61
store i32 -1, i32* %4
br label %80
69: ; preds = %61
%70 = load i32, i32* %11
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %69
store i32 0, i32* %4
br label %80
73: ; preds = %69
%74 = load i32, i32* %11
store i32 %74, i32* @errno
store i32 -1, i32* %4
br label %80
75: ; preds = %58
br label %76
76: ; preds = %75
br label %77
77: ; preds = %76
br label %78
78: ; preds = %77, %31, %3
%79 = load i32, i32* %8
store i32 %79, i32* %4
br label %80
80: ; preds = %78, %73, %72, %68, %57, %52
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.pollfd = type { i32, i64, i32 }
%struct.sockaddr = type { i32 }
@POLLOUT = global i32 0
@errno = global i32 0
@EINPROGRESS = global i32 0
@EINTR = global i32 0
@ETIMEDOUT = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @connect_timeout(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca [1 x %struct.pollfd]
%5 = alloca i32
%6 = alloca i32
%7 = tail call i32 @setnonblocking(i32 %0)
%8 = bitcast [1 x %struct.pollfd]* %4 to i8*
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
store i32 %0, i32* %10
%11 = load i32, i32* @POLLOUT
%12 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
store i64 0, i64* %13
%14 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%15 = tail call i32 @connect(i32 %0, %struct.sockaddr* %14, i32 4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %48
17: ; preds = %3
%18 = load i32, i32* @errno
%19 = load i32, i32* @EINPROGRESS
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %48
21: ; preds = %17, %21
%22 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 %2)
%23 = icmp slt i32 %22, 0
%24 = load i32, i32* @errno
%25 = load i32, i32* @EINTR
%26 = icmp eq i32 %24, %25
%27 = select i1 %23, i1 %26, i1 false
br i1 %27, label %21, label %28
28: ; preds = %21
%29 = icmp eq i32 %22, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = load i32, i32* @ETIMEDOUT
store i32 %31, i32* @errno
br label %48
32: ; preds = %28
br i1 %23, label %48, label %33
33: ; preds = %32
%34 = icmp eq i32 %22, 1
br i1 %34, label %35, label %48
35: ; preds = %33
%36 = bitcast i32* %5 to i8*
%37 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_ERROR
%40 = call i32 @getsockopt(i32 %0, i32 %38, i32 %39, i32* %5, i32* %6)
%41 = icmp eq i32 %40, -1
br i1 %41, label %46, label %42
42: ; preds = %35
%43 = load i32, i32* %5
%44 = icmp eq i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %42
store i32 %43, i32* @errno
br label %46
46: ; preds = %42, %35, %45
%47 = phi i32 [ -1, %45 ], [ -1, %35 ], [ 0, %42 ]
br label %48
48: ; preds = %3, %17, %33, %32, %46, %30
%49 = phi i32 [ -1, %30 ], [ %47, %46 ], [ -1, %32 ], [ %22, %33 ], [ %15, %17 ], [ %15, %3 ]
ret i32 %49
}