void test_free_without_malloc(void) {
  int test = 0;
  char *p;
  char *p2;
  char *p3;
  printf("\tStart  test_free_without_malloc\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);
  p_free(0);
  printf("\t\tExpect: free a pointer that was never allocated\n");
  p_free(&test);
  printf("\t\tExpect: no error\n");
  p_free(p);
  test_malloc_cleanup();
  printf("\tFinish test_free_without_malloc\n");
}


x86 assembly (clang, -O0):
<test_free_without_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <test_malloc_startup@plt>
mov    $0xa,%edi
mov    $0x0,%al
call   1070 <p_malloc@plt>
cltq
mov    %rax,-0x10(%rbp)
mov    $0x14,%edi
mov    $0x0,%al
call   1070 <p_malloc@plt>
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1060 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1080 <p_free@plt>
mov    $0x1e,%edi
mov    $0x0,%al
call   1070 <p_malloc@plt>
cltq
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1060 <assert@plt>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1080 <p_free@plt>
xor    %edi,%edi
mov    $0x0,%al
call   1080 <p_free@plt>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x4(%rbp),%rdi
mov    $0x0,%al
call   1080 <p_free@plt>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1080 <p_free@plt>
mov    $0x0,%al
call   1040 <test_malloc_cleanup@plt>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_free_without_malloc>:
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <test_malloc_startup@plt>
mov    $0xa,%edi
xor    %eax,%eax
call   1070 <p_malloc@plt>
movslq %eax,%r14
mov    $0x14,%edi
xor    %eax,%eax
call   1070 <p_malloc@plt>
movslq %eax,%rbx
xor    %edi,%edi
test   %r14d,%r14d
setne  %dil
xor    %eax,%eax
call   1060 <assert@plt>
xor    %edi,%edi
test   %ebx,%ebx
setne  %dil
xor    %eax,%eax
call   1060 <assert@plt>
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <p_free@plt>
mov    $0x1e,%edi
xor    %eax,%eax
call   1070 <p_malloc@plt>
movslq %eax,%rbx
xor    %edi,%edi
test   %ebx,%ebx
setne  %dil
xor    %eax,%eax
call   1060 <assert@plt>
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <p_free@plt>
xor    %edi,%edi
xor    %eax,%eax
call   1080 <p_free@plt>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
xor    %eax,%eax
call   1080 <p_free@plt>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <p_free@plt>
xor    %eax,%eax
call   1040 <test_malloc_cleanup@plt>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@str = [33 x i8] c"\09Start test_free_without_malloc\00"
@str.4 = [50 x i8] c"\09\09Expect: free a pointer that was never allocated\00"
@str.5 = [19 x i8] c"\09\09Expect: no error\00"
@str.6 = [33 x i8] c"\09Finish test_free_without_malloc\00"
define void @test_free_without_malloc() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @test_malloc_startup()
%5 = tail call i32 (i32, ...) bitcast (i32 (...)* @p_malloc to i32 (i32, ...)*)(i32 10)
%6 = sext i32 %5 to i64
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @p_malloc to i32 (i32, ...)*)(i32 20)
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i8*
%11 = icmp ne i32 %5, 0
%12 = zext i1 %11 to i32
%13 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %12)
%14 = icmp ne i32 %8, 0
%15 = zext i1 %14 to i32
%16 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %15)
%17 = tail call i32 (i8*, ...) bitcast (i32 (...)* @p_free to i32 (i8*, ...)*)(i8* %10)
%18 = tail call i32 (i32, ...) bitcast (i32 (...)* @p_malloc to i32 (i32, ...)*)(i32 30)
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to i8*
%21 = icmp ne i32 %18, 0
%22 = zext i1 %21 to i32
%23 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %22)
%24 = tail call i32 (i8*, ...) bitcast (i32 (...)* @p_free to i32 (i8*, ...)*)(i8* %20)
%25 = tail call i32 (i32, ...) bitcast (i32 (...)* @p_free to i32 (i32, ...)*)(i32 0)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.4, i64 0, i64 0))
%27 = call i32 (i32*, ...) bitcast (i32 (...)* @p_free to i32 (i32*, ...)*)(i32* %1)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%29 = call i32 (i8*, ...) bitcast (i32 (...)* @p_free to i32 (i8*, ...)*)(i8* %7)
%30 = call i32 (...) @test_malloc_cleanup()
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
ret void
}