#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 *);
unsigned long long get_mapping_page_size(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 /*<<< orphan*/ test_init(int, char **);
int main(int argc, char **argv) {
  int is_huge, have_env, shrink_ok, have_helper;
  unsigned long long mapping_size;
  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));
  mapping_size = get_mapping_page_size(p);
  is_huge = (mapping_size > 65536);
  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);
  mapping_size = get_mapping_page_size(p + (32 * 1024 * 1024) - 1);
  if (shrink_ok && mapping_size > 65536)
    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    $0x40,%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,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x31(%rbp)
je     1232 <main+0x92>
mov    -0x30(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
mov    $0x2000000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%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   1090 <cleanup@plt>
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1290 <main+0xf0>
mov    $0x0,%al
call   1090 <cleanup@plt>
lea    0xdb6(%rip),%rdi
mov    $0x2000000,%esi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000000,%edx
call   1070 <memset@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <get_mapping_page_size@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x10000,-0x28(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1351 <main+0x1b1>
cmpl   $0x0,-0x14(%rbp)
jne    1351 <main+0x1b1>
cmpl   $0x0,-0x1c(%rbp)
je     132d <main+0x18d>
cmpl   $0x0,-0x20(%rbp)
je     132d <main+0x18d>
jmp    1311 <main+0x171>
mov    $0x0,%al
call   1090 <cleanup@plt>
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1332 <main+0x192>
mov    $0x0,%al
call   1090 <cleanup@plt>
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1389 <main+0x1e9>
cmpl   $0x0,-0x14(%rbp)
je     1389 <main+0x1e9>
jmp    136a <main+0x1ca>
mov    $0x0,%al
call   1090 <cleanup@plt>
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rdi
add    $0x2000000,%rdi
add    $0xffffffffffffffff,%rdi
call   10b0 <get_mapping_page_size@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     13e6 <main+0x246>
cmpq   $0x10000,-0x28(%rbp)
jbe    13e6 <main+0x246>
jmp    13c7 <main+0x227>
mov    $0x0,%al
call   1090 <cleanup@plt>
lea    0xcd9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
jmp    13eb <main+0x24b>
mov    $0x0,%al
call   1090 <cleanup@plt>
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
call   1050 <test_init@plt>
lea    0xe3d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
lea    0xe3f(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
lea    0xe48(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    11ef <main+0x3f>
xor    %ebp,%ebp
jmp    1205 <main+0x55>
lea    0xe3e(%rip),%rsi
mov    %rax,%rdi
call   10d0 <strstr@plt>
test   %rax,%rax
setne  %bpl
mov    $0x2000000,%edi
call   1090 <malloc@plt>
test   %rax,%rax
jne    1246 <main+0x96>
test   %r14,%r14
setne  %bl
and    %bpl,%bl
xor    %eax,%eax
call   10a0 <cleanup@plt>
cmp    $0x1,%bl
je     1282 <main+0xd2>
lea    0xe0f(%rip),%rdi
mov    $0x2000000,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2000000,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
call   10c0 <get_mapping_page_size@plt>
test   %r15,%r15
je     1295 <main+0xe5>
cmp    $0x10000,%rax
ja     1295 <main+0xe5>
test   %r14,%r14
setne  %bl
and    %bpl,%bl
xor    %eax,%eax
call   10a0 <cleanup@plt>
cmp    $0x1,%bl
jne    12ea <main+0x13a>
lea    0xdea(%rip),%rdi
call   1060 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
test   %r15,%r15
jne    12b2 <main+0x102>
cmp    $0x10001,%rax
jb     12b2 <main+0x102>
xor    %eax,%eax
call   10a0 <cleanup@plt>
lea    0xdf5(%rip),%rdi
jmp    12f1 <main+0x141>
mov    %rbx,%rdi
call   1040 <free@plt>
add    $0x1ffffff,%rbx
mov    %rbx,%rdi
call   10c0 <get_mapping_page_size@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10a0 <cleanup@plt>
test   %r14,%r14
je     1300 <main+0x150>
cmp    $0x10001,%rbx
jb     1300 <main+0x150>
lea    0xda4(%rip),%rdi
jmp    12f1 <main+0x141>
lea    0xd67(%rip),%rdi
call   1060 <puts@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xd80(%rip),%rdi
jmp    1289 <main+0xd9>

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 i64
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @test_init(i32 %12, i8** %13)
%15 = call i32* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%16 = icmp ne i32* %15, null
%17 = zext i1 %16 to i32
store i32 %17, i32* %7
%18 = call i32* @getenv(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp ne i32* %18, null
%20 = zext i1 %19 to i32
store i32 %20, i32* %8
%21 = call i32* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%22 = bitcast i32* %21 to i8*
store i8* %22, i8** %11
%23 = load i8*, i8** %11
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %2
%26 = load i8*, i8** %11
%27 = call i32* @strstr(i8* %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp ne i32* %27, null
br label %29
29: ; preds = %25, %2
%30 = phi i1 [ false, %2 ], [ %28, %25 ]
%31 = zext i1 %30 to i32
store i32 %31, i32* %9
%32 = call i8* @malloc(i32 33554432)
store i8* %32, i8** %11
%33 = load i8*, i8** %11
%34 = icmp ne i8* %33, null
br i1 %34, label %54, label %35
35: ; preds = %29
%36 = load i32, i32* %8
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %47
38: ; preds = %35
%39 = load i32, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %38
br label %42
42: ; preds = %41
%43 = call i32 (...) @cleanup()
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @exit(i32 0)
unreachable
46: ; No predecessors!
br label %53
47: ; preds = %38, %35
br label %48
48: ; preds = %47
%49 = call i32 (...) @cleanup()
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 33554432)
%51 = call i32 @exit(i32 2)
unreachable
52: ; No predecessors!
br label %53
53: ; preds = %52, %46
br label %54
54: ; preds = %53, %29
%55 = load i8*, i8** %11
%56 = call i32 @memset(i8* %55, i32 0, i32 33554432)
%57 = load i8*, i8** %11
%58 = call i64 @get_mapping_page_size(i8* %57)
store i64 %58, i64* %10
%59 = load i64, i64* %10
%60 = icmp ugt i64 %59, 65536
%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 = %54
%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, %54
%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** %11
%100 = call i32 @free(i8* %99)
%101 = load i8*, i8** %11
%102 = getelementptr i8, i8* %101, i64 33554432
%103 = getelementptr i8, i8* %102, i64 -1
%104 = call i64 @get_mapping_page_size(i8* %103)
store i64 %104, i64* %10
%105 = load i32, i32* %8
%106 = icmp ne i32 %105, 0
br i1 %106, label %107, label %116
107: ; preds = %98
%108 = load i64, i64* %10
%109 = icmp ugt i64 %108, 65536
br i1 %109, label %110, label %116
110: ; preds = %107
br label %111
111: ; preds = %110
%112 = call i32 (...) @cleanup()
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
%114 = call i32 @exit(i32 2)
unreachable
115: ; No predecessors!
br label %116
116: ; preds = %115, %107, %98
br label %117
117: ; preds = %116
%118 = call i32 (...) @cleanup()
%119 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%120 = call i32 @exit(i32 0)
unreachable
121: ; No predecessors!
%122 = load i32, i32* %3
ret i32 %122
}

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 = tail call i64 @get_mapping_page_size(i8* %16)
%30 = icmp ugt i64 %29, 65536
%31 = select i1 %5, i1 true, i1 %30
br i1 %31, label %41, label %32
32: ; preds = %27
%33 = select i1 %7, i1 %15, i1 false
%34 = tail call i32 (...) @cleanup()
br i1 %33, label %35, label %38
35: ; preds = %32
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %32
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.10, i64 0, i64 0))
%40 = tail call i32 @exit(i32 2)
unreachable
41: ; preds = %27
%42 = select i1 %5, i1 %30, i1 false
br i1 %42, label %43, label %47
43: ; preds = %41
%44 = tail call i32 (...) @cleanup()
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.14, i64 0, i64 0))
%46 = tail call i32 @exit(i32 2)
unreachable
47: ; preds = %41
%48 = tail call i32 @free(i8* %16)
%49 = getelementptr i8, i8* %16, i64 33554431
%50 = tail call i64 @get_mapping_page_size(i8* %49)
%51 = icmp ugt i64 %50, 65536
%52 = select i1 %7, i1 %51, i1 false
%53 = tail call i32 (...) @cleanup()
br i1 %52, label %54, label %57
54: ; preds = %47
%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 = %47
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.12, i64 0, i64 0))
%59 = tail call i32 @exit(i32 0)
unreachable
}