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

/* Variables and functions */
int EINPROGRESS;
int EINTR;
int ETIMEDOUT;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int connect(int, struct sockaddr *, int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int connect_with_timeout(int sockfd, void *addr, socklen_t addrlen,
                         int timeout) {
  int r, flag;
  flag = fcntl(sockfd, F_GETFL);
  if (!(flag & O_NONBLOCK))
    fcntl(sockfd, F_SETFL, flag | O_NONBLOCK);
  r = connect(sockfd, (struct sockaddr *)addr, addrlen);
  if (r == -1) {
    if (errno != EINPROGRESS) {
      r = -errno;
      perror("connect");
      goto conn_stop;
    }
  } else
    return 0;
  fd_set wset;
  struct timeval tv;
  tv.tv_sec = timeout / 1000;
  tv.tv_usec = timeout % 1000 * 1000;
  for (;;) {
    FD_ZERO(&wset);
    FD_SET(sockfd, &wset);
    r = select(sockfd + 1, NULL, &wset, NULL, &tv);
    if (r == -1) {
      if (errno == EINTR)
        continue;
      r = -errno;
      goto conn_stop;
    } else if (r == 0) {
      r = -ETIMEDOUT;
      goto conn_stop;
    }
    if (FD_ISSET(sockfd, &wset)) {
      int err = 0;
      socklen_t l = sizeof(err);
      r = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &l);
      if (r == -1)
        goto conn_stop;
      if (err != 0)
        r = -err;
      else
        r = 0;
      goto conn_stop;
    }
  }
conn_stop:
  if (!(flag & O_NONBLOCK))
    fcntl(sockfd, F_SETFL, flag);
  return r;
}


