#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 /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
scalar_t__ fsync(int);
int open(char *, int /*<<< orphan*/);
int dirsync(char *dir) {
  int fd = -1;
  if ((fd = open(dir, O_RDONLY)) < 0)
    goto error;
  if (fsync(fd) < 0)
    goto error;
  if (close(fd) < 0)
    goto error;
  return 0;
error:
  if (fd != -1)
    (void)close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<dirsync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1166 <dirsync+0x36>
jmp    11a0 <dirsync+0x70>
mov    -0x14(%rbp),%edi
call   1040 <fsync@plt>
cmp    $0x0,%rax
jge    117d <dirsync+0x4d>
jmp    11a0 <dirsync+0x70>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    1194 <dirsync+0x64>
jmp    11a0 <dirsync+0x70>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <dirsync+0x89>
cmpl   $0xffffffff,-0x14(%rbp)
je     11b2 <dirsync+0x82>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirsync>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1164 <dirsync+0x34>
mov    %ebx,%edi
call   1040 <fsync@plt>
test   %rax,%rax
js     1169 <dirsync+0x39>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1169 <dirsync+0x39>
pop    %rbx
ret
cmp    $0xffffffff,%ebx
je     1170 <dirsync+0x40>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @dirsync(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
br label %21
10: ; preds = %1
%11 = load i32, i32* %4
%12 = call i64 @fsync(i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
br label %21
15: ; preds = %10
%16 = load i32, i32* %4
%17 = call i64 @close(i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
br label %21
20: ; preds = %15
store i32 0, i32* %2
br label %28
21: ; preds = %19, %14, %9
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %4
%26 = call i64 @close(i32 %25)
br label %27
27: ; preds = %24, %21
store i32 -1, i32* %2
br label %28
28: ; preds = %27, %20
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @dirsync(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %11, label %5
5: ; preds = %1
%6 = tail call i64 @fsync(i32 %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %5
%9 = tail call i64 @close(i32 %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %13, label %15
11: ; preds = %1
%12 = icmp eq i32 %3, -1
br i1 %12, label %15, label %13
13: ; preds = %5, %8, %11
%14 = tail call i64 @close(i32 %3)
br label %15
15: ; preds = %11, %13, %8
%16 = phi i32 [ 0, %8 ], [ -1, %13 ], [ -1, %11 ]
ret i32 %16
}