#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(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  int *p = (int *)malloc(sizeof(int));
  *p = 123;
  printf("Hello use after free\n");
  free(p);
  for (int i = 0; i < 1000; i++) {
    volatile int q = 1.0 * 1.0;
    ((void)(q));
  }
  *p = 456;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x7b,(%rax)
lea    0xe9b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3e8,-0x1c(%rbp)
jge    119f <main+0x6f>
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117a <main+0x4a>
mov    -0x18(%rbp),%rax
movl   $0x1c8,(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
movl   $0x7b,(%rax)
lea    0xeb1(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x3e8,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%ecx
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%ecx
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%ecx
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%ecx
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%ecx
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%ecx
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%ecx
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%ecx
add    $0xfffffff8,%eax
jne    1170 <main+0x40>
movl   $0x1c8,(%rbx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Hello use after free\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 @malloc(i32 4)
%10 = inttoptr i64 %9 to i32*
store i32* %10, i32** %6
%11 = load i32*, i32** %6
store i32 123, i32* %11
%12 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = load i32*, i32** %6
%14 = call i32 @free(i32* %13)
store i32 0, i32* %7
br label %15
15: ; preds = %20, %2
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 1000
br i1 %17, label %18, label %23
18: ; preds = %15
store volatile i32 1, i32* %8
%19 = load volatile i32, i32* %8
br label %20
20: ; preds = %18
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
store i32 %22, i32* %7
br label %15
23: ; preds = %15
%24 = load i32*, i32** %6
store i32 456, i32* %24
ret i32 0
}

LLVM IR (-O2):
@str = [21 x i8] c"Hello use after free\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i64 @malloc(i32 4)
%5 = inttoptr i64 %4 to i32*
store i32 123, i32* %5
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @free(i32* %5)
%8 = bitcast i32* %3 to i8*
br label %10
9: ; preds = %10
store i32 456, i32* %5
ret i32 0
10: ; preds = %10, %2
%11 = phi i32 [ 0, %2 ], [ %20, %10 ]
store volatile i32 1, i32* %3
%12 = load volatile i32, i32* %3
store volatile i32 1, i32* %3
%13 = load volatile i32, i32* %3
store volatile i32 1, i32* %3
%14 = load volatile i32, i32* %3
store volatile i32 1, i32* %3
%15 = load volatile i32, i32* %3
store volatile i32 1, i32* %3
%16 = load volatile i32, i32* %3
store volatile i32 1, i32* %3
%17 = load volatile i32, i32* %3
store volatile i32 1, i32* %3
%18 = load volatile i32, i32* %3
store volatile i32 1, i32* %3
%19 = load volatile i32, i32* %3
%20 = add nuw nsw i32 %11, 8
%21 = icmp eq i32 %20, 1000
br i1 %21, label %9, label %10
}