#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_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ 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 *, ...);
int main(int argc, char *argv[]) {
  struct addrinfo hints;
  struct addrinfo *servinfo, *p;
  char ipstr[INET6_ADDRSTRLEN];
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if (getaddrinfo(argv[1], NULL, &hints, &servinfo) != 0) {
    printf("getaddrinfo error\n");
    exit(1);
  }
  printf("IP address for %s:\n\n", argv[1]);
  for (p = servinfo; 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(servinfo);
}


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    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x80(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e06(%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   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     121a <main+0xba>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12de <main+0x17e>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%rax
jne    127e <main+0x11e>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    0xdb3(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    129d <main+0x13d>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    0xd94(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    -0x78(%rbp),%rdx
mov    -0x80(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rcx),%rdi
mov    -0x58(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x78(%rbp),%rdx
mov    -0x60(%rbp),%rsi
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1238 <main+0xd8>
mov    -0x38(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x48(%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,-0x38(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x58(%rbp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x8(%r15),%rdi
lea    -0x30(%rbp),%rcx
xor    %esi,%esi
mov    %r12,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
jne    1288 <main+0x118>
mov    0x8(%r15),%rsi
lea    0xdfe(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <printf@plt>
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
je     126b <main+0xfb>
lea    0xe03(%rip),%r15
nopl   0x0(%rax)
mov    (%rbx),%rdi
mov    0x8(%rbx),%rsi
mov    0x2daa(%rip),%rax
cmp    (%rax),%rdi
lea    0xde2(%rip),%r12
lea    0xdd6(%rip),%rax
cmove  %rax,%r12
mov    %r13,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    1220 <main+0xb0>
mov    -0x30(%rbp),%r15
mov    %r15,%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x38(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd98(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i64, %struct.addrinfo*, i32, 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 = [19 x i8] c"getaddrinfo error\0A\00"
@.str.1 = [21 x i8] c"IP address for %s:\0A\0A\00"
@AF_INET = global i64 0
@.str.2 = [5 x i8] c"IPv4\00"
@.str.3 = [5 x i8] c"IPv6\00"
@.str.4 = [8 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 i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.sockaddr_in*
%14 = alloca %struct.sockaddr_in6*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* @INET6_ADDRSTRLEN
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %9
%18 = alloca i8, i64 %16
store i64 %16, i64* %10
%19 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 32)
%20 = load i64, i64* @AF_UNSPEC
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i64 %20, i64* %21
%22 = load i32, i32* @SOCK_STREAM
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %22, i32* %23
%24 = load i32, i32* @AI_PASSIVE
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %24, i32* %25
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i64 @getaddrinfo(i8* %28, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %7)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %2
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %2
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %39, %struct.addrinfo** %8
br label %40
40: ; preds = %74, %34
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = icmp ne %struct.addrinfo* %41, null
br i1 %42, label %43, label %78
43: ; preds = %40
%44 = load %struct.addrinfo*, %struct.addrinfo** %8
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 0
%46 = load i64, i64* %45
%47 = load i64, i64* @AF_INET
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %57
49: ; preds = %43
%50 = load %struct.addrinfo*, %struct.addrinfo** %8
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 1
%52 = load i64, i64* %51
%53 = inttoptr i64 %52 to %struct.sockaddr_in*
store %struct.sockaddr_in* %53, %struct.sockaddr_in** %13
%54 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i32 0, i32 0
%56 = bitcast i32* %55 to i8*
store i8* %56, i8** %11
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %12
br label %65
57: ; preds = %43
%58 = load %struct.addrinfo*, %struct.addrinfo** %8
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 1
%60 = load i64, i64* %59
%61 = inttoptr i64 %60 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %61, %struct.sockaddr_in6** %14
%62 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%63 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %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.3, i64 0, i64 0), i8** %12
br label %65
65: ; preds = %57, %49
%66 = load %struct.addrinfo*, %struct.addrinfo** %8
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 0
%68 = load i64, i64* %67
%69 = load i8*, i8** %11
%70 = trunc i64 %16 to i32
%71 = call i32 @inet_ntop(i64 %68, i8* %69, i8* %18, i32 %70)
%72 = load i8*, i8** %12
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %72, i8* %18)
br label %74
74: ; preds = %65
%75 = load %struct.addrinfo*, %struct.addrinfo** %8
%76 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %75, i32 0, i32 2
%77 = load %struct.addrinfo*, %struct.addrinfo** %76
store %struct.addrinfo* %77, %struct.addrinfo** %8
br label %40
78: ; preds = %40
%79 = load %struct.addrinfo*, %struct.addrinfo** %7
%80 = call i32 @freeaddrinfo(%struct.addrinfo* %79)
%81 = load i8*, i8** %9
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, %struct.addrinfo*, i32, i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str.1 = [21 x i8] c"IP address for %s:\0A\0A\00"
@AF_INET = global i64 0
@.str.2 = [5 x i8] c"IPv4\00"
@.str.3 = [5 x i8] c"IPv6\00"
@.str.4 = [8 x i8] c"%s: %s\0A\00"
@str = [18 x i8] c"getaddrinfo error\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 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%12 = load i64, i64* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i64 %12, i64* %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 = load i32, i32* @AI_PASSIVE
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %16, i32* %17
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @getaddrinfo(i8* %19, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %2
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = load i8*, i8** %18
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load %struct.addrinfo*, %struct.addrinfo** %4
%29 = icmp eq %struct.addrinfo* %28, null
br i1 %29, label %47, label %30
30: ; preds = %25, %30
%31 = phi %struct.addrinfo* [ %43, %30 ], [ %28, %25 ]
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @AF_INET
%35 = icmp eq i64 %33, %34
%36 = select i1 %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 1
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to i8*
%40 = call i32 @inet_ntop(i64 %33, i8* %39, i8* %10, i32 %7)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %36, i8* %10)
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 2
%43 = load %struct.addrinfo*, %struct.addrinfo** %42
%44 = icmp eq %struct.addrinfo* %43, null
br i1 %44, label %45, label %30
45: ; preds = %30
%46 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %47
47: ; preds = %45, %25
%48 = phi %struct.addrinfo* [ %46, %45 ], [ null, %25 ]
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
ret i32 0
}