#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*/ assert(int);
int /*<<< orphan*/ p_free(void *);
void *p_malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ test_malloc_cleanup();
int /*<<< orphan*/ test_malloc_startup();
void test_malloc_max_count(void) {
  size_t i;
  void *ptrs[100 + 2];
  printf("\tStart  test_malloc_max_count\n");
  test_malloc_startup();
  for (i = 0; i < 100; i++) {
    ptrs[i] = p_malloc(i + 5);
    assert(ptrs[i] != 0);
    if (i == 40 || i == 55) {
      p_free(ptrs[i - 10]);
      ptrs[i - 10] = 0;
    }
    if (i == 81) {
      p_free(ptrs[i]);
      ptrs[i] = 0;
    }
  }
  printf("\t\tExpect: Total number of mallocs exceeded %d (twice)\n", 100);
  assert(p_malloc(500) == 0);
  assert(p_malloc(501) == 0);
  for (i = 0; i < 100; i++)
    p_free(ptrs[i]);
  test_malloc_cleanup();
  test_malloc_startup();
  for (i = 0; i < 100; i++) {
    ptrs[0] = p_malloc(10);
    assert(ptrs[0] != 0);
    p_free(ptrs[0]);
  }
  printf("\t\tExpect: error a third time\n");
  assert(p_malloc(10) == 0);
  test_malloc_cleanup();
  printf("\tFinish test_malloc_max_count\n");
}


