#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  void *s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_port;
  TYPE_1__ sin_addr;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sock_addr;
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ dst_addr;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ connect_nb(int, struct sockaddr *, int, int);
scalar_t__ htons(int /*<<< orphan*/);
void *inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int _connect(int sock, const char *src, const char *dst, uint16_t port,
             int blocking) {
  static int on = 1;
  struct sockaddr_in sock_addr;
  struct sockaddr_in dst_addr;
  memset(&sock_addr, 0x00, sizeof(sock_addr));
  memset(&dst_addr, 0x00, sizeof(dst_addr));
  if (src && *src) {
    sock_addr.sin_family = AF_INET;
    sock_addr.sin_addr.s_addr = inet_addr(src);
    sock_addr.sin_port = 0;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    if (bind(sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) < 0) {
      close(sock);
      return -1;
    }
  }
  dst_addr.sin_family = AF_INET;
  dst_addr.sin_addr.s_addr = inet_addr(dst);
  dst_addr.sin_port = htons(port);
  if (blocking) {
    if (connect(sock, (struct sockaddr *)&dst_addr, sizeof(dst_addr)) < 0) {
      close(sock);
      return -1;
    }
  } else {
    if (connect_nb(sock, (struct sockaddr *)&dst_addr, sizeof(dst_addr), 1) <=
        0) {
      close(sock);
      return -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
je     1253 <_connect+0xd3>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1253 <_connect+0xd3>
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
lea    0x2e4c(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    124e <_connect+0xce>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <_connect+0x177>
jmp    1253 <_connect+0xd3>
mov    0x2d86(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x50(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12b8 <_connect+0x138>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x18,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12b3 <_connect+0x133>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <_connect+0x177>
jmp    12f1 <_connect+0x171>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x18,%edx
mov    $0x1,%ecx
call   1090 <connect_nb@plt>
cmp    $0x0,%rax
jg     12ec <_connect+0x16c>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <_connect+0x177>
jmp    12f1 <_connect+0x171>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
test   %rbp,%rbp
je     1226 <_connect+0xa6>
cmpb   $0x0,0x0(%rbp)
je     1226 <_connect+0xa6>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x28(%rsp)
mov    %rbp,%rdi
call   1070 <inet_addr@plt>
mov    %rax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
lea    0x2e5d(%rip),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1267 <_connect+0xe7>
mov    0x2db3(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    %r12,%rdi
call   1070 <inet_addr@plt>
mov    %rax,0x8(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x18,%edx
test   %r14d,%r14d
je     1282 <_connect+0x102>
call   10a0 <connect@plt>
test   %rax,%rax
jns    1273 <_connect+0xf3>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ecx
call   1090 <connect_nb@plt>
test   %rax,%rax
jg     1273 <_connect+0xf3>
jmp    1267 <_connect+0xe7>

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@_connect.on = internal global i32 1
@AF_INET = global i8* null
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @_connect(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr_in
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 24)
%15 = call i32 @memset(%struct.sockaddr_in* %13, i32 0, i32 24)
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %43
18: ; preds = %5
%19 = load i8*, i8** %8
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %18
%24 = load i8*, i8** @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i8* %24, i8** %25
%26 = load i8*, i8** %8
%27 = call i8* @inet_addr(i8* %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i8* %27, i8** %29
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i64 0, i64* %30
%31 = load i32, i32* %7
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i32 @setsockopt(i32 %31, i32 %32, i32 %33, i32* @_connect.on, i32 4)
%35 = load i32, i32* %7
%36 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%37 = call i64 @bind(i32 %35, %struct.sockaddr* %36, i32 24)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %23
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %6
br label %75
42: ; preds = %23
br label %43
43: ; preds = %42, %18, %5
%44 = load i8*, i8** @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i8* %44, i8** %45
%46 = load i8*, i8** %9
%47 = call i8* @inet_addr(i8* %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %48, i32 0, i32 0
store i8* %47, i8** %49
%50 = load i32, i32* %10
%51 = call i64 @htons(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
store i64 %51, i64* %52
%53 = load i32, i32* %11
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %64
55: ; preds = %43
%56 = load i32, i32* %7
%57 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%58 = call i64 @connect(i32 %56, %struct.sockaddr* %57, i32 24)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
store i32 -1, i32* %6
br label %75
63: ; preds = %55
br label %73
64: ; preds = %43
%65 = load i32, i32* %7
%66 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%67 = call i64 @connect_nb(i32 %65, %struct.sockaddr* %66, i32 24, i32 1)
%68 = icmp sle i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %64
%70 = load i32, i32* %7
%71 = call i32 @close(i32 %70)
store i32 -1, i32* %6
br label %75
72: ; preds = %64
br label %73
73: ; preds = %72, %63
%74 = load i32, i32* %7
store i32 %74, i32* %6
br label %75
75: ; preds = %73, %69, %60, %39
%76 = load i32, i32* %6
ret i32 %76
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@_connect.on = internal global i32 1
@AF_INET = global i8* null
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @_connect(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = bitcast %struct.sockaddr_in* %7 to i8*
%10 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 24)
%11 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 24)
%12 = icmp eq i8* %1, null
br i1 %12, label %28, label %13
13: ; preds = %5
%14 = load i8, i8* %1
%15 = icmp eq i8 %14, 0
br i1 %15, label %28, label %16
16: ; preds = %13
%17 = load i8*, i8** @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i8* %17, i8** %18
%19 = call i8* @inet_addr(i8* %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
store i8* %19, i8** %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i64 0, i64* %21
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %0, i32 %22, i32 %23, i32* @_connect.on, i32 4)
%25 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%26 = call i64 @bind(i32 %0, %struct.sockaddr* %25, i32 24)
%27 = icmp slt i64 %26, 0
br i1 %27, label %43, label %28
28: ; preds = %16, %13, %5
%29 = load i8*, i8** @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2
store i8* %29, i8** %30
%31 = call i8* @inet_addr(i8* %2)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1, i32 0
store i8* %31, i8** %32
%33 = call i64 @htons(i32 %3)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
store i64 %33, i64* %34
%35 = icmp eq i32 %4, 0
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
br i1 %35, label %40, label %37
37: ; preds = %28
%38 = call i64 @connect(i32 %0, %struct.sockaddr* %36, i32 24)
%39 = icmp slt i64 %38, 0
br i1 %39, label %43, label %45
40: ; preds = %28
%41 = call i64 @connect_nb(i32 %0, %struct.sockaddr* %36, i32 24, i32 1)
%42 = icmp slt i64 %41, 1
br i1 %42, label %43, label %45
43: ; preds = %40, %37, %16
%44 = call i32 @close(i32 %0)
br label %45
45: ; preds = %43, %37, %40
%46 = phi i32 [ %0, %40 ], [ %0, %37 ], [ -1, %43 ]
ret i32 %46
}