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

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ eerror(char *);
int /*<<< orphan*/ error(char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ info(char *, char const *, char const *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int getconnection(const char *node, const char *service, int family) {
  struct addrinfo hints;
  struct addrinfo *servinfos;
  struct addrinfo *servinfo;
  int ret;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = family;
  hints.ai_socktype = SOCK_STREAM;
  if ((ret = getaddrinfo(node, service, &hints, &servinfos)) != 0) {
    error("Failed to get address info: %s", gai_strerror(ret));
    return -1;
  }
  for (servinfo = servinfos; servinfo != NULL; servinfo = servinfo->ai_next) {
    if ((ret = socket(servinfo->ai_family, servinfo->ai_socktype,
                      servinfo->ai_protocol)) < 0) {
      eerror("Failed to create socket");
      continue;
    }
    if (connect(ret, servinfo->ai_addr, servinfo->ai_addrlen) < 0) {
      close(ret);
      eerror("Failed to connect socket");
      continue;
    }
    freeaddrinfo(servinfos);
    info("Connected on %s:%s", node, service);
    return ret;
  }
  freeaddrinfo(servinfos);
  error("Failed to get socket");
  return -1;
}


x86 assembly (clang, -O0):
<getconnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     121a <getconnection+0x7a>
mov    -0x54(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    %eax,%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <getconnection+0x153>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12d5 <getconnection+0x135>
mov    -0x50(%rbp),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jge    1263 <getconnection+0xc3>
lea    0xdc6(%rip),%rdi
call   1060 <eerror@plt>
jmp    12c4 <getconnection+0x124>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    129c <getconnection+0xfc>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
lea    0xda5(%rip),%rdi
call   1060 <eerror@plt>
jmp    12c4 <getconnection+0x124>
mov    -0x48(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xd9c(%rip),%rdi
call   10a0 <info@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f3 <getconnection+0x153>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1222 <getconnection+0x82>
mov    -0x48(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1070 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getconnection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %ebp,0x8(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     120c <getconnection+0x6c>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe02(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <error@plt>
jmp    128d <getconnection+0xed>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1272 <getconnection+0xd2>
lea    0xe03(%rip),%r12
lea    0xe14(%rip),%r13
jmp    123e <getconnection+0x9e>
cs nopw 0x0(%rax,%rax,1)
nop
call   1060 <eerror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1276 <getconnection+0xd6>
mov    0x10(%rbx),%esi
mov    (%rbx),%edi
mov    0xc(%rbx),%edx
call   10c0 <socket@plt>
mov    %r12,%rdi
test   %eax,%eax
js     1230 <getconnection+0x90>
mov    %eax,%ebp
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
test   %rax,%rax
jns    12a3 <getconnection+0x103>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r13,%rdi
jmp    1230 <getconnection+0x90>
xor    %edi,%edi
jmp    127a <getconnection+0xda>
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
lea    0xddd(%rip),%rdi
xor    %eax,%eax
call   1070 <error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
lea    0xd9d(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   10a0 <info@plt>
jmp    1292 <getconnection+0xf2>

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

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