#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_malloc_guard(void) {
  char *p;
  char *p2;
  char *p3;
  char *p4;
  printf("\tStart  test_malloc_guard\n");
  test_malloc_startup();
  p = p_malloc(3);
  p2 = p_malloc(3);
  p3 = p_malloc(3);
  p4 = p_malloc(3);
  assert(p != 0);
  assert(p2 != 0);
  assert(p3 != 0);
  assert(p4 != 0);
  p2[-4] = 'x';
  p3[6] = 'a';
  p4[-1] = '5';
  p4[3] = '7';
  p_free(p);
  printf("\t\tExpect: #1 prefix corrupted, #2 postfix corrupted, #3 both\n");
  p_free(p2);
  p_free(p3);
  p_free(p4);
  test_malloc_cleanup();
  printf("\tFinish test_malloc_guard\n");
}


x86 assembly (clang, -O0):
<test_malloc_guard>:
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    $0x3,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x3,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x3,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x3,%edi
call   1070 <p_malloc@plt>
mov    %rax,-0x20(%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>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
movb   $0x78,-0x4(%rax)
mov    -0x18(%rbp),%rax
movb   $0x61,0x6(%rax)
mov    -0x20(%rbp),%rax
movb   $0x35,-0x1(%rax)
mov    -0x20(%rbp),%rax
movb   $0x37,0x3(%rax)
mov    -0x8(%rbp),%rdi
call   1080 <p_free@plt>
lea    0xdf0(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <p_free@plt>
mov    -0x18(%rbp),%rdi
call   1080 <p_free@plt>
mov    -0x20(%rbp),%rdi
call   1080 <p_free@plt>
mov    $0x0,%al
call   1040 <test_malloc_cleanup@plt>
lea    0xe00(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_malloc_guard>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <test_malloc_startup@plt>
mov    $0x3,%edi
call   1070 <p_malloc@plt>
mov    %rax,%r12
mov    $0x3,%edi
call   1070 <p_malloc@plt>
mov    %rax,%r14
mov    $0x3,%edi
call   1070 <p_malloc@plt>
mov    %rax,%r15
mov    $0x3,%edi
call   1070 <p_malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %r12,%r12
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
movb   $0x78,-0x4(%r14)
movb   $0x61,0x6(%r15)
movb   $0x35,-0x1(%rbx)
movb   $0x37,0x3(%rbx)
mov    %r12,%rdi
call   1080 <p_free@plt>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1080 <p_free@plt>
mov    %r15,%rdi
call   1080 <p_free@plt>
mov    %rbx,%rdi
call   1080 <p_free@plt>
xor    %eax,%eax
call   1040 <test_malloc_cleanup@plt>
lea    0xe24(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"\09Start test_malloc_guard\0A\00"
@.str.1 = [62 x i8] c"\09\09Expect: prefix corrupted, postfix corrupted, both\0A\00"
@.str.2 = [27 x i8] c"\09Finish test_malloc_guard\0A\00"
define void @test_malloc_guard() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (...) @test_malloc_startup()
%7 = call i8* @p_malloc(i32 3)
store i8* %7, i8** %1
%8 = call i8* @p_malloc(i32 3)
store i8* %8, i8** %2
%9 = call i8* @p_malloc(i32 3)
store i8* %9, i8** %3
%10 = call i8* @p_malloc(i32 3)
store i8* %10, i8** %4
%11 = load i8*, i8** %1
%12 = icmp ne i8* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %2
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%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** %4
%24 = icmp ne i8* %23, null
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i8*, i8** %2
%28 = getelementptr inbounds i8, i8* %27, i64 -4
store i8 120, i8* %28
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 6
store i8 97, i8* %30
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 -1
store i8 53, i8* %32
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 3
store i8 55, i8* %34
%35 = load i8*, i8** %1
%36 = call i32 @p_free(i8* %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%38 = load i8*, i8** %2
%39 = call i32 @p_free(i8* %38)
%40 = load i8*, i8** %3
%41 = call i32 @p_free(i8* %40)
%42 = load i8*, i8** %4
%43 = call i32 @p_free(i8* %42)
%44 = call i32 (...) @test_malloc_cleanup()
%45 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@str = [26 x i8] c"\09Start test_malloc_guard\00"
@str.3 = [61 x i8] c"\09\09Expect: prefix corrupted, postfix corrupted, both\00"
@str.4 = [26 x i8] c"\09Finish test_malloc_guard\00"
define void @test_malloc_guard() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @test_malloc_startup()
%3 = tail call i8* @p_malloc(i32 3)
%4 = tail call i8* @p_malloc(i32 3)
%5 = tail call i8* @p_malloc(i32 3)
%6 = tail call i8* @p_malloc(i32 3)
%7 = icmp ne i8* %3, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i8* %4, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = icmp ne i8* %5, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = icmp ne i8* %6, null
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = getelementptr inbounds i8, i8* %4, i64 -4
store i8 120, i8* %19
%20 = getelementptr inbounds i8, i8* %5, i64 6
store i8 97, i8* %20
%21 = getelementptr inbounds i8, i8* %6, i64 -1
store i8 53, i8* %21
%22 = getelementptr inbounds i8, i8* %6, i64 3
store i8 55, i8* %22
%23 = tail call i32 @p_free(i8* %3)
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.3, i64 0, i64 0))
%25 = tail call i32 @p_free(i8* %4)
%26 = tail call i32 @p_free(i8* %5)
%27 = tail call i32 @p_free(i8* %6)
%28 = tail call i32 (...) @test_malloc_cleanup()
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.4, i64 0, i64 0))
ret void
}