#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 /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
scalar_t__ fsync(int /*<<< orphan*/);
int safe_fclose(int /*<<< orphan*/ **);
int safe_fsync_close(FILE **f) {
  int r = 0;
  if (*f) {
    if (fflush(*f) || fsync(fileno(*f))) {
      r = -1;
      safe_fclose(f);
    } else
      r = safe_fclose(f);
  }
  return r;
}


x86 assembly (clang, -O0):
<safe_fsync_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     11ba <safe_fsync_close+0x7a>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <fflush@plt>
cmp    $0x0,%rax
jne    1194 <safe_fsync_close+0x54>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <fileno@plt>
mov    %eax,%edi
call   1050 <fsync@plt>
cmp    $0x0,%rax
je     11a9 <safe_fsync_close+0x69>
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <safe_fclose@plt>
jmp    11b5 <safe_fsync_close+0x75>
mov    -0x8(%rbp),%rdi
call   1060 <safe_fclose@plt>
mov    %eax,-0xc(%rbp)
jmp    11ba <safe_fsync_close+0x7a>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fsync_close>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1179 <safe_fsync_close+0x39>
call   1040 <fflush@plt>
test   %rax,%rax
jne    116a <safe_fsync_close+0x2a>
mov    (%rbx),%rdi
call   1030 <fileno@plt>
mov    %eax,%edi
call   1050 <fsync@plt>
test   %rax,%rax
je     117d <safe_fsync_close+0x3d>
mov    %rbx,%rdi
call   1060 <safe_fclose@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
mov    %rbx,%rdi
pop    %rbx
jmp    1060 <safe_fclose@plt>

LLVM IR (-O0):
define i32 @safe_fsync_close(i32** %0) {
%2 = alloca i32**
%3 = alloca i32
store i32** %0, i32*** %2
store i32 0, i32* %3
%4 = load i32**, i32*** %2
%5 = load i32*, i32** %4
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %25
7: ; preds = %1
%8 = load i32**, i32*** %2
%9 = load i32*, i32** %8
%10 = call i64 @fflush(i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %7
%13 = load i32**, i32*** %2
%14 = load i32*, i32** %13
%15 = call i32 @fileno(i32* %14)
%16 = call i64 @fsync(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12, %7
store i32 -1, i32* %3
%19 = load i32**, i32*** %2
%20 = call i32 @safe_fclose(i32** %19)
br label %24
21: ; preds = %12
%22 = load i32**, i32*** %2
%23 = call i32 @safe_fclose(i32** %22)
store i32 %23, i32* %3
br label %24
24: ; preds = %21, %18
br label %25
25: ; preds = %24, %1
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
define i32 @safe_fsync_close(i32** %0) {
%2 = load i32*, i32** %0
%3 = icmp eq i32* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = tail call i64 @fflush(i32* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i32*, i32** %0
%9 = tail call i32 @fileno(i32* %8)
%10 = tail call i64 @fsync(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %7, %4
%13 = tail call i32 @safe_fclose(i32** %0)
br label %16
14: ; preds = %7
%15 = tail call i32 @safe_fclose(i32** %0)
br label %16
16: ; preds = %12, %14, %1
%17 = phi i32 [ -1, %12 ], [ %15, %14 ], [ 0, %1 ]
ret i32 %17
}