#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;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int socket_connect(const char *hostname, const char *port, int *out_sock) {
  int rv;
  struct addrinfo *addrs = NULL, *addr = NULL, hints;
  int local_sock = -1;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  rv = getaddrinfo(hostname, port, &hints, &addrs);
  if (rv != 0) {
    fprintf(stderr, "Failed to get address info for %s:%s. Error: %d\n",
            hostname, port, rv);
    goto out;
  }
  for (addr = addrs; addr != NULL; addr = addr->ai_next) {
    local_sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
    if (local_sock == -1) {
      continue;
    }
    rv = connect(local_sock, addr->ai_addr, addr->ai_addrlen);
    if (rv < 0) {
      close(local_sock);
      local_sock = -1;
      continue;
    }
    break;
  }
  if (local_sock == -1) {
    fprintf(stderr, "Failed to connect to any address.\n");
    rv = -1;
    goto out;
  }
out:
  if (addrs != NULL) {
    freeaddrinfo(addrs);
  }
  if (rv != 0) {
    if (local_sock != -1) {
      close(local_sock);
    }
    *out_sock = -1;
  } else {
    *out_sock = local_sock;
  }
  return rv;
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x54(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1219 <socket_connect+0xa9>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12d6 <socket_connect+0x166>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12a4 <socket_connect+0x134>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1258 <socket_connect+0xe8>
jmp    1293 <socket_connect+0x123>
mov    -0x54(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    128e <socket_connect+0x11e>
mov    -0x54(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x54(%rbp)
jmp    1293 <socket_connect+0x123>
jmp    12a4 <socket_connect+0x134>
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1221 <socket_connect+0xb1>
cmpl   $0xffffffff,-0x54(%rbp)
jne    12d1 <socket_connect+0x161>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12d6 <socket_connect+0x166>
jmp    12d6 <socket_connect+0x166>
cmpq   $0x0,-0x28(%rbp)
je     12ea <socket_connect+0x17a>
mov    -0x28(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1315 <socket_connect+0x1a5>
cmpl   $0xffffffff,-0x54(%rbp)
je     1306 <socket_connect+0x196>
mov    -0x54(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    131e <socket_connect+0x1ae>
mov    -0x54(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     1205 <socket_connect+0x95>
mov    %eax,%r12d
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
mov    %r12d,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1265 <socket_connect+0xf5>
mov    0x8(%rsp),%rbp
jmp    1214 <socket_connect+0xa4>
nopl   0x0(%rax)
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     1248 <socket_connect+0xd8>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <socket_connect+0xa0>
mov    %eax,%ebx
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %eax,%edi
call   1060 <connect@plt>
test   %eax,%eax
jns    12a6 <socket_connect+0x136>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1210 <socket_connect+0xa0>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%r12d
mov    $0xffffffff,%ebx
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1279 <socket_connect+0x109>
call   1080 <freeaddrinfo@plt>
test   %r12d,%r12d
je     1291 <socket_connect+0x121>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%ebx
je     1293 <socket_connect+0x123>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1293 <socket_connect+0x123>
mov    %ebx,%ebp
mov    %ebp,(%r14)
mov    %r12d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r12d
jmp    126a <socket_connect+0xfa>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [49 x i8] c"Failed to get address info for %s:%s. Error: %d\0A\00"
@.str.1 = [35 x i8] c"Failed to connect to any address.\0A\00"
define i32 @socket_connect(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store %struct.addrinfo* null, %struct.addrinfo** %8
store %struct.addrinfo* null, %struct.addrinfo** %9
store i32 -1, i32* %11
%12 = call i32 @memset(%struct.addrinfo* %10, i32 0, i32 32)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 4
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 3
store i32 %15, i32* %16
%17 = load i32, i32* @IPPROTO_TCP
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = call i32 @getaddrinfo(i8* %19, i8* %20, %struct.addrinfo* %10, %struct.addrinfo** %8)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %3
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %4
%27 = load i8*, i8** %5
%28 = load i32, i32* %7
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27, i32 %28)
br label %75
30: ; preds = %3
%31 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %31, %struct.addrinfo** %9
br label %32
32: ; preds = %64, %30
%33 = load %struct.addrinfo*, %struct.addrinfo** %9
%34 = icmp ne %struct.addrinfo* %33, null
br i1 %34, label %35, label %68
35: ; preds = %32
%36 = load %struct.addrinfo*, %struct.addrinfo** %9
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 4
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %9
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 3
%41 = load i32, i32* %40
%42 = load %struct.addrinfo*, %struct.addrinfo** %9
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 2
%44 = load i32, i32* %43
%45 = call i32 @socket(i32 %38, i32 %41, i32 %44)
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %35
br label %64
49: ; preds = %35
%50 = load i32, i32* %11
%51 = load %struct.addrinfo*, %struct.addrinfo** %9
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %9
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @connect(i32 %50, i32 %53, i32 %56)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %49
%61 = load i32, i32* %11
%62 = call i32 @close(i32 %61)
store i32 -1, i32* %11
br label %64
63: ; preds = %49
br label %68
64: ; preds = %60, %48
%65 = load %struct.addrinfo*, %struct.addrinfo** %9
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 5
%67 = load %struct.addrinfo*, %struct.addrinfo** %66
store %struct.addrinfo* %67, %struct.addrinfo** %9
br label %32
68: ; preds = %63, %32
%69 = load i32, i32* %11
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %7
br label %75
74: ; preds = %68
br label %75
75: ; preds = %74, %71, %24
%76 = load %struct.addrinfo*, %struct.addrinfo** %8
%77 = icmp ne %struct.addrinfo* %76, null
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load %struct.addrinfo*, %struct.addrinfo** %8
%80 = call i32 @freeaddrinfo(%struct.addrinfo* %79)
br label %81
81: ; preds = %78, %75
%82 = load i32, i32* %7
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %92
84: ; preds = %81
%85 = load i32, i32* %11
%86 = icmp ne i32 %85, -1
br i1 %86, label %87, label %90
87: ; preds = %84
%88 = load i32, i32* %11
%89 = call i32 @close(i32 %88)
br label %90
90: ; preds = %87, %84
%91 = load i32*, i32** %6
store i32 -1, i32* %91
br label %95
92: ; preds = %81
%93 = load i32, i32* %11
%94 = load i32*, i32** %6
store i32 %93, i32* %94
br label %95
95: ; preds = %92, %90
%96 = load i32, i32* %7
ret i32 %96
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [49 x i8] c"Failed to get address info for %s:%s. Error: %d\0A\00"
@.str.1 = [35 x i8] c"Failed to connect to any address.\0A\00"
define i32 @socket_connect(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo
%6 = bitcast %struct.addrinfo** %4 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %4
%7 = bitcast %struct.addrinfo* %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 32)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @IPPROTO_TCP
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %5, %struct.addrinfo** %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %46, label %23
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i32 %15)
br label %49
23: ; preds = %17, %42
%24 = phi %struct.addrinfo* [ %44, %42 ], [ %18, %17 ]
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 3
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%30 = load i32, i32* %29
%31 = call i32 @socket(i32 %26, i32 %28, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %42, label %33
33: ; preds = %23
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @connect(i32 %31, i32 %35, i32 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %33
%41 = call i32 @close(i32 %31)
br label %42
42: ; preds = %23, %40
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 5
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %46, label %23
46: ; preds = %42, %17
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %33, %46, %20
%50 = phi i32 [ %15, %20 ], [ -1, %46 ], [ %38, %33 ]
%51 = phi i32 [ -1, %20 ], [ -1, %46 ], [ %31, %33 ]
%52 = load %struct.addrinfo*, %struct.addrinfo** %4
%53 = icmp eq %struct.addrinfo* %52, null
br i1 %53, label %56, label %54
54: ; preds = %49
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
br label %56
56: ; preds = %54, %49
%57 = icmp eq i32 %50, 0
br i1 %57, label %62, label %58
58: ; preds = %56
%59 = icmp eq i32 %51, -1
br i1 %59, label %62, label %60
60: ; preds = %58
%61 = call i32 @close(i32 %51)
br label %62
62: ; preds = %56, %58, %60
%63 = phi i32 [ -1, %60 ], [ -1, %58 ], [ %51, %56 ]
store i32 %63, i32* %2
ret i32 %50
}