#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 hostent {
  scalar_t__ h_addrtype;
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int INT_MAX;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname2(char *, int);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ inet_ntop(scalar_t__, scalar_t__, char *, int);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int resolveip_by_family(char **argv, int af) {
  struct hostent *rv;
  int i;
  int v6i = -1;
  int v4i = -1;
  char *ipv6formatted;
  rv = gethostbyname2(argv[1], af);
  if (rv == NULL) {
    return 1;
  }
  for (i = 0; &rv[i] != NULL && rv[i].h_addr_list != NULL && i < INT_MAX - 1;
       i++) {
    if (rv[i].h_addrtype == AF_INET6 && v6i == -1) {
      v6i = i;
    }
    if (rv[i].h_addrtype == AF_INET && v4i == -1) {
      v4i = i;
    }
  }
  if (v6i != -1) {
    ipv6formatted = malloc(sizeof(char) * 50);
    if (!ipv6formatted) {
      fprintf(stderr, "Failed to malloc for ipv6 formatted!?!?\n");
      exit(1);
    }
    inet_ntop(AF_INET6, rv[v6i].h_addr_list[0], ipv6formatted, 50);
    if (!ipv6formatted) {
      fprintf(stderr,
              "You should never get here.  Please let Dan know how you did.\n");
      return 1;
    }
    printf("%s\n", ipv6formatted);
  } else if (v4i != -1) {
    printf("%s\n", inet_ntoa(*(struct in_addr *)rv[v4i].h_addr_list[0]));
  } else {
    printf("No candidate addresses found!?  This should never happen; tell Dan "
           "how you managed to do this.\n");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<resolveip_by_family>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x14(%rbp),%esi
call   1060 <gethostbyname2@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b8 <resolveip_by_family+0x48>
movl   $0x1,-0x4(%rbp)
jmp    13ae <resolveip_by_family+0x23e>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rax
shl    $0x4,%rax
add    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x39(%rbp)
je     1213 <resolveip_by_family+0xa3>
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rax
shl    $0x4,%rax
add    %rax,%rcx
xor    %eax,%eax
cmpq   $0x0,0x8(%rcx)
mov    %al,-0x39(%rbp)
je     1213 <resolveip_by_family+0xa3>
mov    -0x24(%rbp),%eax
mov    0x2dda(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1223 <resolveip_by_family+0xb3>
jmp    129a <resolveip_by_family+0x12a>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
jne    1255 <resolveip_by_family+0xe5>
cmpl   $0xffffffff,-0x28(%rbp)
jne    1255 <resolveip_by_family+0xe5>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rax
jne    1287 <resolveip_by_family+0x117>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1287 <resolveip_by_family+0x117>
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    128c <resolveip_by_family+0x11c>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11bf <resolveip_by_family+0x4f>
cmpl   $0xffffffff,-0x28(%rbp)
je     134d <resolveip_by_family+0x1dd>
mov    $0x32,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12dc <resolveip_by_family+0x16c>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xd33(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rdx
mov    $0x32,%ecx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1336 <resolveip_by_family+0x1c6>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
lea    0xd04(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ae <resolveip_by_family+0x23e>
mov    -0x38(%rbp),%rsi
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13a7 <resolveip_by_family+0x237>
cmpl   $0xffffffff,-0x2c(%rbp)
je     138a <resolveip_by_family+0x21a>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xce9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13a2 <resolveip_by_family+0x232>
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    13a7 <resolveip_by_family+0x237>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolveip_by_family>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x8(%rdi),%rdi
call   1060 <gethostbyname2@plt>
test   %rax,%rax
jne    1194 <resolveip_by_family+0x24>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%r12
cmpq   $0x0,0x8(%rax)
je     121d <resolveip_by_family+0xad>
mov    0x2e3b(%rip),%rax
mov    (%rax),%r8d
add    $0xffffffff,%r8d
mov    0x2e25(%rip),%r15
mov    (%r15),%r9
mov    0x2e2b(%rip),%rax
mov    (%rax),%r10
xor    %edi,%edi
test   %r8d,%r8d
cmovle %edi,%r8d
mov    $0xffffffff,%esi
mov    %r12,%rcx
mov    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
cmp    %rdi,%r8
je     1213 <resolveip_by_family+0xa3>
cmp    $0xffffffff,%ebx
mov    %ebx,%ebp
cmove  %edi,%ebp
mov    (%rcx),%rdx
cmp    %r9,%rdx
cmove  %ebp,%ebx
cmp    $0xffffffff,%esi
mov    %esi,%eax
cmove  %edi,%eax
cmp    %r10,%rdx
cmove  %eax,%esi
add    $0x1,%rdi
cmpq   $0x0,0x18(%rcx)
lea    0x10(%rcx),%rcx
jne    11e0 <resolveip_by_family+0x70>
cmp    $0xffffffff,%ebx
jne    1233 <resolveip_by_family+0xc3>
cmp    $0xffffffff,%esi
jne    1261 <resolveip_by_family+0xf1>
lea    0xe05(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x32,%edi
call   1080 <malloc@plt>
test   %rax,%rax
jne    127c <resolveip_by_family+0x10c>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movslq %esi,%rax
shl    $0x4,%rax
mov    0x8(%r12,%rax,1),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rdi
jmp    12a1 <resolveip_by_family+0x131>
mov    %rax,%r14
mov    (%r15),%rdi
movslq %ebx,%rax
shl    $0x4,%rax
mov    0x8(%r12,%rax,1),%rax
mov    (%rax),%rsi
mov    %r14,%rdx
mov    $0x32,%ecx
call   1030 <inet_ntop@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64, i64* }
%struct.in_addr = type { i32 }
@INT_MAX = global i32 0
@AF_INET6 = global i64 0
@AF_INET = global i64 0
@stderr = global i32 0
@.str = [41 x i8] c"Failed to malloc for ipv6 formatted!?!?\0A\00"
@.str.1 = [62 x i8] c"You should never get here. Please let Dan know how you did.\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [96 x i8] c"No candidate addresses found!? This should never happen; tell Dan how you managed to do this.\0A\00"
define i32 @resolveip_by_family(i8** %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32 -1, i32* %8
store i32 -1, i32* %9
%11 = load i8**, i8*** %4
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* %5
%15 = call %struct.hostent* @gethostbyname2(i8* %13, i32 %14)
store %struct.hostent* %15, %struct.hostent** %6
%16 = load %struct.hostent*, %struct.hostent** %6
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 1, i32* %3
br label %127
19: ; preds = %2
store i32 0, i32* %7
br label %20
20: ; preds = %70, %19
%21 = load %struct.hostent*, %struct.hostent** %6
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i64 %23
%25 = icmp ne %struct.hostent* %24, null
br i1 %25, label %26, label %39
26: ; preds = %20
%27 = load %struct.hostent*, %struct.hostent** %6
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 %29
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i64*, i64** %31
%33 = icmp ne i64* %32, null
br i1 %33, label %34, label %39
34: ; preds = %26
%35 = load i32, i32* %7
%36 = load i32, i32* @INT_MAX
%37 = sub nsw i32 %36, 1
%38 = icmp slt i32 %35, %37
br label %39
39: ; preds = %34, %26, %20
%40 = phi i1 [ false, %26 ], [ false, %20 ], [ %38, %34 ]
br i1 %40, label %41, label %73
41: ; preds = %39
%42 = load %struct.hostent*, %struct.hostent** %6
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i64 %44
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i64, i64* @AF_INET6
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %55
50: ; preds = %41
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = load i32, i32* %7
store i32 %54, i32* %8
br label %55
55: ; preds = %53, %50, %41
%56 = load %struct.hostent*, %struct.hostent** %6
%57 = load i32, i32* %7
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds %struct.hostent, %struct.hostent* %56, i64 %58
%60 = getelementptr inbounds %struct.hostent, %struct.hostent* %59, i32 0, i32 0
%61 = load i64, i64* %60
%62 = load i64, i64* @AF_INET
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %69
64: ; preds = %55
%65 = load i32, i32* %9
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = load i32, i32* %7
store i32 %68, i32* %9
br label %69
69: ; preds = %67, %64, %55
br label %70
70: ; preds = %69
%71 = load i32, i32* %7
%72 = add nsw i32 %71, 1
store i32 %72, i32* %7
br label %20
73: ; preds = %39
%74 = load i32, i32* %8
%75 = icmp ne i32 %74, -1
br i1 %75, label %76, label %104
76: ; preds = %73
%77 = call i8* @malloc(i32 50)
store i8* %77, i8** %10
%78 = load i8*, i8** %10
%79 = icmp ne i8* %78, null
br i1 %79, label %84, label %80
80: ; preds = %76
%81 = load i32, i32* @stderr
%82 = call i32 @fprintf(i32 %81, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%83 = call i32 @exit(i32 1)
unreachable
84: ; preds = %76
%85 = load i64, i64* @AF_INET6
%86 = load %struct.hostent*, %struct.hostent** %6
%87 = load i32, i32* %8
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds %struct.hostent, %struct.hostent* %86, i64 %88
%90 = getelementptr inbounds %struct.hostent, %struct.hostent* %89, i32 0, i32 1
%91 = load i64*, i64** %90
%92 = getelementptr inbounds i64, i64* %91, i64 0
%93 = load i64, i64* %92
%94 = load i8*, i8** %10
%95 = call i32 @inet_ntop(i64 %85, i64 %93, i8* %94, i32 50)
%96 = load i8*, i8** %10
%97 = icmp ne i8* %96, null
br i1 %97, label %101, label %98
98: ; preds = %84
%99 = load i32, i32* @stderr
%100 = call i32 @fprintf(i32 %99, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %127
101: ; preds = %84
%102 = load i8*, i8** %10
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %102)
br label %125
104: ; preds = %73
%105 = load i32, i32* %9
%106 = icmp ne i32 %105, -1
br i1 %106, label %107, label %121
107: ; preds = %104
%108 = load %struct.hostent*, %struct.hostent** %6
%109 = load i32, i32* %9
%110 = sext i32 %109 to i64
%111 = getelementptr inbounds %struct.hostent, %struct.hostent* %108, i64 %110
%112 = getelementptr inbounds %struct.hostent, %struct.hostent* %111, i32 0, i32 1
%113 = load i64*, i64** %112
%114 = getelementptr inbounds i64, i64* %113, i64 0
%115 = load i64, i64* %114
%116 = inttoptr i64 %115 to %struct.in_addr*
%117 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %116, i32 0, i32 0
%118 = load i32, i32* %117
%119 = call i8* @inet_ntoa(i32 %118)
%120 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %119)
br label %124
121: ; preds = %104
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([96 x i8], [96 x i8]* @.str.3, i64 0, i64 0))
%123 = call i32 @exit(i32 1)
unreachable
124: ; preds = %107
br label %125
125: ; preds = %124, %101
%126 = call i32 @exit(i32 0)
unreachable
127: ; preds = %98, %18
%128 = load i32, i32* %3
ret i32 %128
}

LLVM IR (-O2):
%struct.hostent = type { i64, i64* }
%struct.in_addr = type { i32 }
@INT_MAX = global i32 0
@AF_INET6 = global i64 0
@AF_INET = global i64 0
@stderr = global i32 0
@.str = [41 x i8] c"Failed to malloc for ipv6 formatted!?!?\0A\00"
@str = [95 x i8] c"No candidate addresses found!? This should never happen; tell Dan how you managed to do this.\00"
define i32 @resolveip_by_family(i8** nocapture %0, i32 %1) {
%3 = getelementptr inbounds i8*, i8** %0, i64 1
%4 = load i8*, i8** %3
%5 = tail call %struct.hostent* @gethostbyname2(i8* %4, i32 %1)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %75, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%9 = load i64*, i64** %8
%10 = icmp eq i64* %9, null
br i1 %10, label %70, label %11
11: ; preds = %7
%12 = load i32, i32* @INT_MAX
%13 = add i32 %12, -1
%14 = load i64, i64* @AF_INET6
%15 = load i64, i64* @AF_INET
%16 = call i32 @llvm.smax.i32(i32 %13, i32 0)
%17 = zext i32 %16 to i64
br label %18
18: ; preds = %11, %23
%19 = phi i64 [ 0, %11 ], [ %35, %23 ]
%20 = phi i32 [ -1, %11 ], [ %34, %23 ]
%21 = phi i32 [ -1, %11 ], [ %30, %23 ]
%22 = icmp eq i64 %19, %17
br i1 %22, label %39, label %23
23: ; preds = %18
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 %19, i32 0
%25 = load i64, i64* %24
%26 = icmp eq i64 %25, %14
%27 = icmp eq i32 %21, -1
%28 = select i1 %26, i1 %27, i1 false
%29 = trunc i64 %19 to i32
%30 = select i1 %28, i32 %29, i32 %21
%31 = icmp eq i64 %25, %15
%32 = icmp eq i32 %20, -1
%33 = select i1 %31, i1 %32, i1 false
%34 = select i1 %33, i32 %29, i32 %20
%35 = add nuw nsw i64 %19, 1
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 %35, i32 1
%37 = load i64*, i64** %36
%38 = icmp eq i64* %37, null
br i1 %38, label %39, label %18
39: ; preds = %18, %23
%40 = phi i32 [ %21, %18 ], [ %30, %23 ]
%41 = phi i32 [ %20, %18 ], [ %34, %23 ]
%42 = icmp eq i32 %40, -1
br i1 %42, label %58, label %43
43: ; preds = %39
%44 = tail call dereferenceable_or_null(50) i8* @malloc(i32 50)
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = tail call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %43
%51 = load i64, i64* @AF_INET6
%52 = sext i32 %40 to i64
%53 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 %52, i32 1
%54 = load i64*, i64** %53
%55 = load i64, i64* %54
%56 = tail call i32 @inet_ntop(i64 %51, i64 %55, i8* %44, i32 50)
%57 = tail call i32 @puts(i8* %44)
br label %73
58: ; preds = %39
%59 = icmp eq i32 %41, -1
br i1 %59, label %70, label %60
60: ; preds = %58
%61 = sext i32 %41 to i64
%62 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 %61, i32 1
%63 = load i64*, i64** %62
%64 = load i64, i64* %63
%65 = inttoptr i64 %64 to %struct.in_addr*
%66 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %65, i64 0, i32 0
%67 = load i32, i32* %66
%68 = tail call i8* @inet_ntoa(i32 %67)
%69 = tail call i32 @puts(i8* dereferenceable(1) %68)
br label %73
70: ; preds = %7, %58
%71 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([95 x i8], [95 x i8]* @str, i64 0, i64 0))
%72 = tail call i32 @exit(i32 1)
unreachable
73: ; preds = %60, %50
%74 = tail call i32 @exit(i32 0)
unreachable
75: ; preds = %2
ret i32 1
}