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

/* Variables and functions */
unsigned int EINPROGRESS;
unsigned int EINTR;
unsigned int ENETDOWN;
int POLLERR;
int POLLHUP;
int POLLNVAL;
int POLLOUT;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int connect(int, struct sockaddr const *, int /*<<< orphan*/);
unsigned int errno;
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, unsigned int *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ set_sock_block(int);
int /*<<< orphan*/ set_sock_nonblock(int);
int do_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen,
               int timeout) {
  set_sock_nonblock(sockfd);
  int result = connect(sockfd, serv_addr, addrlen);
  if (result < 0) {
    if (errno != EINPROGRESS) {
      return -1;
    } else {
      struct pollfd fdset[1];
      fdset[0].fd = sockfd;
      fdset[0].events = POLLOUT | POLLERR | POLLHUP | POLLNVAL | 0;
      do {
        int fdready = poll(fdset, 1, timeout);
        if (fdready == -1 && errno == EINTR)
          continue;
        if (fdready < 1 || (fdready == 1 && fdset[0].revents != POLLOUT)) {
          unsigned int err_val = 0, sz = sizeof(int);
          getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)(&err_val), &sz);
          errno = err_val;
          if (!errno)
            errno = ENETDOWN;
          return -1;
        }
        break;
      } while (1);
    }
  }
  set_sock_block(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<do_connect>:
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
call   1040 <set_sock_nonblock@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12c6 <do_connect+0x176>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%eax
je     11ad <do_connect+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <do_connect+0x185>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0c(%rip),%rcx
or     (%rcx),%eax
mov    0x2deb(%rip),%rcx
or     (%rcx),%eax
or     $0x0,%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x1,%esi
call   1060 <poll@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1218 <do_connect+0xc8>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%eax
jne    1218 <do_connect+0xc8>
jmp    12ad <do_connect+0x15d>
cmpl   $0x1,-0x2c(%rbp)
jl     123e <do_connect+0xee>
cmpl   $0x1,-0x2c(%rbp)
jne    12a8 <do_connect+0x158>
mov    -0x20(%rbp),%eax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
je     12a8 <do_connect+0x158>
movl   $0x0,-0x30(%rbp)
movl   $0x4,-0x34(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%r8
call   1030 <getsockopt@plt>
mov    -0x30(%rbp),%ecx
mov    0x2d30(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d27(%rip),%rax
cmpl   $0x0,(%rax)
jne    129c <do_connect+0x14c>
mov    0x2d27(%rip),%rax
mov    (%rax),%ecx
mov    0x2d0e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12d5 <do_connect+0x185>
jmp    12bc <do_connect+0x16c>
mov    $0x1,%al
test   $0x1,%al
jne    11dd <do_connect+0x8d>
jmp    12bc <do_connect+0x16c>
jmp    12c1 <do_connect+0x171>
jmp    12c6 <do_connect+0x176>
mov    -0x8(%rbp),%edi
call   1050 <set_sock_block@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r13d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r12d
call   1040 <set_sock_nonblock@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1070 <connect@plt>
test   %eax,%eax
js     118f <do_connect+0x3f>
mov    %r12d,%edi
call   1050 <set_sock_block@plt>
xor    %r14d,%r14d
jmp    1273 <do_connect+0x123>
mov    0x2e12(%rip),%rbx
mov    (%rbx),%eax
mov    0x2e31(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%eax
jne    1273 <do_connect+0x123>
mov    0x2e44(%rip),%rcx
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e12(%rip),%rcx
or     (%rcx),%eax
mov    0x2df1(%rip),%rcx
or     (%rcx),%eax
mov    %r12d,0x8(%rsp)
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%r15
mov    0x2db2(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
call   1060 <poll@plt>
cmp    $0xffffffff,%eax
jne    120e <do_connect+0xbe>
mov    (%rbx),%eax
cmp    0x0(%rbp),%eax
je     11f0 <do_connect+0xa0>
jmp    122e <do_connect+0xde>
test   %eax,%eax
jle    122e <do_connect+0xde>
cmp    $0x1,%eax
jne    117f <do_connect+0x2f>
mov    0x10(%rsp),%eax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%eax
je     117f <do_connect+0x2f>
movl   $0x0,(%rsp)
movl   $0x4,0x4(%rsp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
lea    0x4(%rsp),%r8
mov    %r12d,%edi
call   1030 <getsockopt@plt>
mov    (%rsp),%eax
mov    %eax,(%rbx)
test   %eax,%eax
jne    1273 <do_connect+0x123>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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