#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ so_error;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
long O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int connect(int, struct sockaddr const *, int /*<<< orphan*/);
scalar_t__ errno;
long fcntl(int, int /*<<< orphan*/, long);
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int wd_connect(int sockfd, const struct sockaddr *their_addr, socklen_t addrlen,
               int timeout) {
  long arg = fcntl(sockfd, F_GETFL, NULL);
  arg |= O_NONBLOCK;
  fcntl(sockfd, F_SETFL, arg);
  int res = connect(sockfd, their_addr, addrlen);
  if ((res == -1) && (errno != EINPROGRESS)) {
    goto error;
  } else if (res == 0) {
    goto success;
  } else {
    fd_set fdset;
    struct timeval tv;
    int so_error = 0;
    int len = sizeof(so_error);
    tv.tv_sec = timeout;
    tv.tv_usec = 0;
    FD_ZERO(&fdset);
    FD_SET(sockfd, &fdset);
    res = select(sockfd + 1, NULL, &fdset, NULL, &tv);
    switch (res) {
    case 1:
      getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len);
      if (so_error == 0) {
        goto success;
      }
      break;
    default:
      break;
    }
  }
error:
  return -1;
success:
  arg = fcntl(sockfd, F_GETFL, NULL);
  arg &= (~O_NONBLOCK);
  fcntl(sockfd, F_SETFL, arg);
  return 0;
}


