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

/* Variables and functions */
int const AF_INET;
int const AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, char, int);
int /*<<< orphan*/ printf(char *, int const, int);
int do_test(void) {
  const int family[2] = {AF_INET, AF_INET6};
  int result = 0;
  int gaierr;
  size_t index;
  struct addrinfo hints, *ai, *aitop;
  for (index = 0; index < sizeof(family) / sizeof(family[0]); ++index) {
    memset(&hints, '\0', sizeof(hints));
    hints.ai_family = family[index];
    hints.ai_socktype = SOCK_STREAM;
    gaierr = getaddrinfo(NULL, "54321", &hints, &aitop);
    if (gaierr != 0) {
      gai_strerror(gaierr);
      result = 1;
    } else {
      for (ai = aitop; ai != NULL; ai = ai->ai_next) {
        printf("Should return family: %d. Returned: %d\n", family[index],
               ai->ai_family);
        result |= family[index] != ai->ai_family;
      }
      while (aitop != NULL) {
        ai = aitop;
        aitop = aitop->ai_next;
        freeaddrinfo(ai);
      }
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x2,-0x18(%rbp)
jae    1291 <do_test+0x141>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp,%rax,4),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe4c(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11ea <do_test+0x9a>
mov    -0x10(%rbp),%edi
call   1040 <gai_strerror@plt>
movl   $0x1,-0xc(%rbp)
jmp    127b <do_test+0x12b>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1244 <do_test+0xf4>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp,%rax,4),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
lea    0xdfc(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp,%rax,4),%eax
mov    -0x38(%rbp),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11f2 <do_test+0xa2>
jmp    1249 <do_test+0xf9>
cmpq   $0x0,-0x40(%rbp)
je     1276 <do_test+0x126>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
jmp    1249 <do_test+0xf9>
jmp    127b <do_test+0x12b>
jmp    1280 <do_test+0x130>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    117f <do_test+0x2f>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x10(%rsp),%r14
xor    %r12d,%r12d
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
movl   $0x0,0x10(%rsp)
mov    0x2e5f(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
lea    0xe79(%rip),%rsi
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %r14,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     11b1 <do_test+0x61>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    $0x1,%r12d
jmp    1218 <do_test+0xc8>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1218 <do_test+0xc8>
xor    %r12d,%r12d
lea    0xe49(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <printf@plt>
xor    %eax,%eax
cmpl   $0x0,(%rbx)
setne  %al
or     %eax,%r12d
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    11d0 <do_test+0x80>
jmp    120e <do_test+0xbe>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rdi),%rax
mov    %rax,0x8(%rsp)
call   1070 <freeaddrinfo@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
jne    1200 <do_test+0xb0>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
movl   $0x0,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
lea    0xdc6(%rip),%rsi
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %r14,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     1264 <do_test+0x114>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    $0x1,%r12d
jmp    12c8 <do_test+0x178>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12c8 <do_test+0x178>
lea    0xd99(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <printf@plt>
xor    %eax,%eax
cmpl   $0x0,(%rbx)
setne  %al
or     %eax,%r12d
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
jne    1280 <do_test+0x130>
jmp    12be <do_test+0x16e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rdi),%rax
mov    %rax,0x8(%rsp)
call   1070 <freeaddrinfo@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
jne    12b0 <do_test+0x160>
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, %struct.addrinfo*, i32 }
@AF_INET = constant i32 0
@AF_INET6 = constant i32 0
@SOCK_STREAM = global i32 0
@.str = [6 x i8] c"54321\00"
@.str.1 = [40 x i8] c"Should return family: %d. Returned: %d\0A\00"
define i32 @do_test() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%9 = load i32, i32* @AF_INET
store i32 %9, i32* %8
%10 = getelementptr inbounds i32, i32* %8, i64 1
%11 = load i32, i32* @AF_INET6
store i32 %11, i32* %10
store i32 0, i32* %2
store i64 0, i64* %4
br label %12
12: ; preds = %69, %0
%13 = load i64, i64* %4
%14 = icmp ult i64 %13, 2
br i1 %14, label %15, label %72
15: ; preds = %12
%16 = call i32 @memset(%struct.addrinfo* %5, i8 signext 0, i32 24)
%17 = load i64, i64* %4
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 %17
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %5, %struct.addrinfo** %7)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = load i32, i32* %3
%28 = call i32 @gai_strerror(i32 %27)
store i32 1, i32* %2
br label %68
29: ; preds = %15
%30 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %30, %struct.addrinfo** %6
br label %31
31: ; preds = %52, %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %6
%33 = icmp ne %struct.addrinfo* %32, null
br i1 %33, label %34, label %56
34: ; preds = %31
%35 = load i64, i64* %4
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %6
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %40)
%42 = load i64, i64* %4
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 %42
%44 = load i32, i32* %43
%45 = load %struct.addrinfo*, %struct.addrinfo** %6
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = icmp ne i32 %44, %47
%49 = zext i1 %48 to i32
%50 = load i32, i32* %2
%51 = or i32 %50, %49
store i32 %51, i32* %2
br label %52
52: ; preds = %34
%53 = load %struct.addrinfo*, %struct.addrinfo** %6
%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** %6
br label %31
56: ; preds = %31
br label %57
57: ; preds = %60, %56
%58 = load %struct.addrinfo*, %struct.addrinfo** %7
%59 = icmp ne %struct.addrinfo* %58, null
br i1 %59, label %60, label %67
60: ; preds = %57
%61 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %61, %struct.addrinfo** %6
%62 = load %struct.addrinfo*, %struct.addrinfo** %7
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 1
%64 = load %struct.addrinfo*, %struct.addrinfo** %63
store %struct.addrinfo* %64, %struct.addrinfo** %7
%65 = load %struct.addrinfo*, %struct.addrinfo** %6
%66 = call i32 @freeaddrinfo(%struct.addrinfo* %65)
br label %57
67: ; preds = %57
br label %68
68: ; preds = %67, %26
br label %69
69: ; preds = %68
%70 = load i64, i64* %4
%71 = add i64 %70, 1
store i64 %71, i64* %4
br label %12
72: ; preds = %12
%73 = load i32, i32* %2
ret i32 %73
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, %struct.addrinfo*, i32 }
@AF_INET = constant i32 0
@AF_INET6 = constant i32 0
@SOCK_STREAM = global i32 0
@.str = [6 x i8] c"54321\00"
@.str.1 = [40 x i8] c"Should return family: %d. Returned: %d\0A\00"
define i32 @do_test() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo* %1 to i8*
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 2
%7 = call i32 @memset(%struct.addrinfo* %1, i8 signext 0, i32 24)
store i32 0, i32* %5
%8 = load i32, i32* @SOCK_STREAM
store i32 %8, i32* %6
%9 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load %struct.addrinfo*, %struct.addrinfo** %2
%13 = icmp eq %struct.addrinfo* %12, null
br i1 %13, label %39, label %19
14: ; preds = %0
%15 = call i32 @gai_strerror(i32 %9)
br label %39
16: ; preds = %19
%17 = load %struct.addrinfo*, %struct.addrinfo** %2
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %39, label %32
19: ; preds = %11, %19
%20 = phi %struct.addrinfo* [ %30, %19 ], [ %12, %11 ]
%21 = phi i32 [ %28, %19 ], [ 0, %11 ]
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %23)
%25 = load i32, i32* %22
%26 = icmp ne i32 %25, 0
%27 = zext i1 %26 to i32
%28 = or i32 %21, %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 1
%30 = load %struct.addrinfo*, %struct.addrinfo** %29
%31 = icmp eq %struct.addrinfo* %30, null
br i1 %31, label %16, label %19
32: ; preds = %16, %32
%33 = phi %struct.addrinfo* [ %37, %32 ], [ %17, %16 ]
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i64 0, i32 1
%35 = load %struct.addrinfo*, %struct.addrinfo** %34
store %struct.addrinfo* %35, %struct.addrinfo** %2
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %33)
%37 = load %struct.addrinfo*, %struct.addrinfo** %2
%38 = icmp eq %struct.addrinfo* %37, null
br i1 %38, label %39, label %32
39: ; preds = %32, %11, %16, %14
%40 = phi i32 [ 1, %14 ], [ %28, %16 ], [ 0, %11 ], [ %28, %32 ]
%41 = call i32 @memset(%struct.addrinfo* %1, i8 signext 0, i32 24)
store i32 0, i32* %5
%42 = load i32, i32* @SOCK_STREAM
store i32 %42, i32* %6
%43 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%44 = icmp eq i32 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %39
%46 = call i32 @gai_strerror(i32 %43)
br label %73
47: ; preds = %39
%48 = load %struct.addrinfo*, %struct.addrinfo** %2
%49 = icmp eq %struct.addrinfo* %48, null
br i1 %49, label %73, label %50
50: ; preds = %47, %50
%51 = phi %struct.addrinfo* [ %61, %50 ], [ %48, %47 ]
%52 = phi i32 [ %59, %50 ], [ %40, %47 ]
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i64 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %54)
%56 = load i32, i32* %53
%57 = icmp ne i32 %56, 0
%58 = zext i1 %57 to i32
%59 = or i32 %52, %58
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i64 0, i32 1
%61 = load %struct.addrinfo*, %struct.addrinfo** %60
%62 = icmp eq %struct.addrinfo* %61, null
br i1 %62, label %63, label %50
63: ; preds = %50
%64 = load %struct.addrinfo*, %struct.addrinfo** %2
%65 = icmp eq %struct.addrinfo* %64, null
br i1 %65, label %73, label %66
66: ; preds = %63, %66
%67 = phi %struct.addrinfo* [ %71, %66 ], [ %64, %63 ]
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %67, i64 0, i32 1
%69 = load %struct.addrinfo*, %struct.addrinfo** %68
store %struct.addrinfo* %69, %struct.addrinfo** %2
%70 = call i32 @freeaddrinfo(%struct.addrinfo* %67)
%71 = load %struct.addrinfo*, %struct.addrinfo** %2
%72 = icmp eq %struct.addrinfo* %71, null
br i1 %72, label %73, label %66
73: ; preds = %66, %47, %63, %45
%74 = phi i32 [ 1, %45 ], [ %59, %63 ], [ %40, %47 ], [ %59, %66 ]
ret i32 %74
}