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

/* Variables and functions */
int EINPROGRESS;
int EINTR;
int ETIMEDOUT;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ assert(int);
int connect(int, struct sockaddr const *, int);
int errno;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
int /*<<< orphan*/ nonblock(int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int connect_with_timeout(int fd, const struct sockaddr *sa, socklen_t salen,
                         int timeout) {
  int err;
  struct timeval tv, *tvp = NULL;
  fd_set connect_fdset;
  socklen_t len;
  int optval;
  assert(sa != NULL);
  assert(salen > 0);
  if (timeout > 0) {
    tv.tv_sec = (time_t)timeout;
    tv.tv_usec = 0;
    tvp = &tv;
  }
  nonblock(fd);
  err = connect(fd, sa, salen);
  if (err != 0 && errno == EINPROGRESS) {
    FD_ZERO(&connect_fdset);
    FD_SET(fd, &connect_fdset);
    do {
      err = select(fd + 1, NULL, &connect_fdset, NULL, tvp);
    } while (err < 0 && errno == EINTR);
    if (err < 0)
      return -1;
    if (err == 0) {
      errno = ETIMEDOUT;
      return -1;
    }
    len = sizeof(optval);
    err = getsockopt(fd, SOL_SOCKET, SO_ERROR, &optval, &len);
    if (err != 0)
      return err;
    if (optval != 0) {
      errno = optval;
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<connect_with_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x0,-0x14(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x0,-0x18(%rbp)
jle    11d2 <connect_with_timeout+0x62>
movslq -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <nonblock@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1318 <connect_with_timeout+0x1a8>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%eax
jne    1318 <connect_with_timeout+0x1a8>
lea    -0x3c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x38(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x3c(%rbp),%rdx
mov    %rcx,%rsi
call   1070 <select@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x45(%rbp)
jge    1267 <connect_with_timeout+0xf7>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    1223 <connect_with_timeout+0xb3>
cmpl   $0x0,-0x1c(%rbp)
jge    1288 <connect_with_timeout+0x118>
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <connect_with_timeout+0x1af>
cmpl   $0x0,-0x1c(%rbp)
jne    12b0 <connect_with_timeout+0x140>
mov    0x2d27(%rip),%rax
mov    (%rax),%ecx
mov    0x2d2e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <connect_with_timeout+0x1af>
movl   $0x4,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    0x2cfe(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12f1 <connect_with_timeout+0x181>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    131f <connect_with_timeout+0x1af>
cmpl   $0x0,-0x44(%rbp)
je     1313 <connect_with_timeout+0x1a3>
mov    -0x44(%rbp),%ecx
mov    0x2ccb(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <connect_with_timeout+0x1af>
jmp    1318 <connect_with_timeout+0x1a8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_with_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %edi,%r14d
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r12d,%r12d
setg   %dil
call   1050 <assert@plt>
xor    %r15d,%r15d
test   %ebx,%ebx
jle    11c3 <connect_with_timeout+0x53>
mov    %ebx,%eax
mov    %rax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x18(%rsp),%rbx
jmp    11c5 <connect_with_timeout+0x55>
xor    %ebx,%ebx
mov    %r14d,%edi
call   1030 <nonblock@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1080 <connect@plt>
test   %eax,%eax
je     129e <connect_with_timeout+0x12e>
mov    0x2de6(%rip),%r12
mov    (%r12),%eax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%eax
jne    129e <connect_with_timeout+0x12e>
lea    0x14(%rsp),%r15
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %r15,%rsi
call   1090 <FD_SET@plt>
lea    0x1(%r14),%ebp
mov    0x2d98(%rip),%r13
mov    %ebp,%edi
xor    %esi,%esi
mov    %r15,%rdx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1070 <select@plt>
test   %eax,%eax
jns    123f <connect_with_timeout+0xcf>
mov    0x0(%r13),%ecx
cmp    %ecx,(%r12)
je     1220 <connect_with_timeout+0xb0>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     129e <connect_with_timeout+0x12e>
je     128b <connect_with_timeout+0x11b>
movl   $0x4,0x10(%rsp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %r14d,%edi
call   1040 <getsockopt@plt>
mov    %eax,%r15d
test   %eax,%eax
jne    129e <connect_with_timeout+0x12e>
mov    0xc(%rsp),%eax
test   %eax,%eax
jne    1294 <connect_with_timeout+0x124>
xor    %r15d,%r15d
jmp    129e <connect_with_timeout+0x12e>
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r12)
mov    $0xffffffff,%r15d
mov    %r15d,%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 { i64, i64 }
@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_with_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 i32
%11 = alloca %struct.timeval
%12 = alloca %struct.timeval*
%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
store %struct.timeval* null, %struct.timeval** %12
%16 = load %struct.sockaddr*, %struct.sockaddr** %7
%17 = icmp ne %struct.sockaddr* %16, null
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %8
%21 = icmp sgt i32 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* %9
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %4
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 %28, i64* %29
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i64 0, i64* %30
store %struct.timeval* %11, %struct.timeval** %12
br label %31
31: ; preds = %26, %4
%32 = load i32, i32* %6
%33 = call i32 @nonblock(i32 %32)
%34 = load i32, i32* %6
%35 = load %struct.sockaddr*, %struct.sockaddr** %7
%36 = load i32, i32* %8
%37 = call i32 @connect(i32 %34, %struct.sockaddr* %35, i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %86
40: ; preds = %31
%41 = load i32, i32* @errno
%42 = load i32, i32* @EINPROGRESS
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %86
44: ; preds = %40
%45 = call i32 @FD_ZERO(i32* %13)
%46 = load i32, i32* %6
%47 = call i32 @FD_SET(i32 %46, i32* %13)
br label %48
48: ; preds = %60, %44
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
%51 = load %struct.timeval*, %struct.timeval** %12
%52 = call i32 @select(i32 %50, i32* null, i32* %13, i32* null, %struct.timeval* %51)
store i32 %52, i32* %10
br label %53
53: ; preds = %48
%54 = load i32, i32* %10
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i32, i32* @errno
%58 = load i32, i32* @EINTR
%59 = icmp eq i32 %57, %58
br label %60
60: ; preds = %56, %53
%61 = phi i1 [ false, %53 ], [ %59, %56 ]
br i1 %61, label %48, label %62
62: ; preds = %60
%63 = load i32, i32* %10
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %62
store i32 -1, i32* %5
br label %87
66: ; preds = %62
%67 = load i32, i32* %10
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = load i32, i32* @ETIMEDOUT
store i32 %70, i32* @errno
store i32 -1, i32* %5
br label %87
71: ; preds = %66
store i32 4, i32* %14
%72 = load i32, i32* %6
%73 = load i32, i32* @SOL_SOCKET
%74 = load i32, i32* @SO_ERROR
%75 = call i32 @getsockopt(i32 %72, i32 %73, i32 %74, i32* %15, i32* %14)
store i32 %75, i32* %10
%76 = load i32, i32* %10
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %71
%79 = load i32, i32* %10
store i32 %79, i32* %5
br label %87
80: ; preds = %71
%81 = load i32, i32* %15
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = load i32, i32* %15
store i32 %84, i32* @errno
store i32 -1, i32* %5
br label %87
85: ; preds = %80
br label %86
86: ; preds = %85, %40, %31
store i32 0, i32* %5
br label %87
87: ; preds = %86, %83, %78, %69, %65
%88 = load i32, i32* %5
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i64, i64 }
@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_with_timeout(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = bitcast %struct.timeval* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = bitcast i32* %8 to i8*
%13 = icmp ne %struct.sockaddr* %1, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = icmp sgt i32 %2, 0
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = icmp sgt i32 %3, 0
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = zext i32 %3 to i64
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 %21, i64* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i64 0, i64* %23
br label %24
24: ; preds = %20, %4
%25 = phi %struct.timeval* [ %5, %20 ], [ null, %4 ]
%26 = tail call i32 @nonblock(i32 %0)
%27 = tail call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%28 = icmp eq i32 %27, 0
br i1 %28, label %59, label %29
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = load i32, i32* @EINPROGRESS
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %59
33: ; preds = %29
%34 = call i32 @FD_ZERO(i32* %6)
%35 = call i32 @FD_SET(i32 %0, i32* %6)
%36 = add nsw i32 %0, 1
br label %37
37: ; preds = %37, %33
%38 = call i32 @select(i32 %36, i32* null, i32* %6, i32* null, %struct.timeval* %25)
%39 = icmp slt i32 %38, 0
%40 = load i32, i32* @errno
%41 = load i32, i32* @EINTR
%42 = icmp eq i32 %40, %41
%43 = select i1 %39, i1 %42, i1 false
br i1 %43, label %37, label %44
44: ; preds = %37
br i1 %39, label %59, label %45
45: ; preds = %44
%46 = icmp eq i32 %38, 0
br i1 %46, label %47, label %49
47: ; preds = %45
%48 = load i32, i32* @ETIMEDOUT
br label %57
49: ; preds = %45
store i32 4, i32* %7
%50 = load i32, i32* @SOL_SOCKET
%51 = load i32, i32* @SO_ERROR
%52 = call i32 @getsockopt(i32 %0, i32 %50, i32 %51, i32* %8, i32* %7)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %49
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %54, %47
%58 = phi i32 [ %48, %47 ], [ %55, %54 ]
store i32 %58, i32* @errno
br label %59
59: ; preds = %57, %24, %29, %54, %49, %44
%60 = phi i32 [ -1, %44 ], [ %52, %49 ], [ 0, %54 ], [ 0, %29 ], [ 0, %24 ], [ -1, %57 ]
ret i32 %60
}