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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int tcp_client(const char *hostname, const char *port) {
  int sock, rv;
  struct addrinfo hints, *info, *p;
  char s[INET6_ADDRSTRLEN];
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(hostname, port, &hints, &info)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return -1;
  }
  for (p = info; p != NULL; p = p->ai_next) {
    if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("client: socket");
      continue;
    }
    if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
      close(sock);
      perror("client: connect");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "client: failed to connect\n");
    return -1;
  }
  inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s,
            sizeof s);
  printf("client: connecting to %s\n", s);
  freeaddrinfo(info);
  return sock;
}


x86 assembly (clang, -O0):
<tcp_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1287 <tcp_client+0xc7>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0x7c(%rbp),%edi
mov    %rax,%rdx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13a4 <tcp_client+0x1e4>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     131f <tcp_client+0x15f>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    12d1 <tcp_client+0x111>
lea    0xd4a(%rip),%rdi
call   10a0 <perror@plt>
jmp    130e <tcp_client+0x14e>
mov    -0x1c(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    (%rax),%rsi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1309 <tcp_client+0x149>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
lea    0xd21(%rip),%rdi
call   10a0 <perror@plt>
jmp    130e <tcp_client+0x14e>
jmp    131f <tcp_client+0x15f>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    128f <tcp_client+0xcf>
cmpq   $0x0,-0x50(%rbp)
jne    1354 <tcp_client+0x194>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    0xcf6(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13a4 <tcp_client+0x1e4>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x80(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%rdi
call   1040 <get_in_addr@plt>
mov    -0x80(%rbp),%edi
mov    -0x70(%rbp),%rdx
mov    %eax,%esi
mov    -0x78(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x70(%rbp),%rsi
lea    0xcc2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_client>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsp,-0x38(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x58(%rbp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
lea    -0x30(%rbp),%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
je     1265 <tcp_client+0xa5>
mov    0x2d92(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xdb2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%r13d
jmp    1310 <tcp_client+0x150>
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
je     12bd <tcp_client+0xfd>
lea    0xd9c(%rip),%r12
mov    0x14(%rbx),%esi
mov    0x8(%rbx),%edi
mov    0x10(%rbx),%edx
call   10e0 <socket@plt>
mov    %r12,%rdi
cmp    $0xffffffff,%eax
je     12af <tcp_client+0xef>
mov    %eax,%r13d
mov    (%rbx),%rsi
mov    0xc(%rbx),%edx
mov    %eax,%edi
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12dc <tcp_client+0x11c>
mov    %r13d,%edi
call   1080 <close@plt>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1275 <tcp_client+0xb5>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%r13d
jmp    1310 <tcp_client+0x150>
mov    0x8(%rbx),%r12d
mov    (%rbx),%rdi
call   1040 <get_in_addr@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
lea    0xd4c(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x38(%rbp),%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@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 = [15 x i8] c"client: socket\00"
@.str.2 = [16 x i8] c"client: connect\00"
@.str.3 = [27 x i8] c"client: failed to connect\0A\00"
@.str.4 = [26 x i8] c"client: connecting to %s\0A\00"
define i32 @tcp_client(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i32, i32* @INET6_ADDRSTRLEN
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
%18 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%19 = load i32, i32* @AF_UNSPEC
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %21, i32* %22
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = call i32 @getaddrinfo(i8* %23, i8* %24, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %25, i32* %7
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %2
%28 = load i32, i32* @stderr
%29 = load i32, i32* %7
%30 = call i8* @gai_strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %30)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %91
32: ; preds = %2
%33 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %33, %struct.addrinfo** %10
br label %34
34: ; preds = %66, %32
%35 = load %struct.addrinfo*, %struct.addrinfo** %10
%36 = icmp ne %struct.addrinfo* %35, null
br i1 %36, label %37, label %70
37: ; preds = %34
%38 = load %struct.addrinfo*, %struct.addrinfo** %10
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %10
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 4
%43 = load i32, i32* %42
%44 = load %struct.addrinfo*, %struct.addrinfo** %10
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 3
%46 = load i32, i32* %45
%47 = call i32 @socket(i32 %40, i32 %43, i32 %46)
store i32 %47, i32* %6
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %37
%50 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %66
51: ; preds = %37
%52 = load i32, i32* %6
%53 = load %struct.addrinfo*, %struct.addrinfo** %10
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 0
%55 = load i64, i64* %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %10
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 2
%58 = load i32, i32* %57
%59 = call i32 @connect(i32 %52, i64 %55, i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %65
61: ; preds = %51
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
%64 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %66
65: ; preds = %51
br label %70
66: ; preds = %61, %49
%67 = load %struct.addrinfo*, %struct.addrinfo** %10
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %67, i32 0, i32 5
%69 = load %struct.addrinfo*, %struct.addrinfo** %68
store %struct.addrinfo* %69, %struct.addrinfo** %10
br label %34
70: ; preds = %65, %34
%71 = load %struct.addrinfo*, %struct.addrinfo** %10
%72 = icmp eq %struct.addrinfo* %71, null
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %91
76: ; preds = %70
%77 = load %struct.addrinfo*, %struct.addrinfo** %10
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 1
%79 = load i32, i32* %78
%80 = load %struct.addrinfo*, %struct.addrinfo** %10
%81 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %80, i32 0, i32 0
%82 = load i64, i64* %81
%83 = inttoptr i64 %82 to %struct.sockaddr*
%84 = call i32 @get_in_addr(%struct.sockaddr* %83)
%85 = trunc i64 %15 to i32
%86 = call i32 @inet_ntop(i32 %79, i32 %84, i8* %17, i32 %85)
%87 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %17)
%88 = load %struct.addrinfo*, %struct.addrinfo** %9
%89 = call i32 @freeaddrinfo(%struct.addrinfo* %88)
%90 = load i32, i32* %6
store i32 %90, i32* %3
store i32 1, i32* %13
br label %91
91: ; preds = %76, %73, %27
%92 = load i8*, i8** %11
%93 = load i32, i32* %3
ret i32 %93
}

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