#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ fdatasync(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ usleep(int);
int write(int, char const *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd = open("file.log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd == -1)
    err_sys("open file.log");
  int ret = 0, n = 0;
  char const *ptr = "hello world!";
  while (n++ < 1000) {
    ret = write(fd, ptr, strlen(ptr));
    printf("%5d:write %d\n", n, ret);
    if (ret < 0)
      err_sys("write file.log");
    usleep(10000);
    fdatasync(fd);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
lea    0xe77(%rip),%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11db <main+0x5b>
lea    0xe2a(%rip),%rdi
call   1040 <err_sys@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe1e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x3e8,%eax
jge    1271 <main+0xf1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xddc(%rip),%rdi
call   1070 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jge    125a <main+0xda>
lea    0xdd4(%rip),%rdi
call   1040 <err_sys@plt>
mov    $0x2710,%edi
call   10a0 <usleep@plt>
mov    -0x14(%rbp),%edi
call   1030 <fdatasync@plt>
jmp    11f4 <main+0x74>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e36(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
lea    0xe82(%rip),%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    11cb <main+0x4b>
lea    0xe3a(%rip),%rdi
call   1040 <err_sys@plt>
mov    $0x1,%ebp
lea    0xe37(%rip),%r14
lea    0xe3d(%rip),%r15
lea    0xe44(%rip),%r12
jmp    120d <main+0x8d>
nopw   0x0(%rax,%rax,1)
mov    $0x2710,%edi
call   10a0 <usleep@plt>
mov    %r13d,%edi
call   1030 <fdatasync@plt>
add    $0x1,%ebp
cmp    $0x3e9,%ebp
je     123e <main+0xbe>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %eax,%ebx
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <printf@plt>
test   %ebx,%ebx
jns    11f0 <main+0x70>
mov    %r12,%rdi
call   1040 <err_sys@plt>
jmp    11f0 <main+0x70>
mov    %r13d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"file.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [14 x i8] c"open file.log\00"
@.str.2 = [13 x i8] c"hello world!\00"
@.str.3 = [14 x i8] c"%5d:write %d\0A\00"
@.str.4 = [15 x i8] c"write file.log\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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14, i32 420)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
store i32 0, i32* %7
store i32 0, i32* %8
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8** %9
br label %21
21: ; preds = %38, %20
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
store i32 %23, i32* %8
%24 = icmp slt i32 %22, 1000
br i1 %24, label %25, label %42
25: ; preds = %21
%26 = load i32, i32* %6
%27 = load i8*, i8** %9
%28 = load i8*, i8** %9
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @write(i32 %26, i8* %27, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %8
%32 = load i32, i32* %7
%33 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %32)
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %25
%39 = call i32 @usleep(i32 10000)
%40 = load i32, i32* %6
%41 = call i32 @fdatasync(i32 %40)
br label %21
42: ; preds = %21
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [14 x i8] c"open file.log\00"
@.str.2 = [13 x i8] c"hello world!\00"
@.str.3 = [14 x i8] c"%5d:write %d\0A\00"
@.str.4 = [15 x i8] c"write file.log\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 420)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
br label %13
13: ; preds = %12, %21
%14 = phi i32 [ %24, %21 ], [ 1, %12 ]
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %14, i32 %16)
%18 = icmp slt i32 %16, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 @err_sys(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = tail call i32 @usleep(i32 10000)
%23 = tail call i32 @fdatasync(i32 %8)
%24 = add nuw nsw i32 %14, 1
%25 = icmp eq i32 %24, 1001
br i1 %25, label %26, label %13
26: ; preds = %21
%27 = tail call i32 @close(i32 %8)
ret i32 0
}