#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 /*<<< 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 /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int client_connect(char *host, char *service, int *sock) {
  struct addrinfo hints, *servinfo, *p;
  int rv;
  int sockfd;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(host, service, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    freeaddrinfo(servinfo);
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("socket");
      continue;
    }
    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      perror("connect");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "failed to connect\n");
    freeaddrinfo(servinfo);
    return -1;
  }
  freeaddrinfo(servinfo);
  *sock = sockfd;
  return 0;
}


x86 assembly (clang, -O0):
<client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%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     122a <client_connect+0x9a>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x54(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <client_connect+0x181>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12c1 <client_connect+0x131>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x58(%rbp)
cmp    $0xffffffff,%eax
jne    1274 <client_connect+0xe4>
lea    0xda7(%rip),%rdi
call   1070 <perror@plt>
jmp    12b0 <client_connect+0x120>
mov    -0x58(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ab <client_connect+0x11b>
mov    -0x58(%rbp),%edi
call   1050 <close@plt>
lea    0xd77(%rip),%rdi
call   1070 <perror@plt>
jmp    12b0 <client_connect+0x120>
jmp    12c1 <client_connect+0x131>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1232 <client_connect+0xa2>
cmpq   $0x0,-0x50(%rbp)
jne    12f8 <client_connect+0x168>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd44(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <client_connect+0x181>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x58(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     120f <client_connect+0x7f>
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdff(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    127f <client_connect+0xef>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1268 <client_connect+0xd8>
lea    0xdf1(%rip),%r15
lea    0xdf1(%rip),%r12
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
mov    %r15,%rdi
cmp    $0xffffffff,%eax
je     125a <client_connect+0xca>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1290 <client_connect+0x100>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1070 <perror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1227 <client_connect+0x97>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0xffffffff,%eax
jmp    129f <client_connect+0x10f>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,(%r14)
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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