#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 int /*<<< orphan*/ yes;
struct sockaddr_storage {
  scalar_t__ ss_family;
};
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ EINPROGRESS;
int SOCK_NONBLOCK;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(scalar_t__, int, int /*<<< orphan*/);
int connect_remote(struct sockaddr_storage *remote_addr) {
  int remote_fd = 0, yes = 1;
  if ((remote_fd = socket(remote_addr->ss_family, SOCK_STREAM | SOCK_NONBLOCK,
                          0)) < 0) {
    perror("socket (connect_remote): ");
    return 0;
  }
  if (setsockopt(remote_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
    perror("setsockopt (SO_REUSEADDR, connect_remote): ");
    close(remote_fd);
    return 0;
  }
  if (connect(remote_fd, (struct sockaddr *)remote_addr,
              remote_addr->ss_family == AF_INET
                  ? sizeof(struct sockaddr_in)
                  : sizeof(struct sockaddr_in6)) < 0) {
    if (errno != EINPROGRESS) {
      perror("connect (connect_remote): ");
      close(remote_fd);
      return 0;
    }
  }
  return remote_fd;
}


x86 assembly (clang, -O0):
<connect_remote>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ae <connect_remote+0x5e>
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1276 <connect_remote+0x126>
mov    -0x14(%rbp),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11fc <connect_remote+0xac>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1276 <connect_remote+0x126>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    0x2dcf(%rip),%rax
mov    (%rax),%rdx
mov    $0x4,%eax
cmp    %rdx,%rcx
cmove  %rax,%rax
mov    %eax,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    1270 <connect_remote+0x120>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%rax
je     126b <connect_remote+0x11b>
lea    0xdf4(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1276 <connect_remote+0x126>
jmp    1270 <connect_remote+0x120>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_remote>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
movl   $0x1,0xc(%rsp)
mov    (%rdi),%rdi
mov    0x2e5b(%rip),%rax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11e7 <connect_remote+0x97>
mov    %eax,%ebx
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11f5 <connect_remote+0xa5>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11da <connect_remote+0x8a>
mov    0x2df5(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <connect_remote+0xae>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe12(%rip),%rdi
call   1050 <perror@plt>
jmp    11dc <connect_remote+0x8c>
lea    0xe1e(%rip),%rdi
jmp    1205 <connect_remote+0xb5>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
jmp    11dc <connect_remote+0x8c>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str = [26 x i8] c"socket (connect_remote): \00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [44 x i8] c"setsockopt (SO_REUSEADDR, connect_remote): \00"
@AF_INET = global i64 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.2 = [27 x i8] c"connect (connect_remote): \00"
define i32 @connect_remote(%struct.sockaddr_storage* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_storage*
%4 = alloca i32
%5 = alloca i32
store %struct.sockaddr_storage* %0, %struct.sockaddr_storage** %3
store i32 0, i32* %4
store i32 1, i32* %5
%6 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%7 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = load i32, i32* @SOCK_STREAM
%10 = load i32, i32* @SOCK_NONBLOCK
%11 = or i32 %9, %10
%12 = call i32 @socket(i64 %8, i32 %11, i32 0)
store i32 %12, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %51
16: ; preds = %1
%17 = load i32, i32* %4
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i64 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %5, i32 4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
store i32 0, i32* %2
br label %51
26: ; preds = %16
%27 = load i32, i32* %4
%28 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%29 = bitcast %struct.sockaddr_storage* %28 to %struct.sockaddr*
%30 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%31 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = load i64, i64* @AF_INET
%34 = icmp eq i64 %32, %33
%35 = zext i1 %34 to i64
%36 = select i1 %34, i64 4, i64 4
%37 = trunc i64 %36 to i32
%38 = call i64 @connect(i32 %27, %struct.sockaddr* %29, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %26
%41 = load i64, i64* @errno
%42 = load i64, i64* @EINPROGRESS
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* %4
%47 = call i32 @close(i32 %46)
store i32 0, i32* %2
br label %51
48: ; preds = %40
br label %49
49: ; preds = %48, %26
%50 = load i32, i32* %4
store i32 %50, i32* %2
br label %51
51: ; preds = %49, %44, %22, %14
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str = [26 x i8] c"socket (connect_remote): \00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [44 x i8] c"setsockopt (SO_REUSEADDR, connect_remote): \00"
@AF_INET = global i64 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.2 = [27 x i8] c"connect (connect_remote): \00"
define i32 @connect_remote(%struct.sockaddr_storage* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%4 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 0, i32 0
%5 = load i64, i64* %4
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @SOCK_NONBLOCK
%8 = or i32 %7, %6
%9 = tail call i32 @socket(i64 %5, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %32
13: ; preds = %1
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i64 @setsockopt(i32 %9, i32 %14, i32 %15, i32* %2, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @close(i32 %9)
br label %32
21: ; preds = %13
%22 = bitcast %struct.sockaddr_storage* %0 to %struct.sockaddr*
%23 = call i64 @connect(i32 %9, %struct.sockaddr* %22, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINPROGRESS
%28 = icmp eq i64 %26, %27
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @close(i32 %9)
br label %32
32: ; preds = %21, %25, %29, %18, %11
%33 = phi i32 [ 0, %11 ], [ 0, %18 ], [ 0, %29 ], [ %9, %25 ], [ %9, %21 ]
ret i32 %33
}