#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 socklen_t;
typedef int /*<<< orphan*/ optval;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINPROGRESS;
scalar_t__ EINTR;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
scalar_t__ block_socket(int, int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int clamd_connect(int sock, struct sockaddr *address, int addrlen) {
  int optval, ret;
  fd_set wfds;
  struct timeval tv;
  socklen_t optlen = sizeof(optval);
  if (sock < 0)
    return -1;
  if (block_socket(sock, 0) < 0)
    return -1;
  if ((ret = connect(sock, address, addrlen)) < 0)
    if (errno != EINPROGRESS)
      return -1;
  if (ret == 0)
    goto done;
  do {
    FD_ZERO(&wfds);
    FD_SET(sock, &wfds);
    tv.tv_sec = 60;
    tv.tv_usec = 0;
    ret = select(sock + 1, NULL, &wfds, NULL, &tv);
  } while (ret < 0 && errno == EINTR);
  if (ret <= 0)
    return -1;
  if (!FD_ISSET(sock, &wfds))
    return -1;
  if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &optval, &optlen) < 0)
    return -1;
  if (optval)
    return -1;
done:
  if (block_socket(sock, 1) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<clamd_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x4,-0x34(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119f <clamd_connect+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <clamd_connect+0x1d2>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <block_socket@plt>
cmp    $0x0,%rax
jge    11bf <clamd_connect+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <clamd_connect+0x1d2>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1205 <clamd_connect+0x95>
mov    0x2def(%rip),%rax
mov    (%rax),%rax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
je     1200 <clamd_connect+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <clamd_connect+0x1d2>
jmp    1205 <clamd_connect+0x95>
cmpl   $0x0,-0x1c(%rbp)
jne    1214 <clamd_connect+0xa4>
jmp    1318 <clamd_connect+0x1a8>
jmp    1219 <clamd_connect+0xa9>
lea    -0x20(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x3c,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
call   1070 <select@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x35(%rbp)
jge    1283 <clamd_connect+0x113>
mov    0x2d60(%rip),%rax
mov    (%rax),%rax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    1219 <clamd_connect+0xa9>
cmpl   $0x0,-0x1c(%rbp)
jg     12a4 <clamd_connect+0x134>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <clamd_connect+0x1d2>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    12c5 <clamd_connect+0x155>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <clamd_connect+0x1d2>
mov    -0x8(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
lea    -0x34(%rbp),%r8
call   1050 <getsockopt@plt>
cmp    $0x0,%rax
jge    12fd <clamd_connect+0x18d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <clamd_connect+0x1d2>
cmpl   $0x0,-0x18(%rbp)
je     1313 <clamd_connect+0x1a3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <clamd_connect+0x1d2>
jmp    1318 <clamd_connect+0x1a8>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <block_socket@plt>
cmp    $0x0,%rax
jge    133b <clamd_connect+0x1cb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1342 <clamd_connect+0x1d2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clamd_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x4,0xc(%rsp)
mov    $0xffffffff,%r14d
test   %edi,%edi
js     12a9 <clamd_connect+0x139>
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %esi,%esi
call   1040 <block_socket@plt>
test   %rax,%rax
js     12a9 <clamd_connect+0x139>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1080 <connect@plt>
test   %eax,%eax
js     11c4 <clamd_connect+0x54>
jne    11de <clamd_connect+0x6e>
jmp    128d <clamd_connect+0x11d>
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    12a9 <clamd_connect+0x139>
lea    0x1(%r15),%ebp
lea    0x10(%rsp),%rbx
lea    0x18(%rsp),%r12
mov    0x2ddd(%rip),%r13
mov    0x2dc6(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
movl   $0x3c,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %ebp,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %r12,%r8
call   1070 <select@plt>
test   %eax,%eax
jns    1242 <clamd_connect+0xd2>
mov    (%r14),%rcx
cmp    %rcx,0x0(%r13)
je     1200 <clamd_connect+0x90>
test   %eax,%eax
jle    12a3 <clamd_connect+0x133>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
mov    $0xffffffff,%r14d
je     12a9 <clamd_connect+0x139>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r15d,%edi
call   1050 <getsockopt@plt>
test   %rax,%rax
js     12a9 <clamd_connect+0x139>
cmpl   $0x0,0x14(%rsp)
jne    12a9 <clamd_connect+0x139>
mov    %r15d,%edi
mov    $0x1,%esi
call   1040 <block_socket@plt>
mov    %rax,%r14
sar    $0x3f,%r14
jmp    12a9 <clamd_connect+0x139>
mov    $0xffffffff,%r14d
mov    %r14d,%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 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@EINTR = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @clamd_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 %struct.timeval
%12 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32 %2, i32* %7
store i32 4, i32* %12
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %83
16: ; preds = %3
%17 = load i32, i32* %5
%18 = call i64 @block_socket(i32 %17, i32 0)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %4
br label %83
21: ; preds = %16
%22 = load i32, i32* %5
%23 = load %struct.sockaddr*, %struct.sockaddr** %6
%24 = load i32, i32* %7
%25 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 %24)
store i32 %25, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINPROGRESS
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %4
br label %83
32: ; preds = %27
br label %33
33: ; preds = %32, %21
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
br label %77
37: ; preds = %33
br label %38
38: ; preds = %54, %37
%39 = call i32 @FD_ZERO(i32* %10)
%40 = load i32, i32* %5
%41 = call i32 @FD_SET(i32 %40, i32* %10)
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 60, i32* %42
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %43
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
%46 = call i32 @select(i32 %45, i32* null, i32* %10, i32* null, %struct.timeval* %11)
store i32 %46, i32* %9
br label %47
47: ; preds = %38
%48 = load i32, i32* %9
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp eq i64 %51, %52
br label %54
54: ; preds = %50, %47
%55 = phi i1 [ false, %47 ], [ %53, %50 ]
br i1 %55, label %38, label %56
56: ; preds = %54
%57 = load i32, i32* %9
%58 = icmp sle i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %56
store i32 -1, i32* %4
br label %83
60: ; preds = %56
%61 = load i32, i32* %5
%62 = call i32 @FD_ISSET(i32 %61, i32* %10)
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %60
store i32 -1, i32* %4
br label %83
65: ; preds = %60
%66 = load i32, i32* %5
%67 = load i32, i32* @SOL_SOCKET
%68 = load i32, i32* @SO_ERROR
%69 = call i64 @getsockopt(i32 %66, i32 %67, i32 %68, i32* %8, i32* %12)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %65
store i32 -1, i32* %4
br label %83
72: ; preds = %65
%73 = load i32, i32* %8
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %72
store i32 -1, i32* %4
br label %83
76: ; preds = %72
br label %77
77: ; preds = %76, %36
%78 = load i32, i32* %5
%79 = call i64 @block_socket(i32 %78, i32 1)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %77
store i32 -1, i32* %4
br label %83
82: ; preds = %77
store i32 0, i32* %4
br label %83
83: ; preds = %82, %81, %75, %71, %64, %59, %31, %20, %15
%84 = load i32, i32* %4
ret i32 %84
}

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