#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 /*<<< orphan*/ aiInit;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
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 *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connectToServer(char ipStr[]) {
  int fd;
  int ret;
  struct addrinfo aiInit;
  struct addrinfo *aiList = 0;
  memset(&aiInit, 0, sizeof(aiInit));
  aiInit.ai_family = AF_INET;
  aiInit.ai_socktype = SOCK_STREAM;
  ret = getaddrinfo(ipStr, "6281", &aiInit, &aiList);
  if (ret != 0 || aiList == 0) {
    fprintf(stderr, "Error, cannot lookup addrinfo\n");
    fprintf(stderr, "IP: %s, \terror: %d\n", ipStr, ret);
    perror("getaddrinfo: ");
    return -1;
  }
  fd = socket(aiList->ai_family, aiList->ai_socktype, aiList->ai_protocol);
  if (fd == -1) {
    fprintf(stderr, "Error, could not create socket\n");
    freeaddrinfo(aiList);
    return -1;
  }
  ret = connect(fd, aiList->ai_addr, aiList->ai_addrlen);
  if (ret == -1) {
    close(fd);
    fprintf(stderr, "Error, could not connect over socket\n");
    freeaddrinfo(aiList);
    return -1;
  }
  freeaddrinfo(aiList);
  return fd;
}


x86 assembly (clang, -O0):
<connectToServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe39(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11ec <connectToServer+0x6c>
cmpq   $0x0,-0x38(%rbp)
jne    1239 <connectToServer+0xb9>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <connectToServer+0x171>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    128c <connectToServer+0x10c>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <connectToServer+0x171>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12e2 <connectToServer+0x162>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <connectToServer+0x171>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectToServer>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe38(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1080 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1267 <connectToServer+0xe7>
mov    (%rsp),%rax
test   %rax,%rax
je     1267 <connectToServer+0xe7>
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     121e <connectToServer+0x9e>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1230 <connectToServer+0xb0>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    125c <connectToServer+0xdc>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
jmp    1247 <connectToServer+0xc7>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d6a(%rip),%r14
mov    (%r14),%edi
lea    0xd8d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
lea    0xd9b(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xd9c(%rip),%rdi
call   1060 <perror@plt>
jmp    1257 <connectToServer+0xd7>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"6281\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Error, cannot lookup addrinfo\0A\00"
@.str.2 = [20 x i8] c"IP: %s, \09error: %d\0A\00"
@.str.3 = [14 x i8] c"getaddrinfo: \00"
@.str.4 = [32 x i8] c"Error, could not create socket\0A\00"
@.str.5 = [38 x i8] c"Error, could not connect over socket\0A\00"
define i32 @connectToServer(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store %struct.addrinfo* null, %struct.addrinfo** %7
%8 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 20)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %11, i32* %12
%13 = load i8*, i8** %3
%14 = call i32 @getaddrinfo(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %1
%18 = load %struct.addrinfo*, %struct.addrinfo** %7
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %20, label %28
20: ; preds = %17, %1
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %3
%25 = load i32, i32* %5
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %25)
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %68
28: ; preds = %17
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 4
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %7
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 3
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %7
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = call i32 @socket(i32 %31, i32 %34, i32 %37)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %28
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%44 = load %struct.addrinfo*, %struct.addrinfo** %7
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
store i32 -1, i32* %2
br label %68
46: ; preds = %28
%47 = load i32, i32* %4
%48 = load %struct.addrinfo*, %struct.addrinfo** %7
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %7
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @connect(i32 %47, i32 %50, i32 %53)
store i32 %54, i32* %5
%55 = load i32, i32* %5
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %64
57: ; preds = %46
%58 = load i32, i32* %4
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%62 = load %struct.addrinfo*, %struct.addrinfo** %7
%63 = call i32 @freeaddrinfo(%struct.addrinfo* %62)
store i32 -1, i32* %2
br label %68
64: ; preds = %46
%65 = load %struct.addrinfo*, %struct.addrinfo** %7
%66 = call i32 @freeaddrinfo(%struct.addrinfo* %65)
%67 = load i32, i32* %4
store i32 %67, i32* %2
br label %68
68: ; preds = %64, %57, %41, %20
%69 = load i32, i32* %2
ret i32 %69
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [5 x i8] c"6281\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Error, cannot lookup addrinfo\0A\00"
@.str.2 = [20 x i8] c"IP: %s, \09error: %d\0A\00"
@.str.3 = [14 x i8] c"getaddrinfo: \00"
@.str.4 = [32 x i8] c"Error, could not create socket\0A\00"
@.str.5 = [38 x i8] c"Error, could not connect over socket\0A\00"
define i32 @connectToServer(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%6 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 20)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %9, i32* %10
%11 = 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)
%12 = icmp ne i32 %11, 0
%13 = load %struct.addrinfo*, %struct.addrinfo** %3
%14 = icmp eq %struct.addrinfo* %13, null
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %22
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %11)
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %53
22: ; preds = %1
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 4
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, 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 %31, label %36
31: ; preds = %22
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%34 = load %struct.addrinfo*, %struct.addrinfo** %3
%35 = call i32 @freeaddrinfo(%struct.addrinfo* %34)
br label %53
36: ; preds = %22
%37 = load %struct.addrinfo*, %struct.addrinfo** %3
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @connect(i32 %29, i32 %39, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %36
%45 = call i32 @close(i32 %29)
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%48 = load %struct.addrinfo*, %struct.addrinfo** %3
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
br label %53
50: ; preds = %36
%51 = load %struct.addrinfo*, %struct.addrinfo** %3
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %53
53: ; preds = %50, %44, %31, %16
%54 = phi i32 [ -1, %16 ], [ -1, %31 ], [ -1, %44 ], [ %29, %50 ]
ret i32 %54
}