#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 {
  scalar_t__ st_mode;
};

/* Variables and functions */
scalar_t__ S_IFDIR;
int /*<<< orphan*/ printf(char *);
scalar_t__ stat(char const *, struct stat *);
int dirExists(const char *path) {
  if (path == NULL) {
    printf("ERROR: Could not read filesystem\n");
    return -1;
  }
  struct stat info;
  if (stat(path, &info) != 0) {
    return 0;
  } else if (info.st_mode && S_IFDIR) {
    return 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<dirExists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114f <dirExists+0x2f>
lea    0xec2(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a1 <dirExists+0x81>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1172 <dirExists+0x52>
movl   $0x0,-0x4(%rbp)
jmp    11a1 <dirExists+0x81>
cmpq   $0x0,-0x18(%rbp)
je     119a <dirExists+0x7a>
mov    0x2e54(%rip),%rax
cmpq   $0x0,(%rax)
je     119a <dirExists+0x7a>
movl   $0x1,-0x4(%rbp)
jmp    11a1 <dirExists+0x81>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirExists>:
push   %rax
test   %rdi,%rdi
je     113a <dirExists+0x1a>
mov    %rsp,%rsi
call   1040 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     114d <dirExists+0x2d>
pop    %rcx
ret
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
cmpq   $0x0,(%rsp)
setne  %al
mov    0x2e7c(%rip),%rcx
cmpq   $0x0,(%rcx)
setne  %cl
and    %al,%cl
movzbl %cl,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [34 x i8] c"ERROR: Could not read filesystem\0A\00"
@S_IFDIR = global i64 0
define i32 @dirExists(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %23
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @stat(i8* %10, %struct.stat* %4)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 0, i32* %2
br label %23
14: ; preds = %9
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i64, i64* %15
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i64, i64* @S_IFDIR
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 1, i32* %2
br label %23
22: ; preds = %18, %14
store i32 0, i32* %2
br label %23
23: ; preds = %22, %21, %13, %7
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@S_IFDIR = global i64 0
@str = [33 x i8] c"ERROR: Could not read filesystem\00"
define i32 @dirExists(i8* %0) {
%2 = alloca %struct.stat
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %20
6: ; preds = %1
%7 = bitcast %struct.stat* %2 to i8*
%8 = call i64 @stat(i8* %0, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %6
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i64, i64* %11
%13 = icmp ne i64 %12, 0
%14 = load i64, i64* @S_IFDIR
%15 = icmp ne i64 %14, 0
%16 = select i1 %13, i1 %15, i1 false
%17 = zext i1 %16 to i32
br label %18
18: ; preds = %10, %6
%19 = phi i32 [ 0, %6 ], [ %17, %10 ]
br label %20
20: ; preds = %18, %4
%21 = phi i32 [ -1, %4 ], [ %19, %18 ]
ret i32 %21
}