#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sbrk(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int i;
  int nums_of_blocks = 500, blocksize = 1024;
  int *ptr[nums_of_blocks];
  printf("Before malloc %d blocks, break point %10p\n", nums_of_blocks,
         sbrk(0));
  for (i = 0; i < nums_of_blocks; i++)
    ptr[i] = malloc(blocksize);
  printf("After malloc %d, break point %10p\n", nums_of_blocks, sbrk(0));
  for (i = 0; i < nums_of_blocks - 1; i++)
    free(ptr[i]);
  printf("After free the block 0 ~ 498, break point %10p\n", sbrk(0));
  free(ptr[nums_of_blocks - 1]);
  printf("After free the last block, break point %10p\n", sbrk(0));
  exit(EXIT_SUCCESS);
}


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)
movl   $0x1f4,-0x18(%rbp)
movl   $0x400,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
xor    %edi,%edi
call   1050 <sbrk@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11f3 <main+0xa3>
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x40(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c2 <main+0x72>
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
xor    %edi,%edi
call   1050 <sbrk@plt>
mov    -0x44(%rbp),%esi
mov    %eax,%edx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    124a <main+0xfa>
mov    -0x40(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121a <main+0xca>
xor    %edi,%edi
call   1050 <sbrk@plt>
mov    %eax,%esi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1050 <sbrk@plt>
mov    %eax,%esi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0xfa0,%rsp
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    0xe98(%rip),%rdi
mov    $0x1f4,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,(%rsp,%rbx,8)
add    $0x1,%rbx
cmp    $0x1f4,%rbx
jne    1180 <main+0x30>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    0xe80(%rip),%rdi
mov    $0x1f4,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
nopl   0x0(%rax)
mov    (%rsp,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    $0x1f3,%rbx
jne    11c0 <main+0x70>
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xf98(%rsp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1050 <sbrk@plt>
lea    0xe76(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [43 x i8] c"Before malloc %d blocks, break point %10p\0A\00"
@.str.1 = [35 x i8] c"After malloc %d, break point %10p\0A\00"
@.str.2 = [48 x i8] c"After free the block 0 ~ 498, break point %10p\0A\00"
@.str.3 = [45 x i8] c"After free the last block, break point %10p\0A\00"
@EXIT_SUCCESS = global i32 0
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 i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 500, i32* %7
store i32 1024, i32* %8
%11 = load i32, i32* %7
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %9
%14 = alloca i32*, i64 %12
store i64 %12, i64* %10
%15 = load i32, i32* %7
%16 = call i32 @sbrk(i32 0)
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
store i32 0, i32* %6
br label %18
18: ; preds = %28, %2
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i32, i32* %8
%24 = call i32* @malloc(i32 %23)
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32*, i32** %14, i64 %26
store i32* %24, i32** %27
br label %28
28: ; preds = %22
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %18
31: ; preds = %18
%32 = load i32, i32* %7
%33 = call i32 @sbrk(i32 0)
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %33)
store i32 0, i32* %6
br label %35
35: ; preds = %46, %31
%36 = load i32, i32* %6
%37 = load i32, i32* %7
%38 = sub nsw i32 %37, 1
%39 = icmp slt i32 %36, %38
br i1 %39, label %40, label %49
40: ; preds = %35
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32*, i32** %14, i64 %42
%44 = load i32*, i32** %43
%45 = call i32 @free(i32* %44)
br label %46
46: ; preds = %40
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
br label %35
49: ; preds = %35
%50 = call i32 @sbrk(i32 0)
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %7
%53 = sub nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32*, i32** %14, i64 %54
%56 = load i32*, i32** %55
%57 = call i32 @free(i32* %56)
%58 = call i32 @sbrk(i32 0)
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %58)
%60 = load i32, i32* @EXIT_SUCCESS
%61 = call i32 @exit(i32 %60)
unreachable
}

LLVM IR (-O2):
@.str = [43 x i8] c"Before malloc %d blocks, break point %10p\0A\00"
@.str.1 = [35 x i8] c"After malloc %d, break point %10p\0A\00"
@.str.2 = [48 x i8] c"After free the block 0 ~ 498, break point %10p\0A\00"
@.str.3 = [45 x i8] c"After free the last block, break point %10p\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [500 x i32*]
%4 = tail call i32 @sbrk(i32 0)
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 500, i32 %4)
br label %6
6: ; preds = %2, %6
%7 = phi i64 [ 0, %2 ], [ %10, %6 ]
%8 = tail call i32* @malloc(i32 1024)
%9 = getelementptr inbounds [500 x i32*], [500 x i32*]* %3, i64 0, i64 %7
store i32* %8, i32** %9
%10 = add nuw nsw i64 %7, 1
%11 = icmp eq i64 %10, 500
br i1 %11, label %12, label %6
12: ; preds = %6
%13 = tail call i32 @sbrk(i32 0)
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 500, i32 %13)
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ 0, %12 ], [ %20, %15 ]
%17 = getelementptr inbounds [500 x i32*], [500 x i32*]* %3, i64 0, i64 %16
%18 = load i32*, i32** %17
%19 = tail call i32 @free(i32* %18)
%20 = add nuw nsw i64 %16, 1
%21 = icmp eq i64 %20, 499
br i1 %21, label %22, label %15
22: ; preds = %15
%23 = tail call i32 @sbrk(i32 0)
%24 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [500 x i32*], [500 x i32*]* %3, i64 0, i64 499
%26 = load i32*, i32** %25
%27 = tail call i32 @free(i32* %26)
%28 = tail call i32 @sbrk(i32 0)
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @EXIT_SUCCESS
%31 = tail call i32 @exit(i32 %30)
unreachable
}