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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
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 socket_connect(const char *hostname, char *service) {
  int sock, ret;
  struct addrinfo hints, *serverAddress, *cur;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  if ((ret = getaddrinfo(hostname, service, &hints, &serverAddress)) != 0) {
    printf("connectServer() -> getaddrinfo(): %s\n", gai_strerror(ret));
    return -1;
  }
  sock = -1;
  for (cur = serverAddress; cur != NULL; cur = cur->ai_next) {
    if ((sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_flags)) < 0) {
      perror("connectServer() -> socket()");
      sock = -1;
      continue;
    }
    if (connect(sock, cur->ai_addr, cur->ai_addrlen) < 0) {
      perror("connectServer() -> connect()");
      close(sock);
      sock = -1;
    } else {
      break;
    }
  }
  freeaddrinfo(serverAddress);
  return sock;
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1218 <socket_connect+0x88>
mov    -0x20(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xdf9(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <socket_connect+0x149>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12ca <socket_connect+0x13a>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1270 <socket_connect+0xe0>
lea    0xdc7(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12b9 <socket_connect+0x129>
mov    -0x1c(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    12af <socket_connect+0x11f>
lea    0xdac(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12b4 <socket_connect+0x124>
jmp    12ca <socket_connect+0x13a>
jmp    12b9 <socket_connect+0x129>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1227 <socket_connect+0x97>
mov    -0x50(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     120f <socket_connect+0x7f>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe00(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%ebp
jmp    1288 <socket_connect+0xf8>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1270 <socket_connect+0xe0>
lea    0xe06(%rip),%r14
lea    0xe1b(%rip),%r15
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
test   %eax,%eax
js     1266 <socket_connect+0xd6>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
test   %rax,%rax
jns    127e <socket_connect+0xee>
mov    %r15,%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1227 <socket_connect+0x97>
jmp    1279 <socket_connect+0xe9>
mov    %r14,%rdi
call   1070 <perror@plt>
jmp    125b <socket_connect+0xcb>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1283 <socket_connect+0xf3>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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