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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int NI_MAXHOST;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int getnameinfo(struct sockaddr *, int, char *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int retval;
  int ip_version;
  char address[NI_MAXHOST];
  socklen_t sockaddr_size;
  if (argc != 2) {
    fprintf(stderr, "Invocation: %s <HOSTNAME>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  if ((retval = getaddrinfo(argv[1], NULL, &hints, &result)) != 0) {
    fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(retval));
    exit(EXIT_FAILURE);
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    if (rp->ai_family == AF_INET) {
      ip_version = 4;
      sockaddr_size = sizeof(struct sockaddr_in);
    } else {
      ip_version = 6;
      sockaddr_size = sizeof(struct sockaddr_in6);
    }
    retval = getnameinfo((struct sockaddr *)rp->ai_addr, sockaddr_size, address,
                         NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
    if (retval != 0) {
      fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(retval));
      exit(EXIT_FAILURE);
    }
    fprintf(stdout, "IPv%d: %s\n", ip_version, address);
  }
  if (result != NULL) {
    freeaddrinfo(result);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
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)
cmpl   $0x2,-0x8(%rbp)
je     11eb <main+0x7b>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     1278 <main+0x108>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xdb8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     136c <main+0x1fc>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2d4f(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <main+0x145>
movl   $0x4,-0x50(%rbp)
movl   $0x4,-0x64(%rbp)
jmp    12c3 <main+0x153>
movl   $0x6,-0x50(%rbp)
movl   $0x4,-0x64(%rbp)
mov    -0x70(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x64(%rbp),%esi
mov    0x2cc7(%rip),%rax
mov    (%rax),%ecx
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1050 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     133d <main+0x1cd>
mov    0x2cc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x78(%rbp),%edi
mov    %rax,%rdx
lea    0xd06(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x70(%rbp),%rcx
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%edx
lea    0xced(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1280 <main+0x110>
cmpq   $0x0,-0x40(%rbp)
je     1380 <main+0x210>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2c21(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
cmp    $0x2,%edi
jne    129a <main+0x12a>
lea    -0x58(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x8(%r14),%rdi
lea    -0x30(%rbp),%rcx
xor    %esi,%esi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
jne    12af <main+0x13f>
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
je     128c <main+0x11c>
lea    0xe37(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%rbx),%r12
mov    0x8(%rbx),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%r15
mov    0x2d78(%rip),%rax
mov    (%rax),%ecx
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    $0x4,%esi
mov    %r13,%rdx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1050 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
jne    12c8 <main+0x158>
xor    %eax,%eax
cmp    %r15,%r12
setne  %al
lea    0x4(%rax,%rax,1),%edx
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    %r13,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    1210 <main+0xa0>
mov    -0x30(%rbp),%rdi
test   %rdi,%rdi
je     128c <main+0x11c>
call   1090 <freeaddrinfo@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd53(%rip),%rsi
jmp    12e4 <main+0x174>
mov    0x2d1a(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd55(%rip),%rsi
jmp    12df <main+0x16f>
mov    0x2d01(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd4f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, %struct.addrinfo*, i64, i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Invocation: %s <HOSTNAME>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@.str.1 = [19 x i8] c"getaddrinfo(): %s\0A\00"
@AF_INET = global i64 0
@NI_NUMERICHOST = global i32 0
@.str.2 = [19 x i8] c"getnameinfo(): %s\0A\00"
@stdout = global i32 0
@.str.3 = [11 x i8] c"IPv%d: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %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* @NI_MAXHOST
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = icmp eq i32 %0, 2
br i1 %10, label %17, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%19 = load i64, i64* @AF_UNSPEC
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i64 %19, i64* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i64 0, i64* %23
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @getaddrinfo(i8* %25, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = load %struct.addrinfo*, %struct.addrinfo** %4
%30 = icmp eq %struct.addrinfo* %29, null
br i1 %30, label %68, label %37
31: ; preds = %17
%32 = load i32, i32* @stderr
%33 = call i8* @gai_strerror(i32 %26)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %28, %55
%38 = phi %struct.addrinfo* [ %61, %55 ], [ %29, %28 ]
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @AF_INET
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 1
%43 = load i64, i64* %42
%44 = inttoptr i64 %43 to %struct.sockaddr*
%45 = load i32, i32* @NI_MAXHOST
%46 = load i32, i32* @NI_NUMERICHOST
%47 = call i32 @getnameinfo(%struct.sockaddr* %44, i32 4, i8* %9, i32 %45, i32* null, i32 0, i32 %46)
%48 = icmp eq i32 %47, 0
br i1 %48, label %55, label %49
49: ; preds = %37
%50 = load i32, i32* @stderr
%51 = call i8* @gai_strerror(i32 %47)
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %51)
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %37
%56 = icmp eq i64 %40, %41
%57 = select i1 %56, i32 4, i32 6
%58 = load i32, i32* @stdout
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %57, i8* %9)
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 2
%61 = load %struct.addrinfo*, %struct.addrinfo** %60
%62 = icmp eq %struct.addrinfo* %61, null
br i1 %62, label %63, label %37
63: ; preds = %55
%64 = load %struct.addrinfo*, %struct.addrinfo** %4
%65 = icmp eq %struct.addrinfo* %64, null
br i1 %65, label %68, label %66
66: ; preds = %63
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %64)
br label %68
68: ; preds = %28, %66, %63
%69 = load i32, i32* @EXIT_SUCCESS
%70 = call i32 @exit(i32 %69)
unreachable
}