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

/* Variables and functions */
int RAND_MAX;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int abs(int);
int atoi(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
unsigned long long random();
int /*<<< orphan*/ srandom(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int main(int argc, char **argv) {
  FILE *f;
  int count, maxburst, length;
  if (argc < 4) {
    printf("USAGE: trasher <filename> <count> <maxburst>\n");
    return 1;
  }
  f = fopen(argv[1], "rb+");
  if (!f) {
    perror(argv[1]);
    return 2;
  }
  count = atoi(argv[2]);
  maxburst = atoi(argv[3]);
  srandom(time(0));
  fseek(f, 0, SEEK_END);
  length = ftell(f);
  fseek(f, 0, SEEK_SET);
  while (count--) {
    int burst = 1 + random() * (uint64_t)(abs(maxburst) - 1) / RAND_MAX;
    int pos = random() * (uint64_t)length / RAND_MAX;
    fseek(f, pos, SEEK_SET);
    if (maxburst < 0)
      burst = -maxburst;
    if (pos + burst > length)
      continue;
    while (burst--) {
      int val = random() * 256ULL / RAND_MAX;
      if (maxburst < 0)
        val = 0;
      fwrite(&val, 1, 1, f);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11e8 <main+0x38>
lea    0xe29(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x1eb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe37(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1224 <main+0x74>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    139b <main+0x1eb>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x20(%rbp)
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
je     1394 <main+0x1e4>
mov    $0x0,%al
call   1080 <random@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <abs@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
imul   %rcx,%rax
mov    0x2d1c(%rip),%rcx
movslq (%rcx),%rcx
xor    %edx,%edx
div    %rcx
add    $0x1,%rax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <random@plt>
movslq -0x24(%rbp),%rcx
imul   %rcx,%rax
mov    0x2cf7(%rip),%rcx
movslq (%rcx),%rcx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2cce(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
cmpl   $0x0,-0x20(%rbp)
jge    131b <main+0x16b>
xor    %eax,%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    132f <main+0x17f>
jmp    1286 <main+0xd6>
jmp    1334 <main+0x184>
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x28(%rbp)
cmp    $0x0,%eax
je     138f <main+0x1df>
mov    $0x0,%al
call   1080 <random@plt>
shl    $0x8,%rax
mov    0x2c86(%rip),%rcx
movslq (%rcx),%rcx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1376 <main+0x1c6>
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0x30(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10d0 <fwrite@plt>
jmp    1334 <main+0x184>
jmp    1286 <main+0xd6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x3,%edi
jg     11c9 <main+0x29>
lea    0xe4a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
jmp    1374 <main+0x1d4>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe29(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1385 <main+0x1e5>
mov    %rax,%r12
mov    0x10(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
mov    0x18(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r13d
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1030 <srandom@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1060 <ftell@plt>
mov    %eax,%ebp
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r14d,0xc(%rsp)
test   %r14d,%r14d
je     1374 <main+0x1d4>
mov    %r13d,%ecx
neg    %ecx
mov    %r13d,%eax
mov    %ecx,0x10(%rsp)
cmovns %ecx,%eax
add    $0xffffffff,%eax
cltq
mov    %rax,0x20(%rsp)
mov    %ebp,0x14(%rsp)
movslq %ebp,%rax
mov    %rax,0x18(%rsp)
xor    %r15d,%r15d
mov    0x2d63(%rip),%r14
jmp    128b <main+0xeb>
nop
cmpl   $0x0,0xc(%rsp)
je     1372 <main+0x1d2>
xor    %eax,%eax
call   1080 <random@plt>
imul   0x20(%rsp),%rax
movslq (%r14),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     12c0 <main+0x120>
xor    %edx,%edx
div    %rcx
mov    %rax,%rbx
jmp    12c6 <main+0x126>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edx,%edx
div    %ecx
mov    %eax,%ebx
add    $0x1,%ebx
xor    %eax,%eax
call   1080 <random@plt>
imul   0x18(%rsp),%rax
movslq (%r14),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     12f0 <main+0x150>
xor    %edx,%edx
div    %rcx
mov    %rax,%rbp
jmp    12f6 <main+0x156>
nop
xor    %edx,%edx
div    %ecx
mov    %eax,%ebp
addl   $0xffffffff,0xc(%rsp)
mov    0x2cce(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %ebp,%esi
call   1090 <fseek@plt>
test   %r13d,%r13d
cmovs  0x10(%rsp),%ebx
add    %ebx,%ebp
cmp    0x14(%rsp),%ebp
jle    1347 <main+0x1a7>
jmp    1280 <main+0xe0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edx,%edx
div    %rcx
test   %r13d,%r13d
movsbl %al,%edi
cmovs  %r15d,%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
test   %ebx,%ebx
je     1280 <main+0xe0>
add    $0xffffffff,%ebx
xor    %eax,%eax
call   1080 <random@plt>
shl    $0x8,%rax
movslq (%r14),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
jne    1330 <main+0x190>
xor    %edx,%edx
div    %ecx
jmp    1335 <main+0x195>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
call   10b0 <perror@plt>
mov    $0x2,%ebx
jmp    1374 <main+0x1d4>

LLVM IR (-O0):
@.str = [46 x i8] c"USAGE: trasher <filename> <count> <maxburst>\0A\00"
@.str.1 = [4 x i8] c"rb+\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@RAND_MAX = global i32 0
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
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %108
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %29, label %24
24: ; preds = %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @perror(i8* %27)
store i32 2, i32* %3
br label %108
29: ; preds = %17
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %7
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 3
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %8
%38 = call i32 @time(i32 0)
%39 = call i32 @srandom(i32 %38)
%40 = load i32*, i32** %6
%41 = load i32, i32* @SEEK_END
%42 = call i32 @fseek(i32* %40, i32 0, i32 %41)
%43 = load i32*, i32** %6
%44 = call i32 @ftell(i32* %43)
store i32 %44, i32* %9
%45 = load i32*, i32** %6
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @fseek(i32* %45, i32 0, i32 %46)
br label %48
48: ; preds = %106, %87, %29
%49 = load i32, i32* %7
%50 = add nsw i32 %49, -1
store i32 %50, i32* %7
%51 = icmp ne i32 %49, 0
br i1 %51, label %52, label %107
52: ; preds = %48
%53 = call i64 (...) @random()
%54 = load i32, i32* %8
%55 = call i32 @abs(i32 %54)
%56 = sub nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = mul i64 %53, %57
%59 = load i32, i32* @RAND_MAX
%60 = sext i32 %59 to i64
%61 = udiv i64 %58, %60
%62 = add i64 1, %61
%63 = trunc i64 %62 to i32
store i32 %63, i32* %10
%64 = call i64 (...) @random()
%65 = load i32, i32* %9
%66 = sext i32 %65 to i64
%67 = mul i64 %64, %66
%68 = load i32, i32* @RAND_MAX
%69 = sext i32 %68 to i64
%70 = udiv i64 %67, %69
%71 = trunc i64 %70 to i32
store i32 %71, i32* %11
%72 = load i32*, i32** %6
%73 = load i32, i32* %11
%74 = load i32, i32* @SEEK_SET
%75 = call i32 @fseek(i32* %72, i32 %73, i32 %74)
%76 = load i32, i32* %8
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %52
%79 = load i32, i32* %8
%80 = sub nsw i32 0, %79
store i32 %80, i32* %10
br label %81
81: ; preds = %78, %52
%82 = load i32, i32* %11
%83 = load i32, i32* %10
%84 = add nsw i32 %82, %83
%85 = load i32, i32* %9
%86 = icmp sgt i32 %84, %85
br i1 %86, label %87, label %88
87: ; preds = %81
br label %48
88: ; preds = %81
br label %89
89: ; preds = %103, %88
%90 = load i32, i32* %10
%91 = add nsw i32 %90, -1
store i32 %91, i32* %10
%92 = icmp ne i32 %90, 0
br i1 %92, label %93, label %106
93: ; preds = %89
%94 = call i64 (...) @random()
%95 = mul i64 %94, 256
%96 = load i32, i32* @RAND_MAX
%97 = sext i32 %96 to i64
%98 = udiv i64 %95, %97
%99 = trunc i64 %98 to i32
store i32 %99, i32* %12
%100 = load i32, i32* %8
%101 = icmp slt i32 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %93
store i32 0, i32* %12
br label %103
103: ; preds = %102, %93
%104 = load i32*, i32** %6
%105 = call i32 @fwrite(i32* %12, i32 1, i32 1, i32* %104)
br label %89
106: ; preds = %89
br label %48
107: ; preds = %48
store i32 0, i32* %3
br label %108
108: ; preds = %107, %24, %15
%109 = load i32, i32* %3
ret i32 %109
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"rb+\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@RAND_MAX = global i32 0
@str = [45 x i8] c"USAGE: trasher <filename> <count> <maxburst>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
br label %75
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %7
%13 = tail call i32 @perror(i8* %12)
br label %75
14: ; preds = %6
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = tail call i32 @time(i32 0)
%22 = tail call i32 @srandom(i32 %21)
%23 = load i32, i32* @SEEK_END
%24 = tail call i32 @fseek(i32* %9, i32 0, i32 %23)
%25 = tail call i32 @ftell(i32* %9)
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @fseek(i32* %9, i32 0, i32 %26)
%28 = icmp eq i32 %17, 0
br i1 %28, label %75, label %29
29: ; preds = %14
%30 = tail call i32 @llvm.abs.i32(i32 %20, i1 true)
%31 = add nsw i32 %30, -1
%32 = sext i32 %31 to i64
%33 = sext i32 %25 to i64
%34 = icmp slt i32 %20, 0
%35 = sub nsw i32 0, %20
br label %36
36: ; preds = %29, %73
%37 = phi i32 [ %17, %29 ], [ %38, %73 ]
%38 = add nsw i32 %37, -1
%39 = tail call i64 (...) @random()
%40 = mul i64 %39, %32
%41 = load i32, i32* @RAND_MAX
%42 = sext i32 %41 to i64
%43 = udiv i64 %40, %42
%44 = trunc i64 %43 to i32
%45 = add i32 %44, 1
%46 = tail call i64 (...) @random()
%47 = mul i64 %46, %33
%48 = load i32, i32* @RAND_MAX
%49 = sext i32 %48 to i64
%50 = udiv i64 %47, %49
%51 = trunc i64 %50 to i32
%52 = load i32, i32* @SEEK_SET
%53 = tail call i32 @fseek(i32* %9, i32 %51, i32 %52)
%54 = select i1 %34, i32 %35, i32 %45
%55 = add nsw i32 %54, %51
%56 = icmp sgt i32 %55, %25
%57 = icmp eq i32 %54, 0
%58 = or i1 %56, %57
br i1 %58, label %73, label %59
59: ; preds = %36, %59
%60 = phi i32 [ %61, %59 ], [ %54, %36 ]
%61 = add nsw i32 %60, -1
%62 = tail call i64 (...) @random()
%63 = shl i64 %62, 8
%64 = load i32, i32* @RAND_MAX
%65 = sext i32 %64 to i64
%66 = udiv i64 %63, %65
%67 = trunc i64 %66 to i32
%68 = shl i32 %67, 24
%69 = ashr exact i32 %68, 24
%70 = select i1 %34, i32 0, i32 %69
%71 = tail call i32 @fputc(i32 %70, i32* %9)
%72 = icmp eq i32 %61, 0
br i1 %72, label %73, label %59
73: ; preds = %59, %36
%74 = icmp eq i32 %38, 0
br i1 %74, label %75, label %36
75: ; preds = %73, %14, %11, %4
%76 = phi i32 [ 1, %4 ], [ 2, %11 ], [ 0, %14 ], [ 0, %73 ]
ret i32 %76
}