#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 O_CREAT;
int O_WRONLY;
long atol(char *);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fsync(int);
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) {
  int fd, ch, rc;
  char *filename;
  long i, j, val, seed, size, length;
  if (argc != 3) {
    printf("a.out filename length\n");
    exit(0);
  }
  filename = argv[1];
  length = atol(argv[2]);
  i = 0;
  j = 0;
  seed = 1234;
  srandom(seed);
  fd = open(filename, O_CREAT | O_WRONLY, 0600);
  if (fd < 0) {
    printf("Fail to open file %s, errno = %d.\n", filename, errno);
    exit(0);
  }
  while (i < length) {
    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++;
    if ((val & 0xffff) == 0xffff) {
      rc = fsync(fd);
      printf("%2ld: Sync offset = %8ld, rc = %d.\n", j, i, rc);
      if (rc)
        break;
      j++;
    }
  }
  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,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atol@plt>
mov    %rax,-0x58(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x4d2,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <srandom@plt>
mov    -0x28(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1252 <main+0xc2>
mov    -0x28(%rbp),%rsi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1257 <main+0xc7>
mov    -0x30(%rbp),%rax
cmp    -0x58(%rbp),%rax
jge    131f <main+0x18f>
mov    $0x0,%al
call   1070 <random@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x1,-0x50(%rbp)
je     12b9 <main+0x129>
mov    -0x30(%rbp),%rsi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
and    $0xffff,%rax
cmp    $0xffff,%rax
jne    131a <main+0x18a>
mov    -0x14(%rbp),%edi
call   1080 <fsync@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     130e <main+0x17e>
jmp    131f <main+0x18f>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1257 <main+0xc7>
mov    -0x14(%rbp),%edi
call   1060 <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    12ad <main+0x10d>
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    0x2dec(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
test   %eax,%eax
js     12c0 <main+0x120>
mov    %eax,%r13d
test   %r14,%r14
jle    1273 <main+0xd3>
lea    0x4(%rsp),%rbp
xor    %ebx,%ebx
xor    %r15d,%r15d
jmp    121c <main+0x7c>
nopl   0x0(%rax)
mov    %r12,%rbp
add    $0x1,%r15
cmp    %rbx,%r14
je     1273 <main+0xd3>
xor    %eax,%eax
call   1080 <random@plt>
mov    %rax,%r12
mov    %r12d,0x4(%rsp)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x1,%rax
jne    128c <main+0xec>
add    $0x1,%rbx
cmp    $0xffff,%r12w
jne    1217 <main+0x77>
mov    %r13d,%edi
call   1090 <fsync@plt>
mov    %rbp,%r12
mov    %eax,%ebp
lea    0xdf2(%rip),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
je     1210 <main+0x70>
mov    %r13d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
lea    0xd87(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xdc2(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    0x2d11(%rip),%rax
mov    (%rax),%edx
lea    0xd30(%rip),%rdi
jmp    129c <main+0xfc>

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

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = 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"
@.str.3 = [36 x i8] c"%2ld: Sync offset = %8ld, rc = %d.\0A\00"
@str = [22 x i8] c"a.out filename length\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 ([22 x i8], [22 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 = tail call i32 @open(i8* %11, i32 %18, i32 384)
%20 = icmp slt i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %9
%22 = icmp sgt i64 %14, 0
br i1 %22, label %27, label %51
23: ; preds = %9
%24 = load i32, i32* @errno
%25 = 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 %24)
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %21, %48
%28 = phi i64 [ %39, %48 ], [ 0, %21 ]
%29 = phi i64 [ %49, %48 ], [ 0, %21 ]
%30 = tail call i64 (...) @random()
%31 = trunc i64 %30 to i32
store i32 %31, i32* %3
%32 = call i64 @write(i32 %19, i32* %3, i32 1)
%33 = icmp eq i64 %32, 1
br i1 %33, label %38, label %34
34: ; preds = %27
%35 = load i32, i32* @errno
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %28, i32 %35)
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %27
%39 = add nuw nsw i64 %28, 1
%40 = and i64 %30, 65535
%41 = icmp eq i64 %40, 65535
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = tail call i32 @fsync(i32 %19)
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i64 %29, i64 %39, i32 %43)
%45 = icmp eq i32 %43, 0
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = add nsw i64 %29, 1
br label %48
48: ; preds = %46, %38
%49 = phi i64 [ %47, %46 ], [ %29, %38 ]
%50 = icmp eq i64 %39, %14
br i1 %50, label %51, label %27
51: ; preds = %48, %42, %21
%52 = tail call i32 @close(i32 %19)
ret i32 0
}