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

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


x86 assembly (clang, -O0):
<showip>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x98(%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,-0x90(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x44(%rbp)
test   $0x1,%al
jne    1211 <showip+0xa1>
jmp    1250 <showip+0xe0>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x9c(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x9c(%rbp),%edi
mov    %rax,%rdx
lea    0xdc8(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    1336 <showip+0x1c6>
mov    -0x10(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     131f <showip+0x1af>
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
mov    0x2d58(%rip),%rcx
cmp    (%rcx),%eax
jne    12b4 <showip+0x144>
lea    0xd91(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    12d9 <showip+0x169>
lea    0xd72(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x88(%rbp)
mov    -0x88(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x90(%rbp),%rdx
mov    -0x98(%rbp),%rax
mov    -0x74(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x90(%rbp),%rdx
mov    -0x70(%rbp),%rsi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    126a <showip+0xfa>
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):
<showip>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r12
mov    %rsp,%rbx
mov    0x2e5a(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x58(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    -0x30(%rbp),%rcx
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
je     1217 <showip+0xa7>
mov    0x2dde(%rip),%rax
mov    %rbx,%r14
mov    (%rax),%ebx
mov    $0x1,%edi
call   1050 <gai_strerror@plt>
lea    0xe00(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rbx
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x2,%eax
jmp    1299 <showip+0x129>
lea    0xdf3(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x30(%rbp),%r15
test   %r15,%r15
je     1290 <showip+0x120>
mov    %rbx,-0x38(%rbp)
lea    0xdf6(%rip),%r12
nopl   0x0(%rax)
mov    (%r15),%edi
mov    0x2d96(%rip),%rax
cmp    (%rax),%edi
lea    0xdda(%rip),%rbx
lea    0xdce(%rip),%rax
cmove  %rax,%rbx
mov    0x8(%r15),%rsi
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    1240 <showip+0xd0>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rbx
jmp    1292 <showip+0x122>
xor    %edi,%edi
call   1090 <freeaddrinfo@plt>
xor    %eax,%eax
mov    %rbx,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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