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

/* Variables and functions */
scalar_t__ EINPROGRESS;
scalar_t__ ETIMEDOUT;
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 /*<<< orphan*/ SYS_connect;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
scalar_t__ syscall(int /*<<< orphan*/, int, struct sockaddr const *,
                   int /*<<< orphan*/);
int connect(int sock, const struct sockaddr *addr, socklen_t addrlen) {
  int flags, optval, optlen;
  fd_set mask;
  struct timeval timeout = {10, 0};
  if ((flags = fcntl(sock, F_GETFL, 0)) == -1 ||
      fcntl(sock, F_SETFL, flags | O_NONBLOCK) != 0)
    return -1;
  if (syscall(SYS_connect, sock, addr, addrlen) == 0)
    return fcntl(sock, F_SETFL, flags);
  else if (errno != EINPROGRESS)
    return -1;
  FD_ZERO(&mask);
  FD_SET(sock, &mask);
  switch (select(sock + 1, NULL, &mask, NULL, &timeout)) {
  case 0:
    errno = ETIMEDOUT;
  case -1:
    return -1;
  default:
    optlen = sizeof(optval);
    (void)getsockopt(sock, SOL_SOCKET, SO_ERROR, &optval, &optlen);
    if (optval != 0) {
      errno = optval;
      return -1;
    }
    break;
  }
  return fcntl(sock, F_SETFL, flags);
}


x86 assembly (clang, -O0):
<connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
je     11c2 <connect+0x62>
mov    -0x8(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e2e(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     11ce <connect+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <connect+0x1b6>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1050 <syscall@plt>
cmp    $0x0,%rax
jne    120c <connect+0xac>
mov    -0x8(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
jmp    1316 <connect+0x1b6>
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
je     1232 <connect+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <connect+0x1b6>
jmp    1237 <connect+0xd7>
lea    -0x24(%rbp),%rdi
mov    %rdi,-0x40(%rbp)
call   1060 <FD_ZERO@plt>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1080 <FD_SET@plt>
mov    -0x40(%rbp),%rdx
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
call   1070 <select@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
sub    $0xffffffff,%eax
je     12a1 <connect+0x141>
jmp    127d <connect+0x11d>
mov    -0x34(%rbp),%eax
test   %eax,%eax
jne    12ad <connect+0x14d>
jmp    128d <connect+0x12d>
mov    0x2d14(%rip),%rax
mov    (%rax),%rcx
mov    0x2d22(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <connect+0x1b6>
movl   $0x4,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    0x2cf1(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1040 <getsockopt@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12fa <connect+0x19a>
movslq -0x1c(%rbp),%rcx
mov    0x2cd5(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1316 <connect+0x1b6>
jmp    12ff <connect+0x19f>
mov    -0x8(%rbp),%edi
mov    0x2cbf(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%ebx
movq   $0xa,0x8(%rsp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     11bd <connect+0x5d>
mov    %eax,%ebp
mov    0x2e25(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
or     %ebp,%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
je     11cf <connect+0x6f>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1050 <syscall@plt>
test   %rax,%rax
je     1248 <connect+0xe8>
mov    0x2dcf(%rip),%r12
mov    (%r12),%rax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
jne    11bd <connect+0x5d>
lea    0x14(%rsp),%r15
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x8(%rsp),%r8
xor    %esi,%esi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     12ab <connect+0x14b>
test   %eax,%eax
jne    1263 <connect+0x103>
mov    0x2d6c(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
jmp    11bd <connect+0x5d>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %ebp,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fcntl@plt>
movl   $0x4,(%rsp)
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    0x2d3e(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %rsp,%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
movslq 0x4(%rsp),%rax
test   %rax,%rax
je     129e <connect+0x13e>
mov    %rax,(%r12)
jmp    11bd <connect+0x5d>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <fcntl@plt>
mov    %eax,%r14d
jmp    11bd <connect+0x5d>

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

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