#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;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_socktype;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_UNSPEC;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct addrinfo hints, *res, *p;
  int status;
  char ipstr[INET6_ADDRSTRLEN];
  if (argc != 2) {
    fprintf(stderr, "usage: %s hostname\n", argv[0]);
    return 1;
  }
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
    return 2;
  }
  printf("IP addresses for %s:\n\n", argv[1]);
  for (p = res; p != NULL; p = p->ai_next) {
    void *addr;
    char *ipver;
    if (p->ai_family == AF_INET) {
      struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
      addr = &(ipv4->sin_addr);
      ipver = "IPv4";
    } else {
      struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
      addr = &(ipv6->sin6_addr);
      ipver = "IPv6";
    }
    inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
    printf("  %s: %s\n", ipver, ipstr);
  }
  freeaddrinfo(res);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x90(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f8 <main+0x88>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe20(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    136a <main+0x1fa>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1286 <main+0x116>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x94(%rbp),%edi
mov    %rax,%rdx
lea    0xda6(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    136a <main+0x1fa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1353 <main+0x1e3>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    0x2d23(%rip),%rcx
cmp    (%rcx),%rax
jne    12ea <main+0x17a>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    0xd5b(%rip),%rax
mov    %rax,-0x70(%rbp)
jmp    1309 <main+0x199>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    0xd3c(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    -0x88(%rbp),%rdx
mov    -0x90(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rcx),%rdi
mov    -0x68(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x88(%rbp),%rdx
mov    -0x70(%rbp),%rsi
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    12a4 <main+0x134>
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

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,%r15
mov    %rsp,%r12
mov    0x2e5a(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
cmp    $0x2,%edi
jne    1215 <main+0xa5>
lea    -0x58(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x8(%r15),%rdi
lea    -0x30(%rbp),%rcx
xor    %esi,%esi
mov    %rbx,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     1237 <main+0xc7>
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xe13(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x2,%ebx
jmp    12ba <main+0x14a>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xdd8(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%ebx
jmp    12ba <main+0x14a>
mov    0x8(%r15),%rsi
lea    0xde3(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x30(%rbp),%r15
test   %r15,%r15
je     12b3 <main+0x143>
mov    %r12,-0x38(%rbp)
lea    0xde7(%rip),%r12
nop
mov    (%r15),%rdi
mov    0x8(%r15),%rsi
mov    0x2d72(%rip),%rax
cmp    (%rax),%rdi
lea    0xdc9(%rip),%rbx
lea    0xdbd(%rip),%rax
cmove  %rax,%rbx
mov    %r13,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%r15),%r15
test   %r15,%r15
jne    1260 <main+0xf0>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%r12
xor    %ebx,%ebx
jmp    12b5 <main+0x145>
xor    %edi,%edi
call   1090 <freeaddrinfo@plt>
mov    %r12,%rsp
mov    %ebx,%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, i64, %struct.addrinfo*, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s hostname\0A\00"
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [23 x i8] c"IP addresses for %s:\0A\0A\00"
@AF_INET = global i64 0
@.str.3 = [5 x i8] c"IPv4\00"
@.str.4 = [5 x i8] c"IPv6\00"
@.str.5 = [10 x i8] c" %s: %s\0A\00"
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 i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca %struct.sockaddr_in*
%16 = alloca %struct.sockaddr_in6*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* @INET6_ADDRSTRLEN
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %10
%20 = alloca i8, i64 %18
store i64 %18, i64* %11
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 2
br i1 %22, label %23, label %29
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 1, i32* %3
store i32 1, i32* %12
br label %92
29: ; preds = %2
%30 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%31 = load i64, i64* @AF_UNSPEC
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i64 %31, i64* %32
%33 = load i32, i32* @SOCK_STREAM
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %33, i32* %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 %45
40: ; preds = %29
%41 = load i32, i32* @stderr
%42 = load i32, i32* %9
%43 = call i8* @gai_strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %43)
store i32 2, i32* %3
store i32 1, i32* %12
br label %92
45: ; preds = %29
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %50, %struct.addrinfo** %8
br label %51
51: ; preds = %85, %45
%52 = load %struct.addrinfo*, %struct.addrinfo** %8
%53 = icmp ne %struct.addrinfo* %52, null
br i1 %53, label %54, label %89
54: ; preds = %51
%55 = load %struct.addrinfo*, %struct.addrinfo** %8
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 0
%57 = load i64, i64* %56
%58 = load i64, i64* @AF_INET
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %68
60: ; preds = %54
%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_in*
store %struct.sockaddr_in* %64, %struct.sockaddr_in** %15
%65 = load %struct.sockaddr_in*, %struct.sockaddr_in** %15
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %65, i32 0, i32 0
%67 = bitcast i32* %66 to i8*
store i8* %67, i8** %13
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %14
br label %76
68: ; preds = %54
%69 = load %struct.addrinfo*, %struct.addrinfo** %8
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 1
%71 = load i64, i64* %70
%72 = inttoptr i64 %71 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %72, %struct.sockaddr_in6** %16
%73 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %16
%74 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %73, i32 0, i32 0
%75 = bitcast i32* %74 to i8*
store i8* %75, i8** %13
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8** %14
br label %76
76: ; preds = %68, %60
%77 = load %struct.addrinfo*, %struct.addrinfo** %8
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 0
%79 = load i64, i64* %78
%80 = load i8*, i8** %13
%81 = trunc i64 %18 to i32
%82 = call i32 @inet_ntop(i64 %79, i8* %80, i8* %20, i32 %81)
%83 = load i8*, i8** %14
%84 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %83, i8* %20)
br label %85
85: ; preds = %76
%86 = load %struct.addrinfo*, %struct.addrinfo** %8
%87 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %86, i32 0, i32 2
%88 = load %struct.addrinfo*, %struct.addrinfo** %87
store %struct.addrinfo* %88, %struct.addrinfo** %8
br label %51
89: ; preds = %51
%90 = load %struct.addrinfo*, %struct.addrinfo** %7
%91 = call i32 @freeaddrinfo(%struct.addrinfo* %90)
store i32 0, i32* %3
store i32 1, i32* %12
br label %92
92: ; preds = %89, %40, %23
%93 = load i8*, i8** %10
%94 = load i32, i32* %3
ret i32 %94
}

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