#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 */

/* Variables and functions */
int /*<<< orphan*/ cleanup();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ *getenv(char *);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *strstr(void *, char *);
int test_addr_huge(void *);
int /*<<< orphan*/ test_init(int, char **);
int main(int argc, char **argv) {
  int is_huge, have_env, shrink_ok, have_helper;
  void *p;
  test_init(argc, argv);
  have_env = getenv("HUGETLB_MORECORE") != NULL;
  shrink_ok = getenv("HUGETLB_MORECORE_SHRINK") != NULL;
  p = getenv("LD_PRELOAD");
  have_helper = p != NULL && strstr(p, "heapshrink") != NULL;
  p = malloc((32 * 1024 * 1024));
  if (!p) {
    if (shrink_ok && have_helper) {
      do {
        cleanup();
        printf("PASS (inconclusive)\n");
        exit(0);
      } while (0);
    } else
      do {
        cleanup();
        printf("FAIL\t"
               "malloc(%d) failed\n"
               "\n",
               (32 * 1024 * 1024));
        exit(2);
      } while (0);
  }
  memset(p, 0, (32 * 1024 * 1024));
  is_huge = test_addr_huge(p + (32 * 1024 * 1024) - 1) == 1;
  if (have_env && !is_huge) {
    if (shrink_ok && have_helper) {
      do {
        cleanup();
        printf("PASS (inconclusive)\n");
        exit(0);
      } while (0);
    } else
      do {
        cleanup();
        printf("FAIL\t"
               "Heap not on hugepages"
               "\n");
        exit(2);
      } while (0);
  }
  if (!have_env && is_huge)
    do {
      cleanup();
      printf("FAIL\t"
             "Heap unexpectedly on hugepages"
             "\n");
      exit(2);
    } while (0);
  free(p);
  if (shrink_ok && test_addr_huge(p + (32 * 1024 * 1024) - 1) == 1)
    do {
      cleanup();
      printf("FAIL\t"
             "Heap did not shrink"
             "\n");
      exit(2);
    } while (0);
  do {
    cleanup();
    printf("PASS\n");
    exit(0);
  } while (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <test_init@plt>
lea    0xe37(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
lea    0xe2d(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
lea    0xe2a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
je     1232 <main+0x92>
mov    -0x28(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
mov    $0x2000000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12b4 <main+0x114>
cmpl   $0x0,-0x1c(%rbp)
je     128b <main+0xeb>
cmpl   $0x0,-0x20(%rbp)
je     128b <main+0xeb>
jmp    126f <main+0xcf>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1290 <main+0xf0>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xdb6(%rip),%rdi
mov    $0x2000000,%esi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000000,%edx
call   1070 <memset@plt>
mov    -0x28(%rbp),%rdi
add    $0x2000000,%rdi
add    $0xffffffffffffffff,%rdi
call   1080 <test_addr_huge@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1353 <main+0x1b3>
cmpl   $0x0,-0x14(%rbp)
jne    1353 <main+0x1b3>
cmpl   $0x0,-0x1c(%rbp)
je     132f <main+0x18f>
cmpl   $0x0,-0x20(%rbp)
je     132f <main+0x18f>
jmp    1313 <main+0x173>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1334 <main+0x194>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    138b <main+0x1eb>
cmpl   $0x0,-0x14(%rbp)
je     138b <main+0x1eb>
jmp    136c <main+0x1cc>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
cmpl   $0x0,-0x1c(%rbp)
je     13df <main+0x23f>
mov    -0x28(%rbp),%rdi
add    $0x2000000,%rdi
add    $0xffffffffffffffff,%rdi
call   1080 <test_addr_huge@plt>
cmp    $0x1,%eax
jne    13df <main+0x23f>
jmp    13c0 <main+0x220>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xce0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
jmp    13e4 <main+0x244>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xcd6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
call   1050 <test_init@plt>
lea    0xe3c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
lea    0xe3e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
lea    0xe47(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    11f0 <main+0x40>
xor    %ebp,%ebp
jmp    1206 <main+0x56>
lea    0xe3d(%rip),%rsi
mov    %rax,%rdi
call   10d0 <strstr@plt>
test   %rax,%rax
setne  %bpl
mov    $0x2000000,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
jne    1247 <main+0x97>
test   %r14,%r14
setne  %bl
and    %bpl,%bl
xor    %eax,%eax
call   10b0 <cleanup@plt>
cmp    $0x1,%bl
je     128a <main+0xda>
lea    0xe0e(%rip),%rdi
mov    $0x2000000,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2000000,%edx
call   1080 <memset@plt>
mov    %rbx,%r12
add    $0x1ffffff,%r12
mov    %r12,%rdi
call   1090 <test_addr_huge@plt>
test   %r15,%r15
je     129d <main+0xed>
cmp    $0x1,%eax
je     129d <main+0xed>
test   %r14,%r14
setne  %bl
and    %bpl,%bl
xor    %eax,%eax
call   10b0 <cleanup@plt>
cmp    $0x1,%bl
jne    12b7 <main+0x107>
lea    0xde2(%rip),%rdi
call   1060 <puts@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
test   %r15,%r15
jne    12c0 <main+0x110>
cmp    $0x1,%eax
jne    12c0 <main+0x110>
xor    %eax,%eax
call   10b0 <cleanup@plt>
lea    0xdf0(%rip),%rdi
jmp    12e8 <main+0x138>
lea    0xd9a(%rip),%rdi
jmp    12e8 <main+0x138>
mov    %rbx,%rdi
call   1040 <free@plt>
test   %r14,%r14
je     12f7 <main+0x147>
mov    %r12,%rdi
call   1090 <test_addr_huge@plt>
cmp    $0x1,%eax
jne    12f7 <main+0x147>
xor    %eax,%eax
call   10b0 <cleanup@plt>
lea    0xda4(%rip),%rdi
call   1060 <puts@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10b0 <cleanup@plt>
lea    0xd82(%rip),%rdi
jmp    1291 <main+0xe1>

LLVM IR (-O0):
@.str = [17 x i8] c"HUGETLB_MORECORE\00"
@.str.1 = [24 x i8] c"HUGETLB_MORECORE_SHRINK\00"
@.str.2 = [11 x i8] c"LD_PRELOAD\00"
@.str.3 = [11 x i8] c"heapshrink\00"
@.str.4 = [21 x i8] c"PASS (inconclusive)\0A\00"
@.str.5 = [25 x i8] c"FAIL\09malloc(%d) failed\0A\0A\00"
@.str.6 = [28 x i8] c"FAIL\09Heap not on hugepages\0A\00"
@.str.7 = [37 x i8] c"FAIL\09Heap unexpectedly on hugepages\0A\00"
@.str.8 = [26 x i8] c"FAIL\09Heap did not shrink\0A\00"
@.str.9 = [6 x i8] c"PASS\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = load i8**, i8*** %5
%13 = call i32 @test_init(i32 %11, i8** %12)
%14 = call i32* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i32* %14, null
%16 = zext i1 %15 to i32
store i32 %16, i32* %7
%17 = call i32* @getenv(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp ne i32* %17, null
%19 = zext i1 %18 to i32
store i32 %19, i32* %8
%20 = call i32* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%21 = bitcast i32* %20 to i8*
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = load i8*, i8** %10
%26 = call i32* @strstr(i8* %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp ne i32* %26, null
br label %28
28: ; preds = %24, %2
%29 = phi i1 [ false, %2 ], [ %27, %24 ]
%30 = zext i1 %29 to i32
store i32 %30, i32* %9
%31 = call i8* @malloc(i32 33554432)
store i8* %31, i8** %10
%32 = load i8*, i8** %10
%33 = icmp ne i8* %32, null
br i1 %33, label %53, label %34
34: ; preds = %28
%35 = load i32, i32* %8
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = load i32, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %37
br label %41
41: ; preds = %40
%42 = call i32 (...) @cleanup()
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 0)
unreachable
45: ; No predecessors!
br label %52
46: ; preds = %37, %34
br label %47
47: ; preds = %46
%48 = call i32 (...) @cleanup()
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 33554432)
%50 = call i32 @exit(i32 2)
unreachable
51: ; No predecessors!
br label %52
52: ; preds = %51, %45
br label %53
53: ; preds = %52, %28
%54 = load i8*, i8** %10
%55 = call i32 @memset(i8* %54, i32 0, i32 33554432)
%56 = load i8*, i8** %10
%57 = getelementptr i8, i8* %56, i64 33554432
%58 = getelementptr i8, i8* %57, i64 -1
%59 = call i32 @test_addr_huge(i8* %58)
%60 = icmp eq i32 %59, 1
%61 = zext i1 %60 to i32
store i32 %61, i32* %6
%62 = load i32, i32* %7
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %86
64: ; preds = %53
%65 = load i32, i32* %6
%66 = icmp ne i32 %65, 0
br i1 %66, label %86, label %67
67: ; preds = %64
%68 = load i32, i32* %8
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %79
70: ; preds = %67
%71 = load i32, i32* %9
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %79
73: ; preds = %70
br label %74
74: ; preds = %73
%75 = call i32 (...) @cleanup()
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%77 = call i32 @exit(i32 0)
unreachable
78: ; No predecessors!
br label %85
79: ; preds = %70, %67
br label %80
80: ; preds = %79
%81 = call i32 (...) @cleanup()
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%83 = call i32 @exit(i32 2)
unreachable
84: ; No predecessors!
br label %85
85: ; preds = %84, %78
br label %86
86: ; preds = %85, %64, %53
%87 = load i32, i32* %7
%88 = icmp ne i32 %87, 0
br i1 %88, label %98, label %89
89: ; preds = %86
%90 = load i32, i32* %6
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %98
92: ; preds = %89
br label %93
93: ; preds = %92
%94 = call i32 (...) @cleanup()
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0))
%96 = call i32 @exit(i32 2)
unreachable
97: ; No predecessors!
br label %98
98: ; preds = %97, %89, %86
%99 = load i8*, i8** %10
%100 = call i32 @free(i8* %99)
%101 = load i32, i32* %8
%102 = icmp ne i32 %101, 0
br i1 %102, label %103, label %115
103: ; preds = %98
%104 = load i8*, i8** %10
%105 = getelementptr i8, i8* %104, i64 33554432
%106 = getelementptr i8, i8* %105, i64 -1
%107 = call i32 @test_addr_huge(i8* %106)
%108 = icmp eq i32 %107, 1
br i1 %108, label %109, label %115
109: ; preds = %103
br label %110
110: ; preds = %109
%111 = call i32 (...) @cleanup()
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
%113 = call i32 @exit(i32 2)
unreachable
114: ; No predecessors!
br label %115
115: ; preds = %114, %103, %98
br label %116
116: ; preds = %115
%117 = call i32 (...) @cleanup()
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%119 = call i32 @exit(i32 0)
unreachable
120: ; No predecessors!
%121 = load i32, i32* %3
ret i32 %121
}

LLVM IR (-O2):
@.str = [17 x i8] c"HUGETLB_MORECORE\00"
@.str.1 = [24 x i8] c"HUGETLB_MORECORE_SHRINK\00"
@.str.2 = [11 x i8] c"LD_PRELOAD\00"
@.str.3 = [11 x i8] c"heapshrink\00"
@.str.5 = [25 x i8] c"FAIL\09malloc(%d) failed\0A\0A\00"
@str.10 = [27 x i8] c"FAIL\09Heap not on hugepages\00"
@str.11 = [20 x i8] c"PASS (inconclusive)\00"
@str.12 = [5 x i8] c"PASS\00"
@str.13 = [25 x i8] c"FAIL\09Heap did not shrink\00"
@str.14 = [36 x i8] c"FAIL\09Heap unexpectedly on hugepages\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 @test_init(i32 %0, i8** %1)
%4 = tail call i32* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
%6 = tail call i32* @getenv(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp ne i32* %6, null
%8 = tail call i32* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = bitcast i32* %8 to i8*
%12 = tail call i32* @strstr(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp ne i32* %12, null
br label %14
14: ; preds = %10, %2
%15 = phi i1 [ false, %2 ], [ %13, %10 ]
%16 = tail call dereferenceable_or_null(33554432) i8* @malloc(i32 33554432)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = select i1 %7, i1 %15, i1 false
%20 = tail call i32 (...) @cleanup()
br i1 %19, label %21, label %24
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %18
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 33554432)
%26 = tail call i32 @exit(i32 2)
unreachable
27: ; preds = %14
%28 = tail call i32 @memset(i8* %16, i32 0, i32 33554432)
%29 = getelementptr i8, i8* %16, i64 33554431
%30 = tail call i32 @test_addr_huge(i8* %29)
%31 = icmp eq i32 %30, 1
%32 = select i1 %5, i1 true, i1 %31
br i1 %32, label %42, label %33
33: ; preds = %27
%34 = select i1 %7, i1 %15, i1 false
%35 = tail call i32 (...) @cleanup()
br i1 %34, label %36, label %39
36: ; preds = %33
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
%38 = tail call i32 @exit(i32 0)
unreachable
39: ; preds = %33
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.10, i64 0, i64 0))
%41 = tail call i32 @exit(i32 2)
unreachable
42: ; preds = %27
%43 = select i1 %5, i1 %31, i1 false
br i1 %43, label %44, label %48
44: ; preds = %42
%45 = tail call i32 (...) @cleanup()
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.14, i64 0, i64 0))
%47 = tail call i32 @exit(i32 2)
unreachable
48: ; preds = %42
%49 = tail call i32 @free(i8* %16)
br i1 %7, label %50, label %57
50: ; preds = %48
%51 = tail call i32 @test_addr_huge(i8* %29)
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = tail call i32 (...) @cleanup()
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.13, i64 0, i64 0))
%56 = tail call i32 @exit(i32 2)
unreachable
57: ; preds = %48, %50
%58 = tail call i32 (...) @cleanup()
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.12, i64 0, i64 0))
%60 = tail call i32 @exit(i32 0)
unreachable
}