#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 */
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
#define EACCES 133
#define EMFILE 132
#define ENFILE 131
#define ENOENT 130
#define ENOMEM 129
#define ENOTDIR 128
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, ...);
int dir_exists(const char *path) {
  DIR *dir = opendir(path);
  if (dir == NULL) {
    printf("Error openning directory \"%s\", error was:\n  ", path);
    switch (errno) {
    case EACCES:
      printf("Access denied.\n");
      closedir(dir);
      return -1;
    case EMFILE:
      printf("Too many file descriptors in use by process.\n");
      closedir(dir);
      return -1;
    case ENFILE:
      printf("Too many files are currently open in the system.\n");
      closedir(dir);
      break;
    case ENOENT:
      printf("Directory does not exist, or is an empty string.\n");
      closedir(dir);
      return -1;
    case ENOMEM:
      printf("Insufficient memory to complete the operation..\n");
      closedir(dir);
      return -1;
    default:
    case ENOTDIR:
      printf("\"%s\" is not a directory.\n", path);
      closedir(dir);
      return -1;
    }
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<dir_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    126e <dir_exists+0x13e>
mov    -0x10(%rbp),%rsi
lea    0xeb9(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x5,%eax
ja     123d <dir_exists+0x10d>
mov    -0x20(%rbp),%rax
lea    0xe74(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <dir_exists+0x14e>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <dir_exists+0x14e>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
jmp    1269 <dir_exists+0x139>
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <dir_exists+0x14e>
lea    0xec6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <dir_exists+0x14e>
jmp    1242 <dir_exists+0x112>
mov    -0x10(%rbp),%rsi
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <dir_exists+0x14e>
jmp    126e <dir_exists+0x13e>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_exists>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <opendir@plt>
mov    %rax,%r14
xor    %r15d,%r15d
test   %rax,%rax
jne    11f4 <dir_exists+0xb4>
lea    0xeb1(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e6c(%rip),%rcx
mov    $0xffffff7f,%eax
add    (%rcx),%eax
cmp    $0x4,%eax
ja     1199 <dir_exists+0x59>
lea    0xe79(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xec4(%rip),%rdi
jmp    11e6 <dir_exists+0xa6>
lea    0xea1(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%r15d
jmp    11f4 <dir_exists+0xb4>
lea    0xecf(%rip),%rdi
jmp    11e6 <dir_exists+0xa6>
lea    0xef7(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
xor    %edi,%edi
call   1060 <closedir@plt>
jmp    11f4 <dir_exists+0xb4>
lea    0xf10(%rip),%rdi
jmp    11e6 <dir_exists+0xa6>
lea    0xf34(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r15d
xor    %r14d,%r14d
mov    %r14,%rdi
call   1060 <closedir@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [45 x i8] c"Error openning directory \22%s\22, error was:\0A \00"
@errno = global i32 0
@.str.1 = [16 x i8] c"Access denied.\0A\00"
@.str.2 = [46 x i8] c"Too many file descriptors in use by process.\0A\00"
@.str.3 = [50 x i8] c"Too many files are currently open in the system.\0A\00"
@.str.4 = [50 x i8] c"Directory does not exist, or is an empty string.\0A\00"
@.str.5 = [49 x i8] c"Insufficient memory to complete the operation..\0A\00"
@.str.6 = [26 x i8] c"\22%s\22 is not a directory.\0A\00"
define i32 @dir_exists(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32* @opendir(i8* %5)
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %40
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @errno
switch i32 %12, label %33 [
i32 133, label %13
i32 132, label %17
i32 131, label %21
i32 130, label %25
i32 129, label %29
i32 128, label %34
]
13: ; preds = %9
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32*, i32** %4
%16 = call i32 @closedir(i32* %15)
store i32 -1, i32* %2
br label %43
17: ; preds = %9
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32*, i32** %4
%20 = call i32 @closedir(i32* %19)
store i32 -1, i32* %2
br label %43
21: ; preds = %9
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32*, i32** %4
%24 = call i32 @closedir(i32* %23)
br label %39
25: ; preds = %9
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32*, i32** %4
%28 = call i32 @closedir(i32* %27)
store i32 -1, i32* %2
br label %43
29: ; preds = %9
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32*, i32** %4
%32 = call i32 @closedir(i32* %31)
store i32 -1, i32* %2
br label %43
33: ; preds = %9
br label %34
34: ; preds = %9, %33
%35 = load i8*, i8** %3
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %35)
%37 = load i32*, i32** %4
%38 = call i32 @closedir(i32* %37)
store i32 -1, i32* %2
br label %43
39: ; preds = %21
br label %40
40: ; preds = %39, %1
%41 = load i32*, i32** %4
%42 = call i32 @closedir(i32* %41)
store i32 0, i32* %2
br label %43
43: ; preds = %40, %34, %29, %25, %17, %13
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [45 x i8] c"Error openning directory \22%s\22, error was:\0A \00"
@errno = global i32 0
@.str.6 = [26 x i8] c"\22%s\22 is not a directory.\0A\00"
@str = [48 x i8] c"Insufficient memory to complete the operation..\00"
@str.7 = [49 x i8] c"Directory does not exist, or is an empty string.\00"
@str.8 = [49 x i8] c"Too many files are currently open in the system.\00"
@str.9 = [45 x i8] c"Too many file descriptors in use by process.\00"
@str.10 = [15 x i8] c"Access denied.\00"
define i32 @dir_exists(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %20
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = load i32, i32* @errno
switch i32 %6, label %18 [
i32 133, label %7
i32 132, label %9
i32 131, label %11
i32 130, label %14
i32 129, label %16
]
7: ; preds = %4
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
br label %20
9: ; preds = %4
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.9, i64 0, i64 0))
br label %20
11: ; preds = %4
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.8, i64 0, i64 0))
%13 = tail call i32 @closedir(i32* null)
br label %20
14: ; preds = %4
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.7, i64 0, i64 0))
br label %20
16: ; preds = %4
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %20
18: ; preds = %4
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %0)
br label %20
20: ; preds = %1, %11, %18, %16, %14, %9, %7
%21 = phi i32* [ null, %18 ], [ null, %16 ], [ null, %14 ], [ null, %9 ], [ null, %7 ], [ %2, %11 ], [ %2, %1 ]
%22 = phi i32 [ -1, %18 ], [ -1, %16 ], [ -1, %14 ], [ -1, %9 ], [ -1, %7 ], [ 0, %11 ], [ 0, %1 ]
%23 = tail call i32 @closedir(i32* %21)
ret i32 %22
}