#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*/ create_pool(int);
int /*<<< orphan*/ destroy_pool();
int /*<<< orphan*/ my_free(void *);
void *my_malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int test_three() {
  printf("Test Three: Free Middle Block\n");
  int error = 1;
  create_pool(1000);
  void *bl1 = my_malloc(200);
  void *bl2 = my_malloc(200);
  void *bl3 = my_malloc(200);
  void *bl4 = my_malloc(200);
  void *bl5 = my_malloc(200);
  my_free(bl3);
  bl3 = my_malloc(210);
  if (!bl3) {
    printf("Memory not allocated: %d\n", 210);
  } else {
    printf("Memory allocated: %d\n", 210);
    error = 2;
  }
  bl3 = my_malloc(150);
  if (!bl3) {
    printf("Memory not allocated: %d\n", 150);
    error = 3;
  } else {
    printf("Memory allocated: %d\n", 150);
  }
  void *bl6 = my_malloc(60);
  if (!bl6) {
    printf("Memory not allocated: %d\n", 60);
  } else {
    printf("Memory allocated: %d\n", 60);
    error = 4;
  }
  bl6 = my_malloc(50);
  if (!bl6) {
    printf("Memory not allocated: %d\n", 50);
    error = 5;
  } else {
    printf("Memory allocated: %d\n", 50);
  }
  my_free(bl1);
  my_free(bl2);
  my_free(bl3);
  my_free(bl4);
  my_free(bl5);
  my_free(bl6);
  printf("Test Three Complete.\n");
  destroy_pool();
  return error;
}


