#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 in_addr {
  int dummy;
};
struct in6_addr {
  int dummy;
};
struct hostent {
  char *h_name;
  scalar_t__ h_addrtype;
  int /*<<< orphan*/ **h_addr_list;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ EXIT_FAILURE;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
struct hostent *gethostbyname2(char *, int);
int /*<<< orphan*/ h_errno;
char *hstrerror(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void print_ips(char *hostname, int iptype) {
  struct hostent *ht = gethostbyname2(hostname, iptype);
  if (!ht) {
    printf("Error: %s\n", hstrerror(h_errno));
    exit(EXIT_FAILURE);
  }
  printf("Host: %s\n", ht->h_name);
  int i;
  char addr[INET6_ADDRSTRLEN];
  for (i = 0; ht->h_addr_list[i] != NULL; i++) {
    void *ptr = NULL;
    if (ht->h_addrtype == AF_INET) {
      struct in_addr *tmp = (struct in_addr *)ht->h_addr_list[i];
      ptr = tmp;
      printf("IPv4: ");
    } else if (ht->h_addrtype == AF_INET6) {
      struct in6_addr *tmp = (struct in6_addr *)ht->h_addr_list[i];
      ptr = tmp;
      printf("IPv6: ");
    }
    if (!inet_ntop(ht->h_addrtype, ptr, addr, INET6_ADDRSTRLEN)) {
      perror("inet_ntop");
      exit(EXIT_FAILURE);
    }
    printf("%s\n", addr);
  }
}


x86 assembly (clang, -O0):
<print_ips>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <gethostbyname2@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <print_ips+0x57>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1070 <hstrerror@plt>
mov    %rax,%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe46(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     130d <print_ips+0x1ad>
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
jne    1265 <print_ips+0x105>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ac <print_ips+0x14c>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%rax
jne    12a7 <print_ips+0x147>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ac <print_ips+0x14c>
mov    -0x50(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d25(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    12ed <print_ips+0x18d>
lea    0xd49(%rip),%rdi
call   1060 <perror@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x50(%rbp),%rsi
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ff <print_ips+0x9f>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_ips>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
call   1060 <gethostbyname2@plt>
test   %rax,%rax
je     1271 <print_ips+0x101>
mov    %rax,%r15
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x10(%r15),%rax
mov    (%rax),%r13
test   %r13,%r13
je     1248 <print_ips+0xd8>
mov    $0x8,%ebx
lea    0xe3f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%r15),%rax
mov    %r12,%rdi
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
je     1206 <print_ips+0x96>
lea    0xe22(%rip),%rdi
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
jne    1210 <print_ips+0xa0>
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1213 <print_ips+0xa3>
nop
xor    %r13d,%r13d
mov    0x8(%r15),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%ecx
mov    %r13,%rsi
mov    %r14,%rdx
call   1030 <inet_ntop@plt>
test   %eax,%eax
je     1257 <print_ips+0xe7>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    0x10(%r15),%rax
mov    (%rax,%rbx,1),%r13
add    $0x8,%rbx
test   %r13,%r13
jne    11e0 <print_ips+0x70>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc5(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1080 <hstrerror@plt>
lea    0xd7a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1263 <print_ips+0xf3>

LLVM IR (-O0):
%struct.hostent = type { i8*, i64, i32** }
%struct.in_addr = type { i32 }
%struct.in6_addr = type { i32 }
@.str = [11 x i8] c"Error: %s\0A\00"
@h_errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"Host: %s\0A\00"
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@.str.2 = [7 x i8] c"IPv4: \00"
@AF_INET6 = global i64 0
@.str.3 = [7 x i8] c"IPv6: \00"
@.str.4 = [10 x i8] c"inet_ntop\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define void @print_ips(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.hostent*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca %struct.in_addr*
%11 = alloca %struct.in6_addr*
store i8* %0, i8** %3
store i32 %1, i32* %4
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = call %struct.hostent* @gethostbyname2(i8* %12, i32 %13)
store %struct.hostent* %14, %struct.hostent** %5
%15 = load %struct.hostent*, %struct.hostent** %5
%16 = icmp ne %struct.hostent* %15, null
br i1 %16, label %23, label %17
17: ; preds = %2
%18 = load i32, i32* @h_errno
%19 = call i8* @hstrerror(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load %struct.hostent*, %struct.hostent** %5
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @INET6_ADDRSTRLEN
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
store i8* %30, i8** %7
%31 = alloca i8, i64 %29
store i64 %29, i64* %8
store i32 0, i32* %6
br label %32
32: ; preds = %92, %23
%33 = load %struct.hostent*, %struct.hostent** %5
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 2
%35 = load i32**, i32*** %34
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32*, i32** %35, i64 %37
%39 = load i32*, i32** %38
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %95
41: ; preds = %32
store i8* null, i8** %9
%42 = load %struct.hostent*, %struct.hostent** %5
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i32 0, i32 1
%44 = load i64, i64* %43
%45 = load i64, i64* @AF_INET
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %59
47: ; preds = %41
%48 = load %struct.hostent*, %struct.hostent** %5
%49 = getelementptr inbounds %struct.hostent, %struct.hostent* %48, i32 0, i32 2
%50 = load i32**, i32*** %49
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32*, i32** %50, i64 %52
%54 = load i32*, i32** %53
%55 = bitcast i32* %54 to %struct.in_addr*
store %struct.in_addr* %55, %struct.in_addr** %10
%56 = load %struct.in_addr*, %struct.in_addr** %10
%57 = bitcast %struct.in_addr* %56 to i8*
store i8* %57, i8** %9
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %78
59: ; preds = %41
%60 = load %struct.hostent*, %struct.hostent** %5
%61 = getelementptr inbounds %struct.hostent, %struct.hostent* %60, i32 0, i32 1
%62 = load i64, i64* %61
%63 = load i64, i64* @AF_INET6
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %77
65: ; preds = %59
%66 = load %struct.hostent*, %struct.hostent** %5
%67 = getelementptr inbounds %struct.hostent, %struct.hostent* %66, i32 0, i32 2
%68 = load i32**, i32*** %67
%69 = load i32, i32* %6
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32*, i32** %68, i64 %70
%72 = load i32*, i32** %71
%73 = bitcast i32* %72 to %struct.in6_addr*
store %struct.in6_addr* %73, %struct.in6_addr** %11
%74 = load %struct.in6_addr*, %struct.in6_addr** %11
%75 = bitcast %struct.in6_addr* %74 to i8*
store i8* %75, i8** %9
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %77
77: ; preds = %65, %59
br label %78
78: ; preds = %77, %47
%79 = load %struct.hostent*, %struct.hostent** %5
%80 = getelementptr inbounds %struct.hostent, %struct.hostent* %79, i32 0, i32 1
%81 = load i64, i64* %80
%82 = load i8*, i8** %9
%83 = load i32, i32* @INET6_ADDRSTRLEN
%84 = call i32 @inet_ntop(i64 %81, i8* %82, i8* %31, i32 %83)
%85 = icmp ne i32 %84, 0
br i1 %85, label %90, label %86
86: ; preds = %78
%87 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %78
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %31)
br label %92
92: ; preds = %90
%93 = load i32, i32* %6
%94 = add nsw i32 %93, 1
store i32 %94, i32* %6
br label %32
95: ; preds = %32
%96 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
%struct.hostent = type { i8*, i64, i32** }
@.str = [11 x i8] c"Error: %s\0A\00"
@h_errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"Host: %s\0A\00"
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@.str.2 = [7 x i8] c"IPv4: \00"
@AF_INET6 = global i64 0
@.str.3 = [7 x i8] c"IPv6: \00"
@.str.4 = [10 x i8] c"inet_ntop\00"
define void @print_ips(i8* %0, i32 %1) {
%3 = tail call %struct.hostent* @gethostbyname2(i8* %0, i32 %1)
%4 = icmp eq %struct.hostent* %3, null
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i32, i32* @h_errno
%7 = tail call i8* @hstrerror(i32 %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @INET6_ADDRSTRLEN
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 2
%19 = load i32**, i32*** %18
%20 = load i32*, i32** %19
%21 = icmp eq i32* %20, null
br i1 %21, label %54, label %22
22: ; preds = %11
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %3, i64 0, i32 1
br label %24
24: ; preds = %22, %47
%25 = phi i64 [ 0, %22 ], [ %49, %47 ]
%26 = phi i32* [ %20, %22 ], [ %52, %47 ]
%27 = load i64, i64* %23
%28 = load i64, i64* @AF_INET
%29 = icmp eq i64 %27, %28
br i1 %29, label %33, label %30
30: ; preds = %24
%31 = load i64, i64* @AF_INET6
%32 = icmp eq i64 %27, %31
br i1 %32, label %33, label %37
33: ; preds = %30, %24
%34 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %24 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %30 ]
%35 = bitcast i32* %26 to i8*
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %34)
br label %37
37: ; preds = %33, %30
%38 = phi i8* [ null, %30 ], [ %35, %33 ]
%39 = load i64, i64* %23
%40 = load i32, i32* @INET6_ADDRSTRLEN
%41 = call i32 @inet_ntop(i64 %39, i8* %38, i8* %17, i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %37
%48 = call i32 @puts(i8* %17)
%49 = add nuw i64 %25, 1
%50 = load i32**, i32*** %18
%51 = getelementptr inbounds i32*, i32** %50, i64 %49
%52 = load i32*, i32** %51
%53 = icmp eq i32* %52, null
br i1 %53, label %54, label %24
54: ; preds = %47, %11
ret void
}