#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 int ssize_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fdatasync(int);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int main(void) {
  char buf[4096];
  int fd;
  ssize_t res;
  memset(buf, 0, 4096);
  fd = open("./mnt/testfsync.data", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    perror("Unable to open testfile");
    return 1;
  }
  res = write(fd, buf, 4096);
  if (res != 4096) {
    perror("Error writing");
    (void)close(fd);
    return 1;
  }
  fdatasync(fd);
  fsync(fd);
  (void)close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
lea    0xe40(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    11f0 <main+0x80>
lea    0xe36(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1268 <main+0xf8>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x1000,-0x1018(%rbp)
je     1240 <main+0xd0>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1268 <main+0xf8>
mov    -0x1014(%rbp),%edi
call   1030 <fdatasync@plt>
mov    -0x1014(%rbp),%edi
call   1070 <fsync@plt>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x1000,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    0x2e32(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e30(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe4e(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f3 <main+0x83>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1040 <write@plt>
cmp    $0x1000,%eax
jne    1206 <main+0x96>
mov    %ebx,%edi
call   1030 <fdatasync@plt>
mov    %ebx,%edi
call   1070 <fsync@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
ret
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    11ea <main+0x7a>
lea    0xe20(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%eax
jmp    11ea <main+0x7a>

LLVM IR (-O0):
@.str = [21 x i8] c"./mnt/testfsync.data\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [24 x i8] c"Unable to open testfile\00"
@.str.2 = [14 x i8] c"Error writing\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 4096)
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %35
18: ; preds = %0
%19 = load i32, i32* %3
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%21 = call i32 @write(i32 %19, i8* %20, i32 4096)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 4096
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
store i32 1, i32* %1
br label %35
28: ; preds = %18
%29 = load i32, i32* %3
%30 = call i32 @fdatasync(i32 %29)
%31 = load i32, i32* %3
%32 = call i32 @fsync(i32 %31)
%33 = load i32, i32* %3
%34 = call i32 @close(i32 %33)
store i32 0, i32* %1
br label %35
35: ; preds = %28, %24, %16
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [21 x i8] c"./mnt/testfsync.data\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [24 x i8] c"Unable to open testfile\00"
@.str.2 = [14 x i8] c"Error writing\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 4096)
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %24
14: ; preds = %0
%15 = call i32 @write(i32 %10, i8* %2, i32 4096)
%16 = icmp eq i32 %15, 4096
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @close(i32 %10)
br label %24
20: ; preds = %14
%21 = call i32 @fdatasync(i32 %10)
%22 = call i32 @fsync(i32 %10)
%23 = call i32 @close(i32 %10)
br label %24
24: ; preds = %20, %17, %12
%25 = phi i32 [ 1, %12 ], [ 1, %17 ], [ 0, %20 ]
ret i32 %25
}