#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_family;
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ ipstr;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ EXIT_FAILURE;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ recv(int, void *, size_t, int);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
bool init_client() {
  int status;
  struct addrinfo hints, *res, *it, tmp;
  int sockfd;
  char ipstr[INET6_ADDRSTRLEN];
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  const char *hostname = "localhost";
  const char *servname = "1988";
  printf("--\n");
  printf("Addr info associated to host [%s] and service name [%s]\n", hostname,
         servname);
  status = getaddrinfo(hostname, servname, &hints, &res);
  if (status != (0)) {
    fprintf(stderr, "getaddrinfo failed, reason: %s\n", gai_strerror(status));
    exit(EXIT_FAILURE);
  }
  for (it = res; it != NULL; it = it->ai_next) {
    void *addr;
    const char *ip_version;
    if (it->ai_family == AF_INET) {
      struct sockaddr_in *ipv4 = (struct sockaddr_in *)it->ai_addr;
      addr = &(ipv4->sin_addr);
      ip_version = "IPv4";
      tmp = *it;
    } else if (it->ai_family == AF_INET6) {
      struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)it->ai_addr;
      addr = &(ipv6->sin6_addr);
      ip_version = "IPv6";
      tmp = *it;
    } else {
      continue;
    }
    inet_ntop(it->ai_family, addr, ipstr, sizeof(ipstr));
    printf("Address %s = %s\n", ip_version, ipstr);
  }
  printf("--\n");
  for (it = res; it != NULL; it = it->ai_next) {
    sockfd = socket(it->ai_family, it->ai_socktype, it->ai_protocol);
    if (sockfd == (-1)) {
      fprintf(stderr, "socket failed, reason: %s\n", strerror(errno));
      continue;
    }
    fprintf(stdout, "socket creation succeed\n");
    if (connect(sockfd, res->ai_addr, res->ai_addrlen) == (-1)) {
      perror("connect()");
      continue;
    }
    printf("Connected to remote server\n");
    char buffer[1024];
    int flags = 0;
    size_t length = sizeof(buffer);
    ssize_t bytes_recv = recv(sockfd, (void *)buffer, length, flags);
    if (bytes_recv > 0) {
      fprintf(stdout, "Got a new message:\n");
      fprintf(stdout, "\t%s [%ld chars]\n", buffer, bytes_recv);
    } else if (bytes_recv == 0) {
      printf("the remote side has closed the connection on you!");
    } else {
      perror("recv()");
    }
    close(sockfd);
    break;
  }
  freeaddrinfo(res);
  return true;
}


