#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printBuffer(char *, size_t);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
void bufferOverReadUsingHeap(char *input, size_t length) {
  char *buf = (char *)malloc(sizeof(
      "This is a very secret message which should not be exposed to public. "
      "This is a very secret message which should not be exposed to public. "
      "This is a very secret message which should not be exposed to public. "
      "This is a very secret message which should not be exposed to public."));
  strcpy(
      buf,
      "This is a very secret message which should not be exposed to public. "
      "This is a very secret message which should not be exposed to public. "
      "This is a very secret message which should not be exposed to public. "
      "This is a very secret message which should not be exposed to public.");
  char *buf2 = (char *)malloc(sizeof(buf));
  memcpy(buf2, buf, sizeof(buf));
  free(buf);
  free(buf2);
  char *buffer = (char *)malloc(length);
  strcpy(buffer, input);
  printf("Buffer on Heap:\n");
  printBuffer(buffer, length);
}


x86 assembly (clang, -O0):
<bufferOverReadUsingHeap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x114,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1040 <strcpy@plt>
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
lea    0xf33(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <printBuffer@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufferOverReadUsingHeap>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x114,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe7e(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
lea    0xf40(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1080 <printBuffer@plt>

LLVM IR (-O0):
@.str = [276 x i8] c"This is a very secret message which should not be exposed to public. This is a very secret message which should not be exposed to public. This is a very secret message which should not be exposed to public. This is a very secret message which should not be exposed to public.\00"
@.str.1 = [17 x i8] c"Buffer on Heap:\0A\00"
define void @bufferOverReadUsingHeap(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = call i64 @malloc(i64 276)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([276 x i8], [276 x i8]* @.str, i64 0, i64 0))
%12 = call i64 @malloc(i64 8)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = load i8*, i8** %5
%16 = call i32 @memcpy(i8* %14, i8* %15, i32 8)
%17 = load i8*, i8** %5
%18 = call i32 @free(i8* %17)
%19 = load i8*, i8** %6
%20 = call i32 @free(i8* %19)
%21 = load i64, i64* %4
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = load i8*, i8** %3
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%28 = load i8*, i8** %7
%29 = load i64, i64* %4
%30 = call i32 @printBuffer(i8* %28, i64 %29)
ret void
}

LLVM IR (-O2):
@.str = [276 x i8] c"This is a very secret message which should not be exposed to public. This is a very secret message which should not be exposed to public. This is a very secret message which should not be exposed to public. This is a very secret message which should not be exposed to public.\00"
@str = [16 x i8] c"Buffer on Heap:\00"
define void @bufferOverReadUsingHeap(i8* %0, i64 %1) {
%3 = tail call i64 @malloc(i64 276)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([276 x i8], [276 x i8]* @.str, i64 0, i64 0))
%6 = tail call i64 @malloc(i64 8)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @memcpy(i8* %7, i8* %4, i32 8)
%9 = tail call i32 @free(i8* %4)
%10 = tail call i32 @free(i8* %7)
%11 = tail call i64 @malloc(i64 %1)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @strcpy(i8* %12, i8* %0)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @printBuffer(i8* %12, i64 %1)
ret void
}