#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*/ close(int);
int errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
size_t write(int, char *, size_t);
int writeOver(int fd, int filesize, unsigned int blockSize) {
  size_t bufSize = blockSize * sizeof(char);
  char *buffer = malloc(bufSize);
  if (!buffer) {
    close(fd);
    return -1;
  }
  int dataLeft = filesize;
  srand(time(NULL));
  while (dataLeft > 0) {
    size_t written = 0;
    int i;
    for (i = 0; i < bufSize; i++)
      buffer[i] = rand() % 256;
    if (dataLeft < bufSize) {
      written = write(fd, buffer, dataLeft);
    } else
      written = write(fd, buffer, bufSize);
    dataLeft -= written;
    if (written <= 0)
      break;
  }
  free(buffer);
  return errno;
}


x86 assembly (clang, -O0):
<writeOver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x0,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b8 <writeOver+0x48>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <writeOver+0x120>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
cmpl   $0x0,-0x24(%rbp)
jle    127b <writeOver+0x10b>
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    121d <writeOver+0xad>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11e7 <writeOver+0x77>
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1244 <writeOver+0xd4>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x30(%rbp)
jmp    1258 <writeOver+0xe8>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
movslq -0x24(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     1276 <writeOver+0x106>
jmp    127b <writeOver+0x10b>
jmp    11ce <writeOver+0x5e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeOver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r12d
mov    %edi,%r14d
mov    %edx,%ebp
mov    %rbp,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     120a <writeOver+0x9a>
mov    %rax,%r13
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
jmp    11cd <writeOver+0x5d>
cs nopw 0x0(%rax,%rax,1)
movslq %r12d,%rdx
cmp    %rbp,%rdx
cmovae %rbp,%rdx
mov    %r14d,%edi
mov    %r13,%rsi
call   1040 <write@plt>
sub    %eax,%r12d
test   %rax,%rax
je     11f7 <writeOver+0x87>
test   %r12d,%r12d
jle    11f7 <writeOver+0x87>
test   %r15d,%r15d
je     11b0 <writeOver+0x40>
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <rand@plt>
mov    %al,0x0(%r13,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11e0 <writeOver+0x70>
jmp    11b0 <writeOver+0x40>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
jmp    1217 <writeOver+0xa7>
mov    %r14d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @writeOver(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = load i32, i32* %7
%14 = zext i32 %13 to i64
%15 = mul i64 %14, 1
store i64 %15, i64* %8
%16 = load i64, i64* %8
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = icmp ne i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %3
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
store i32 -1, i32* %4
br label %77
23: ; preds = %3
%24 = load i32, i32* %6
store i32 %24, i32* %10
%25 = call i32 @time(i32* null)
%26 = call i32 @srand(i32 %25)
br label %27
27: ; preds = %72, %23
%28 = load i32, i32* %10
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %73
30: ; preds = %27
store i64 0, i64* %11
store i32 0, i32* %12
br label %31
31: ; preds = %44, %30
%32 = load i32, i32* %12
%33 = sext i32 %32 to i64
%34 = load i64, i64* %8
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %47
36: ; preds = %31
%37 = call i32 (...) @rand()
%38 = srem i32 %37, 256
%39 = trunc i32 %38 to i8
%40 = load i8*, i8** %9
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 %39, i8* %43
br label %44
44: ; preds = %36
%45 = load i32, i32* %12
%46 = add nsw i32 %45, 1
store i32 %46, i32* %12
br label %31
47: ; preds = %31
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = load i64, i64* %8
%51 = icmp ult i64 %49, %50
br i1 %51, label %52, label %58
52: ; preds = %47
%53 = load i32, i32* %5
%54 = load i8*, i8** %9
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = call i64 @write(i32 %53, i8* %54, i64 %56)
store i64 %57, i64* %11
br label %63
58: ; preds = %47
%59 = load i32, i32* %5
%60 = load i8*, i8** %9
%61 = load i64, i64* %8
%62 = call i64 @write(i32 %59, i8* %60, i64 %61)
store i64 %62, i64* %11
br label %63
63: ; preds = %58, %52
%64 = load i64, i64* %11
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = sub i64 %66, %64
%68 = trunc i64 %67 to i32
store i32 %68, i32* %10
%69 = load i64, i64* %11
%70 = icmp ule i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %63
br label %73
72: ; preds = %63
br label %27
73: ; preds = %71, %27
%74 = load i8*, i8** %9
%75 = call i32 @free(i8* %74)
%76 = load i32, i32* @errno
store i32 %76, i32* %4
br label %77
77: ; preds = %73, %20
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @writeOver(i32 %0, i32 %1, i32 %2) {
%4 = zext i32 %2 to i64
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @close(i32 %0)
br label %35
9: ; preds = %3
%10 = tail call i32 @time(i32* null)
%11 = tail call i32 @srand(i32 %10)
%12 = icmp eq i32 %2, 0
br label %13
13: ; preds = %24, %9
%14 = phi i32 [ %1, %9 ], [ %30, %24 ]
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %13
br i1 %12, label %24, label %17
17: ; preds = %16, %17
%18 = phi i64 [ %22, %17 ], [ 0, %16 ]
%19 = tail call i32 (...) @rand()
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds i8, i8* %5, i64 %18
store i8 %20, i8* %21
%22 = add nuw nsw i64 %18, 1
%23 = icmp eq i64 %22, %4
br i1 %23, label %24, label %17
24: ; preds = %17, %16
%25 = sext i32 %14 to i64
%26 = icmp ult i64 %25, %4
%27 = select i1 %26, i64 %25, i64 %4
%28 = tail call i64 @write(i32 %0, i8* %5, i64 %27)
%29 = trunc i64 %28 to i32
%30 = sub i32 %14, %29
%31 = icmp eq i64 %28, 0
br i1 %31, label %32, label %13
32: ; preds = %24, %13
%33 = tail call i32 @free(i8* %5)
%34 = load i32, i32* @errno
br label %35
35: ; preds = %32, %7
%36 = phi i32 [ %34, %32 ], [ -1, %7 ]
ret i32 %36
}