#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;
};

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ ENOENT;
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ realpath(char const *, char *);
int dexists(const char *path) {
  char buf[PATH_MAX];
  struct stat info;
  realpath(path, &buf[0]);
  if (lstat(buf, &info) != 0) {
    if (errno == ENOENT || errno == EACCES)
      return 0;
  }
  return S_ISDIR(info.st_mode) ? 1 : 0;
}


x86 assembly (clang, -O0):
<dexists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <realpath@plt>
mov    -0x38(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
je     11d4 <dexists+0xa4>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
je     11bc <dexists+0x8c>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11cf <dexists+0x9f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11f7 <dexists+0xc7>
jmp    11d4 <dexists+0xa4>
mov    -0x28(%rbp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dexists>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2eb3(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1050 <realpath@plt>
lea    -0x10(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <lstat@plt>
test   %rax,%rax
je     118d <dexists+0x5d>
mov    0x2e60(%rip),%rax
mov    (%rax),%rcx
mov    0x2e46(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     11a0 <dexists+0x70>
mov    0x2e58(%rip),%rdx
cmp    (%rdx),%rcx
je     11a0 <dexists+0x70>
mov    -0x10(%rbp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EACCES = global i64 0
define i32 @dexists(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.stat
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %11, i64 0
%14 = call i32 @realpath(i8* %12, i8* %13)
%15 = call i64 @lstat(i8* %11, %struct.stat* %6)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @EACCES
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21, %17
store i32 0, i32* %2
store i32 1, i32* %7
br label %34
26: ; preds = %21
br label %27
27: ; preds = %26, %1
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @S_ISDIR(i32 %29)
%31 = icmp ne i64 %30, 0
%32 = zext i1 %31 to i64
%33 = select i1 %31, i32 1, i32 0
store i32 %33, i32* %2
store i32 1, i32* %7
br label %34
34: ; preds = %27, %25
%35 = load i8*, i8** %4
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EACCES = global i64 0
define i32 @dexists(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast %struct.stat* %2 to i8*
%7 = call i32 @realpath(i8* %0, i8* %5)
%8 = call i64 @lstat(i8* %5, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
%14 = load i64, i64* @EACCES
%15 = icmp eq i64 %11, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %23, label %17
17: ; preds = %10, %1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i64 @S_ISDIR(i32 %19)
%21 = icmp ne i64 %20, 0
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %10, %17
%24 = phi i32 [ %22, %17 ], [ 0, %10 ]
ret i32 %24
}