#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 {
  int dummy;
};
typedef int /*<<< orphan*/ one;

/* Variables and functions */
scalar_t__ EINPROGRESS;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int const *, int);
int socket(int, int, int /*<<< orphan*/);
int usock_connect(struct sockaddr *sa, int sa_len, int family, int socktype,
                  bool server) {
  int sock;
  sock = socket(family, socktype, 0);
  if (sock < 0)
    return -1;
  if (server) {
    const int one = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
    if (!bind(sock, sa, sa_len) &&
        (socktype != SOCK_STREAM || !listen(sock, SOMAXCONN)))
      return sock;
  } else {
    if (!connect(sock, sa, sa_len) || errno == EINPROGRESS)
      return sock;
  }
  close(sock);
  return -1;
}


x86 assembly (clang, -O0):
<usock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119f <usock_connect+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <usock_connect+0x119>
cmpl   $0x0,-0x20(%rbp)
je     1228 <usock_connect+0xc8>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%eax
jne    1223 <usock_connect+0xc3>
mov    -0x1c(%rbp),%eax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%eax
jne    1218 <usock_connect+0xb8>
mov    -0x24(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1050 <listen@plt>
cmp    $0x0,%eax
jne    1223 <usock_connect+0xc3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1279 <usock_connect+0x119>
jmp    126a <usock_connect+0x10a>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <connect@plt>
cmp    $0x0,%eax
je     125a <usock_connect+0xfa>
mov    0x2d79(%rip),%rax
mov    (%rax),%rax
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
jne    1265 <usock_connect+0x105>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1279 <usock_connect+0x119>
jmp    126a <usock_connect+0x10a>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usock_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%ebp
mov    %ecx,%r12d
mov    %esi,%r14d
mov    %rdi,%r15
mov    %edx,%edi
mov    %ecx,%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     121e <usock_connect+0xbe>
mov    %eax,%ebx
test   %ebp,%ebp
je     11f0 <usock_connect+0x90>
movl   $0x1,0xc(%rsp)
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1060 <bind@plt>
test   %eax,%eax
jne    1217 <usock_connect+0xb7>
mov    0x2df3(%rip),%rax
cmp    %r12d,(%rax)
jne    1223 <usock_connect+0xc3>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <listen@plt>
test   %eax,%eax
jne    1217 <usock_connect+0xb7>
jmp    1223 <usock_connect+0xc3>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1070 <connect@plt>
test   %eax,%eax
je     1223 <usock_connect+0xc3>
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%rax
je     1223 <usock_connect+0xc3>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOCK_STREAM = global i32 0
@SOMAXCONN = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @usock_connect(%struct.sockaddr* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = load i32, i32* %9
%15 = load i32, i32* %10
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %12
%17 = load i32, i32* %12
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %5
store i32 -1, i32* %6
br label %61
20: ; preds = %5
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %45
23: ; preds = %20
store i32 1, i32* %13
%24 = load i32, i32* %12
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %13, i32 4)
%28 = load i32, i32* %12
%29 = load %struct.sockaddr*, %struct.sockaddr** %7
%30 = load i32, i32* %8
%31 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %44, label %33
33: ; preds = %23
%34 = load i32, i32* %10
%35 = load i32, i32* @SOCK_STREAM
%36 = icmp ne i32 %34, %35
br i1 %36, label %42, label %37
37: ; preds = %33
%38 = load i32, i32* %12
%39 = load i32, i32* @SOMAXCONN
%40 = call i32 @listen(i32 %38, i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %37, %33
%43 = load i32, i32* %12
store i32 %43, i32* %6
br label %61
44: ; preds = %37, %23
br label %58
45: ; preds = %20
%46 = load i32, i32* %12
%47 = load %struct.sockaddr*, %struct.sockaddr** %7
%48 = load i32, i32* %8
%49 = call i32 @connect(i32 %46, %struct.sockaddr* %47, i32 %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %45
%52 = load i64, i64* @errno
%53 = load i64, i64* @EINPROGRESS
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %51, %45
%56 = load i32, i32* %12
store i32 %56, i32* %6
br label %61
57: ; preds = %51
br label %58
58: ; preds = %57, %44
%59 = load i32, i32* %12
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %6
br label %61
61: ; preds = %58, %55, %42, %19
%62 = load i32, i32* %6
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SOCK_STREAM = global i32 0
@SOMAXCONN = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @usock_connect(%struct.sockaddr* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %36, label %9
9: ; preds = %5
%10 = icmp eq i32 %4, 0
br i1 %10, label %27, label %11
11: ; preds = %9
%12 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %7, i32 %13, i32 %14, i32* %6, i32 4)
%16 = call i32 @bind(i32 %7, %struct.sockaddr* %0, i32 %1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = load i32, i32* @SOCK_STREAM
%20 = icmp eq i32 %19, %3
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @SOMAXCONN
%23 = call i32 @listen(i32 %7, i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %21, %11
br label %34
26: ; preds = %18, %21
br label %36
27: ; preds = %9
%28 = tail call i32 @connect(i32 %7, %struct.sockaddr* %0, i32 %1)
%29 = icmp eq i32 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %27
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINPROGRESS
%33 = icmp eq i64 %31, %32
br i1 %33, label %36, label %34
34: ; preds = %25, %30
%35 = call i32 @close(i32 %7)
br label %36
36: ; preds = %27, %30, %26, %5, %34
%37 = phi i32 [ -1, %34 ], [ %7, %26 ], [ -1, %5 ], [ %7, %30 ], [ %7, %27 ]
ret i32 %37
}