#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;
};
typedef int /*<<< orphan*/ addrCriteria;

/* 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 *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sysError(int /*<<< orphan*/);
int createClientSocket(const char *host, const char *service) {
  struct addrinfo addrCriteria;
  memset(&addrCriteria, 0, sizeof(addrCriteria));
  addrCriteria.ai_family = AF_UNSPEC;
  addrCriteria.ai_socktype = SOCK_STREAM;
  addrCriteria.ai_protocol = IPPROTO_TCP;
  struct addrinfo *servAddr;
  int ret = getaddrinfo(host, service, &addrCriteria, &servAddr);
  if (ret < 0) {
    sysError(gai_strerror(ret));
  }
  int servSock = -1;
  for (struct addrinfo *addr = servAddr; addr != NULL; addr = addr->ai_next) {
    servSock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
    if (servSock < 0)
      continue;
    if (connect(servSock, addr->ai_addr, addr->ai_addrlen) == 0)
      break;
    close(servSock);
    servSock = -1;
  }
  freeaddrinfo(servAddr);
  return servSock;
}


x86 assembly (clang, -O0):
<createClientSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11f5 <createClientSocket+0x75>
mov    -0x3c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edi
call   1030 <sysError@plt>
movl   $0xffffffff,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     127f <createClientSocket+0xff>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    123b <createClientSocket+0xbb>
jmp    126e <createClientSocket+0xee>
mov    -0x40(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    125f <createClientSocket+0xdf>
jmp    127f <createClientSocket+0xff>
mov    -0x40(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x40(%rbp)
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1204 <createClientSocket+0x84>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x40(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createClientSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
jns    11ee <createClientSocket+0x6e>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    %eax,%edi
call   1030 <sysError@plt>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1209 <createClientSocket+0x89>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1240 <createClientSocket+0xc0>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     123c <createClientSocket+0xbc>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1200 <createClientSocket+0x80>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1070 <connect@plt>
test   %rax,%rax
je     123c <createClientSocket+0xbc>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
jmp    1200 <createClientSocket+0x80>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
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
@IPPROTO_TCP = global i32 0
define i32 @createClientSocket(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 32)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @IPPROTO_TCP
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i32 @getaddrinfo(i8* %17, i8* %18, %struct.addrinfo* %5, %struct.addrinfo** %6)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = load i32, i32* %7
%24 = call i32 @gai_strerror(i32 %23)
%25 = call i32 @sysError(i32 %24)
br label %26
26: ; preds = %22, %2
store i32 -1, i32* %8
%27 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %27, %struct.addrinfo** %9
br label %28
28: ; preds = %59, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %9
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %63
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %9
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %9
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 3
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %9
%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* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %31
br label %59
45: ; preds = %31
%46 = load i32, i32* %8
%47 = load %struct.addrinfo*, %struct.addrinfo** %9
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %9
%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 eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %45
br label %63
56: ; preds = %45
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %8
br label %59
59: ; preds = %56, %44
%60 = load %struct.addrinfo*, %struct.addrinfo** %9
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 5
%62 = load %struct.addrinfo*, %struct.addrinfo** %61
store %struct.addrinfo* %62, %struct.addrinfo** %9
br label %28
63: ; preds = %55, %28
%64 = load %struct.addrinfo*, %struct.addrinfo** %6
%65 = call i32 @freeaddrinfo(%struct.addrinfo* %64)
%66 = load i32, i32* %8
ret i32 %66
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
define i32 @createClientSocket(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @IPPROTO_TCP
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = bitcast %struct.addrinfo** %4 to i8*
%14 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @gai_strerror(i32 %14)
%18 = call i32 @sysError(i32 %17)
br label %19
19: ; preds = %16, %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %49, label %22
22: ; preds = %19, %41
%23 = phi %struct.addrinfo* [ %44, %41 ], [ %20, %19 ]
%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 %41, label %32
32: ; preds = %22
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @connect(i32 %30, i32 %34, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %32
%40 = call i32 @close(i32 %30)
br label %41
41: ; preds = %22, %39
%42 = phi i32 [ %30, %22 ], [ -1, %39 ]
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %46, label %22
46: ; preds = %32, %41
%47 = phi i32 [ %42, %41 ], [ %30, %32 ]
%48 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %49
49: ; preds = %46, %19
%50 = phi %struct.addrinfo* [ null, %19 ], [ %48, %46 ]
%51 = phi i32 [ -1, %19 ], [ %47, %46 ]
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
ret i32 %51
}