#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ clen;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ TCP_NODELAY;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ set_noblock(int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_directly(struct addrinfo *ai_dest) {
  int sock_fd;
  socklen_t clen;
  if ((sock_fd = socket(ai_dest->ai_family, ai_dest->ai_socktype,
                        ai_dest->ai_protocol)) == -1) {
    return -1;
  }
  clen = 1;
  setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &clen, sizeof(clen));
  clen = 1;
  setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &clen, sizeof(clen));
  if (connect(sock_fd, ai_dest->ai_addr, ai_dest->ai_addrlen) == -1) {
    return -1;
  }
  set_noblock(sock_fd);
  return sock_fd;
}


x86 assembly (clang, -O0):
<connect_directly>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    117e <connect_directly+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <connect_directly+0xcc>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11fe <connect_directly+0xbe>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <connect_directly+0xcc>
mov    -0x14(%rbp),%edi
call   1040 <set_noblock@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_directly>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x10(%rdi),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   1060 <socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     11da <connect_directly+0x9a>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movl   $0x1,0x4(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
je     11da <connect_directly+0x9a>
mov    %ebx,%edi
call   1040 <set_noblock@plt>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @connect_directly(%struct.addrinfo* %0) {
%2 = alloca i32
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %3
%6 = load %struct.addrinfo*, %struct.addrinfo** %3
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
%8 = load i32, i32* %7
%9 = load %struct.addrinfo*, %struct.addrinfo** %3
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 3
%11 = load i32, i32* %10
%12 = load %struct.addrinfo*, %struct.addrinfo** %3
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 2
%14 = load i32, i32* %13
%15 = call i32 @socket(i32 %8, i32 %11, i32 %14)
store i32 %15, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %41
18: ; preds = %1
store i32 1, i32* %5
%19 = load i32, i32* %4
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, i32* %5, i32 4)
store i32 1, i32* %5
%23 = load i32, i32* %4
%24 = load i32, i32* @IPPROTO_TCP
%25 = load i32, i32* @TCP_NODELAY
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %5, i32 4)
%27 = load i32, i32* %4
%28 = load %struct.addrinfo*, %struct.addrinfo** %3
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %3
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @connect(i32 %27, i32 %30, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %18
store i32 -1, i32* %2
br label %41
37: ; preds = %18
%38 = load i32, i32* %4
%39 = call i32 @set_noblock(i32 %38)
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %41
41: ; preds = %37, %36, %17
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @connect_directly(%struct.addrinfo* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 4
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 3
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%9 = load i32, i32* %8
%10 = tail call i32 @socket(i32 %5, i32 %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %27, label %12
12: ; preds = %1
store i32 1, i32* %2
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %10, i32 %13, i32 %14, i32* %2, i32 4)
store i32 1, i32* %2
%16 = load i32, i32* @IPPROTO_TCP
%17 = load i32, i32* @TCP_NODELAY
%18 = call i32 @setsockopt(i32 %10, i32 %16, i32 %17, i32* %2, i32 4)
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @connect(i32 %10, i32 %20, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %27, label %25
25: ; preds = %12
%26 = call i32 @set_noblock(i32 %10)
br label %27
27: ; preds = %12, %1, %25
%28 = phi i32 [ %10, %25 ], [ -1, %1 ], [ -1, %12 ]
ret i32 %28
}