#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 long off_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
long atol(char *);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ lseek(int, long, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
long random();
int /*<<< orphan*/ srandom(long);
long write(int, int *, int);
int main(int argc, char **argv) {
  off_t offset;
  int fd, ch, rc;
  char *filename;
  long i, j, val, seed, size, count;
  if (argc != 3) {
    printf("a.out filename count\n");
    exit(0);
  }
  filename = argv[1];
  count = atol(argv[2]);
  i = 0;
  seed = 1234;
  srandom(seed);
  fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600);
  if (fd < 0) {
    printf("Fail to open file %s, errno = %d.\n", filename, errno);
    exit(0);
  }
  while (i < count) {
    for (j = 0; j < 128; j++) {
      val = random();
      ch = val;
      size = write(fd, &ch, 1);
      if (size != 1) {
        printf("Fail to write file, offset = %ld, errno = %d.\n", i, errno);
        exit(0);
      }
    }
    i++;
    offset = i * 128 * 1024 * 1024;
    if (lseek(fd, offset, SEEK_SET) < 0) {
      printf("Fail to seek file, offset = %ld, errno = %d.\n", i, errno);
      exit(0);
    }
  }
  close(fd);
}


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     11c5 <main+0x35>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atol@plt>
mov    %rax,-0x60(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x4d2,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <srandom@plt>
mov    -0x30(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
mov    0x2dd8(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1253 <main+0xc3>
mov    -0x30(%rbp),%rsi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1258 <main+0xc8>
mov    -0x38(%rbp),%rax
cmp    -0x60(%rbp),%rax
jge    134c <main+0x1bc>
movq   $0x0,-0x40(%rbp)
cmpq   $0x80,-0x40(%rbp)
jge    12e6 <main+0x156>
mov    $0x0,%al
call   1080 <random@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x1,-0x58(%rbp)
je     12d0 <main+0x140>
mov    -0x38(%rbp),%rsi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    12d5 <main+0x145>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    126e <main+0xde>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
shl    $0x7,%rax
shl    $0xa,%rax
shl    $0xa,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2cb4(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    1347 <main+0x1b7>
mov    -0x38(%rbp),%rsi
mov    0x2ca0(%rip),%rax
mov    (%rax),%edx
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1258 <main+0xc8>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    12bc <main+0x11c>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%rdi
call   10a0 <atol@plt>
mov    %rax,%r14
mov    $0x4d2,%edi
call   1030 <srandom@plt>
mov    0x2e23(%rip),%rax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
test   %eax,%eax
js     12cf <main+0x12f>
mov    %eax,%ebp
xor    %eax,%eax
test   %r14,%r14
cmovle %rax,%r14
lea    0x4(%rsp),%r13
mov    0x2db3(%rip),%r12
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
cmp    %r14,%r15
je     1283 <main+0xe3>
mov    $0x80,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <random@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x1,%rax
jne    129b <main+0xfb>
add    $0xffffffffffffffff,%rbx
jne    1230 <main+0x90>
add    $0x1,%r15
mov    %r15,%rsi
shl    $0x1b,%rsi
mov    (%r12),%edx
mov    %ebp,%edi
call   1070 <lseek@plt>
test   %rax,%rax
jns    1220 <main+0x80>
mov    0x2d58(%rip),%rax
mov    (%rax),%edx
lea    0xdd1(%rip),%rdi
jmp    12ab <main+0x10b>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d2e(%rip),%rax
mov    (%rax),%edx
lea    0xd78(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xdbd(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edx
lea    0xd21(%rip),%rdi
mov    %rbx,%rsi
jmp    12ae <main+0x10e>

LLVM IR (-O0):
@.str = [22 x i8] c"a.out filename count\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [35 x i8] c"Fail to open file %s, errno = %d.\0A\00"
@errno = global i32 0
@.str.2 = [47 x i8] c"Fail to write file, offset = %ld, errno = %d.\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [46 x i8] c"Fail to seek file, offset = %ld, errno = %d.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 3
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %10
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i64 @atol(i8* %28)
store i64 %29, i64* %16
store i64 0, i64* %11
store i64 1234, i64* %14
%30 = load i64, i64* %14
%31 = call i32 @srandom(i64 %30)
%32 = load i8*, i8** %10
%33 = load i32, i32* @O_CREAT
%34 = load i32, i32* @O_WRONLY
%35 = or i32 %33, %34
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %32, i32 %37, i32 384)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %22
%42 = load i8*, i8** %10
%43 = load i32, i32* @errno
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %42, i32 %43)
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %22
br label %47
47: ; preds = %89, %46
%48 = load i64, i64* %11
%49 = load i64, i64* %16
%50 = icmp slt i64 %48, %49
br i1 %50, label %51, label %90
51: ; preds = %47
store i64 0, i64* %12
br label %52
52: ; preds = %69, %51
%53 = load i64, i64* %12
%54 = icmp slt i64 %53, 128
br i1 %54, label %55, label %72
55: ; preds = %52
%56 = call i64 (...) @random()
store i64 %56, i64* %13
%57 = load i64, i64* %13
%58 = trunc i64 %57 to i32
store i32 %58, i32* %8
%59 = load i32, i32* %7
%60 = call i64 @write(i32 %59, i32* %8, i32 1)
store i64 %60, i64* %15
%61 = load i64, i64* %15
%62 = icmp ne i64 %61, 1
br i1 %62, label %63, label %68
63: ; preds = %55
%64 = load i64, i64* %11
%65 = load i32, i32* @errno
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %64, i32 %65)
%67 = call i32 @exit(i32 0)
unreachable
68: ; preds = %55
br label %69
69: ; preds = %68
%70 = load i64, i64* %12
%71 = add nsw i64 %70, 1
store i64 %71, i64* %12
br label %52
72: ; preds = %52
%73 = load i64, i64* %11
%74 = add nsw i64 %73, 1
store i64 %74, i64* %11
%75 = load i64, i64* %11
%76 = mul nsw i64 %75, 128
%77 = mul nsw i64 %76, 1024
%78 = mul nsw i64 %77, 1024
store i64 %78, i64* %6
%79 = load i32, i32* %7
%80 = load i64, i64* %6
%81 = load i32, i32* @SEEK_SET
%82 = call i64 @lseek(i32 %79, i64 %80, i32 %81)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %89
84: ; preds = %72
%85 = load i64, i64* %11
%86 = load i32, i32* @errno
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i64 %85, i32 %86)
%88 = call i32 @exit(i32 0)
unreachable
89: ; preds = %72
br label %47
90: ; preds = %47
%91 = load i32, i32* %7
%92 = call i32 @close(i32 %91)
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [35 x i8] c"Fail to open file %s, errno = %d.\0A\00"
@errno = global i32 0
@.str.2 = [47 x i8] c"Fail to write file, offset = %ld, errno = %d.\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [46 x i8] c"Fail to seek file, offset = %ld, errno = %d.\0A\00"
@str = [21 x i8] c"a.out filename count\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i64 @atol(i8* %13)
%15 = tail call i32 @srandom(i64 1234)
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_WRONLY
%18 = or i32 %17, %16
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = tail call i32 @open(i8* %11, i32 %20, i32 384)
%22 = icmp slt i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %9
%24 = call i64 @llvm.smax.i64(i64 %14, i64 0)
br label %29
25: ; preds = %9
%26 = load i32, i32* @errno
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 %26)
%28 = tail call i32 @exit(i32 0)
unreachable
29: ; preds = %23, %45
%30 = phi i64 [ %46, %45 ], [ 0, %23 ]
%31 = icmp eq i64 %30, %24
br i1 %31, label %55, label %35
32: ; preds = %35
%33 = add nuw nsw i64 %36, 1
%34 = icmp eq i64 %33, 128
br i1 %34, label %45, label %35
35: ; preds = %29, %32
%36 = phi i64 [ %33, %32 ], [ 0, %29 ]
%37 = tail call i64 (...) @random()
%38 = trunc i64 %37 to i32
store i32 %38, i32* %3
%39 = call i64 @write(i32 %21, i32* %3, i32 1)
%40 = icmp eq i64 %39, 1
br i1 %40, label %32, label %41
41: ; preds = %35
%42 = load i32, i32* @errno
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %30, i32 %42)
%44 = tail call i32 @exit(i32 0)
unreachable
45: ; preds = %32
%46 = add nuw i64 %30, 1
%47 = shl nsw i64 %46, 27
%48 = load i32, i32* @SEEK_SET
%49 = tail call i64 @lseek(i32 %21, i64 %47, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %29
51: ; preds = %45
%52 = load i32, i32* @errno
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i64 %46, i32 %52)
%54 = tail call i32 @exit(i32 0)
unreachable
55: ; preds = %29
%56 = tail call i32 @close(i32 %21)
ret i32 0
}