#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 EXIT_SUCCESS;
void *mem_alloc(int);
int /*<<< orphan*/ mem_dump();
int /*<<< orphan*/ mem_free(void *);
void *mem_realloc(void *, int);
int main() {
  void *arr[5];
  arr[0] = mem_alloc(20);
  arr[1] = mem_alloc(20);
  arr[2] = mem_alloc(20);
  arr[3] = mem_alloc(20);
  arr[4] = mem_alloc(30);
  void *virtual_page = mem_alloc(5000);
  mem_dump();
  mem_free(arr[0]);
  mem_free(arr[4]);
  mem_dump();
  virtual_page = mem_realloc(virtual_page, 9000);
  arr[1] = mem_realloc(arr[1], 40);
  mem_dump();
  mem_free(virtual_page);
  mem_free(arr[3]);
  mem_free(arr[2]);
  mem_free(arr[1]);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x14,%edi
call   1060 <mem_alloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x14,%edi
call   1060 <mem_alloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x14,%edi
call   1060 <mem_alloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x14,%edi
call   1060 <mem_alloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x1e,%edi
call   1060 <mem_alloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x1388,%edi
call   1060 <mem_alloc@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1050 <mem_dump@plt>
mov    -0x30(%rbp),%rdi
call   1030 <mem_free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <mem_free@plt>
mov    $0x0,%al
call   1050 <mem_dump@plt>
mov    -0x38(%rbp),%rdi
mov    $0x2328,%esi
call   1040 <mem_realloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x28,%esi
call   1040 <mem_realloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <mem_dump@plt>
mov    -0x38(%rbp),%rdi
call   1030 <mem_free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <mem_free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <mem_free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <mem_free@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x14,%edi
call   1060 <mem_alloc@plt>
mov    %rax,%r13
mov    $0x14,%edi
call   1060 <mem_alloc@plt>
mov    %rax,%r12
mov    $0x14,%edi
call   1060 <mem_alloc@plt>
mov    %rax,%r14
mov    $0x14,%edi
call   1060 <mem_alloc@plt>
mov    %rax,%r15
mov    $0x1e,%edi
call   1060 <mem_alloc@plt>
mov    %rax,%rbx
mov    $0x1388,%edi
call   1060 <mem_alloc@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1050 <mem_dump@plt>
mov    %r13,%rdi
call   1030 <mem_free@plt>
mov    %rbx,%rdi
call   1030 <mem_free@plt>
xor    %eax,%eax
call   1050 <mem_dump@plt>
mov    %rbp,%rdi
mov    $0x2328,%esi
call   1040 <mem_realloc@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x28,%esi
call   1040 <mem_realloc@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1050 <mem_dump@plt>
mov    %rbx,%rdi
call   1030 <mem_free@plt>
mov    %r15,%rdi
call   1030 <mem_free@plt>
mov    %r14,%rdi
call   1030 <mem_free@plt>
mov    %rbp,%rdi
call   1030 <mem_free@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i8*]
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i8* @mem_alloc(i32 20)
%5 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
store i8* %4, i8** %5
%6 = call i8* @mem_alloc(i32 20)
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
store i8* %6, i8** %7
%8 = call i8* @mem_alloc(i32 20)
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
store i8* %8, i8** %9
%10 = call i8* @mem_alloc(i32 20)
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
store i8* %10, i8** %11
%12 = call i8* @mem_alloc(i32 30)
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 4
store i8* %12, i8** %13
%14 = call i8* @mem_alloc(i32 5000)
store i8* %14, i8** %3
%15 = call i32 (...) @mem_dump()
%16 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @mem_free(i8* %17)
%19 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 4
%20 = load i8*, i8** %19
%21 = call i32 @mem_free(i8* %20)
%22 = call i32 (...) @mem_dump()
%23 = load i8*, i8** %3
%24 = call i8* @mem_realloc(i8* %23, i32 9000)
store i8* %24, i8** %3
%25 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
%26 = load i8*, i8** %25
%27 = call i8* @mem_realloc(i8* %26, i32 40)
%28 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
store i8* %27, i8** %28
%29 = call i32 (...) @mem_dump()
%30 = load i8*, i8** %3
%31 = call i32 @mem_free(i8* %30)
%32 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
%33 = load i8*, i8** %32
%34 = call i32 @mem_free(i8* %33)
%35 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @mem_free(i8* %36)
%38 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
%39 = load i8*, i8** %38
%40 = call i32 @mem_free(i8* %39)
%41 = load i32, i32* @EXIT_SUCCESS
ret i32 %41
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i8* @mem_alloc(i32 20)
%2 = tail call i8* @mem_alloc(i32 20)
%3 = tail call i8* @mem_alloc(i32 20)
%4 = tail call i8* @mem_alloc(i32 20)
%5 = tail call i8* @mem_alloc(i32 30)
%6 = tail call i8* @mem_alloc(i32 5000)
%7 = tail call i32 (...) @mem_dump()
%8 = tail call i32 @mem_free(i8* %1)
%9 = tail call i32 @mem_free(i8* %5)
%10 = tail call i32 (...) @mem_dump()
%11 = tail call i8* @mem_realloc(i8* %6, i32 9000)
%12 = tail call i8* @mem_realloc(i8* %2, i32 40)
%13 = tail call i32 (...) @mem_dump()
%14 = tail call i32 @mem_free(i8* %11)
%15 = tail call i32 @mem_free(i8* %4)
%16 = tail call i32 @mem_free(i8* %3)
%17 = tail call i32 @mem_free(i8* %12)
%18 = load i32, i32* @EXIT_SUCCESS
ret i32 %18
}