#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 EINVAL;
int /*<<< orphan*/ O_FSYNC;
int fdatasync(int);
int fsync(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void fileSync(char *fname) {
  int fd;
  fd = open(fname, O_FSYNC);
  if (fd == -1) {
    printf("Error opening file\n");
  }
  int ret;
  ret = fsync(fd);
  if (ret == -1) {
    if (ret == EINVAL) {
      if (fdatasync(fd) == -1) {
        perror("fdatasyncfailed");
      }
    } else {
      printf("Error synchronizing file\n");
    }
  } else {
    printf("Synced %p\n", ret);
  }
}


x86 assembly (clang, -O0):
<fileSync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1189 <fileSync+0x39>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <fsync@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11e5 <fileSync+0x95>
mov    -0x10(%rbp),%eax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
jne    11d2 <fileSync+0x82>
mov    -0xc(%rbp),%edi
call   1030 <fdatasync@plt>
cmp    $0xffffffff,%eax
jne    11cd <fileSync+0x7d>
lea    0xe4c(%rip),%rdi
call   1070 <perror@plt>
jmp    11e0 <fileSync+0x90>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f6 <fileSync+0xa6>
mov    -0x10(%rbp),%esi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileSync>:
push   %rbx
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1182 <fileSync+0x22>
lea    0xeb7(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1060 <fsync@plt>
cmp    $0xffffffff,%eax
je     119f <fileSync+0x3f>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
pop    %rbx
jmp    1050 <printf@plt>
mov    0x2e32(%rip),%rax
cmpl   $0xffffffff,(%rax)
je     11b8 <fileSync+0x58>
lea    0xe69(%rip),%rdi
pop    %rbx
jmp    1040 <puts@plt>
mov    %ebx,%edi
call   1030 <fdatasync@plt>
cmp    $0xffffffff,%eax
je     11c6 <fileSync+0x66>
pop    %rbx
ret
lea    0xe33(%rip),%rdi
pop    %rbx
jmp    1080 <perror@plt>

LLVM IR (-O0):
@O_FSYNC = global i32 0
@.str = [20 x i8] c"Error opening file\0A\00"
@EINVAL = global i32 0
@.str.1 = [16 x i8] c"fdatasyncfailed\00"
@.str.2 = [26 x i8] c"Error synchronizing file\0A\00"
@.str.3 = [11 x i8] c"Synced %p\0A\00"
define void @fileSync(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_FSYNC
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = load i32, i32* %3
%14 = call i32 @fsync(i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %31
17: ; preds = %12
%18 = load i32, i32* %4
%19 = load i32, i32* @EINVAL
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = load i32, i32* %3
%23 = call i32 @fdatasync(i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
br label %30
28: ; preds = %17
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %27
br label %34
31: ; preds = %12
%32 = load i32, i32* %4
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %31, %30
ret void
}

LLVM IR (-O2):
@O_FSYNC = global i32 0
@EINVAL = global i32 0
@.str.1 = [16 x i8] c"fdatasyncfailed\00"
@.str.3 = [11 x i8] c"Synced %p\0A\00"
@str = [25 x i8] c"Error synchronizing file\00"
@str.4 = [19 x i8] c"Error opening file\00"
define void @fileSync(i8* nocapture %0) {
%2 = load i32, i32* @O_FSYNC
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
br label %7
7: ; preds = %5, %1
%8 = tail call i32 @fsync(i32 %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %20
10: ; preds = %7
%11 = load i32, i32* @EINVAL
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = tail call i32 @fdatasync(i32 %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %22
18: ; preds = %10
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %22
20: ; preds = %7
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %8)
br label %22
22: ; preds = %18, %16, %13, %20
ret void
}