#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*/ free(char *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ test_free_null_ptr();
int main() {
  void *p[20];
  int i;
  char c;
  char *x;
  test_free_null_ptr();
  for (i = 0; i < 20; i++) {
    p[i] = malloc(20 - i);
    free(p[i]);
  }
  for (i = 0; i < 20; i++) {
    c = *(((char *)p[i]) + 3);
  }
  x = malloc(64);
  free(x);
  *(x + 32) = 0xe;
  x = malloc(2);
  free(x);
  *(x + 2) = 0xe;
  printf("all done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <test_free_null_ptr@plt>
movl   $0x0,-0xb4(%rbp)
cmpl   $0x14,-0xb4(%rbp)
jge    11ba <main+0x7a>
mov    $0x14,%edi
sub    -0xb4(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
movslq -0xb4(%rbp),%rax
mov    %rcx,-0xb0(%rbp,%rax,8)
ff
movslq -0xb4(%rbp),%rax
mov    -0xb0(%rbp,%rax,8),%rdi
ff
call   1030 <free@plt>
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    1163 <main+0x23>
movl   $0x0,-0xb4(%rbp)
cmpl   $0x14,-0xb4(%rbp)
jge    11fd <main+0xbd>
movslq -0xb4(%rbp),%rax
mov    -0xb0(%rbp,%rax,8),%rax
ff
mov    0x3(%rax),%al
mov    %al,-0xb5(%rbp)
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    11c4 <main+0x84>
mov    $0x40,%edi
call   1050 <malloc@plt>
mov    %rax,-0xc0(%rbp)
mov    -0xc0(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc0(%rbp),%rax
movb   $0xe,0x20(%rax)
mov    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,-0xc0(%rbp)
mov    -0xc0(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc0(%rbp),%rax
movb   $0xe,0x2(%rax)
lea    0xdac(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <test_free_null_ptr@plt>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x13,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x12,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x11,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0xf,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0xe,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0xd,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0xb,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x9,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x8,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x7,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x6,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x5,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x3,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x40,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
mov    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
call   1030 <free@plt>
lea    0xd25(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"all done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [20 x i8*]
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i32 (...) @test_free_null_ptr()
store i32 0, i32* %3
br label %7
7: ; preds = %22, %0
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 20
br i1 %9, label %10, label %25
10: ; preds = %7
%11 = load i32, i32* %3
%12 = sub nsw i32 20, %11
%13 = call i8* @malloc(i32 %12)
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %15
store i8* %13, i8** %16
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %18
%20 = load i8*, i8** %19
%21 = call i32 @free(i8* %20)
br label %22
22: ; preds = %10
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %7
25: ; preds = %7
store i32 0, i32* %3
br label %26
26: ; preds = %36, %25
%27 = load i32, i32* %3
%28 = icmp slt i32 %27, 20
br i1 %28, label %29, label %39
29: ; preds = %26
%30 = load i32, i32* %3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %31
%33 = load i8*, i8** %32
%34 = getelementptr inbounds i8, i8* %33, i64 3
%35 = load i8, i8* %34
store i8 %35, i8* %4
br label %36
36: ; preds = %29
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 1
store i32 %38, i32* %3
br label %26
39: ; preds = %26
%40 = call i8* @malloc(i32 64)
store i8* %40, i8** %5
%41 = load i8*, i8** %5
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 32
store i8 14, i8* %44
%45 = call i8* @malloc(i32 2)
store i8* %45, i8** %5
%46 = load i8*, i8** %5
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i64 2
store i8 14, i8* %49
%50 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@str = [9 x i8] c"all done\00"
define i32 @main() {
%1 = tail call i32 (...) @test_free_null_ptr()
%2 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%3 = tail call i32 @free(i8* %2)
%4 = tail call dereferenceable_or_null(19) i8* @malloc(i32 19)
%5 = tail call i32 @free(i8* %4)
%6 = tail call dereferenceable_or_null(18) i8* @malloc(i32 18)
%7 = tail call i32 @free(i8* %6)
%8 = tail call dereferenceable_or_null(17) i8* @malloc(i32 17)
%9 = tail call i32 @free(i8* %8)
%10 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
%11 = tail call i32 @free(i8* %10)
%12 = tail call dereferenceable_or_null(15) i8* @malloc(i32 15)
%13 = tail call i32 @free(i8* %12)
%14 = tail call dereferenceable_or_null(14) i8* @malloc(i32 14)
%15 = tail call i32 @free(i8* %14)
%16 = tail call dereferenceable_or_null(13) i8* @malloc(i32 13)
%17 = tail call i32 @free(i8* %16)
%18 = tail call dereferenceable_or_null(12) i8* @malloc(i32 12)
%19 = tail call i32 @free(i8* %18)
%20 = tail call dereferenceable_or_null(11) i8* @malloc(i32 11)
%21 = tail call i32 @free(i8* %20)
%22 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
%23 = tail call i32 @free(i8* %22)
%24 = tail call dereferenceable_or_null(9) i8* @malloc(i32 9)
%25 = tail call i32 @free(i8* %24)
%26 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%27 = tail call i32 @free(i8* %26)
%28 = tail call dereferenceable_or_null(7) i8* @malloc(i32 7)
%29 = tail call i32 @free(i8* %28)
%30 = tail call dereferenceable_or_null(6) i8* @malloc(i32 6)
%31 = tail call i32 @free(i8* %30)
%32 = tail call dereferenceable_or_null(5) i8* @malloc(i32 5)
%33 = tail call i32 @free(i8* %32)
%34 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%35 = tail call i32 @free(i8* %34)
%36 = tail call dereferenceable_or_null(3) i8* @malloc(i32 3)
%37 = tail call i32 @free(i8* %36)
%38 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
%39 = tail call i32 @free(i8* %38)
%40 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%41 = tail call i32 @free(i8* %40)
%42 = tail call dereferenceable_or_null(64) i8* @malloc(i32 64)
%43 = tail call i32 @free(i8* %42)
%44 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
%45 = tail call i32 @free(i8* %44)
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
ret i32 0
}