#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_APPEND;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fsync(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ usleep(unsigned int);
int /*<<< orphan*/ write(int, char const *, int);
int main(int argc, char *argv[]) {
  const char *pattern = "abcde";
  const int pattern_len = strlen(pattern);
  unsigned int delay_usecs = 500000;
  int count = pattern_len;
  int fd = open("data", O_APPEND | O_RDWR | O_SYNC);
  if (fd == -1) {
    printf("%s\n", strerror(errno));
    return 1;
  }
  for (int i = 0; i < count; ++i) {
    write(fd, &pattern[i % pattern_len], 1);
    fsync(fd);
    usleep(delay_usecs);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x7a120,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
lea    0xe2a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1217 <main+0x97>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe05(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1275 <main+0xf5>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1266 <main+0xe6>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
movslq %edx,%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x28(%rbp),%edi
call   1070 <fsync@plt>
mov    -0x20(%rbp),%edi
call   10a0 <usleep@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    121e <main+0x9e>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe72(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x2e23(%rip),%rcx
mov    0x2e44(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%r14d
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1208 <main+0x88>
mov    %eax,%r15d
test   %r14d,%r14d
jle    11fc <main+0x7c>
mov    %r14d,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %r15d,%edi
call   1070 <fsync@plt>
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    11d0 <main+0x50>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    122a <main+0xaa>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdee(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"abcde\00"
@.str.1 = [5 x i8] c"data\00"
@O_APPEND = global i32 0
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %6
%12 = load i8*, i8** %6
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
store i32 500000, i32* %8
%14 = load i32, i32* %7
store i32 %14, i32* %9
%15 = load i32, i32* @O_APPEND
%16 = load i32, i32* @O_RDWR
%17 = or i32 %15, %16
%18 = load i32, i32* @O_SYNC
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %2
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %51
27: ; preds = %2
store i32 0, i32* %11
br label %28
28: ; preds = %45, %27
%29 = load i32, i32* %11
%30 = load i32, i32* %9
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %48
32: ; preds = %28
%33 = load i32, i32* %10
%34 = load i8*, i8** %6
%35 = load i32, i32* %11
%36 = load i32, i32* %7
%37 = srem i32 %35, %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %34, i64 %38
%40 = call i32 @write(i32 %33, i8* %39, i32 1)
%41 = load i32, i32* %10
%42 = call i32 @fsync(i32 %41)
%43 = load i32, i32* %8
%44 = call i32 @usleep(i32 %43)
br label %45
45: ; preds = %32
%46 = load i32, i32* %11
%47 = add nsw i32 %46, 1
store i32 %47, i32* %11
br label %28
48: ; preds = %28
%49 = load i32, i32* %10
%50 = call i32 @close(i32 %49)
store i32 0, i32* %3
br label %51
51: ; preds = %48, %23
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [6 x i8] c"abcde\00"
@.str.1 = [5 x i8] c"data\00"
@O_APPEND = global i32 0
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_APPEND
%5 = load i32, i32* @O_RDWR
%6 = or i32 %5, %4
%7 = load i32, i32* @O_SYNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = icmp sgt i32 %3, 0
br i1 %12, label %13, label %19
13: ; preds = %11
%14 = zext i32 %3 to i64
br label %21
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %29
19: ; preds = %21, %11
%20 = tail call i32 @close(i32 %9)
br label %29
21: ; preds = %13, %21
%22 = phi i64 [ 0, %13 ], [ %27, %21 ]
%23 = getelementptr inbounds [6 x i8], [6 x i8]* @.str, i64 0, i64 %22
%24 = tail call i32 @write(i32 %9, i8* %23, i32 1)
%25 = tail call i32 @fsync(i32 %9)
%26 = tail call i32 @usleep(i32 500000)
%27 = add nuw nsw i64 %22, 1
%28 = icmp eq i64 %27, %14
br i1 %28, label %19, label %21
29: ; preds = %19, %15
%30 = phi i32 [ 1, %15 ], [ 0, %19 ]
ret i32 %30
}