x86 assembly (clang, -O0):
<init_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4f0,%rsp
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x4e8(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x4e0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x90(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1070 <memset@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xd8c(%rip),%rax
mov    %rax,-0x98(%rbp)
lea    0xd88(%rip),%rax
mov    %rax,-0xa0(%rbp)
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x98(%rbp),%rsi
mov    -0xa0(%rbp),%rdx
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x98(%rbp),%rdi
mov    -0xa0(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10e0 <getaddrinfo@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1317 <init_client+0x127>
mov    0x2cd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4ec(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0x4ec(%rbp),%edi
mov    %rax,%rdx
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1436 <init_client+0x246>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2ca0(%rip),%rcx
cmp    (%rcx),%rax
jne    1383 <init_client+0x193>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0xb8(%rbp)
mov    -0xb8(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    0xd07(%rip),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x48(%rbp),%rsi
lea    -0x78(%rbp),%rdi
mov    $0x30,%edx
call   10a0 <memcpy@plt>
jmp    13e6 <init_client+0x1f6>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2c37(%rip),%rcx
cmp    (%rcx),%rax
jne    13dc <init_client+0x1ec>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0xc0(%rbp)
mov    -0xc0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    0xcb3(%rip),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x48(%rbp),%rsi
lea    -0x78(%rbp),%rdi
mov    $0x30,%edx
call   10a0 <memcpy@plt>
jmp    13e1 <init_client+0x1f1>
jmp    1425 <init_client+0x235>
jmp    13e6 <init_client+0x1f6>
mov    -0x4e0(%rbp),%rdx
mov    -0x4e8(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rcx),%rdi
mov    -0xa8(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x4e0(%rbp),%rdx
mov    -0xb0(%rbp),%rsi
lea    0xc58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    131f <init_client+0x12f>
lea    0xbd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     15e4 <init_client+0x3f4>
mov    -0x48(%rbp),%rax
mov    (%rax),%rdi
mov    -0x48(%rbp),%rax
mov    0x1c(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%edx
call   1110 <socket@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0xffffffff,-0x7c(%rbp)
jne    14b7 <init_client+0x2c7>
mov    0x2b33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4f0(%rbp)
mov    0x2b14(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x4f0(%rbp),%edi
mov    %rax,%rdx
lea    0xbdc(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    15d3 <init_client+0x3e3>
mov    0x2ada(%rip),%rax
mov    (%rax),%edi
lea    0xbdb(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x7c(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    14ff <init_client+0x30f>
lea    0xbc6(%rip),%rdi
call   10b0 <perror@plt>
jmp    15d3 <init_client+0x3e3>
lea    0xbbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4c4(%rbp)
movq   $0x400,-0x4d0(%rbp)
mov    -0x7c(%rbp),%edi
lea    -0x4c0(%rbp),%rsi
mov    -0x4d0(%rbp),%rdx
mov    -0x4c4(%rbp),%ecx
call   1040 <recv@plt>
mov    %rax,-0x4d8(%rbp)
cmpq   $0x0,-0x4d8(%rbp)
jle    1594 <init_client+0x3a4>
mov    0x2a3e(%rip),%rax
mov    (%rax),%edi
lea    0xb7e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2a27(%rip),%rax
mov    (%rax),%edi
lea    -0x4c0(%rbp),%rdx
mov    -0x4d8(%rbp),%rcx
lea    0xb6d(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    15c6 <init_client+0x3d6>
cmpq   $0x0,-0x4d8(%rbp)
jne    15b5 <init_client+0x3c5>
lea    0xb5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    15c1 <init_client+0x3d1>
lea    0xb7c(%rip),%rdi
call   10b0 <perror@plt>
jmp    15c6 <init_client+0x3d6>
mov    -0x7c(%rbp),%edi
call   1080 <close@plt>
jmp    15e4 <init_client+0x3f4>
mov    -0x48(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    144c <init_client+0x25c>
mov    -0x40(%rbp),%rdi
call   1100 <freeaddrinfo@plt>
mov    -0x88(%rbp),%rax
mov    %rax,%rsp
mov    $0x1,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_client>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %rsp,-0x38(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r12
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x68(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1080 <memset@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    0xebc(%rip),%rdi
call   1050 <puts@plt>
lea    0xda0(%rip),%rdi
lea    0xd8a(%rip),%rbx
lea    0xd8d(%rip),%r13
mov    %rbx,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    -0x30(%rbp),%rcx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   10e0 <getaddrinfo@plt>
test   %eax,%eax
jne    146c <init_client+0x27c>
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
je     1316 <init_client+0x126>
mov    0x2d24(%rip),%rax
mov    (%rax),%rax
lea    0xdaa(%rip),%r15
jmp    12f6 <init_client+0x106>
mov    0x10(%rbx),%rsi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
lea    0xd9c(%rip),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%rax
mov    0x20(%rbx),%rbx
test   %rbx,%rbx
je     1316 <init_client+0x126>
mov    (%rbx),%rdi
mov    %r15,%r13
cmp    %rax,%rdi
je     12c0 <init_client+0xd0>
lea    0xd65(%rip),%r13
mov    0x2cb9(%rip),%rcx
cmp    (%rcx),%rdi
je     12c0 <init_client+0xd0>
jmp    12ed <init_client+0xfd>
lea    0xe02(%rip),%rdi
call   1050 <puts@plt>
mov    -0x30(%rbp),%r14
test   %r14,%r14
je     143d <init_client+0x24d>
mov    0x2c62(%rip),%r13
lea    0xd61(%rip),%r15
lea    0xd73(%rip),%r12
jmp    1387 <init_client+0x197>
cs nopw 0x0(%rax,%rax,1)
mov    0x2c61(%rip),%rax
mov    (%rax),%ebx
mov    0x2c48(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %ebx,%edi
lea    0xd13(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x20(%r14),%r14
test   %r14,%r14
je     143d <init_client+0x24d>
mov    (%r14),%rdi
mov    0x1c(%r14),%esi
mov    0x18(%r14),%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
je     1350 <init_client+0x160>
mov    %eax,%ebx
mov    0x0(%r13),%edi
mov    %r15,%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rsi
mov    0x8(%rax),%edx
mov    %ebx,%edi
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    13cd <init_client+0x1dd>
mov    %r12,%rdi
call   10b0 <perror@plt>
jmp    137a <init_client+0x18a>
lea    0xd4e(%rip),%rdi
call   1050 <puts@plt>
lea    -0x470(%rbp),%rsi
mov    $0x400,%edx
mov    %ebx,%edi
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %rax,%rax
jle    1426 <init_client+0x236>
mov    %rax,%r14
mov    0x0(%r13),%edi
lea    0xcc0(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x0(%r13),%edi
lea    0xcc2(%rip),%rsi
lea    -0x470(%rbp),%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1436 <init_client+0x246>
jne    145e <init_client+0x26e>
lea    0xcb7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    -0x30(%rbp),%rdi
call   1100 <freeaddrinfo@plt>
mov    -0x38(%rbp),%rsp
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb3(%rip),%rdi
call   10b0 <perror@plt>
jmp    1436 <init_client+0x246>
mov    0x2b45(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1070 <gai_strerror@plt>
lea    0xbc5(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2b0a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i64, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"1988\00"
@.str.2 = [4 x i8] c"--\0A\00"
@.str.3 = [57 x i8] c"Addr info associated to host [%s] and service name [%s]\0A\00"
@stderr = global i32 0
@.str.4 = [32 x i8] c"getaddrinfo failed, reason: %s\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i64 0
@.str.5 = [5 x i8] c"IPv4\00"
@AF_INET6 = global i64 0
@.str.6 = [5 x i8] c"IPv6\00"
@.str.7 = [17 x i8] c"Address %s = %s\0A\00"
@.str.8 = [27 x i8] c"socket failed, reason: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.9 = [25 x i8] c"socket creation succeed\0A\00"
@.str.10 = [10 x i8] c"connect()\00"
@.str.11 = [28 x i8] c"Connected to remote server\0A\00"
@.str.12 = [20 x i8] c"Got a new message:\0A\00"
@.str.13 = [17 x i8] c"\09%s [%ld chars]\0A\00"
@.str.14 = [50 x i8] c"the remote side has closed the connection on you!\00"
@.str.15 = [7 x i8] c"recv()\00"
define i32 @init_client() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.sockaddr_in*
%14 = alloca %struct.sockaddr_in6*
%15 = alloca [1024 x i8]
%16 = alloca i32
%17 = alloca i64
%18 = alloca i64
%19 = load i32, i32* @INET6_ADDRSTRLEN
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %7
%22 = alloca i8, i64 %20
store i64 %20, i64* %8
%23 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 48)
%24 = load i64, i64* @AF_UNSPEC
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 0
store i64 %24, i64* %25
%26 = load i32, i32* @SOCK_STREAM
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 4
store i32 %26, i32* %27
%28 = load i32, i32* @AI_PASSIVE
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 6
store i32 %28, i32* %29
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %9
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %10
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%31 = load i8*, i8** %9
%32 = load i8*, i8** %10
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i8* %31, i8* %32)
%34 = load i8*, i8** %9
%35 = load i8*, i8** %10
%36 = call i32 @getaddrinfo(i8* %34, i8* %35, %struct.addrinfo* %2, %struct.addrinfo** %3)
store i32 %36, i32* %1
%37 = load i32, i32* %1
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %0
%40 = load i32, i32* @stderr
%41 = load i32, i32* %1
%42 = call i8* @gai_strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %0
%47 = load %struct.addrinfo*, %struct.addrinfo** %3
store %struct.addrinfo* %47, %struct.addrinfo** %4
br label %48
48: ; preds = %96, %46
%49 = load %struct.addrinfo*, %struct.addrinfo** %4
%50 = icmp ne %struct.addrinfo* %49, null
br i1 %50, label %51, label %100
51: ; preds = %48
%52 = load %struct.addrinfo*, %struct.addrinfo** %4
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 0
%54 = load i64, i64* %53
%55 = load i64, i64* @AF_INET
%56 = icmp eq i64 %54, %55
br i1 %56, label %57, label %68
57: ; preds = %51
%58 = load %struct.addrinfo*, %struct.addrinfo** %4
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 2
%60 = load i64, i64* %59
%61 = inttoptr i64 %60 to %struct.sockaddr_in*
store %struct.sockaddr_in* %61, %struct.sockaddr_in** %13
%62 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %62, i32 0, i32 0
%64 = bitcast i32* %63 to i8*
store i8* %64, i8** %11
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8** %12
%65 = load %struct.addrinfo*, %struct.addrinfo** %4
%66 = bitcast %struct.addrinfo* %5 to i8*
%67 = bitcast %struct.addrinfo* %65 to i8*
br label %87
68: ; preds = %51
%69 = load %struct.addrinfo*, %struct.addrinfo** %4
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 0
%71 = load i64, i64* %70
%72 = load i64, i64* @AF_INET6
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %85
74: ; preds = %68
%75 = load %struct.addrinfo*, %struct.addrinfo** %4
%76 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %75, i32 0, i32 2
%77 = load i64, i64* %76
%78 = inttoptr i64 %77 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %78, %struct.sockaddr_in6** %14
%79 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%80 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %79, i32 0, i32 0
%81 = bitcast i32* %80 to i8*
store i8* %81, i8** %11
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8** %12
%82 = load %struct.addrinfo*, %struct.addrinfo** %4
%83 = bitcast %struct.addrinfo* %5 to i8*
%84 = bitcast %struct.addrinfo* %82 to i8*
br label %86
85: ; preds = %68
br label %96
86: ; preds = %74
br label %87
87: ; preds = %86, %57
%88 = load %struct.addrinfo*, %struct.addrinfo** %4
%89 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %88, i32 0, i32 0
%90 = load i64, i64* %89
%91 = load i8*, i8** %11
%92 = trunc i64 %20 to i32
%93 = call i32 @inet_ntop(i64 %90, i8* %91, i8* %22, i32 %92)
%94 = load i8*, i8** %12
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %94, i8* %22)
br label %96
96: ; preds = %87, %85
%97 = load %struct.addrinfo*, %struct.addrinfo** %4
%98 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %97, i32 0, i32 5
%99 = load %struct.addrinfo*, %struct.addrinfo** %98
store %struct.addrinfo* %99, %struct.addrinfo** %4
br label %48
100: ; preds = %48
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%102 = load %struct.addrinfo*, %struct.addrinfo** %3
store %struct.addrinfo* %102, %struct.addrinfo** %4
br label %103
103: ; preds = %165, %100
%104 = load %struct.addrinfo*, %struct.addrinfo** %4
%105 = icmp ne %struct.addrinfo* %104, null
br i1 %105, label %106, label %169
106: ; preds = %103
%107 = load %struct.addrinfo*, %struct.addrinfo** %4
%108 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %107, i32 0, i32 0
%109 = load i64, i64* %108
%110 = load %struct.addrinfo*, %struct.addrinfo** %4
%111 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %110, i32 0, i32 4
%112 = load i32, i32* %111
%113 = load %struct.addrinfo*, %struct.addrinfo** %4
%114 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %113, i32 0, i32 3
%115 = load i32, i32* %114
%116 = call i32 @socket(i64 %109, i32 %112, i32 %115)
store i32 %116, i32* %6
%117 = load i32, i32* %6
%118 = icmp eq i32 %117, -1
br i1 %118, label %119, label %124
119: ; preds = %106
%120 = load i32, i32* @stderr
%121 = load i32, i32* @errno
%122 = call i8* @strerror(i32 %121)
%123 = call i32 (i32, i8*, ...) @fprintf(i32 %120, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %122)
br label %165
124: ; preds = %106
%125 = load i32, i32* @stdout
%126 = call i32 (i32, i8*, ...) @fprintf(i32 %125, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
%127 = load i32, i32* %6
%128 = load %struct.addrinfo*, %struct.addrinfo** %3
%129 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %128, i32 0, i32 2
%130 = load i64, i64* %129
%131 = load %struct.addrinfo*, %struct.addrinfo** %3
%132 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %131, i32 0, i32 1
%133 = load i32, i32* %132
%134 = call i32 @connect(i32 %127, i64 %130, i32 %133)
%135 = icmp eq i32 %134, -1
br i1 %135, label %136, label %138
136: ; preds = %124
%137 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0))
br label %165
138: ; preds = %124
%139 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0))
store i32 0, i32* %16
store i64 1024, i64* %17
%140 = load i32, i32* %6
%141 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%142 = load i64, i64* %17
%143 = load i32, i32* %16
%144 = call i64 @recv(i32 %140, i8* %141, i64 %142, i32 %143)
store i64 %144, i64* %18
%145 = load i64, i64* %18
%146 = icmp sgt i64 %145, 0
br i1 %146, label %147, label %154
147: ; preds = %138
%148 = load i32, i32* @stdout
%149 = call i32 (i32, i8*, ...) @fprintf(i32 %148, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
%150 = load i32, i32* @stdout
%151 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%152 = load i64, i64* %18
%153 = call i32 (i32, i8*, ...) @fprintf(i32 %150, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i64 0, i64 0), i8* %151, i64 %152)
br label %162
154: ; preds = %138
%155 = load i64, i64* %18
%156 = icmp eq i64 %155, 0
br i1 %156, label %157, label %159
157: ; preds = %154
%158 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.14, i64 0, i64 0))
br label %161
159: ; preds = %154
%160 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0))
br label %161
161: ; preds = %159, %157
br label %162
162: ; preds = %161, %147
%163 = load i32, i32* %6
%164 = call i32 @close(i32 %163)
br label %169
165: ; preds = %136, %119
%166 = load %struct.addrinfo*, %struct.addrinfo** %4
%167 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %166, i32 0, i32 5
%168 = load %struct.addrinfo*, %struct.addrinfo** %167
store %struct.addrinfo* %168, %struct.addrinfo** %4
br label %103
169: ; preds = %162, %103
%170 = load %struct.addrinfo*, %struct.addrinfo** %3
%171 = call i32 @freeaddrinfo(%struct.addrinfo* %170)
%172 = load i8*, i8** %7
ret i32 1
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i64, i32, i32, %struct.addrinfo*, i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"1988\00"
@.str.3 = [57 x i8] c"Addr info associated to host [%s] and service name [%s]\0A\00"
@stderr = global i32 0
@.str.4 = [32 x i8] c"getaddrinfo failed, reason: %s\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i64 0
@.str.5 = [5 x i8] c"IPv4\00"
@AF_INET6 = global i64 0
@.str.6 = [5 x i8] c"IPv6\00"
@.str.7 = [17 x i8] c"Address %s = %s\0A\00"
@.str.8 = [27 x i8] c"socket failed, reason: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.9 = [25 x i8] c"socket creation succeed\0A\00"
@.str.10 = [10 x i8] c"connect()\00"
@.str.12 = [20 x i8] c"Got a new message:\0A\00"
@.str.13 = [17 x i8] c"\09%s [%ld chars]\0A\00"
@.str.14 = [50 x i8] c"the remote side has closed the connection on you!\00"
@.str.15 = [7 x i8] c"recv()\00"
@str.16 = [3 x i8] c"--\00"
@str.17 = [27 x i8] c"Connected to remote server\00"
define i32 @init_client() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = alloca [1024 x i8]
%4 = bitcast %struct.addrinfo* %1 to i8*
%5 = bitcast %struct.addrinfo** %2 to i8*
%6 = load i32, i32* @INET6_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 48)
%11 = load i64, i64* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
store i64 %11, i64* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 6
store i32 %15, i32* %16
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.16, i64 0, i64 0))
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %0
%22 = load %struct.addrinfo*, %struct.addrinfo** %2
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %54, label %24
24: ; preds = %21
%25 = load i64, i64* @AF_INET
br label %32
26: ; preds = %0
%27 = load i32, i32* @stderr
%28 = call i8* @gai_strerror(i32 %19)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %24, %49
%33 = phi i64 [ %50, %49 ], [ %25, %24 ]
%34 = phi %struct.addrinfo* [ %52, %49 ], [ %22, %24 ]
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 0
%36 = load i64, i64* %35
%37 = icmp eq i64 %36, %33
br i1 %37, label %41, label %38
38: ; preds = %32
%39 = load i64, i64* @AF_INET6
%40 = icmp eq i64 %36, %39
br i1 %40, label %41, label %49
41: ; preds = %38, %32
%42 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), %32 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), %38 ]
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 2
%44 = load i64, i64* %43
%45 = inttoptr i64 %44 to i8*
%46 = call i32 @inet_ntop(i64 %36, i8* %45, i8* %9, i32 %6)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %42, i8* %9)
%48 = load i64, i64* @AF_INET
br label %49
49: ; preds = %38, %41
%50 = phi i64 [ %33, %38 ], [ %48, %41 ]
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 5
%52 = load %struct.addrinfo*, %struct.addrinfo** %51
%53 = icmp eq %struct.addrinfo* %52, null
br i1 %53, label %54, label %32
54: ; preds = %49, %21
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.16, i64 0, i64 0))
%56 = load %struct.addrinfo*, %struct.addrinfo** %2
%57 = icmp eq %struct.addrinfo* %56, null
br i1 %57, label %107, label %58
58: ; preds = %54, %103
%59 = phi %struct.addrinfo* [ %105, %103 ], [ %56, %54 ]
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i64 0, i32 0
%61 = load i64, i64* %60
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i64 0, i32 4
%63 = load i32, i32* %62
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i64 0, i32 3
%65 = load i32, i32* %64
%66 = call i32 @socket(i64 %61, i32 %63, i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %73
68: ; preds = %58
%69 = load i32, i32* @stderr
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %71)
br label %103
73: ; preds = %58
%74 = load i32, i32* @stdout
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
%76 = load %struct.addrinfo*, %struct.addrinfo** %2
%77 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %76, i64 0, i32 2
%78 = load i64, i64* %77
%79 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %76, i64 0, i32 1
%80 = load i32, i32* %79
%81 = call i32 @connect(i32 %66, i64 %78, i32 %80)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %85
83: ; preds = %73
%84 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0))
br label %103
85: ; preds = %73
%86 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.17, i64 0, i64 0))
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%88 = call i64 @recv(i32 %66, i8* %87, i64 1024, i32 0)
%89 = icmp sgt i64 %88, 0
br i1 %89, label %90, label %95
90: ; preds = %85
%91 = load i32, i32* @stdout
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %91, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
%93 = load i32, i32* @stdout
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i64 0, i64 0), i8* %87, i64 %88)
br label %101
95: ; preds = %85
%96 = icmp eq i64 %88, 0
br i1 %96, label %97, label %99
97: ; preds = %95
%98 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.14, i64 0, i64 0))
br label %101
99: ; preds = %95
%100 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0))
br label %101
101: ; preds = %97, %99, %90
%102 = call i32 @close(i32 %66)
br label %107
103: ; preds = %83, %68
%104 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i64 0, i32 5
%105 = load %struct.addrinfo*, %struct.addrinfo** %104
%106 = icmp eq %struct.addrinfo* %105, null
br i1 %106, label %107, label %58
107: ; preds = %103, %54, %101
%108 = load %struct.addrinfo*, %struct.addrinfo** %2
%109 = call i32 @freeaddrinfo(%struct.addrinfo* %108)
ret i32 1
}