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

/* Variables and functions */
int S_IFDIR;
int S_IFMT;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int dir_exists(const char *path) {
  struct stat s;
  if (!path)
    return 0;
  if (stat(path, &s) != 0) {
    perror("stat");
    return 0;
  }
  if ((s.st_mode & S_IFMT) != S_IFDIR) {
    fprintf(stderr, "%s is not a directory!\n", path);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<dir_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1153 <dir_exists+0x23>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <dir_exists+0x99>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1182 <dir_exists+0x52>
lea    0xe8f(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <dir_exists+0x99>
mov    -0x18(%rbp),%eax
mov    0x2e54(%rip),%rcx
and    (%rcx),%eax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%eax
je     11c2 <dir_exists+0x92>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe54(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <dir_exists+0x99>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_exists>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
test   %rdi,%rdi
je     1185 <dir_exists+0x55>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
test   %rax,%rax
jne    118b <dir_exists+0x5b>
mov    0x2e8b(%rip),%rax
mov    (%rax),%ecx
and    0x8(%rsp),%ecx
mov    0x2e66(%rip),%rdx
mov    $0x1,%eax
cmp    (%rdx),%ecx
je     1185 <dir_exists+0x55>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe6e(%rip),%rdi
call   1050 <perror@plt>
jmp    1183 <dir_exists+0x53>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"%s is not a directory!\0A\00"
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 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i32 0, i32* %2
br label %26
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i64 @stat(i8* %9, %struct.stat* %4)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %26
14: ; preds = %8
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @S_IFMT
%18 = and i32 %16, %17
%19 = load i32, i32* @S_IFDIR
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %3
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 0, i32* %2
br label %26
25: ; preds = %14
store i32 1, i32* %2
br label %26
26: ; preds = %25, %21, %12, %7
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"%s is not a directory!\0A\00"
define i32 @dir_exists(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %20, label %5
5: ; preds = %1
%6 = call i64 @stat(i8* %0, %struct.stat* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %20
10: ; preds = %5
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @S_IFMT
%14 = and i32 %13, %12
%15 = load i32, i32* @S_IFDIR
%16 = icmp eq i32 %14, %15
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %20
20: ; preds = %10, %1, %17, %8
%21 = phi i32 [ 0, %8 ], [ 0, %17 ], [ 0, %1 ], [ 1, %10 ]
ret i32 %21
}