#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 */
void *endHeap();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
scalar_t__ malloc(unsigned long);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  void *p1, *p2, *oldBrk, *newBrk1, *newBrk2;
  unsigned long largeSize = 16384;
  oldBrk = endHeap();
  p1 = (void *)malloc(1);
  newBrk1 = endHeap();
  largeSize = ((unsigned long)(newBrk1 - oldBrk) > 16384
                   ? (unsigned long)(newBrk1 - oldBrk)
                   : 16384);
  free(p1);
  printf("-- Testing merging of deallocated large blocks ( >= %u bytes)\n",
         (unsigned)largeSize);
  p1 = (void *)malloc(largeSize);
  p2 = (void *)malloc(largeSize);
  if (p1 == NULL || p2 == NULL)
    printf("* ERROR: unable to allocate memory of size %u bytes\n",
           (unsigned)largeSize);
  newBrk1 = endHeap();
  free(p1);
  free(p2);
  p1 = (void *)malloc(largeSize * 2);
  if (p1 == NULL)
    printf("* ERROR: unable to allocate memory of size %u bytes\n",
           (unsigned)largeSize);
  newBrk2 = endHeap();
  if (((double)(newBrk2 - oldBrk)) / ((double)(newBrk1 - oldBrk)) > 1)
    printf("* ERROR: not good enough usage of memory (probably incorrect "
           "merging of deallocated memory blocks)\nYour implementation used %u "
           "bytes more than expected\n",
           ((unsigned)(newBrk2 - newBrk1)));
  else
    printf("Test passed OK\n");
  exit(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)