x86 assembly (clang, -O0):
<connect_with_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    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e2b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11df <connect_with_timeout+0x5f>
mov    -0x8(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2e0a(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1090 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1237 <connect_with_timeout+0xb7>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%eax
je     1232 <connect_with_timeout+0xb2>
xor    %eax,%eax
mov    0x2da4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
lea    0xdd8(%rip),%rdi
call   1080 <perror@plt>
jmp    1373 <connect_with_timeout+0x1f3>
jmp    1243 <connect_with_timeout+0xc3>
movl   $0x0,-0x4(%rbp)
jmp    13a4 <connect_with_timeout+0x224>
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x2c(%rbp)
lea    -0x24(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x24(%rbp),%rdx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
call   1070 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12d1 <connect_with_timeout+0x151>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    0x2cef(%rip),%rcx
cmp    (%rcx),%eax
jne    12be <connect_with_timeout+0x13e>
jmp    1265 <connect_with_timeout+0xe5>
xor    %eax,%eax
mov    0x2cf9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    1373 <connect_with_timeout+0x1f3>
cmpl   $0x0,-0x1c(%rbp)
jne    12ee <connect_with_timeout+0x16e>
xor    %eax,%eax
mov    0x2cc4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    1373 <connect_with_timeout+0x1f3>
jmp    12f3 <connect_with_timeout+0x173>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     136e <connect_with_timeout+0x1ee>
movl   $0x0,-0x34(%rbp)
movl   $0x4,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cc7(%rip),%rax
mov    (%rax),%esi
mov    0x2c8e(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   1050 <getsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    134b <connect_with_timeout+0x1cb>
jmp    1373 <connect_with_timeout+0x1f3>
cmpl   $0x0,-0x34(%rbp)
je     1362 <connect_with_timeout+0x1e2>
xor    %eax,%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1369 <connect_with_timeout+0x1e9>
movl   $0x0,-0x1c(%rbp)
jmp    1373 <connect_with_timeout+0x1f3>
jmp    1265 <connect_with_timeout+0xe5>
mov    -0x20(%rbp),%eax
mov    0x2c63(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    139e <connect_with_timeout+0x21e>
mov    -0x8(%rbp),%edi
mov    0x2c36(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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,%r15d
mov    %edx,%r13d
mov    %rsi,%r14
mov    %edi,%r12d
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebx
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
test   %ebx,%edx
jne    11d0 <connect_with_timeout+0x50>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
or     %ebx,%edx
mov    %r12d,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r13d,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1315 <connect_with_timeout+0x195>
mov    0x2dd2(%rip),%rax
mov    (%rax),%ebp
mov    0x2de1(%rip),%rax
cmp    (%rax),%ebp
jne    1326 <connect_with_timeout+0x1a6>
mov    %ebx,0xc(%rsp)
movslq %r15d,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x18(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0x1c(%rsp)
lea    0x1(%r12),%r15d
lea    0x24(%rsp),%rbx
lea    0x18(%rsp),%r13
mov    0x2d5a(%rip),%r14
jmp    1262 <connect_with_timeout+0xe2>
nopl   0x0(%rax,%rax,1)
mov    0x2d69(%rip),%rax
mov    (%rax),%ebp
cmp    (%r14),%ebp
jne    12ef <connect_with_timeout+0x16f>
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
mov    %r15d,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %r13,%r8
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     1250 <connect_with_timeout+0xd0>
test   %eax,%eax
je     12e2 <connect_with_timeout+0x162>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1262 <connect_with_timeout+0xe2>
movl   $0x0,0x10(%rsp)
movl   $0x4,0x14(%rsp)
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
mov    0x2cf8(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %r12d,%edi
call   1050 <getsockopt@plt>
xor    %ebp,%ebp
sub    0x10(%rsp),%ebp
cmp    $0xffffffff,%eax
cmove  %eax,%ebp
jmp    12f1 <connect_with_timeout+0x171>
mov    0x2cbf(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12f1 <connect_with_timeout+0x171>
neg    %ebp
mov    0xc(%rsp),%ebx
mov    0x2ce4(%rip),%rax
test   %ebx,(%rax)
jne    1315 <connect_with_timeout+0x195>
mov    0x2cc1(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
neg    %ebp
lea    0xcd1(%rip),%rdi
call   1080 <perror@plt>
jmp    12f5 <connect_with_timeout+0x175>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i32 0
@EINPROGRESS = global i32 0
@.str = [8 x i8] c"connect\00"
@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, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %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 (i32, i32, ...) @fcntl(i32 %16, i32 %17)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%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* %11
%27 = load i32, i32* @O_NONBLOCK
%28 = or i32 %26, %27
%29 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25, i32 %28)
br label %30
30: ; preds = %23, %4
%31 = load i32, i32* %6
%32 = load i8*, i8** %7
%33 = bitcast i8* %32 to %struct.sockaddr*
%34 = load i32, i32* %8
%35 = call i32 @connect(i32 %31, %struct.sockaddr* %33, i32 %34)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %47
38: ; preds = %30
%39 = load i32, i32* @errno
%40 = load i32, i32* @EINPROGRESS
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* @errno
%44 = sub nsw i32 0, %43
store i32 %44, i32* %10
%45 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %101
46: ; preds = %38
br label %48
47: ; preds = %30
store i32 0, i32* %5
br label %113
48: ; preds = %46
%49 = load i32, i32* %9
%50 = sdiv i32 %49, 1000
%51 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %9
%53 = srem i32 %52, 1000
%54 = mul nsw i32 %53, 1000
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i32 %54, i32* %55
br label %56
56: ; preds = %100, %69, %48
%57 = call i32 @FD_ZERO(i32* %12)
%58 = load i32, i32* %6
%59 = call i32 @FD_SET(i32 %58, i32* %12)
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
%62 = call i32 @select(i32 %61, i32* null, i32* %12, i32* null, %struct.timeval* %13)
store i32 %62, i32* %10
%63 = load i32, i32* %10
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %73
65: ; preds = %56
%66 = load i32, i32* @errno
%67 = load i32, i32* @EINTR
%68 = icmp eq i32 %66, %67
br i1 %68, label %69, label %70
69: ; preds = %65
br label %56
70: ; preds = %65
%71 = load i32, i32* @errno
%72 = sub nsw i32 0, %71
store i32 %72, i32* %10
br label %101
73: ; preds = %56
%74 = load i32, i32* %10
%75 = icmp eq i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i32, i32* @ETIMEDOUT
%78 = sub nsw i32 0, %77
store i32 %78, i32* %10
br label %101
79: ; preds = %73
br label %80
80: ; preds = %79
%81 = load i32, i32* %6
%82 = call i64 @FD_ISSET(i32 %81, i32* %12)
%83 = icmp ne i64 %82, 0
br i1 %83, label %84, label %100
84: ; preds = %80
store i32 0, i32* %14
store i32 4, i32* %15
%85 = load i32, i32* %6
%86 = load i32, i32* @SOL_SOCKET
%87 = load i32, i32* @SO_ERROR
%88 = call i32 @getsockopt(i32 %85, i32 %86, i32 %87, i32* %14, i32* %15)
store i32 %88, i32* %10
%89 = load i32, i32* %10
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %92
91: ; preds = %84
br label %101
92: ; preds = %84
%93 = load i32, i32* %14
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = load i32, i32* %14
%97 = sub nsw i32 0, %96
store i32 %97, i32* %10
br label %99
98: ; preds = %92
store i32 0, i32* %10
br label %99
99: ; preds = %98, %95
br label %101
100: ; preds = %80
br label %56
101: ; preds = %99, %91, %76, %70, %42
%102 = load i32, i32* %11
%103 = load i32, i32* @O_NONBLOCK
%104 = and i32 %102, %103
%105 = icmp ne i32 %104, 0
br i1 %105, label %111, label %106
106: ; preds = %101
%107 = load i32, i32* %6
%108 = load i32, i32* @F_SETFL
%109 = load i32, i32* %11
%110 = call i32 (i32, i32, ...) @fcntl(i32 %107, i32 %108, i32 %109)
br label %111
111: ; preds = %106, %101
%112 = load i32, i32* %10
store i32 %112, i32* %5
br label %113
113: ; preds = %111, %47
%114 = load i32, i32* %5
ret i32 %114
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i32 0
@EINPROGRESS = global i32 0
@.str = [8 x i8] c"connect\00"
@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, i8* %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 i32 (i32, i32, ...) @fcntl(i32 %0, i32 %9)
%11 = load i32, i32* @O_NONBLOCK
%12 = and i32 %11, %10
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %4
%15 = load i32, i32* @F_SETFL
%16 = or i32 %11, %10
%17 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %15, i32 %16)
br label %18
18: ; preds = %14, %4
%19 = bitcast i8* %1 to %struct.sockaddr*
%20 = tail call i32 @connect(i32 %0, %struct.sockaddr* %19, i32 %2)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %71
22: ; preds = %18
%23 = load i32, i32* @errno
%24 = load i32, i32* @EINPROGRESS
%25 = icmp eq i32 %23, %24
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = sub nsw i32 0, %23
%28 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %63
29: ; preds = %22
%30 = sdiv i32 %3, 1000
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %30, i32* %31
%32 = srem i32 %3, 1000
%33 = mul nsw i32 %32, 1000
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i32 %33, i32* %34
%35 = add nsw i32 %0, 1
br label %36
36: ; preds = %44, %29
%37 = call i32 @FD_ZERO(i32* %5)
%38 = call i32 @FD_SET(i32 %0, i32* %5)
%39 = call i32 @select(i32 %35, i32* null, i32* %5, i32* null, %struct.timeval* %6)
switch i32 %39, label %50 [
i32 -1, label %40
i32 0, label %47
]
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = load i32, i32* @EINTR
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40, %50
br label %36
45: ; preds = %40
%46 = sub nsw i32 0, %41
br label %63
47: ; preds = %36
%48 = load i32, i32* @ETIMEDOUT
%49 = sub nsw i32 0, %48
br label %63
50: ; preds = %36
%51 = call i64 @FD_ISSET(i32 %0, i32* %5)
%52 = icmp eq i64 %51, 0
br i1 %52, label %44, label %53
53: ; preds = %50
%54 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%55 = bitcast i32* %8 to i8*
store i32 4, i32* %8
%56 = load i32, i32* @SOL_SOCKET
%57 = load i32, i32* @SO_ERROR
%58 = call i32 @getsockopt(i32 %0, i32 %56, i32 %57, i32* %7, i32* %8)
%59 = icmp eq i32 %58, -1
%60 = load i32, i32* %7
%61 = sub nsw i32 0, %60
%62 = select i1 %59, i32 -1, i32 %61
br label %63
63: ; preds = %53, %47, %45, %26
%64 = phi i32 [ %27, %26 ], [ %46, %45 ], [ %49, %47 ], [ %62, %53 ]
%65 = load i32, i32* @O_NONBLOCK
%66 = and i32 %65, %10
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %63
%69 = load i32, i32* @F_SETFL
%70 = call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %69, i32 %10)
br label %71
71: ; preds = %63, %68, %18
%72 = phi i32 [ 0, %18 ], [ %64, %68 ], [ %64, %63 ]
ret i32 %72
}