#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_free_twice(void) {
  char *p;
  char *p2;
  char *p3;
  printf("\tStart  test_free_twice\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");
  p_free(p);
  test_malloc_cleanup();
  printf("\tFinish test_free_twice\n");
}


x86 assembly (clang, -O0):
<test_free_twice>:
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    0xe25(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <p_free@plt>
lea    0xe34(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1080 <p_free@plt>
mov    $0x0,%al
call   1040 <test_malloc_cleanup@plt>
lea    0xe2c(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_free_twice>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%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,%r14
mov    $0x14,%edi
call   1070 <p_malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1060 <assert@plt>
mov    %r15,%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    0xe38(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1080 <p_free@plt>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1080 <p_free@plt>
xor    %eax,%eax
call   1040 <test_malloc_cleanup@plt>
lea    0xe3f(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"\09Start test_free_twice\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 = [25 x i8] c"\09Finish test_free_twice\0A\00"
define void @test_free_twice() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%5 = call i32 (...) @test_malloc_startup()
%6 = call i8* @p_malloc(i32 10)
store i8* %6, i8** %1
%7 = call i8* @p_malloc(i32 20)
store i8* %7, i8** %2
%8 = load i8*, i8** %1
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %2
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %2
%17 = call i32 @p_free(i8* %16)
%18 = call i8* @p_malloc(i32 30)
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i8*, i8** %3
%24 = call i32 @p_free(i8* %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%26 = load i8*, i8** %2
%27 = call i32 @p_free(i8* %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%29 = load i8*, i8** %1
%30 = call i32 @p_free(i8* %29)
%31 = call i32 (...) @test_malloc_cleanup()
%32 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@str = [24 x i8] c"\09Start test_free_twice\00"
@str.4 = [35 x i8] c"\09\09Expect: malloc was already freed\00"
@str.5 = [19 x i8] c"\09\09Expect: no error\00"
@str.6 = [24 x i8] c"\09Finish test_free_twice\00"
define void @test_free_twice() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 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.4, 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.5, i64 0, i64 0))
%20 = tail call i32 @p_free(i8* %3)
%21 = tail call i32 (...) @test_malloc_cleanup()
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.6, i64 0, i64 0))
ret void
}