#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_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ fsync(int);
int open(char const *, int, int);
int /*<<< orphan*/ unlink(char const *);
int main(void) {
  static const char *filename = "/tmp/test-fsync.tmp";
  int fd, err = 1;
  fd = open(filename, O_RDWR | O_CREAT, 0666);
  if (fd < 0)
    return 1;
  (void)unlink(filename);
  if (fsync(fd) < 0)
    goto err;
  err = 0;
err:
  (void)close(fd);
  return err;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2ee3(%rip),%rdi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1192 <main+0x52>
movl   $0x1,-0x4(%rbp)
jmp    11ca <main+0x8a>
mov    0x2ea7(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%edi
call   1050 <fsync@plt>
cmp    $0x0,%rax
jge    11b5 <main+0x75>
jmp    11bc <main+0x7c>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e96(%rip),%rax
mov    0x2ea7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea4(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     118f <main+0x4f>
mov    %eax,%ebp
lea    0xe8d(%rip),%rdi
call   1030 <unlink@plt>
mov    %ebp,%edi
call   1050 <fsync@plt>
mov    %rax,%rbx
shr    $0x3f,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1194 <main+0x54>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@main.filename = internal global i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i32 0, i32 0)
@.str = [20 x i8] c"/tmp/test-fsync.tmp\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %3
%4 = load i8*, i8** @main.filename
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* %4, i32 %7, i32 438)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 1, i32* %1
br label %24
12: ; preds = %0
%13 = load i8*, i8** @main.filename
%14 = call i32 @unlink(i8* %13)
%15 = load i32, i32* %2
%16 = call i64 @fsync(i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
br label %20
19: ; preds = %12
store i32 0, i32* %3
br label %20
20: ; preds = %19, %18
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %3
store i32 %23, i32* %1
br label %24
24: ; preds = %20, %11
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [20 x i8] c"/tmp/test-fsync.tmp\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = icmp slt i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %0
%7 = tail call i32 @unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%8 = tail call i64 @fsync(i32 %4)
%9 = lshr i64 %8, 63
%10 = trunc i64 %9 to i32
%11 = tail call i32 @close(i32 %4)
br label %12
12: ; preds = %0, %6
%13 = phi i32 [ %10, %6 ], [ 1, %0 ]
ret i32 %13
}