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

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_client(const char *hostname, const char *service, int family,
                   int socktype) {
  struct addrinfo hints, *res, *ressave;
  int n, sockfd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = family;
  hints.ai_socktype = socktype;
  n = getaddrinfo(hostname, service, &hints, &res);
  if (n < 0) {
    fprintf(stderr, "getaddrinfo error:: [%s]\n", gai_strerror(n));
    return -1;
  }
  ressave = res;
  sockfd = -1;
  while (res) {
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (!(sockfd < 0)) {
      if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
        break;
      close(sockfd);
      sockfd = -1;
    }
    res = res->ai_next;
  }
  freeaddrinfo(ressave);
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1206 <connect_client+0x86>
mov    0x2e05(%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    0xe0b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <connect_client+0x11a>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0xffffffff,-0x58(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     128b <connect_client+0x10b>
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jl     127a <connect_client+0xfa>
mov    -0x58(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    126b <connect_client+0xeb>
jmp    128b <connect_client+0x10b>
mov    -0x58(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x58(%rbp)
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1215 <connect_client+0x95>
mov    -0x50(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %ebp,0x10(%rsp)
mov    %ebx,0x14(%rsp)
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
js     122b <connect_client+0xab>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1253 <connect_client+0xd3>
mov    %rbx,%rax
jmp    11f3 <connect_client+0x73>
nopl   0x0(%rax)
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rax
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1258 <connect_client+0xd8>
mov    (%rax),%edi
mov    0x4(%rax),%esi
mov    0x18(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11e0 <connect_client+0x60>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
mov    %ebp,%edi
call   1070 <connect@plt>
test   %rax,%rax
je     1258 <connect_client+0xd8>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
jmp    11e0 <connect_client+0x60>
mov    0x2dae(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdbe(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1260 <connect_client+0xe0>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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