x86 assembly (clang, -O0):
<test_malloc_max_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <test_malloc_startup@plt>
movq   $0x0,-0x8(%rbp)
cmpq   $0x64,-0x8(%rbp)
jae    124c <test_malloc_max_count+0xec>
mov    -0x8(%rbp),%rax
add    $0x5,%rax
mov    %eax,%edi
call   1070 <p_malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,-0x340(%rbp,%rax,8)
ff
mov    -0x8(%rbp),%rax
cmpq   $0x0,-0x340(%rbp,%rax,8)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x28,-0x8(%rbp)
je     11e1 <test_malloc_max_count+0x81>
cmpq   $0x37,-0x8(%rbp)
jne    120a <test_malloc_max_count+0xaa>
mov    -0x8(%rbp),%rax
sub    $0xa,%rax
mov    -0x340(%rbp,%rax,8),%rdi
ff
call   1080 <p_free@plt>
mov    -0x8(%rbp),%rax
sub    $0xa,%rax
movq   $0x0,-0x340(%rbp,%rax,8)
cmpq   $0x51,-0x8(%rbp)
jne    1236 <test_malloc_max_count+0xd6>
mov    -0x8(%rbp),%rax
mov    -0x340(%rbp,%rax,8),%rdi
ff
call   1080 <p_free@plt>
mov    -0x8(%rbp),%rax
movq   $0x0,-0x340(%rbp,%rax,8)
jmp    123b <test_malloc_max_count+0xdb>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1188 <test_malloc_max_count+0x28>
lea    0xdcc(%rip),%rdi
mov    $0x64,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1f4,%edi
call   1070 <p_malloc@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x1f5,%edi
call   1070 <p_malloc@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movq   $0x0,-0x8(%rbp)
cmpq   $0x64,-0x8(%rbp)
jae    12ca <test_malloc_max_count+0x16a>
mov    -0x8(%rbp),%rax
mov    -0x340(%rbp,%rax,8),%rdi
ff
call   1080 <p_free@plt>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    129d <test_malloc_max_count+0x13d>
mov    $0x0,%al
call   1040 <test_malloc_cleanup@plt>
mov    $0x0,%al
call   1050 <test_malloc_startup@plt>
movq   $0x0,-0x8(%rbp)
cmpq   $0x64,-0x8(%rbp)
jae    132e <test_malloc_max_count+0x1ce>
mov    $0xa,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x340(%rbp)
cmpq   $0x0,-0x340(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x340(%rbp),%rdi
call   1080 <p_free@plt>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    12e0 <test_malloc_max_count+0x180>
lea    0xd21(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xa,%edi
call   1070 <p_malloc@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x0,%al
call   1040 <test_malloc_cleanup@plt>
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_malloc_max_count>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x338,%rsp
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <test_malloc_startup@plt>
mov    %rsp,%r15
jmp    11ca <test_malloc_max_count+0x5a>
nopl   0x0(%rax,%rax,1)
lea    (%rsp,%rbx,8),%r12
add    $0xffffffffffffffb0,%r12
mov    -0x50(%r15),%r14
mov    %r14,%rdi
call   1090 <p_free@plt>
movq   $0x0,(%r12)
add    $0x1,%rbx
add    $0x8,%r15
cmp    $0x64,%rbx
je     11fd <test_malloc_max_count+0x8d>
lea    0x5(%rbx),%edi
call   1080 <p_malloc@plt>
mov    %rax,%r14
mov    %rax,(%r15)
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
cmp    $0x28,%rbx
je     11a0 <test_malloc_max_count+0x30>
cmp    $0x37,%rbx
je     11a0 <test_malloc_max_count+0x30>
cmp    $0x51,%rbx
jne    11bc <test_malloc_max_count+0x4c>
mov    %r15,%r12
jmp    11ac <test_malloc_max_count+0x3c>
lea    0xdfc(%rip),%rdi
xor    %ebx,%ebx
mov    $0x64,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1f4,%edi
call   1080 <p_malloc@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    $0x1f5,%edi
call   1080 <p_malloc@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp,%rbx,8),%rdi
call   1090 <p_free@plt>
add    $0x1,%rbx
cmp    $0x64,%rbx
jne    1250 <test_malloc_max_count+0xe0>
xor    %eax,%eax
call   1050 <test_malloc_cleanup@plt>
xor    %eax,%eax
call   1060 <test_malloc_startup@plt>
mov    $0x64,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1080 <p_malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    %r14,%rdi
call   1090 <p_free@plt>
add    $0xffffffffffffffff,%rbx
jne    1280 <test_malloc_max_count+0x110>
lea    0xda5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1080 <p_malloc@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
xor    %eax,%eax
call   1050 <test_malloc_cleanup@plt>
lea    0xd97(%rip),%rdi
add    $0x338,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"\09Start test_malloc_max_count\0A\00"
@.str.1 = [55 x i8] c"\09\09Expect: Total number of mallocs exceeded %d (twice)\0A\00"
@.str.2 = [30 x i8] c"\09\09Expect: error a third time\0A\00"
@.str.3 = [31 x i8] c"\09Finish test_malloc_max_count\0A\00"
define void @test_malloc_max_count() {
%1 = alloca i64
%2 = alloca [102 x i8*]
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @test_malloc_startup()
store i64 0, i64* %1
br label %5
5: ; preds = %46, %0
%6 = load i64, i64* %1
%7 = icmp ult i64 %6, 100
br i1 %7, label %8, label %49
8: ; preds = %5
%9 = load i64, i64* %1
%10 = add i64 %9, 5
%11 = trunc i64 %10 to i32
%12 = call i8* @p_malloc(i32 %11)
%13 = load i64, i64* %1
%14 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 %13
store i8* %12, i8** %14
%15 = load i64, i64* %1
%16 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 %15
%17 = load i8*, i8** %16
%18 = icmp ne i8* %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i64, i64* %1
%22 = icmp eq i64 %21, 40
br i1 %22, label %26, label %23
23: ; preds = %8
%24 = load i64, i64* %1
%25 = icmp eq i64 %24, 55
br i1 %25, label %26, label %35
26: ; preds = %23, %8
%27 = load i64, i64* %1
%28 = sub i64 %27, 10
%29 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = call i32 @p_free(i8* %30)
%32 = load i64, i64* %1
%33 = sub i64 %32, 10
%34 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 %33
store i8* null, i8** %34
br label %35
35: ; preds = %26, %23
%36 = load i64, i64* %1
%37 = icmp eq i64 %36, 81
br i1 %37, label %38, label %45
38: ; preds = %35
%39 = load i64, i64* %1
%40 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 %39
%41 = load i8*, i8** %40
%42 = call i32 @p_free(i8* %41)
%43 = load i64, i64* %1
%44 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 %43
store i8* null, i8** %44
br label %45
45: ; preds = %38, %35
br label %46
46: ; preds = %45
%47 = load i64, i64* %1
%48 = add i64 %47, 1
store i64 %48, i64* %1
br label %5
49: ; preds = %5
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 100)
%51 = call i8* @p_malloc(i32 500)
%52 = icmp eq i8* %51, null
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = call i8* @p_malloc(i32 501)
%56 = icmp eq i8* %55, null
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
store i64 0, i64* %1
br label %59
59: ; preds = %67, %49
%60 = load i64, i64* %1
%61 = icmp ult i64 %60, 100
br i1 %61, label %62, label %70
62: ; preds = %59
%63 = load i64, i64* %1
%64 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 %63
%65 = load i8*, i8** %64
%66 = call i32 @p_free(i8* %65)
br label %67
67: ; preds = %62
%68 = load i64, i64* %1
%69 = add i64 %68, 1
store i64 %69, i64* %1
br label %59
70: ; preds = %59
%71 = call i32 (...) @test_malloc_cleanup()
%72 = call i32 (...) @test_malloc_startup()
store i64 0, i64* %1
br label %73
73: ; preds = %87, %70
%74 = load i64, i64* %1
%75 = icmp ult i64 %74, 100
br i1 %75, label %76, label %90
76: ; preds = %73
%77 = call i8* @p_malloc(i32 10)
%78 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 0
store i8* %77, i8** %78
%79 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 0
%80 = load i8*, i8** %79
%81 = icmp ne i8* %80, null
%82 = zext i1 %81 to i32
%83 = call i32 @assert(i32 %82)
%84 = getelementptr inbounds [102 x i8*], [102 x i8*]* %2, i64 0, i64 0
%85 = load i8*, i8** %84
%86 = call i32 @p_free(i8* %85)
br label %87
87: ; preds = %76
%88 = load i64, i64* %1
%89 = add i64 %88, 1
store i64 %89, i64* %1
br label %73
90: ; preds = %73
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%92 = call i8* @p_malloc(i32 10)
%93 = icmp eq i8* %92, null
%94 = zext i1 %93 to i32
%95 = call i32 @assert(i32 %94)
%96 = call i32 (...) @test_malloc_cleanup()
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [55 x i8] c"\09\09Expect: Total number of mallocs exceeded %d (twice)\0A\00"
@str = [30 x i8] c"\09Start test_malloc_max_count\00"
@str.4 = [29 x i8] c"\09\09Expect: error a third time\00"
@str.5 = [30 x i8] c"\09Finish test_malloc_max_count\00"
define void @test_malloc_max_count() {
%1 = alloca [102 x i8*]
%2 = bitcast [102 x i8*]* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @test_malloc_startup()
br label %5
5: ; preds = %0, %22
%6 = phi i64 [ 0, %0 ], [ %23, %22 ]
%7 = trunc i64 %6 to i32
%8 = add nuw nsw i32 %7, 5
%9 = tail call i8* @p_malloc(i32 %8)
%10 = getelementptr inbounds [102 x i8*], [102 x i8*]* %1, i64 0, i64 %6
store i8* %9, i8** %10
%11 = icmp ne i8* %9, null
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
switch i64 %6, label %22 [
i64 55, label %14
i64 40, label %14
i64 81, label %18
]
14: ; preds = %5, %5
%15 = add nsw i64 %6, -10
%16 = getelementptr inbounds [102 x i8*], [102 x i8*]* %1, i64 0, i64 %15
%17 = load i8*, i8** %16
br label %18
18: ; preds = %5, %14
%19 = phi i8* [ %17, %14 ], [ %9, %5 ]
%20 = phi i8** [ %16, %14 ], [ %10, %5 ]
%21 = tail call i32 @p_free(i8* %19)
store i8* null, i8** %20
br label %22
22: ; preds = %18, %5
%23 = add nuw nsw i64 %6, 1
%24 = icmp eq i64 %23, 100
br i1 %24, label %25, label %5
25: ; preds = %22
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 100)
%27 = tail call i8* @p_malloc(i32 500)
%28 = icmp eq i8* %27, null
%29 = zext i1 %28 to i32
%30 = tail call i32 @assert(i32 %29)
%31 = tail call i8* @p_malloc(i32 501)
%32 = icmp eq i8* %31, null
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
br label %35
35: ; preds = %25, %35
%36 = phi i64 [ 0, %25 ], [ %40, %35 ]
%37 = getelementptr inbounds [102 x i8*], [102 x i8*]* %1, i64 0, i64 %36
%38 = load i8*, i8** %37
%39 = tail call i32 @p_free(i8* %38)
%40 = add nuw nsw i64 %36, 1
%41 = icmp eq i64 %40, 100
br i1 %41, label %42, label %35
42: ; preds = %35
%43 = tail call i32 (...) @test_malloc_cleanup()
%44 = tail call i32 (...) @test_malloc_startup()
br label %45
45: ; preds = %42, %45
%46 = phi i64 [ 0, %42 ], [ %52, %45 ]
%47 = tail call i8* @p_malloc(i32 10)
%48 = icmp ne i8* %47, null
%49 = zext i1 %48 to i32
%50 = tail call i32 @assert(i32 %49)
%51 = tail call i32 @p_free(i8* %47)
%52 = add nuw nsw i64 %46, 1
%53 = icmp eq i64 %52, 100
br i1 %53, label %54, label %45
54: ; preds = %45
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.4, i64 0, i64 0))
%56 = tail call i8* @p_malloc(i32 10)
%57 = icmp eq i8* %56, null
%58 = zext i1 %57 to i32
%59 = tail call i32 @assert(i32 %58)
%60 = tail call i32 (...) @test_malloc_cleanup()
%61 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
ret void
}