#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ lseek(int, unsigned long, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, unsigned long);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, unsigned char *, int);
int main(int argc, char **argv) {
  struct stat stat;
  int fd, cycles;
  if (argc != 3) {
    fprintf(stderr, "Usage: <filename> <cycles>\n");
    exit(1);
  }
  srand(time(NULL));
  char *filename = argv[1];
  cycles = atoi(argv[2]);
  fd = open(filename, O_RDWR);
  if (fd == -1) {
    perror("open");
    exit(1);
  }
  fstat(fd, &stat);
  while (cycles--) {
    unsigned char buf[32];
    unsigned long offset = rand() % stat.st_size;
    int writelen = 1 + rand() % 31;
    int j;
    for (j = 0; j < writelen; j++)
      buf[j] = (char)rand();
    lseek(fd, offset, SEEK_SET);
    printf("Writing %d bytes at offset %lu\n", writelen, offset);
    write(fd, buf, writelen);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ff <main+0x3f>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1260 <main+0xa0>
lea    0xdcb(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10d0 <fstat@plt>
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x20(%rbp)
cmp    $0x0,%eax
je     1315 <main+0x155>
mov    $0x0,%al
call   10e0 <rand@plt>
cltd
idivl  -0x18(%rbp)
movslq %edx,%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   10e0 <rand@plt>
mov    $0x1f,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x5c(%rbp)
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x5c(%rbp),%eax
jge    12d9 <main+0x119>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    %al,%cl
movslq -0x60(%rbp),%rax
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    12ae <main+0xee>
mov    -0x1c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    0x2ce9(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0x5c(%rbp),%esi
mov    -0x58(%rbp),%rdx
lea    0xd25(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x5c(%rbp),%edx
call   1030 <write@plt>
jmp    126c <main+0xac>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0x3,%edi
jne    12fb <main+0x13b>
mov    %rsi,%rbx
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    0x8(%rbx),%rbp
mov    0x10(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r13d
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     131a <main+0x15a>
mov    %eax,%r12d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
test   %r13d,%r13d
je     12ea <main+0x12a>
mov    0x8(%rsp),%eax
mov    %eax,0xc(%rsp)
jmp    127c <main+0xbc>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %ebp,%rbp
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1050 <lseek@plt>
lea    0xdc3(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
call   1040 <printf@plt>
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %r13d,%r13d
je     12ea <main+0x12a>
add    $0xffffffff,%r13d
xor    %eax,%eax
call   10e0 <rand@plt>
cltd
idivl  0xc(%rsp)
mov    %edx,%ebp
xor    %eax,%eax
call   10e0 <rand@plt>
cltq
imul   $0xffffffff84210843,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x4,%ecx
add    %edx,%ecx
mov    %ecx,%edx
shl    $0x5,%edx
sub    %edx,%ecx
lea    (%rax,%rcx,1),%ebx
add    $0x1,%ebx
add    %ecx,%eax
js     1240 <main+0x80>
mov    %ebx,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   10e0 <rand@plt>
mov    %al,0x10(%rsp,%r14,1)
add    $0x1,%r14
cmp    %r14,%r15
jne    12d0 <main+0x110>
jmp    1240 <main+0x80>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xcf5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xcfb(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"Usage: <filename> <cycles>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [32 x i8] c"Writing %d bytes at offset %lu\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [32 x i8]
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i32 @time(i32* null)
%22 = call i32 @srand(i32 %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %9
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %8
%30 = load i8*, i8** %9
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %20
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
%39 = load i32, i32* %7
%40 = call i32 @fstat(i32 %39, %struct.stat* %6)
br label %41
41: ; preds = %67, %38
%42 = load i32, i32* %8
%43 = add nsw i32 %42, -1
store i32 %43, i32* %8
%44 = icmp ne i32 %42, 0
br i1 %44, label %45, label %79
45: ; preds = %41
%46 = call i32 (...) @rand()
%47 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%48 = load i32, i32* %47
%49 = srem i32 %46, %48
%50 = sext i32 %49 to i64
store i64 %50, i64* %11
%51 = call i32 (...) @rand()
%52 = srem i32 %51, 31
%53 = add nsw i32 1, %52
store i32 %53, i32* %12
store i32 0, i32* %13
br label %54
54: ; preds = %64, %45
%55 = load i32, i32* %13
%56 = load i32, i32* %12
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %67
58: ; preds = %54
%59 = call i32 (...) @rand()
%60 = trunc i32 %59 to i8
%61 = load i32, i32* %13
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 %62
store i8 %60, i8* %63
br label %64
64: ; preds = %58
%65 = load i32, i32* %13
%66 = add nsw i32 %65, 1
store i32 %66, i32* %13
br label %54
67: ; preds = %54
%68 = load i32, i32* %7
%69 = load i64, i64* %11
%70 = load i32, i32* @SEEK_SET
%71 = call i32 @lseek(i32 %68, i64 %69, i32 %70)
%72 = load i32, i32* %12
%73 = load i64, i64* %11
%74 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %72, i64 %73)
%75 = load i32, i32* %7
%76 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%77 = load i32, i32* %12
%78 = call i32 @write(i32 %75, i8* %76, i32 %77)
br label %41
79: ; preds = %41
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"Usage: <filename> <cycles>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [32 x i8] c"Writing %d bytes at offset %lu\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [32 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = icmp eq i32 %0, 3
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32 @time(i32* null)
%13 = tail call i32 @srand(i32 %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* %15, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %11
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %11
%26 = call i32 @fstat(i32 %20, %struct.stat* %3)
%27 = icmp eq i32 %18, 0
br i1 %27, label %57, label %28
28: ; preds = %25
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i32, i32* %30
br label %32
32: ; preds = %28, %51
%33 = phi i32 [ %18, %28 ], [ %34, %51 ]
%34 = add nsw i32 %33, -1
%35 = tail call i32 (...) @rand()
%36 = srem i32 %35, %31
%37 = sext i32 %36 to i64
%38 = tail call i32 (...) @rand()
%39 = srem i32 %38, 31
%40 = add nsw i32 %39, 1
%41 = icmp slt i32 %39, 0
br i1 %41, label %51, label %42
42: ; preds = %32
%43 = zext i32 %40 to i64
br label %44
44: ; preds = %42, %44
%45 = phi i64 [ 0, %42 ], [ %49, %44 ]
%46 = tail call i32 (...) @rand()
%47 = trunc i32 %46 to i8
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %45
store i8 %47, i8* %48
%49 = add nuw nsw i64 %45, 1
%50 = icmp eq i64 %49, %43
br i1 %50, label %51, label %44
51: ; preds = %44, %32
%52 = load i32, i32* @SEEK_SET
%53 = tail call i32 @lseek(i32 %20, i64 %37, i32 %52)
%54 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %40, i64 %37)
%55 = call i32 @write(i32 %20, i8* %29, i32 %40)
%56 = icmp eq i32 %34, 0
br i1 %56, label %57, label %32
57: ; preds = %51, %25
ret i32 0
}