#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 addrinfo {
  scalar_t__ ai_family;
  struct addrinfo *ai_next;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  struct addrinfo hints, *result, *ai;
  int ret;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  ret = getaddrinfo(argv[1], NULL, &hints, &result);
  if (ret) {
    printf("getaddrinfo(%s) failed: %i: %s\n", argv[1], ret, gai_strerror(ret));
    return 1;
  }
  for (ai = result; ai != NULL; ai = ai->ai_next) {
    if (ai->ai_family == AF_INET)
      printf("this is an IPv4 result\n");
    else if (ai->ai_family == AF_INET6)
      printf("this is an IPv6 result\n");
    else
      printf("eww what's this?\n");
  }
  freeaddrinfo(result);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11e8 <main+0x98>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%edx
mov    %rax,%rcx
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1281 <main+0x131>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1278 <main+0x128>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
jne    1225 <main+0xd5>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1262 <main+0x112>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
jne    124f <main+0xff>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125d <main+0x10d>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1262 <main+0x112>
jmp    1267 <main+0x117>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11f0 <main+0xa0>
mov    -0x28(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e5c(%rip),%r15
mov    (%r15),%rax
mov    %rax,0x8(%rsp)
mov    0x8(%r14),%rdi
mov    %rsp,%rcx
xor    %esi,%esi
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11d3 <main+0x73>
mov    %eax,%ebp
mov    0x8(%r14),%rbx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xe43(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebx
jmp    1236 <main+0xd6>
mov    (%rsp),%rbp
xor    %ebx,%ebx
test   %rbp,%rbp
je     122f <main+0xcf>
mov    0x2dfb(%rip),%r12
lea    0xe45(%rip),%rbx
lea    0xe2d(%rip),%r13
lea    0xe4e(%rip),%r14
jmp    1215 <main+0xb5>
nopl   0x0(%rax)
cmp    (%r15),%rax
cmove  %r14,%rdi
call   1030 <puts@plt>
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
je     1227 <main+0xc7>
mov    0x0(%rbp),%rax
mov    %rbx,%rdi
cmp    (%r12),%rax
je     1200 <main+0xa0>
mov    %r13,%rdi
jmp    1200 <main+0xa0>
mov    (%rsp),%rdi
xor    %ebx,%ebx
jmp    1231 <main+0xd1>
xor    %edi,%edi
call   1080 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, %struct.addrinfo* }
@AF_INET = global i64 0
@.str = [32 x i8] c"getaddrinfo(%s) failed: %i: %s\0A\00"
@.str.1 = [24 x i8] c"this is an IPv4 result\0A\00"
@AF_INET6 = global i64 0
@.str.2 = [24 x i8] c"this is an IPv6 result\0A\00"
@.str.3 = [18 x i8] c"eww what's this?\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 16)
%11 = load i64, i64* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i64 %11, i64* %12
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @getaddrinfo(i8* %15, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* %9
%24 = load i32, i32* %9
%25 = call i8* @gai_strerror(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %23, i8* %25)
store i32 1, i32* %3
br label %59
27: ; preds = %2
%28 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %28, %struct.addrinfo** %8
br label %29
29: ; preds = %52, %27
%30 = load %struct.addrinfo*, %struct.addrinfo** %8
%31 = icmp ne %struct.addrinfo* %30, null
br i1 %31, label %32, label %56
32: ; preds = %29
%33 = load %struct.addrinfo*, %struct.addrinfo** %8
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* @AF_INET
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %51
40: ; preds = %32
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* @AF_INET6
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %50
48: ; preds = %40
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %46
br label %51
51: ; preds = %50, %38
br label %52
52: ; preds = %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %8
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load %struct.addrinfo*, %struct.addrinfo** %54
store %struct.addrinfo* %55, %struct.addrinfo** %8
br label %29
56: ; preds = %29
%57 = load %struct.addrinfo*, %struct.addrinfo** %7
%58 = call i32 @freeaddrinfo(%struct.addrinfo* %57)
br label %59
59: ; preds = %56, %19
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, %struct.addrinfo* }
@AF_INET = global i64 0
@.str = [32 x i8] c"getaddrinfo(%s) failed: %i: %s\0A\00"
@AF_INET6 = global i64 0
@str = [17 x i8] c"eww what's this?\00"
@str.4 = [23 x i8] c"this is an IPv6 result\00"
@str.5 = [23 x i8] c"this is an IPv4 result\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 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 16)
%8 = load i64, i64* @AF_INET
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i64 %8, i64* %9
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @getaddrinfo(i8* %11, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = icmp eq %struct.addrinfo* %15, null
br i1 %16, label %37, label %21
17: ; preds = %2
%18 = load i8*, i8** %10
%19 = call i8* @gai_strerror(i32 %12)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %12, i8* %19)
br label %40
21: ; preds = %14, %21
%22 = phi %struct.addrinfo* [ %33, %21 ], [ %15, %14 ]
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @AF_INET
%26 = icmp eq i64 %24, %25
%27 = load i64, i64* @AF_INET6
%28 = icmp eq i64 %24, %27
%29 = select i1 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0)
%30 = select i1 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0), i8* %29
%31 = call i32 @puts(i8* dereferenceable(1) %30)
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%33 = load %struct.addrinfo*, %struct.addrinfo** %32
%34 = icmp eq %struct.addrinfo* %33, null
br i1 %34, label %35, label %21
35: ; preds = %21
%36 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %37
37: ; preds = %35, %14
%38 = phi %struct.addrinfo* [ %36, %35 ], [ null, %14 ]
%39 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
br label %40
40: ; preds = %37, %17
%41 = phi i32 [ 1, %17 ], [ 0, %37 ]
ret i32 %41
}