movq   $0x4000,-0x40(%rbp)
mov    $0x0,%al
call   1050 <endHeap@plt>
mov    %rax,-0x28(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <endHeap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x4000,%rax
jbe    11bd <main+0x6d>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
jmp    11cb <main+0x7b>
mov    $0x4000,%eax
mov    %rax,-0x48(%rbp)
jmp    11cb <main+0x7b>
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %eax,%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1220 <main+0xd0>
cmpq   $0x0,-0x20(%rbp)
jne    1234 <main+0xe4>
mov    -0x40(%rbp),%rax
mov    %eax,%esi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <endHeap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
shl    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1281 <main+0x131>
mov    -0x40(%rbp),%rax
mov    %eax,%esi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <endHeap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
cvtsi2sd %rax,%xmm0
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
movsd  0xd48(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    12e2 <main+0x192>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f0 <main+0x1a0>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
xor    %eax,%eax
call   1060 <endHeap@plt>
mov    %rax,%r14
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1060 <endHeap@plt>
sub    %r14,%rax
cmp    $0x4001,%rax
mov    $0x4000,%ebx
cmovae %rax,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xe60(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
test   %r12,%r12
je     11d1 <main+0x71>
test   %r13,%r13
jne    11e1 <main+0x81>
lea    0xe6f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <endHeap@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    %rbx,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
jne    121b <main+0xbb>
lea    0xe35(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <endHeap@plt>
mov    %rax,%rcx
sub    %r14,%rcx
cvtsi2sd %rcx,%xmm0
mov    %r15,%rcx
sub    %r14,%rcx
cvtsi2sd %rcx,%xmm1
divsd  %xmm1,%xmm0
ucomisd 0xdbc(%rip),%xmm0
jbe    1260 <main+0x100>
sub    %r15d,%eax
lea    0xe2c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xeae(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [63 x i8] c"-- Testing merging of deallocated large blocks ( >= %u bytes)\0A\00"
@.str.1 = [53 x i8] c"* ERROR: unable to allocate memory of size %u bytes\0A\00"
@.str.2 = [153 x i8] c"* ERROR: not good enough usage of memory (probably incorrect merging of deallocated memory blocks)\0AYour implementation used %u bytes more than expected\0A\00"
@.str.3 = [16 x i8] c"Test passed OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 16384, i64* %11
%12 = call i8* (...) @endHeap()
store i8* %12, i8** %8
%13 = call i64 @malloc(i64 1)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %6
%15 = call i8* (...) @endHeap()
store i8* %15, i8** %9
%16 = load i8*, i8** %9
%17 = load i8*, i8** %8
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = icmp ugt i64 %20, 16384
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = load i8*, i8** %9
%24 = load i8*, i8** %8
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
br label %29
28: ; preds = %2
br label %29
29: ; preds = %28, %22
%30 = phi i64 [ %27, %22 ], [ 16384, %28 ]
store i64 %30, i64* %11
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
%33 = load i64, i64* %11
%34 = trunc i64 %33 to i32
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i64, i64* %11
%37 = call i64 @malloc(i64 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %6
%39 = load i64, i64* %11
%40 = call i64 @malloc(i64 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %7
%42 = load i8*, i8** %6
%43 = icmp eq i8* %42, null
br i1 %43, label %47, label %44
44: ; preds = %29
%45 = load i8*, i8** %7
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %51
47: ; preds = %44, %29
%48 = load i64, i64* %11
%49 = trunc i64 %48 to i32
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %44
%52 = call i8* (...) @endHeap()
store i8* %52, i8** %9
%53 = load i8*, i8** %6
%54 = call i32 @free(i8* %53)
%55 = load i8*, i8** %7
%56 = call i32 @free(i8* %55)
%57 = load i64, i64* %11
%58 = mul i64 %57, 2
%59 = call i64 @malloc(i64 %58)
%60 = inttoptr i64 %59 to i8*
store i8* %60, i8** %6
%61 = load i8*, i8** %6
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %67
63: ; preds = %51
%64 = load i64, i64* %11
%65 = trunc i64 %64 to i32
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %63, %51
%68 = call i8* (...) @endHeap()
store i8* %68, i8** %10
%69 = load i8*, i8** %10
%70 = load i8*, i8** %8
%71 = ptrtoint i8* %69 to i64
%72 = ptrtoint i8* %70 to i64
%73 = sub i64 %71, %72
%74 = sitofp i64 %73 to double
%75 = load i8*, i8** %9
%76 = load i8*, i8** %8
%77 = ptrtoint i8* %75 to i64
%78 = ptrtoint i8* %76 to i64
%79 = sub i64 %77, %78
%80 = sitofp i64 %79 to double
%81 = fdiv double %74, %80
%82 = fcmp ogt double %81, 1.000000e+00
br i1 %82, label %83, label %91
83: ; preds = %67
%84 = load i8*, i8** %10
%85 = load i8*, i8** %9
%86 = ptrtoint i8* %84 to i64
%87 = ptrtoint i8* %85 to i64
%88 = sub i64 %86, %87
%89 = trunc i64 %88 to i32
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([153 x i8], [153 x i8]* @.str.2, i64 0, i64 0), i32 %89)
br label %93
91: ; preds = %67
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %93
93: ; preds = %91, %83
%94 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [63 x i8] c"-- Testing merging of deallocated large blocks ( >= %u bytes)\0A\00"
@.str.1 = [53 x i8] c"* ERROR: unable to allocate memory of size %u bytes\0A\00"
@.str.2 = [153 x i8] c"* ERROR: not good enough usage of memory (probably incorrect merging of deallocated memory blocks)\0AYour implementation used %u bytes more than expected\0A\00"
@str = [15 x i8] c"Test passed OK\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* (...) @endHeap()
%4 = tail call i64 @malloc(i64 1)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i8* (...) @endHeap()
%7 = ptrtoint i8* %6 to i64
%8 = ptrtoint i8* %3 to i64
%9 = sub i64 %7, %8
%10 = icmp ugt i64 %9, 16384
%11 = select i1 %10, i64 %9, i64 16384
%12 = tail call i32 @free(i8* %5)
%13 = trunc i64 %11 to i32
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = tail call i64 @malloc(i64 %11)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i64 @malloc(i64 %11)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i64 %15, 0
%20 = icmp eq i64 %17, 0
%21 = select i1 %19, i1 true, i1 %20
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %24
24: ; preds = %2, %22
%25 = tail call i8* (...) @endHeap()
%26 = tail call i32 @free(i8* %16)
%27 = tail call i32 @free(i8* %18)
%28 = shl i64 %11, 1
%29 = tail call i64 @malloc(i64 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %33
33: ; preds = %31, %24
%34 = tail call i8* (...) @endHeap()
%35 = ptrtoint i8* %34 to i64
%36 = sub i64 %35, %8
%37 = sitofp i64 %36 to double
%38 = ptrtoint i8* %25 to i64
%39 = sub i64 %38, %8
%40 = sitofp i64 %39 to double
%41 = fdiv double %37, %40
%42 = fcmp ogt double %41, 1.000000e+00
br i1 %42, label %43, label %47
43: ; preds = %33
%44 = sub i64 %35, %38
%45 = trunc i64 %44 to i32
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([153 x i8], [153 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %49
47: ; preds = %33
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = tail call i32 @exit(i32 0)
unreachable
}