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

/* Variables and functions */
int /*<<< orphan*/ Close(int);
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ err_msg(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
struct addrinfo *host_serv(char const *, char const *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int tcp_connect(const char *hostname, const char *service) {
  int sockfd;
  struct addrinfo *all_addrs, *addr;
  if ((all_addrs = host_serv(hostname, service, 0, SOCK_STREAM)) == NULL)
    exit(EXIT_FAILURE);
  for (addr = all_addrs; addr != NULL; addr = addr->ai_next) {
    if ((sockfd = socket(addr->ai_family, addr->ai_socktype, 0)) < 0)
      continue;
    if (connect(sockfd, addr->ai_addr, addr->ai_addrlen) == 0)
      break;
    Close(sockfd);
  }
  freeaddrinfo(all_addrs);
  if (addr == NULL) {
    err_msg("error connecting: %s", strerror(errno));
    exit(EXIT_FAILURE);
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
xor    %edx,%edx
call   1030 <host_serv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c4 <tcp_connect+0x44>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     123a <tcp_connect+0xba>
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11fd <tcp_connect+0x7d>
jmp    1229 <tcp_connect+0xa9>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jne    1221 <tcp_connect+0xa1>
jmp    123a <tcp_connect+0xba>
mov    -0x14(%rbp),%edi
call   1040 <Close@plt>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11cc <tcp_connect+0x4c>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1278 <tcp_connect+0xf8>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xd9b(%rip),%rdi
call   1050 <err_msg@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
xor    %edx,%edx
call   1030 <host_serv@plt>
test   %rax,%rax
je     1219 <tcp_connect+0x99>
mov    %rax,%r14
mov    %rax,%rbx
jmp    11b9 <tcp_connect+0x39>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     11f5 <tcp_connect+0x75>
mov    0x8(%rbx),%esi
mov    0xc(%rbx),%edi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     11b0 <tcp_connect+0x30>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
test   %rax,%rax
je     11e6 <tcp_connect+0x66>
mov    %ebp,%edi
call   1040 <Close@plt>
jmp    11b0 <tcp_connect+0x30>
mov    %r14,%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14,%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdee(%rip),%rdi
mov    %eax,%esi
call   1050 <err_msg@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, %struct.addrinfo* }
@SOCK_STREAM = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [21 x i8] c"error connecting: %s\00"
@errno = global i32 0
define i32 @tcp_connect(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = load i8*, i8** %4
%10 = load i32, i32* @SOCK_STREAM
%11 = call %struct.addrinfo* @host_serv(i8* %8, i8* %9, i32 0, i32 %10)
store %struct.addrinfo* %11, %struct.addrinfo** %6
%12 = icmp eq %struct.addrinfo* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %17, %struct.addrinfo** %7
br label %18
18: ; preds = %45, %16
%19 = load %struct.addrinfo*, %struct.addrinfo** %7
%20 = icmp ne %struct.addrinfo* %19, null
br i1 %20, label %21, label %49
21: ; preds = %18
%22 = load %struct.addrinfo*, %struct.addrinfo** %7
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i32 0, i32 3
%24 = load i32, i32* %23
%25 = load %struct.addrinfo*, %struct.addrinfo** %7
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %24, i32 %27, i32 0)
store i32 %28, i32* %5
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %21
br label %45
31: ; preds = %21
%32 = load i32, i32* %5
%33 = load %struct.addrinfo*, %struct.addrinfo** %7
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 1
%35 = load i32, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %7
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i64 @connect(i32 %32, i32 %35, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %31
br label %49
42: ; preds = %31
%43 = load i32, i32* %5
%44 = call i32 @Close(i32 %43)
br label %45
45: ; preds = %42, %30
%46 = load %struct.addrinfo*, %struct.addrinfo** %7
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 4
%48 = load %struct.addrinfo*, %struct.addrinfo** %47
store %struct.addrinfo* %48, %struct.addrinfo** %7
br label %18
49: ; preds = %41, %18
%50 = load %struct.addrinfo*, %struct.addrinfo** %6
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
%52 = load %struct.addrinfo*, %struct.addrinfo** %7
%53 = icmp eq %struct.addrinfo* %52, null
br i1 %53, label %54, label %60
54: ; preds = %49
%55 = load i32, i32* @errno
%56 = call i32 @strerror(i32 %55)
%57 = call i32 @err_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %56)
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %49
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, %struct.addrinfo* }
@SOCK_STREAM = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [21 x i8] c"error connecting: %s\00"
@errno = global i32 0
define i32 @tcp_connect(i8* %0, i8* %1) {
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call %struct.addrinfo* @host_serv(i8* %0, i8* %1, i32 0, i32 %3)
%5 = icmp eq %struct.addrinfo* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2, %26
%10 = phi %struct.addrinfo* [ %28, %26 ], [ %4, %2 ]
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 3
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 2
%14 = load i32, i32* %13
%15 = tail call i32 @socket(i32 %12, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %26, label %17
17: ; preds = %9
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i64 @connect(i32 %15, i32 %19, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %17
%25 = tail call i32 @Close(i32 %15)
br label %26
26: ; preds = %9, %24
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i64 0, i32 4
%28 = load %struct.addrinfo*, %struct.addrinfo** %27
%29 = icmp eq %struct.addrinfo* %28, null
br i1 %29, label %30, label %9
30: ; preds = %26
%31 = tail call i32 @freeaddrinfo(%struct.addrinfo* %4)
%32 = load i32, i32* @errno
%33 = tail call i32 @strerror(i32 %32)
%34 = tail call i32 @err_msg(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %17
%38 = tail call i32 @freeaddrinfo(%struct.addrinfo* %4)
ret i32 %15
}