#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 *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tl1Socket(const char *host, const char *service) {
  char *iFunc = "tl1Socket";
  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 rtnVal = getaddrinfo(host, service, &addrCriteria, &servAddr);
  if (rtnVal != 0)
    printf("%s ::: %s => %s\n", iFunc, "getaddrinfo() failed",
           gai_strerror(rtnVal));
  int sock = -1;
  for (struct addrinfo *addr = servAddr; addr != NULL; addr = addr->ai_next) {
    sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
    if (sock < 0) {
      printf("%s ::: %s => %s\n", iFunc, "socket()",
             "socket creation failed; try next address");
      continue;
    }
    if (connect(sock, addr->ai_addr, addr->ai_addrlen) == 0) {
      printf("%s ::: %s => %s\n", iFunc, "connect()",
             "socket connection succeeded");
      break;
    }
    close(sock);
    sock = -1;
  }
  freeaddrinfo(servAddr);
  return sock;
}


x86 assembly (clang, -O0):
<tl1Socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe69(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     121b <tl1Socket+0x9b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x44(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x58(%rbp),%rsi
mov    %rax,%rcx
lea    0xdfb(%rip),%rdi
lea    0xe05(%rip),%rdx
call   1030 <printf@plt>
movl   $0xffffffff,-0x48(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12e1 <tl1Socket+0x161>
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   10a0 <socket@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    127f <tl1Socket+0xff>
mov    -0x18(%rbp),%rsi
lea    0xda3(%rip),%rdi
lea    0xdc2(%rip),%rdx
lea    0xdc4(%rip),%rcx
call   1030 <printf@plt>
jmp    12d0 <tl1Socket+0x150>
mov    -0x48(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    12c1 <tl1Socket+0x141>
mov    -0x18(%rbp),%rsi
lea    0xd61(%rip),%rdi
lea    0xdb2(%rip),%rdx
lea    0xdb5(%rip),%rcx
call   1030 <printf@plt>
jmp    12e1 <tl1Socket+0x161>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x48(%rbp)
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    122a <tl1Socket+0xaa>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x48(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tl1Socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
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    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e17(%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
je     1208 <tl1Socket+0x88>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe18(%rip),%rdi
lea    0xe07(%rip),%rsi
lea    0xe1b(%rip),%rdx
mov    %rax,%rcx
call   1030 <printf@plt>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     127e <tl1Socket+0xfe>
lea    0xdf2(%rip),%r14
lea    0xde1(%rip),%r15
lea    0xe0a(%rip),%r12
lea    0xe0c(%rip),%r13
jmp    124a <tl1Socket+0xca>
nop
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1030 <printf@plt>
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     12a6 <tl1Socket+0x126>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1230 <tl1Socket+0xb0>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   1070 <connect@plt>
test   %rax,%rax
je     1285 <tl1Socket+0x105>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    1241 <tl1Socket+0xc1>
mov    $0xffffffff,%ebx
jmp    12a6 <tl1Socket+0x126>
lea    0xd7e(%rip),%rdi
lea    0xd6d(%rip),%rsi
lea    0xdc8(%rip),%rdx
lea    0xdcb(%rip),%rcx
call   1030 <printf@plt>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [10 x i8] c"tl1Socket\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [17 x i8] c"%s ::: %s => %s\0A\00"
@.str.2 = [21 x i8] c"getaddrinfo() failed\00"
@.str.3 = [9 x i8] c"socket()\00"
@.str.4 = [41 x i8] c"socket creation failed; try next address\00"
@.str.5 = [10 x i8] c"connect()\00"
@.str.6 = [28 x i8] c"socket connection succeeded\00"
define i32 @tl1Socket(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 eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = call i8* @gai_strerror(i32 %14)
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %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 %50, label %22
22: ; preds = %19, %45
%23 = phi %struct.addrinfo* [ %48, %45 ], [ %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 %32, label %34
32: ; preds = %22
%33 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %45
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 eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
br label %50
43: ; preds = %34
%44 = call i32 @close(i32 %30)
br label %45
45: ; preds = %43, %32
%46 = phi i32 [ %30, %32 ], [ -1, %43 ]
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%48 = load %struct.addrinfo*, %struct.addrinfo** %47
%49 = icmp eq %struct.addrinfo* %48, null
br i1 %49, label %50, label %22
50: ; preds = %45, %19, %41
%51 = phi i32 [ %30, %41 ], [ -1, %19 ], [ %46, %45 ]
%52 = load %struct.addrinfo*, %struct.addrinfo** %4
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
ret i32 %51
}