#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);
int /*<<< orphan*/ rand();
int read_and_write(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  srand(time(NULL));
  int buff_size = 10;
  int *buff = NULL;
  buff = (int *)malloc(sizeof(int) * buff_size);
  printf("First error, go beyond memory boundaries {%d}\n",
         read_and_write(buff, buff_size + 10, rand()));
  free(buff);
  buff = (int *)malloc(sizeof(int) * buff_size);
  printf("Second error, forget to free memory {%d}\n",
         read_and_write(buff, buff_size, rand()));
  buff = (int *)malloc(sizeof(int) * buff_size);
  printf("Third error, free memory twice {%d}\n",
         read_and_write(buff, buff_size, rand()));
  free(buff);
  free(buff);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
movl   $0xa,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1090 <rand@plt>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    %eax,%edx
call   1050 <read_and_write@plt>
mov    %eax,%esi
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1090 <rand@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   1050 <read_and_write@plt>
mov    %eax,%esi
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1090 <rand@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    %eax,%edx
call   1050 <read_and_write@plt>
mov    %eax,%esi
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x28,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <rand@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
mov    %eax,%edx
call   1050 <read_and_write@plt>
lea    0xe57(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x28,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <rand@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %eax,%edx
call   1050 <read_and_write@plt>
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x28,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <rand@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %eax,%edx
call   1050 <read_and_write@plt>
lea    0xe46(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [47 x i8] c"First error, go beyond memory boundaries {%d}\0A\00"
@.str.1 = [42 x i8] c"Second error, forget to free memory {%d}\0A\00"
@.str.2 = [37 x i8] c"Third error, free memory twice {%d}\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @time(i32* null)
%9 = call i32 @srand(i32 %8)
store i32 10, i32* %6
store i32* null, i32** %7
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = mul i64 4, %11
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = load i32, i32* %6
%18 = add nsw i32 %17, 10
%19 = call i32 (...) @rand()
%20 = call i32 @read_and_write(i32* %16, i32 %18, i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32*, i32** %7
%23 = call i32 @free(i32* %22)
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = mul i64 4, %25
%27 = trunc i64 %26 to i32
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i32*
store i32* %29, i32** %7
%30 = load i32*, i32** %7
%31 = load i32, i32* %6
%32 = call i32 (...) @rand()
%33 = call i32 @read_and_write(i32* %30, i32 %31, i32 %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = mul i64 4, %36
%38 = trunc i64 %37 to i32
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i32*
store i32* %40, i32** %7
%41 = load i32*, i32** %7
%42 = load i32, i32* %6
%43 = call i32 (...) @rand()
%44 = call i32 @read_and_write(i32* %41, i32 %42, i32 %43)
%45 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = load i32*, i32** %7
%47 = call i32 @free(i32* %46)
%48 = load i32*, i32** %7
%49 = call i32 @free(i32* %48)
ret i32 0
}

LLVM IR (-O2):
@.str = [47 x i8] c"First error, go beyond memory boundaries {%d}\0A\00"
@.str.1 = [42 x i8] c"Second error, forget to free memory {%d}\0A\00"
@.str.2 = [37 x i8] c"Third error, free memory twice {%d}\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @time(i32* null)
%4 = tail call i32 @srand(i32 %3)
%5 = tail call i64 @malloc(i32 40)
%6 = inttoptr i64 %5 to i32*
%7 = tail call i32 (...) @rand()
%8 = tail call i32 @read_and_write(i32* %6, i32 20, i32 %7)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 @free(i32* %6)
%11 = tail call i64 @malloc(i32 40)
%12 = inttoptr i64 %11 to i32*
%13 = tail call i32 (...) @rand()
%14 = tail call i32 @read_and_write(i32* %12, i32 10, i32 %13)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = tail call i64 @malloc(i32 40)
%17 = inttoptr i64 %16 to i32*
%18 = tail call i32 (...) @rand()
%19 = tail call i32 @read_and_write(i32* %17, i32 10, i32 %18)
%20 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = tail call i32 @free(i32* %17)
%22 = tail call i32 @free(i32* %17)
ret i32 0
}