#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;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ EXIT_FAILURE;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int establishConnection(char *serv) {
  int socket_fd;
  struct addrinfo hints, *servinfo, *p;
  char s[INET_ADDRSTRLEN];
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(serv, "1337", &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    exit(EXIT_FAILURE);
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((socket_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) ==
        -1) {
      perror("client: socket");
      continue;
    }
    if (connect(socket_fd, p->ai_addr, p->ai_addrlen) == -1) {
      perror("client: connect");
      close(socket_fd);
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "client: failed to connect\n");
    exit(EXIT_FAILURE);
  }
  inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s,
            sizeof s);
  printf("client: connecting to %s\n", s);
  freeaddrinfo(servinfo);
  return socket_fd;
}


x86 assembly (clang, -O0):
<establishConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdc1(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     128e <establishConnection+0xbe>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x54(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xd8c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1326 <establishConnection+0x156>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    12d8 <establishConnection+0x108>
lea    0xd48(%rip),%rdi
call   10a0 <perror@plt>
jmp    1315 <establishConnection+0x145>
mov    -0xc(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1310 <establishConnection+0x140>
lea    0xd27(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
jmp    1315 <establishConnection+0x145>
jmp    1326 <establishConnection+0x156>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1296 <establishConnection+0xc6>
cmpq   $0x0,-0x40(%rbp)
jne    1356 <establishConnection+0x186>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
lea    0xcf4(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c71(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x70(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
call   1040 <get_in_addr@plt>
mov    -0x70(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %eax,%esi
mov    -0x68(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x60(%rbp),%rsi
lea    0xcc5(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    -0xc(%rbp),%eax
mov    -0x48(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establishConnection>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
mov    %rsp,-0x38(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x58(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
lea    0xdcb(%rip),%rsi
lea    -0x30(%rbp),%rcx
mov    %rbx,%rdi
mov    %r15,%rdx
call   10d0 <getaddrinfo@plt>
test   %eax,%eax
jne    131d <establishConnection+0x14d>
mov    -0x30(%rbp),%r12
test   %r12,%r12
je     1304 <establishConnection+0x134>
lea    0xdc5(%rip),%r15
mov    0x14(%r12),%esi
mov    0x8(%r12),%edi
mov    0x10(%r12),%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     12ab <establishConnection+0xdb>
mov    %eax,%ebx
mov    (%r12),%rsi
mov    0xc(%r12),%edx
mov    %eax,%edi
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12b9 <establishConnection+0xe9>
mov    %r15,%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0x18(%r12),%r12
test   %r12,%r12
jne    1260 <establishConnection+0x90>
jmp    1304 <establishConnection+0x134>
lea    0xd64(%rip),%rdi
call   10a0 <perror@plt>
jmp    129f <establishConnection+0xcf>
mov    0x8(%r12),%r15d
mov    (%r12),%rdi
call   1040 <get_in_addr@plt>
mov    %r15d,%edi
mov    %eax,%esi
mov    %r13,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
lea    0xd72(%rip),%rdi
mov    %r13,%rsi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    -0x38(%rbp),%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0xd21(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1340 <establishConnection+0x170>
mov    0x2cb4(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xcd1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"1337\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [15 x i8] c"client: socket\00"
@.str.3 = [16 x i8] c"client: connect\00"
@.str.4 = [27 x i8] c"client: failed to connect\0A\00"
@.str.5 = [26 x i8] c"client: connecting to %s\0A\00"
define i32 @establishConnection(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i32, i32* @INET_ADDRSTRLEN
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%15 = load i32, i32* @AF_UNSPEC
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_STREAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 4
store i32 %17, i32* %18
%19 = load i8*, i8** %2
%20 = call i32 @getaddrinfo(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %5)
store i32 %20, i32* %9
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %1
%23 = load i32, i32* @stderr
%24 = load i32, i32* %9
%25 = call i8* @gai_strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %1
%30 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %30, %struct.addrinfo** %6
br label %31
31: ; preds = %63, %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %6
%33 = icmp ne %struct.addrinfo* %32, null
br i1 %33, label %34, label %67
34: ; preds = %31
%35 = load %struct.addrinfo*, %struct.addrinfo** %6
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %6
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 4
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %6
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = call i32 @socket(i32 %37, i32 %40, i32 %43)
store i32 %44, i32* %3
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %34
%47 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %63
48: ; preds = %34
%49 = load i32, i32* %3
%50 = load %struct.addrinfo*, %struct.addrinfo** %6
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i64, i64* %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %6
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 2
%55 = load i32, i32* %54
%56 = call i32 @connect(i32 %49, i64 %52, i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %62
58: ; preds = %48
%59 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32, i32* %3
%61 = call i32 @close(i32 %60)
br label %63
62: ; preds = %48
br label %67
63: ; preds = %58, %46
%64 = load %struct.addrinfo*, %struct.addrinfo** %6
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 5
%66 = load %struct.addrinfo*, %struct.addrinfo** %65
store %struct.addrinfo* %66, %struct.addrinfo** %6
br label %31
67: ; preds = %62, %31
%68 = load %struct.addrinfo*, %struct.addrinfo** %6
%69 = icmp eq %struct.addrinfo* %68, null
br i1 %69, label %70, label %75
70: ; preds = %67
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %67
%76 = load %struct.addrinfo*, %struct.addrinfo** %6
%77 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %76, i32 0, i32 1
%78 = load i32, i32* %77
%79 = load %struct.addrinfo*, %struct.addrinfo** %6
%80 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %79, i32 0, i32 0
%81 = load i64, i64* %80
%82 = inttoptr i64 %81 to %struct.sockaddr*
%83 = call i32 @get_in_addr(%struct.sockaddr* %82)
%84 = trunc i64 %11 to i32
%85 = call i32 @inet_ntop(i32 %78, i32 %83, i8* %13, i32 %84)
%86 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %13)
%87 = load %struct.addrinfo*, %struct.addrinfo** %5
%88 = call i32 @freeaddrinfo(%struct.addrinfo* %87)
%89 = load i32, i32* %3
%90 = load i8*, i8** %7
ret i32 %89
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"1337\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [15 x i8] c"client: socket\00"
@.str.3 = [16 x i8] c"client: connect\00"
@.str.4 = [27 x i8] c"client: failed to connect\0A\00"
@.str.5 = [26 x i8] c"client: connecting to %s\0A\00"
define i32 @establishConnection(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = load i32, i32* @INET_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 32)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load %struct.addrinfo*, %struct.addrinfo** %3
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %52, label %26
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = call i8* @gai_strerror(i32 %15)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %17, %48
%27 = phi %struct.addrinfo* [ %50, %48 ], [ %18, %17 ]
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 4
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 3
%33 = load i32, i32* %32
%34 = call i32 @socket(i32 %29, i32 %31, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %48
38: ; preds = %26
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%40 = load i64, i64* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 2
%42 = load i32, i32* %41
%43 = call i32 @connect(i32 %34, i64 %40, i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %57
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @close(i32 %34)
br label %48
48: ; preds = %45, %36
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 5
%50 = load %struct.addrinfo*, %struct.addrinfo** %49
%51 = icmp eq %struct.addrinfo* %50, null
br i1 %51, label %52, label %26
52: ; preds = %48, %17
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %38
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%60 = load i32, i32* %58
%61 = load i64, i64* %59
%62 = inttoptr i64 %61 to %struct.sockaddr*
%63 = call i32 @get_in_addr(%struct.sockaddr* %62)
%64 = call i32 @inet_ntop(i32 %60, i32 %63, i8* %9, i32 %6)
%65 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %9)
%66 = load %struct.addrinfo*, %struct.addrinfo** %3
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
ret i32 %34
}