#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 sockaddr {
  scalar_t__ sa_family;
};
typedef int /*<<< orphan*/ on;

/* Variables and functions */
scalar_t__ AF_INET;
int EINPROGRESS;
int EINTR;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
scalar_t__ connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int sane_connect(int sock, struct sockaddr *sa, int len) {
  if (sa->sa_family == AF_INET) {
    int on = 1;
    (void)setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on));
  }
  while (1) {
    if (connect(sock, sa, len) == 0) {
      break;
    }
    int ec = errno;
    if (ec == EINTR) {
      continue;
    }
    if (ec == EINPROGRESS) {
      break;
    }
    return -1;
  }
  return (0);
}


x86 assembly (clang, -O0):
<sane_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2ea0(%rip),%rcx
cmp    (%rcx),%rax
jne    1174 <sane_connect+0x54>
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
jmp    1179 <sane_connect+0x59>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <connect@plt>
cmp    $0x0,%rax
jne    1197 <sane_connect+0x77>
jmp    11dd <sane_connect+0xbd>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%eax
jne    11ba <sane_connect+0x9a>
jmp    1179 <sane_connect+0x59>
mov    -0x1c(%rbp),%eax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%eax
jne    11d1 <sane_connect+0xb1>
jmp    11dd <sane_connect+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <sane_connect+0xc4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sane_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%rax
mov    0x2ea3(%rip),%rcx
cmp    (%rcx),%rax
jne    116e <sane_connect+0x4e>
movl   $0x1,0x4(%rsp)
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2e4b(%rip),%r12
mov    0x2e5c(%rip),%r13
mov    $0x2,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     11d4 <sane_connect+0xb4>
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%ecx
xor    %edx,%edx
cmp    0x0(%r13),%ecx
sete   %dl
cmp    (%r12),%ecx
lea    0x1(%rdx,%rdx,1),%ecx
cmove  %r15d,%ecx
cmp    $0x2,%ecx
je     1190 <sane_connect+0x70>
cmp    $0x3,%ecx
je     11d4 <sane_connect+0xb4>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EINPROGRESS = global i32 0
define i32 @sane_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
store i32 %0, i32* %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32 %2, i32* %7
%10 = load %struct.sockaddr*, %struct.sockaddr** %6
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @AF_INET
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %3
store i32 1, i32* %8
%16 = load i32, i32* %5
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_KEEPALIVE
%19 = bitcast i32* %8 to i8*
%20 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i8* %19, i32 4)
br label %21
21: ; preds = %15, %3
br label %22
22: ; preds = %21, %34
%23 = load i32, i32* %5
%24 = load %struct.sockaddr*, %struct.sockaddr** %6
%25 = load i32, i32* %7
%26 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
br label %41
29: ; preds = %22
%30 = load i32, i32* @errno
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = load i32, i32* @EINTR
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %29
br label %22
35: ; preds = %29
%36 = load i32, i32* %9
%37 = load i32, i32* @EINPROGRESS
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
br label %41
40: ; preds = %35
store i32 -1, i32* %4
br label %42
41: ; preds = %39, %28
store i32 0, i32* %4
br label %42
42: ; preds = %41, %40
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EINPROGRESS = global i32 0
define i32 @sane_connect(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%6 = load i64, i64* %5
%7 = load i64, i64* @AF_INET
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_KEEPALIVE
%13 = call i32 @setsockopt(i32 %0, i32 %11, i32 %12, i8* %10, i32 4)
br label %14
14: ; preds = %9, %3
br label %15
15: ; preds = %14, %18
%16 = call i64 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %15
%19 = load i32, i32* @errno
%20 = load i32, i32* @EINTR
%21 = icmp eq i32 %19, %20
%22 = load i32, i32* @EINPROGRESS
%23 = icmp eq i32 %19, %22
%24 = select i1 %23, i32 3, i32 1
%25 = select i1 %21, i32 2, i32 %24
switch i32 %25, label %29 [
i32 2, label %15
i32 3, label %27
i32 1, label %26
]
26: ; preds = %18
br label %27
27: ; preds = %18, %15, %26
%28 = phi i32 [ -1, %26 ], [ 0, %15 ], [ 0, %18 ]
ret i32 %28
29: ; preds = %18
unreachable
}