x86 assembly (clang, -O0):
<test_three>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    $0x3e8,%edi
call   1050 <create_pool@plt>
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <my_free@plt>
mov    $0xd2,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f7 <test_three+0xa7>
lea    0xe39(%rip),%rdi
mov    $0xd2,%esi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1211 <test_three+0xc1>
lea    0xe3b(%rip),%rdi
mov    $0xd2,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
mov    $0x96,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1249 <test_three+0xf9>
lea    0xdee(%rip),%rdi
mov    $0x96,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    125c <test_three+0x10c>
lea    0xde9(%rip),%rdi
mov    $0x96,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3c,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    128d <test_three+0x13d>
lea    0xda3(%rip),%rdi
mov    $0x3c,%esi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a7 <test_three+0x157>
lea    0xda5(%rip),%rdi
mov    $0x3c,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x4,-0x4(%rbp)
mov    $0x32,%edi
call   1030 <my_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12df <test_three+0x18f>
lea    0xd58(%rip),%rdi
mov    $0x32,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    12f2 <test_three+0x1a2>
lea    0xd53(%rip),%rdi
mov    $0x32,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <my_free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <my_free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <my_free@plt>
mov    -0x28(%rbp),%rdi
call   1060 <my_free@plt>
mov    -0x30(%rbp),%rdi
call   1060 <my_free@plt>
mov    -0x38(%rbp),%rdi
call   1060 <my_free@plt>
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <destroy_pool@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_three>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xebb(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3e8,%edi
call   1060 <create_pool@plt>
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,0x20(%rsp)
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,0x18(%rsp)
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,%rbx
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,0x10(%rsp)
mov    $0xc8,%edi
call   1030 <my_malloc@plt>
mov    %rax,0x8(%rsp)
mov    %rbx,%rdi
call   1070 <my_free@plt>
mov    $0xd2,%edi
call   1030 <my_malloc@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    0xe12(%rip),%r14
lea    0xe25(%rip),%r13
mov    %r13,%rdi
cmove  %r14,%rdi
mov    $0x2,%ebp
sub    %ecx,%ebp
mov    $0xd2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x96,%edi
call   1030 <my_malloc@plt>
mov    %rax,%r12
test   %rax,%rax
mov    %r13,%rdi
cmove  %r14,%rdi
mov    $0x3,%ebx
cmovne %ebp,%ebx
mov    $0x96,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3c,%edi
call   1030 <my_malloc@plt>
test   %rax,%rax
mov    %r13,%rdi
cmove  %r14,%rdi
mov    $0x4,%ebp
cmove  %ebx,%ebp
mov    $0x3c,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x32,%edi
call   1030 <my_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
cmove  %r14,%r13
mov    $0x5,%r14d
cmovne %ebp,%r14d
mov    %r13,%rdi
mov    $0x32,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x20(%rsp),%rdi
call   1070 <my_free@plt>
mov    0x18(%rsp),%rdi
call   1070 <my_free@plt>
mov    %r12,%rdi
call   1070 <my_free@plt>
mov    0x10(%rsp),%rdi
call   1070 <my_free@plt>
mov    0x8(%rsp),%rdi
call   1070 <my_free@plt>
mov    %r15,%rdi
call   1070 <my_free@plt>
lea    0xd80(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1080 <destroy_pool@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Test Three: Free Middle Block\0A\00"
@.str.1 = [26 x i8] c"Memory not allocated: %d\0A\00"
@.str.2 = [22 x i8] c"Memory allocated: %d\0A\00"
@.str.3 = [22 x i8] c"Test Three Complete.\0A\00"
define i32 @test_three() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
%9 = call i32 @create_pool(i32 1000)
%10 = call i8* @my_malloc(i32 200)
store i8* %10, i8** %2
%11 = call i8* @my_malloc(i32 200)
store i8* %11, i8** %3
%12 = call i8* @my_malloc(i32 200)
store i8* %12, i8** %4
%13 = call i8* @my_malloc(i32 200)
store i8* %13, i8** %5
%14 = call i8* @my_malloc(i32 200)
store i8* %14, i8** %6
%15 = load i8*, i8** %4
%16 = call i32 @my_free(i8* %15)
%17 = call i8* @my_malloc(i32 210)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %0
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 210)
br label %24
22: ; preds = %0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 210)
store i32 2, i32* %1
br label %24
24: ; preds = %22, %20
%25 = call i8* @my_malloc(i32 150)
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = icmp ne i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 150)
store i32 3, i32* %1
br label %32
30: ; preds = %24
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 150)
br label %32
32: ; preds = %30, %28
%33 = call i8* @my_malloc(i32 60)
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = icmp ne i8* %34, null
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 60)
br label %40
38: ; preds = %32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 60)
store i32 4, i32* %1
br label %40
40: ; preds = %38, %36
%41 = call i8* @my_malloc(i32 50)
store i8* %41, i8** %7
%42 = load i8*, i8** %7
%43 = icmp ne i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 50)
store i32 5, i32* %1
br label %48
46: ; preds = %40
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 50)
br label %48
48: ; preds = %46, %44
%49 = load i8*, i8** %2
%50 = call i32 @my_free(i8* %49)
%51 = load i8*, i8** %3
%52 = call i32 @my_free(i8* %51)
%53 = load i8*, i8** %4
%54 = call i32 @my_free(i8* %53)
%55 = load i8*, i8** %5
%56 = call i32 @my_free(i8* %55)
%57 = load i8*, i8** %6
%58 = call i32 @my_free(i8* %57)
%59 = load i8*, i8** %7
%60 = call i32 @my_free(i8* %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%62 = call i32 (...) @destroy_pool()
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [26 x i8] c"Memory not allocated: %d\0A\00"
@.str.2 = [22 x i8] c"Memory allocated: %d\0A\00"
@str = [30 x i8] c"Test Three: Free Middle Block\00"
@str.4 = [21 x i8] c"Test Three Complete.\00"
define i32 @test_three() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @create_pool(i32 1000)
%3 = tail call i8* @my_malloc(i32 200)
%4 = tail call i8* @my_malloc(i32 200)
%5 = tail call i8* @my_malloc(i32 200)
%6 = tail call i8* @my_malloc(i32 200)
%7 = tail call i8* @my_malloc(i32 200)
%8 = tail call i32 @my_free(i8* %5)
%9 = tail call i8* @my_malloc(i32 210)
%10 = icmp eq i8* %9, null
%11 = select i1 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0)
%12 = select i1 %10, i32 1, i32 2
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %11, i32 210)
%14 = tail call i8* @my_malloc(i32 150)
%15 = icmp eq i8* %14, null
%16 = select i1 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0)
%17 = select i1 %15, i32 3, i32 %12
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %16, i32 150)
%19 = tail call i8* @my_malloc(i32 60)
%20 = icmp eq i8* %19, null
%21 = select i1 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0)
%22 = select i1 %20, i32 %17, i32 4
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %21, i32 60)
%24 = tail call i8* @my_malloc(i32 50)
%25 = icmp eq i8* %24, null
%26 = select i1 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0)
%27 = select i1 %25, i32 5, i32 %22
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %26, i32 50)
%29 = tail call i32 @my_free(i8* %3)
%30 = tail call i32 @my_free(i8* %4)
%31 = tail call i32 @my_free(i8* %14)
%32 = tail call i32 @my_free(i8* %6)
%33 = tail call i32 @my_free(i8* %7)
%34 = tail call i32 @my_free(i8* %24)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0))
%36 = tail call i32 (...) @destroy_pool()
ret i32 %27
}