#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 */
int /*<<< orphan*/ F_GETFL;
int O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ dv_error(char *);
scalar_t__ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int fcntl(int, int /*<<< orphan*/);
scalar_t__ fflush(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
scalar_t__ fsync(int);
void dv_fclose(FILE *fp) {
  if (errno) {
    dv_error("");
  }
  int fd = fileno(fp);
  if (fd < 0) {
    dv_error("Failed to acquire file descriptor from FILE pointer.");
  }
  int flags = fcntl(fd, F_GETFL);
  if ((flags & O_WRONLY) || (flags & O_RDWR)) {
    if (fflush(fp)) {
      dv_error("Failed to flush file buffer to disk.");
    }
    if (fsync(fd)) {
      dv_error("Failed to sync to disk.");
    }
  }
  if (fclose(fp)) {
    dv_error("Failed to close file.");
  }
}


x86 assembly (clang, -O0):
<dv_fclose>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
cmpq   $0x0,(%rax)
je     1189 <dv_fclose+0x29>
lea    0xf03(%rip),%rdi
call   1080 <dv_error@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ab <dv_fclose+0x4b>
lea    0xe5a(%rip),%rdi
call   1080 <dv_error@plt>
mov    -0xc(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2df7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11e9 <dv_fclose+0x89>
mov    -0x10(%rbp),%eax
mov    0x2e0a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     122b <dv_fclose+0xcb>
mov    -0x8(%rbp),%rdi
call   1060 <fflush@plt>
cmp    $0x0,%rax
je     1208 <dv_fclose+0xa8>
lea    0xe32(%rip),%rdi
call   1080 <dv_error@plt>
mov    -0xc(%rbp),%edi
call   1070 <fsync@plt>
cmp    $0x0,%rax
je     1226 <dv_fclose+0xc6>
lea    0xe39(%rip),%rdi
call   1080 <dv_error@plt>
jmp    122b <dv_fclose+0xcb>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     124a <dv_fclose+0xea>
lea    0xe2d(%rip),%rdi
call   1080 <dv_error@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dv_fclose>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e6b(%rip),%rax
cmpq   $0x0,(%rax)
je     117f <dv_fclose+0x1f>
lea    0xf0d(%rip),%rdi
call   1080 <dv_error@plt>
mov    %rbx,%rdi
call   1050 <fileno@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1199 <dv_fclose+0x39>
lea    0xe6c(%rip),%rdi
call   1080 <dv_error@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    0x2e10(%rip),%rcx
test   %eax,(%rcx)
jne    11bf <dv_fclose+0x5f>
mov    0x2e2d(%rip),%rcx
test   %eax,(%rcx)
je     11f0 <dv_fclose+0x90>
mov    %rbx,%rdi
call   1060 <fflush@plt>
test   %rax,%rax
je     11d8 <dv_fclose+0x78>
lea    0xe62(%rip),%rdi
call   1080 <dv_error@plt>
mov    %ebp,%edi
call   1070 <fsync@plt>
test   %rax,%rax
je     11f0 <dv_fclose+0x90>
lea    0xe6f(%rip),%rdi
call   1080 <dv_error@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     120f <dv_fclose+0xaf>
lea    0xe6e(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <dv_error@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [53 x i8] c"Failed to acquire file descriptor from FILE pointer.\00"
@F_GETFL = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str.2 = [37 x i8] c"Failed to flush file buffer to disk.\00"
@.str.3 = [24 x i8] c"Failed to sync to disk.\00"
@.str.4 = [22 x i8] c"Failed to close file.\00"
define void @dv_fclose(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32* %0, i32** %2
%5 = load i64, i64* @errno
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @dv_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i32*, i32** %2
%11 = call i32 @fileno(i32* %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @dv_error(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = load i32, i32* %3
%18 = load i32, i32* @F_GETFL
%19 = call i32 @fcntl(i32 %17, i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = load i32, i32* @O_WRONLY
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %16
%25 = load i32, i32* %4
%26 = load i32, i32* @O_RDWR
%27 = and i32 %25, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %24, %16
%30 = load i32*, i32** %2
%31 = call i64 @fflush(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @dv_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* %3
%37 = call i64 @fsync(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @dv_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
br label %42
42: ; preds = %41, %24
%43 = load i32*, i32** %2
%44 = call i64 @fclose(i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @dv_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [53 x i8] c"Failed to acquire file descriptor from FILE pointer.\00"
@F_GETFL = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str.2 = [37 x i8] c"Failed to flush file buffer to disk.\00"
@.str.3 = [24 x i8] c"Failed to sync to disk.\00"
@.str.4 = [22 x i8] c"Failed to close file.\00"
define void @dv_fclose(i32* nocapture %0) {
%2 = load i64, i64* @errno
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @dv_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i32 @fileno(i32* %0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @dv_error(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = load i32, i32* @F_GETFL
%13 = tail call i32 @fcntl(i32 %7, i32 %12)
%14 = load i32, i32* @O_WRONLY
%15 = and i32 %14, %13
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i32, i32* @O_RDWR
%19 = and i32 %18, %13
%20 = icmp eq i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %17, %11
%22 = tail call i64 @fflush(i32* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = tail call i32 @dv_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = tail call i64 @fsync(i32 %7)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = tail call i32 @dv_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %26, %29, %17
%32 = tail call i64 @fclose(i32* %0)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = tail call i32 @dv_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
ret void
}