#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*/ alloc_test_free(void *);
int alloc_test_get_allocated();
void *alloc_test_malloc(int);
int /*<<< orphan*/ alloc_test_set_limit(int);
int /*<<< orphan*/ assert(int);
void test_malloc_free(void) {
  void *block, *block2, *block3, *block4;
  unsigned char *ptr;
  int i;
  assert(alloc_test_get_allocated() == 0);
  block = alloc_test_malloc(1024);
  assert(block != NULL);
  assert(alloc_test_get_allocated() == 1024);
  ptr = block;
  for (i = 0; i < 1024; ++i) {
    assert(ptr[i] != 0);
  }
  alloc_test_free(block);
  assert(alloc_test_get_allocated() == 0);
  alloc_test_set_limit(3);
  block = alloc_test_malloc(1024);
  assert(block != NULL);
  block2 = alloc_test_malloc(1024);
  assert(block2 != NULL);
  block3 = alloc_test_malloc(1024);
  assert(block3 != NULL);
  block4 = alloc_test_malloc(1024);
  assert(block4 == NULL);
  alloc_test_free(block);
  alloc_test_free(block2);
  alloc_test_free(block3);
  alloc_test_free(block4);
}


x86 assembly (clang, -O0):
<test_malloc_free>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1050 <alloc_test_get_allocated@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x0,%al
call   1050 <alloc_test_get_allocated@plt>
cmp    $0x400,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x400,-0x2c(%rbp)
jge    11ee <test_malloc_free+0x9e>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b7 <test_malloc_free+0x67>
mov    -0x8(%rbp),%rdi
call   1040 <alloc_test_free@plt>
mov    $0x0,%al
call   1050 <alloc_test_get_allocated@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x3,%edi
call   1030 <alloc_test_set_limit@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <alloc_test_free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <alloc_test_free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <alloc_test_free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <alloc_test_free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_malloc_free>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <alloc_test_get_allocated@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
xor    %eax,%eax
call   1050 <alloc_test_get_allocated@plt>
xor    %edi,%edi
cmp    $0x400,%eax
sete   %dil
call   1060 <assert@plt>
xor    %edi,%edi
cmpb   $0x0,(%r14,%rbx,1)
setne  %dil
call   1060 <assert@plt>
add    $0x1,%rbx
cmp    $0x400,%rbx
jne    11a0 <test_malloc_free+0x50>
mov    %r14,%rdi
call   1040 <alloc_test_free@plt>
xor    %eax,%eax
call   1050 <alloc_test_get_allocated@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    $0x3,%edi
call   1030 <alloc_test_set_limit@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,%r12
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    $0x400,%edi
call   1070 <alloc_test_malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1060 <assert@plt>
mov    %r14,%rdi
call   1040 <alloc_test_free@plt>
mov    %r15,%rdi
call   1040 <alloc_test_free@plt>
mov    %r12,%rdi
call   1040 <alloc_test_free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <alloc_test_free@plt>

LLVM IR (-O0):
define void @test_malloc_free() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = call i32 (...) @alloc_test_get_allocated()
%8 = icmp eq i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = call i8* @alloc_test_malloc(i32 1024)
store i8* %11, i8** %1
%12 = load i8*, i8** %1
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = call i32 (...) @alloc_test_get_allocated()
%17 = icmp eq i32 %16, 1024
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i8*, i8** %1
store i8* %20, i8** %5
store i32 0, i32* %6
br label %21
21: ; preds = %34, %0
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 1024
br i1 %23, label %24, label %37
24: ; preds = %21
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = icmp ne i32 %30, 0
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
br label %34
34: ; preds = %24
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %21
37: ; preds = %21
%38 = load i8*, i8** %1
%39 = call i32 @alloc_test_free(i8* %38)
%40 = call i32 (...) @alloc_test_get_allocated()
%41 = icmp eq i32 %40, 0
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = call i32 @alloc_test_set_limit(i32 3)
%45 = call i8* @alloc_test_malloc(i32 1024)
store i8* %45, i8** %1
%46 = load i8*, i8** %1
%47 = icmp ne i8* %46, null
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = call i8* @alloc_test_malloc(i32 1024)
store i8* %50, i8** %2
%51 = load i8*, i8** %2
%52 = icmp ne i8* %51, null
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = call i8* @alloc_test_malloc(i32 1024)
store i8* %55, i8** %3
%56 = load i8*, i8** %3
%57 = icmp ne i8* %56, null
%58 = zext i1 %57 to i32
%59 = call i32 @assert(i32 %58)
%60 = call i8* @alloc_test_malloc(i32 1024)
store i8* %60, i8** %4
%61 = load i8*, i8** %4
%62 = icmp eq i8* %61, null
%63 = zext i1 %62 to i32
%64 = call i32 @assert(i32 %63)
%65 = load i8*, i8** %1
%66 = call i32 @alloc_test_free(i8* %65)
%67 = load i8*, i8** %2
%68 = call i32 @alloc_test_free(i8* %67)
%69 = load i8*, i8** %3
%70 = call i32 @alloc_test_free(i8* %69)
%71 = load i8*, i8** %4
%72 = call i32 @alloc_test_free(i8* %71)
ret void
}

LLVM IR (-O2):
define void @test_malloc_free() {
%1 = tail call i32 (...) @alloc_test_get_allocated()
%2 = icmp eq i32 %1, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i8* @alloc_test_malloc(i32 1024)
%6 = icmp ne i8* %5, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 (...) @alloc_test_get_allocated()
%10 = icmp eq i32 %9, 1024
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
br label %13
13: ; preds = %0, %13
%14 = phi i64 [ 0, %0 ], [ %20, %13 ]
%15 = getelementptr inbounds i8, i8* %5, i64 %14
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = add nuw nsw i64 %14, 1
%21 = icmp eq i64 %20, 1024
br i1 %21, label %22, label %13
22: ; preds = %13
%23 = tail call i32 @alloc_test_free(i8* %5)
%24 = tail call i32 (...) @alloc_test_get_allocated()
%25 = icmp eq i32 %24, 0
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
%28 = tail call i32 @alloc_test_set_limit(i32 3)
%29 = tail call i8* @alloc_test_malloc(i32 1024)
%30 = icmp ne i8* %29, null
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = tail call i8* @alloc_test_malloc(i32 1024)
%34 = icmp ne i8* %33, null
%35 = zext i1 %34 to i32
%36 = tail call i32 @assert(i32 %35)
%37 = tail call i8* @alloc_test_malloc(i32 1024)
%38 = icmp ne i8* %37, null
%39 = zext i1 %38 to i32
%40 = tail call i32 @assert(i32 %39)
%41 = tail call i8* @alloc_test_malloc(i32 1024)
%42 = icmp eq i8* %41, null
%43 = zext i1 %42 to i32
%44 = tail call i32 @assert(i32 %43)
%45 = tail call i32 @alloc_test_free(i8* %29)
%46 = tail call i32 @alloc_test_free(i8* %33)
%47 = tail call i32 @alloc_test_free(i8* %37)
%48 = tail call i32 @alloc_test_free(i8* %41)
ret void
}