#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*/ assert(int);
int /*<<< orphan*/ p_free(char *);
char *p_malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ test_malloc_cleanup();
int /*<<< orphan*/ test_malloc_startup();
void test_memory_not_freed(void) {
  char *p;
  char *p2;
  char *p3;
  char *p4;
  printf("\tStart  test_memory_not_freed\n");
  test_malloc_startup();
  p = p_malloc(10);
  p2 = p_malloc(20);
  assert(p != 0);
  assert(p2 != 0);
  p_free(p2);
  p3 = p_malloc(30);
  assert(p3 != 0);
  p_free(p3);
  printf("\t\tExpect: malloc was already freed\n");
  p_free(p2);
  printf("\t\tExpect: no error\n");
  p4 = p_malloc(40);
  assert(p4 != 0);
  printf("\t\tExpect: malloc #0 and #3 were not freed\n");
  test_malloc_cleanup();
  printf("\tFinish test_memory_not_freed\n");
}


x86 assembly (clang, -O0):
<test_memory_not_freed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <test_malloc_startup@plt>
mov    $0xa,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x14,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1080 <p_free@plt>
mov    $0x1e,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1080 <p_free@plt>
lea    0xe2b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <p_free@plt>
lea    0xe3a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x28,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    0xe22(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <test_malloc_cleanup@plt>
lea    0xe3a(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_memory_not_freed>:
push   %r14
push   %rbx
push   %rax
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <test_malloc_startup@plt>
mov    $0xa,%edi
call   1070 <p_malloc@plt>
mov    %rax,%rbx
mov    $0x14,%edi
call   1070 <p_malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
mov    %r14,%rdi
call   1080 <p_free@plt>
mov    $0x1e,%edi
call   1070 <p_malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   1080 <p_free@plt>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1080 <p_free@plt>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x28,%edi
call   1070 <p_malloc@plt>
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1060 <assert@plt>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <test_malloc_cleanup@plt>
lea    0xe54(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"\09Start test_memory_not_freed\0A\00"
@.str.1 = [36 x i8] c"\09\09Expect: malloc was already freed\0A\00"
@.str.2 = [20 x i8] c"\09\09Expect: no error\0A\00"
@.str.3 = [43 x i8] c"\09\09Expect: malloc and were not freed\0A\00"
@.str.4 = [31 x i8] c"\09Finish test_memory_not_freed\0A\00"
define void @test_memory_not_freed() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (...) @test_malloc_startup()
%7 = call i8* @p_malloc(i32 10)
store i8* %7, i8** %1
%8 = call i8* @p_malloc(i32 20)
store i8* %8, i8** %2
%9 = load i8*, i8** %1
%10 = icmp ne i8* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8*, i8** %2
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %2
%18 = call i32 @p_free(i8* %17)
%19 = call i8* @p_malloc(i32 30)
store i8* %19, i8** %3
%20 = load i8*, i8** %3
%21 = icmp ne i8* %20, null
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i8*, i8** %3
%25 = call i32 @p_free(i8* %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%27 = load i8*, i8** %2
%28 = call i32 @p_free(i8* %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%30 = call i8* @p_malloc(i32 40)
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = icmp ne i8* %31, null
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 (...) @test_malloc_cleanup()
%37 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@str = [30 x i8] c"\09Start test_memory_not_freed\00"
@str.5 = [35 x i8] c"\09\09Expect: malloc was already freed\00"
@str.6 = [19 x i8] c"\09\09Expect: no error\00"
@str.7 = [42 x i8] c"\09\09Expect: malloc and were not freed\00"
@str.8 = [30 x i8] c"\09Finish test_memory_not_freed\00"
define void @test_memory_not_freed() {
%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 (...) @test_malloc_startup()
%3 = tail call i8* @p_malloc(i32 10)
%4 = tail call i8* @p_malloc(i32 20)
%5 = icmp ne i8* %3, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp ne i8* %4, null
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = tail call i32 @p_free(i8* %4)
%12 = tail call i8* @p_malloc(i32 30)
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @p_free(i8* %12)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.5, i64 0, i64 0))
%18 = tail call i32 @p_free(i8* %4)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%20 = tail call i8* @p_malloc(i32 40)
%21 = icmp ne i8* %20, null
%22 = zext i1 %21 to i32
%23 = tail call i32 @assert(i32 %22)
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.7, i64 0, i64 0))
%25 = tail call i32 (...) @test_malloc_cleanup()
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.8, i64 0, i64 0))
ret void
}