x86 assembly (clang, -O0):
<wd_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f0 <wd_connect+0x90>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
je     11f0 <wd_connect+0x90>
jmp    12ac <wd_connect+0x14c>
cmpl   $0x0,-0x24(%rbp)
jne    11ff <wd_connect+0x9f>
jmp    12b8 <wd_connect+0x158>
movl   $0x0,-0x3c(%rbp)
movl   $0x4,-0x40(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
mov    %rdi,-0x48(%rbp)
call   1050 <FD_ZERO@plt>
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1080 <FD_SET@plt>
mov    -0x48(%rbp),%rdx
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rsi
call   1060 <select@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
jne    1298 <wd_connect+0x138>
jmp    1262 <wd_connect+0x102>
mov    -0x8(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1040 <getsockopt@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    1293 <wd_connect+0x133>
jmp    12b8 <wd_connect+0x158>
jmp    129d <wd_connect+0x13d>
jmp    129d <wd_connect+0x13d>
jmp    12a2 <wd_connect+0x142>
jmp    12a7 <wd_connect+0x147>
jmp    12ac <wd_connect+0x14c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1303 <wd_connect+0x1a3>
mov    -0x8(%rbp),%edi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d08(%rip),%rax
mov    (%rax),%rax
xor    $0xffffffffffffffff,%rax
and    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wd_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e50(%rip),%r12
or     (%r12),%rax
mov    0x2e2d(%rip),%r13
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fcntl@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1070 <connect@plt>
test   %eax,%eax
je     1261 <wd_connect+0x101>
cmp    $0xffffffff,%eax
jne    11e2 <wd_connect+0x82>
mov    0x2df6(%rip),%rax
mov    (%rax),%rcx
mov    0x2e04(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    128a <wd_connect+0x12a>
movl   $0x0,0xc(%rsp)
movl   $0x4,0x10(%rsp)
mov    %r14d,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x18(%rsp),%r8
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1060 <select@plt>
cmp    $0x1,%eax
jne    125a <wd_connect+0xfa>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
cmpl   $0x0,0xc(%rsp)
je     1261 <wd_connect+0x101>
mov    $0xffffffff,%eax
jmp    128a <wd_connect+0x12a>
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    (%r12),%rdx
not    %rdx
and    %rax,%rdx
mov    0x0(%r13),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32, i64 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i64 0
@F_SETFL = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @wd_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 i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%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 i64 @fcntl(i32 %16, i32 %17, i64 0)
store i64 %18, i64* %10
%19 = load i64, i64* @O_NONBLOCK
%20 = load i64, i64* %10
%21 = or i64 %20, %19
store i64 %21, i64* %10
%22 = load i32, i32* %6
%23 = load i32, i32* @F_SETFL
%24 = load i64, i64* %10
%25 = call i64 @fcntl(i32 %22, i32 %23, i64 %24)
%26 = load i32, i32* %6
%27 = load %struct.sockaddr*, %struct.sockaddr** %7
%28 = load i32, i32* %8
%29 = call i32 @connect(i32 %26, %struct.sockaddr* %27, i32 %28)
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %4
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINPROGRESS
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
br label %65
37: ; preds = %32, %4
%38 = load i32, i32* %11
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %37
br label %66
41: ; preds = %37
store i32 0, i32* %14
store i32 4, i32* %15
%42 = load i32, i32* %9
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %42, i32* %43
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %44
%45 = call i32 @FD_ZERO(i32* %12)
%46 = load i32, i32* %6
%47 = call i32 @FD_SET(i32 %46, i32* %12)
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
%50 = call i32 @select(i32 %49, i32* null, i32* %12, i32* null, %struct.timeval* %13)
store i32 %50, i32* %11
%51 = load i32, i32* %11
switch i32 %51, label %61 [
i32 1, label %52
]
52: ; preds = %41
%53 = load i32, i32* %6
%54 = load i32, i32* @SOL_SOCKET
%55 = load i32, i32* @SO_ERROR
%56 = call i32 @getsockopt(i32 %53, i32 %54, i32 %55, i32* %14, i32* %15)
%57 = load i32, i32* %14
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %52
br label %66
60: ; preds = %52
br label %62
61: ; preds = %41
br label %62
62: ; preds = %61, %60
br label %63
63: ; preds = %62
br label %64
64: ; preds = %63
br label %65
65: ; preds = %64, %36
store i32 -1, i32* %5
br label %78
66: ; preds = %59, %40
%67 = load i32, i32* %6
%68 = load i32, i32* @F_GETFL
%69 = call i64 @fcntl(i32 %67, i32 %68, i64 0)
store i64 %69, i64* %10
%70 = load i64, i64* @O_NONBLOCK
%71 = xor i64 %70, -1
%72 = load i64, i64* %10
%73 = and i64 %72, %71
store i64 %73, i64* %10
%74 = load i32, i32* %6
%75 = load i32, i32* @F_SETFL
%76 = load i64, i64* %10
%77 = call i64 @fcntl(i32 %74, i32 %75, i64 %76)
store i32 0, i32* %5
br label %78
78: ; preds = %66, %65
%79 = load i32, i32* %5
ret i32 %79
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32, i64 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i64 0
@F_SETFL = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @wd_connect(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = load i32, i32* @F_GETFL
%10 = tail call i64 @fcntl(i32 %0, i32 %9, i64 0)
%11 = load i64, i64* @O_NONBLOCK
%12 = or i64 %11, %10
%13 = load i32, i32* @F_SETFL
%14 = tail call i64 @fcntl(i32 %0, i32 %13, i64 %12)
%15 = tail call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
switch i32 %15, label %20 [
i32 -1, label %16
i32 0, label %40
]
16: ; preds = %4
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINPROGRESS
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %48
20: ; preds = %16, %4
%21 = bitcast i32* %5 to i8*
%22 = bitcast %struct.timeval* %6 to i8*
%23 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%24 = bitcast i32* %8 to i8*
store i32 4, i32* %8
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %3, i32* %25
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %26
%27 = call i32 @FD_ZERO(i32* %5)
%28 = call i32 @FD_SET(i32 %0, i32* %5)
%29 = add nsw i32 %0, 1
%30 = call i32 @select(i32 %29, i32* null, i32* %5, i32* null, %struct.timeval* %6)
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %39
32: ; preds = %20
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_ERROR
%35 = call i32 @getsockopt(i32 %0, i32 %33, i32 %34, i32* %7, i32* %8)
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
br label %40
39: ; preds = %32, %20
br label %48
40: ; preds = %38, %4
%41 = load i32, i32* @F_GETFL
%42 = call i64 @fcntl(i32 %0, i32 %41, i64 0)
%43 = load i64, i64* @O_NONBLOCK
%44 = xor i64 %43, -1
%45 = and i64 %42, %44
%46 = load i32, i32* @F_SETFL
%47 = call i64 @fcntl(i32 %0, i32 %46, i64 %45)
br label %48
48: ; preds = %39, %16, %40
%49 = phi i32 [ 0, %40 ], [ -1, %39 ], [ -1, %16 ]
ret i32 %49
}