#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int INET6_ADDRSTRLEN;
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 *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int getConnection(char *hostname) {
  int sockfd;
  struct addrinfo hints, *servinfo, *p;
  int rv;
  char s[INET6_ADDRSTRLEN];
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(hostname, "3491", &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      continue;
    }
    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "client: failed to connect\n");
    return 2;
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<getConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,-0x60(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2d(%rip),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     1227 <getConnection+0xa7>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    12f1 <getConnection+0x171>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12a6 <getConnection+0x126>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1265 <getConnection+0xe5>
jmp    1295 <getConnection+0x115>
mov    -0x14(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1290 <getConnection+0x110>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1295 <getConnection+0x115>
jmp    12a6 <getConnection+0x126>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    122f <getConnection+0xaf>
cmpq   $0x0,-0x48(%rbp)
jne    12db <getConnection+0x15b>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    12f1 <getConnection+0x171>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getConnection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    %rsp,%r15
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe3b(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     1201 <getConnection+0x81>
mov    0x2dfa(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe17(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%ebp
jmp    126f <getConnection+0xef>
mov    (%rsp),%rbx
jmp    1214 <getConnection+0x94>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1248 <getConnection+0xc8>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <getConnection+0x90>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1266 <getConnection+0xe6>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1210 <getConnection+0x90>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%ebp
jmp    126f <getConnection+0xef>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %r15,%rsp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"3491\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [27 x i8] c"client: failed to connect\0A\00"
define i32 @getConnection(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 = tail call i8* @llvm.stacksave()
%7 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 32)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %10, i32* %11
%12 = 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)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load %struct.addrinfo*, %struct.addrinfo** %3
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %44, label %21
17: ; preds = %1
%18 = load i32, i32* @stderr
%19 = call i8* @gai_strerror(i32 %12)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %50
21: ; preds = %14, %40
%22 = phi %struct.addrinfo* [ %42, %40 ], [ %15, %14 ]
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 4
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %24, i32 %26, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %40, label %31
31: ; preds = %21
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @connect(i32 %29, i32 %33, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %47
38: ; preds = %31
%39 = call i32 @close(i32 %29)
br label %40
40: ; preds = %21, %38
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 5
%42 = load %struct.addrinfo*, %struct.addrinfo** %41
%43 = icmp eq %struct.addrinfo* %42, null
br i1 %43, label %44, label %21
44: ; preds = %40, %14
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %50
47: ; preds = %31
%48 = load %struct.addrinfo*, %struct.addrinfo** %3
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
br label %50
50: ; preds = %47, %44, %17
%51 = phi i32 [ 1, %17 ], [ 2, %44 ], [ %29, %47 ]
ret i32 %51
}