#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));
  cycles = atoi(argv[2]);
  fd = open("dump.rdb", 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    $0x50,%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    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
lea    0xded(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1257 <main+0x97>
lea    0xddd(%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     130c <main+0x14c>
mov    $0x0,%al
call   10e0 <rand@plt>
cltd
idivl  -0x18(%rbp)
movslq %edx,%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   10e0 <rand@plt>
mov    $0x1f,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    12d0 <main+0x110>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    %al,%cl
movslq -0x50(%rbp),%rax
mov    %cl,-0x40(%rbp,%rax,1)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    12a5 <main+0xe5>
mov    -0x1c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    0x2cf2(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rdx
lea    0xd37(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   1030 <write@plt>
jmp    1263 <main+0xa3>
xor    %eax,%eax
add    $0x50,%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    0x10(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r13d
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%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    0xdcc(%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    0xd04(%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"
@.str.1 = [9 x i8] c"dump.rdb\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.3 = [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 [32 x i8]
%10 = alloca i64
%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 ne i32 %13, 3
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 @time(i32* null)
%21 = call i32 @srand(i32 %20)
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %8
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %19
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %19
%34 = load i32, i32* %7
%35 = call i32 @fstat(i32 %34, %struct.stat* %6)
br label %36
36: ; preds = %62, %33
%37 = load i32, i32* %8
%38 = add nsw i32 %37, -1
store i32 %38, i32* %8
%39 = icmp ne i32 %37, 0
br i1 %39, label %40, label %74
40: ; preds = %36
%41 = call i32 (...) @rand()
%42 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%43 = load i32, i32* %42
%44 = srem i32 %41, %43
%45 = sext i32 %44 to i64
store i64 %45, i64* %10
%46 = call i32 (...) @rand()
%47 = srem i32 %46, 31
%48 = add nsw i32 1, %47
store i32 %48, i32* %11
store i32 0, i32* %12
br label %49
49: ; preds = %59, %40
%50 = load i32, i32* %12
%51 = load i32, i32* %11
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %62
53: ; preds = %49
%54 = call i32 (...) @rand()
%55 = trunc i32 %54 to i8
%56 = load i32, i32* %12
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 %57
store i8 %55, i8* %58
br label %59
59: ; preds = %53
%60 = load i32, i32* %12
%61 = add nsw i32 %60, 1
store i32 %61, i32* %12
br label %49
62: ; preds = %49
%63 = load i32, i32* %7
%64 = load i64, i64* %10
%65 = load i32, i32* @SEEK_SET
%66 = call i32 @lseek(i32 %63, i64 %64, i32 %65)
%67 = load i32, i32* %11
%68 = load i64, i64* %10
%69 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %67, i64 %68)
%70 = load i32, i32* %7
%71 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%72 = load i32, i32* %11
%73 = call i32 @write(i32 %70, i8* %71, i32 %72)
br label %36
74: ; preds = %36
ret i32 0
}

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