#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*/ free(void *);
int /*<<< orphan*/ function_use_stack();
void *malloc(int);
scalar_t__ malloc_count_peak();
int /*<<< orphan*/ malloc_count_print_status();
int /*<<< orphan*/ printf(char *, long long);
void *realloc(void *, int);
void *stack_count_clear();
scalar_t__ stack_count_usage(void *);
int main() {
  void *a = malloc(2 * 1024 * 1024);
  free(a);
  printf("our peak memory allocation: %lld\n", (long long)malloc_count_peak());
  void *b = malloc(3 * 1024 * 1024);
  malloc_count_print_status();
  b = realloc(b, 2 * 1024 * 1024);
  malloc_count_print_status();
  b = realloc(b, 4 * 1024 * 1024);
  malloc_count_print_status();
  free(b);
  void *c = realloc(NULL, 1 * 1024 * 1024);
  c = realloc(c, 0);
  {
    void *base = stack_count_clear();
    function_use_stack();
    printf("maximum stack usage: %lld\n", (long long)stack_count_usage(base));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x200000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
mov    $0x0,%al
call   1030 <malloc_count_peak@plt>
mov    %rax,%rsi
lea    0xe39(%rip),%rdi
call   1050 <printf@plt>
mov    $0x300000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <malloc_count_print_status@plt>
mov    -0x18(%rbp),%rdi
mov    $0x200000,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <malloc_count_print_status@plt>
mov    -0x18(%rbp),%rdi
mov    $0x400000,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <malloc_count_print_status@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100000,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <stack_count_clear@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <function_use_stack@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <stack_count_usage@plt>
mov    %rax,%rsi
lea    0xdc0(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x200000,%edi
call   1090 <malloc@plt>
mov    %rax,%rdi
call   1040 <free@plt>
xor    %eax,%eax
call   1030 <malloc_count_peak@plt>
lea    0xe4f(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0x300000,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <malloc_count_print_status@plt>
mov    %rbx,%rdi
mov    $0x200000,%esi
call   10a0 <realloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <malloc_count_print_status@plt>
mov    %rbx,%rdi
mov    $0x400000,%esi
call   10a0 <realloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <malloc_count_print_status@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %edi,%edi
mov    $0x100000,%esi
call   10a0 <realloc@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   10a0 <realloc@plt>
xor    %eax,%eax
call   1080 <stack_count_clear@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <function_use_stack@plt>
mov    %rbx,%rdi
call   10b0 <stack_count_usage@plt>
lea    0xde9(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [34 x i8] c"our peak memory allocation: %lld\0A\00"
@.str.1 = [27 x i8] c"maximum stack usage: %lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i8* @malloc(i32 2097152)
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @free(i8* %7)
%9 = call i64 (...) @malloc_count_peak()
%10 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i64 %9)
%11 = call i8* @malloc(i32 3145728)
store i8* %11, i8** %3
%12 = call i32 (...) @malloc_count_print_status()
%13 = load i8*, i8** %3
%14 = call i8* @realloc(i8* %13, i32 2097152)
store i8* %14, i8** %3
%15 = call i32 (...) @malloc_count_print_status()
%16 = load i8*, i8** %3
%17 = call i8* @realloc(i8* %16, i32 4194304)
store i8* %17, i8** %3
%18 = call i32 (...) @malloc_count_print_status()
%19 = load i8*, i8** %3
%20 = call i32 @free(i8* %19)
%21 = call i8* @realloc(i8* null, i32 1048576)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = call i8* @realloc(i8* %22, i32 0)
store i8* %23, i8** %4
%24 = call i8* (...) @stack_count_clear()
store i8* %24, i8** %5
%25 = call i32 (...) @function_use_stack()
%26 = load i8*, i8** %5
%27 = call i64 @stack_count_usage(i8* %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %27)
ret i32 0
}

LLVM IR (-O2):
@.str = [34 x i8] c"our peak memory allocation: %lld\0A\00"
@.str.1 = [27 x i8] c"maximum stack usage: %lld\0A\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(2097152) i8* @malloc(i32 2097152)
%2 = tail call i32 @free(i8* %1)
%3 = tail call i64 (...) @malloc_count_peak()
%4 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i64 %3)
%5 = tail call dereferenceable_or_null(3145728) i8* @malloc(i32 3145728)
%6 = tail call i32 (...) @malloc_count_print_status()
%7 = tail call i8* @realloc(i8* %5, i32 2097152)
%8 = tail call i32 (...) @malloc_count_print_status()
%9 = tail call i8* @realloc(i8* %7, i32 4194304)
%10 = tail call i32 (...) @malloc_count_print_status()
%11 = tail call i32 @free(i8* %9)
%12 = tail call i8* @realloc(i8* null, i32 1048576)
%13 = tail call i8* @realloc(i8* %12, i32 0)
%14 = tail call i8* (...) @stack_count_clear()
%15 = tail call i32 (...) @function_use_stack()
%16 = tail call i64 @stack_count_usage(i8* %14)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %16)
ret i32 0
}