#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*/ _SC_PAGESIZE;
void *endHeap();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
size_t sysconf(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int i;
  char *p, *q, *r;
  size_t pagesize;
  void *lowbreak, *highbreak;
  char *progname;
  if (argc > 0)
    progname = argv[0];
  else
    progname = "";
  fprintf(stderr, "%s, line %d: %s", progname, 25,
          "-- This test will search for memory leaks\n");
  fprintf(stderr, "%s, line %d: %s", progname, 26,
          "At most 3.0x pages are allocated and recycled\n");
  lowbreak = endHeap();
  pagesize = sysconf(_SC_PAGESIZE);
  for (i = 0; i < 10000; i++) {
    p = malloc(pagesize);
    q = malloc(pagesize * 2 + 1);
    r = malloc(1);
    free(p);
    free(q);
    free(r);
  }
  highbreak = endHeap();
  fprintf(stderr, "%s: Used memory in test: 0x%x (= %2.2f * pagesize)\n",
          progname, (unsigned)(highbreak - lowbreak),
          (1.0 * (highbreak - lowbreak)) / pagesize);
  if (highbreak - lowbreak > 7 * pagesize)
    fprintf(stderr, "%s, line %d: %s", progname, 55,
            "* ERROR: This malloc has a memory leak\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1180 <main+0x30>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    118b <main+0x3b>
lea    0xf36(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rdx
lea    0xe81(%rip),%rsi
mov    $0x19,%ecx
lea    0xe85(%rip),%r8
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rdx
lea    0xe5a(%rip),%rsi
mov    $0x1a,%ecx
lea    0xe89(%rip),%r8
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1040 <endHeap@plt>
mov    %rax,-0x40(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2710,-0x14(%rbp)
jge    1267 <main+0x117>
mov    -0x38(%rbp),%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11fd <main+0xad>
xor    %eax,%eax
call   1040 <endHeap@plt>
mov    %rax,-0x48(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%ecx
cvtsi2sd %rax,%xmm0
movaps 0xd68(%rip),%xmm2
movq   -0x38(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xd67(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
lea    0xdc7(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
imul   $0x7,-0x38(%rbp),%rcx
cmp    %rcx,%rax
jbe    130a <main+0x1ba>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rdx
lea    0xd29(%rip),%rsi
mov    $0x37,%ecx
lea    0xdbb(%rip),%r8
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
jle    1167 <main+0x17>
mov    (%rsi),%rbp
jmp    116e <main+0x1e>
lea    0xf4f(%rip),%rbp
mov    0x2e63(%rip),%rbx
mov    (%rbx),%edi
lea    0xea2(%rip),%r14
lea    0xeab(%rip),%r8
mov    %r14,%rsi
mov    %rbp,%rdx
mov    $0x19,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xebb(%rip),%r8
mov    %r14,%rsi
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdx
mov    $0x1a,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1040 <endHeap@plt>
mov    %rax,0x8(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,%r12
lea    (%rax,%rax,1),%r13d
add    $0x1,%r13d
mov    $0x2710,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %r13d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0xffffffff,%r14d
jne    11f0 <main+0xa0>
xor    %eax,%eax
call   1040 <endHeap@plt>
mov    %rax,%rbp
sub    0x8(%rsp),%rbp
cvtsi2sd %rbp,%xmm0
movq   %r12,%xmm1
punpckldq 0xdae(%rip),%xmm1
subpd  0xdb6(%rip),%xmm1
mov    0x2d77(%rip),%r14
mov    (%r14),%edi
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
divsd  %xmm2,%xmm0
lea    0xe0f(%rip),%rsi
mov    0x10(%rsp),%rbx
mov    %rbx,%rdx
mov    %ebp,%ecx
mov    $0x1,%al
call   1050 <fprintf@plt>
lea    0x0(,%r12,8),%rax
sub    %r12,%rax
cmp    %rax,%rbp
jbe    12bc <main+0x16c>
mov    (%r14),%edi
lea    0xd7a(%rip),%rsi
lea    0xe11(%rip),%r8
mov    %rbx,%rdx
mov    $0x37,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [16 x i8] c"%s, line %d: %s\00"
@.str.2 = [43 x i8] c"-- This test will search for memory leaks\0A\00"
@.str.3 = [47 x i8] c"At most 3.0x pages are allocated and recycled\0A\00"
@_SC_PAGESIZE = global i32 0
@.str.4 = [52 x i8] c"%s: Used memory in test: 0x%x (= %2.2f * pagesize)\0A\00"
@.str.5 = [40 x i8] c"* ERROR: This malloc has a memory leak\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
store i8* %19, i8** %13
br label %21
20: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %13
br label %21
21: ; preds = %20, %16
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %13
%24 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %23, i32 25, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %13
%27 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 26, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%28 = call i8* (...) @endHeap()
store i8* %28, i8** %11
%29 = load i32, i32* @_SC_PAGESIZE
%30 = call i64 @sysconf(i32 %29)
store i64 %30, i64* %10
store i32 0, i32* %6
br label %31
31: ; preds = %50, %21
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 10000
br i1 %33, label %34, label %53
34: ; preds = %31
%35 = load i64, i64* %10
%36 = trunc i64 %35 to i32
%37 = call i8* @malloc(i32 %36)
store i8* %37, i8** %7
%38 = load i64, i64* %10
%39 = mul i64 %38, 2
%40 = add i64 %39, 1
%41 = trunc i64 %40 to i32
%42 = call i8* @malloc(i32 %41)
store i8* %42, i8** %8
%43 = call i8* @malloc(i32 1)
store i8* %43, i8** %9
%44 = load i8*, i8** %7
%45 = call i32 @free(i8* %44)
%46 = load i8*, i8** %8
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %9
%49 = call i32 @free(i8* %48)
br label %50
50: ; preds = %34
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %31
53: ; preds = %31
%54 = call i8* (...) @endHeap()
store i8* %54, i8** %12
%55 = load i32, i32* @stderr
%56 = load i8*, i8** %13
%57 = load i8*, i8** %12
%58 = load i8*, i8** %11
%59 = ptrtoint i8* %57 to i64
%60 = ptrtoint i8* %58 to i64
%61 = sub i64 %59, %60
%62 = trunc i64 %61 to i32
%63 = load i8*, i8** %12
%64 = load i8*, i8** %11
%65 = ptrtoint i8* %63 to i64
%66 = ptrtoint i8* %64 to i64
%67 = sub i64 %65, %66
%68 = sitofp i64 %67 to double
%69 = fmul double 1.000000e+00, %68
%70 = load i64, i64* %10
%71 = uitofp i64 %70 to double
%72 = fdiv double %69, %71
%73 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i8* %56, i32 %62, double %72)
%74 = load i8*, i8** %12
%75 = load i8*, i8** %11
%76 = ptrtoint i8* %74 to i64
%77 = ptrtoint i8* %75 to i64
%78 = sub i64 %76, %77
%79 = load i64, i64* %10
%80 = mul i64 7, %79
%81 = icmp ugt i64 %78, %80
br i1 %81, label %82, label %86
82: ; preds = %53
%83 = load i32, i32* @stderr
%84 = load i8*, i8** %13
%85 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %84, i32 55, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
br label %86
86: ; preds = %82, %53
ret i32 0
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [16 x i8] c"%s, line %d: %s\00"
@.str.2 = [43 x i8] c"-- This test will search for memory leaks\0A\00"
@.str.3 = [47 x i8] c"At most 3.0x pages are allocated and recycled\0A\00"
@_SC_PAGESIZE = global i32 0
@.str.4 = [52 x i8] c"%s: Used memory in test: 0x%x (= %2.2f * pagesize)\0A\00"
@.str.5 = [40 x i8] c"* ERROR: This malloc has a memory leak\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i8*, i8** %1
br label %6
6: ; preds = %2, %4
%7 = phi i8* [ %5, %4 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %2 ]
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 25, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 26, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i8* (...) @endHeap()
%13 = load i32, i32* @_SC_PAGESIZE
%14 = tail call i64 @sysconf(i32 %13)
%15 = trunc i64 %14 to i32
%16 = shl i32 %15, 1
%17 = or i32 %16, 1
br label %18
18: ; preds = %6, %18
%19 = phi i32 [ 0, %6 ], [ %26, %18 ]
%20 = tail call i8* @malloc(i32 %15)
%21 = tail call i8* @malloc(i32 %17)
%22 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%23 = tail call i32 @free(i8* %20)
%24 = tail call i32 @free(i8* %21)
%25 = tail call i32 @free(i8* %22)
%26 = add nuw nsw i32 %19, 1
%27 = icmp eq i32 %26, 10000
br i1 %27, label %28, label %18
28: ; preds = %18
%29 = tail call i8* (...) @endHeap()
%30 = load i32, i32* @stderr
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %12 to i64
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
%35 = sitofp i64 %33 to double
%36 = uitofp i64 %14 to double
%37 = fdiv double %35, %36
%38 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i8* %7, i32 %34, double %37)
%39 = mul i64 %14, 7
%40 = icmp ugt i64 %33, %39
br i1 %40, label %41, label %44
41: ; preds = %28
%42 = load i32, i32* @stderr
%43 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 55, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %41, %28
ret i32 0
}