#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int ENOTDIR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int fstat(int, struct stat *);
int fsync(int);
int open(char *, int /*<<< orphan*/);
int cDirectoryFsync(char *path) {
  int fd = 0;
  int ret = 0;
  struct stat fdstat = {0};
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    return errno;
  }
  ret = fstat(fd, &fdstat);
  if (ret < 0) {
    close(fd);
    return errno;
  }
  if (!S_ISDIR(fdstat.st_mode)) {
    close(fd);
    return ENOTDIR;
  }
  ret = fsync(fd);
  close(fd);
  if (ret < 0) {
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cDirectoryFsync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ba <cDirectoryFsync+0x5a>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124b <cDirectoryFsync+0xeb>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ec <cDirectoryFsync+0x8c>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124b <cDirectoryFsync+0xeb>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1216 <cDirectoryFsync+0xb6>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124b <cDirectoryFsync+0xeb>
mov    -0x14(%rbp),%edi
call   1060 <fsync@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x18(%rbp)
jge    1244 <cDirectoryFsync+0xe4>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124b <cDirectoryFsync+0xeb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cDirectoryFsync>:
push   %rbp
push   %rbx
push   %rax
movq   $0x0,(%rsp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11ae <cDirectoryFsync+0x5e>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %eax,%eax
js     11a7 <cDirectoryFsync+0x57>
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %eax,%eax
je     11b7 <cDirectoryFsync+0x67>
mov    %ebx,%edi
call   1050 <fsync@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e30(%rip),%rax
sar    $0x1f,%ebp
and    (%rax),%ebp
jmp    11c7 <cDirectoryFsync+0x77>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e1b(%rip),%rax
jmp    11c5 <cDirectoryFsync+0x75>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOTDIR = global i32 0
define i32 @cDirectoryFsync(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %5
%7 = bitcast %struct.stat* %6 to i8*
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* @errno
store i32 %14, i32* %2
br label %43
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i32 @fstat(i32 %16, %struct.stat* %6)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* @errno
store i32 %23, i32* %2
br label %43
24: ; preds = %15
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @S_ISDIR(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* @ENOTDIR
store i32 %32, i32* %2
br label %43
33: ; preds = %24
%34 = load i32, i32* %4
%35 = call i32 @fsync(i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %5
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = load i32, i32* @errno
store i32 %41, i32* %2
br label %43
42: ; preds = %33
store i32 0, i32* %2
br label %43
43: ; preds = %42, %40, %29, %20, %13
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOTDIR = global i32 0
define i32 @cDirectoryFsync(i8* nocapture %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i32, i32* @errno
br label %30
9: ; preds = %1
%10 = bitcast i64* %2 to %struct.stat*
%11 = call i32 @fstat(i32 %5, %struct.stat* %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @close(i32 %5)
%15 = load i32, i32* @errno
br label %30
16: ; preds = %9
%17 = bitcast i64* %2 to i32*
%18 = load i32, i32* %17
%19 = tail call i32 @S_ISDIR(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = tail call i32 @close(i32 %5)
%23 = load i32, i32* @ENOTDIR
br label %30
24: ; preds = %16
%25 = tail call i32 @fsync(i32 %5)
%26 = tail call i32 @close(i32 %5)
%27 = icmp slt i32 %25, 0
%28 = load i32, i32* @errno
%29 = select i1 %27, i32 %28, i32 0
br label %30
30: ; preds = %24, %21, %13, %7
%31 = phi i32 [ %8, %7 ], [ %15, %13 ], [ %23, %21 ], [ %29, %24 ]
ret i32 %31
}