#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__ ENOENT;
int S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ stat(char const *, struct stat *);
bool dir_exists(const char *path) {
  struct stat s;
  if (0 == stat(path, &s))
    return S_ISDIR(s.st_mode);
  else if (ENOENT == errno)
    return 0;
  else
    assert(0);
}


x86 assembly (clang, -O0):
<dir_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1167 <dir_exists+0x37>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
mov    %eax,-0x4(%rbp)
jmp    1199 <dir_exists+0x69>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
jne    118d <dir_exists+0x5d>
movl   $0x0,-0x4(%rbp)
jmp    1199 <dir_exists+0x69>
xor    %edi,%edi
call   1040 <assert@plt>
jmp    1199 <dir_exists+0x69>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_exists>:
push   %rax
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     115f <dir_exists+0x2f>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e91(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     115d <dir_exists+0x2d>
xor    %edi,%edi
call   1040 <assert@plt>
pop    %rcx
ret
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOENT = global i64 0
@errno = global i64 0
define i32 @dir_exists(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @stat(i8* %5, %struct.stat* %4)
%7 = icmp eq i64 0, %6
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%10 = load i32, i32* %9
%11 = call i32 @S_ISDIR(i32 %10)
store i32 %11, i32* %2
br label %20
12: ; preds = %1
%13 = load i64, i64* @ENOENT
%14 = load i64, i64* @errno
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 0, i32* %2
br label %20
17: ; preds = %12
%18 = call i32 @assert(i32 0)
br label %19
19: ; preds = %17
br label %20
20: ; preds = %8, %16, %19
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOENT = global i64 0
@errno = global i64 0
define i32 @dir_exists(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i32 @S_ISDIR(i32 %8)
br label %16
10: ; preds = %1
%11 = load i64, i64* @ENOENT
%12 = load i64, i64* @errno
%13 = icmp eq i64 %11, %12
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = call i32 @assert(i32 0)
br label %16
16: ; preds = %10, %14, %6
%17 = phi i32 [ %9, %6 ], [ undef, %14 ], [ 0, %10 ]
ret i32 %17
}