#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 ELOOP 132
#define EMFILE 131
#define ENOENT 130
#define ENOMEM 129
#define ENOTDIR 128
int errno;
int /*<<< orphan*/ errors_printMessageAndExit(char *, ...);
int /*<<< orphan*/ *opendir(char const *);
DIR *openDirectory(const char *dirName) {
  DIR *result = opendir(dirName);
  if (result == NULL) {
    switch (errno) {
    case EACCES:
      errors_printMessageAndExit("No permission to read directory '%s'",
                                 dirName);
    case ELOOP:
      errors_printMessageAndExit("Too many symbolic links had to be resolved "
                                 "while trying to look up '%s'",
                                 dirName);
    case EMFILE:
      errors_printMessageAndExit("\x1B[7mToo many files open\x1B[27m");
    case ENOENT:
      errors_printMessageAndExit("No directory named '%s' exists", dirName);
    case ENOMEM:
      errors_printMessageAndExit(
          "Not enough memory available to open directory '%s'", dirName);
    case ENOTDIR:
      errors_printMessageAndExit("'%s' is not a directory", dirName);
    default:
      errors_printMessageAndExit(
          "An unknown error occurred while trying to open directory '%s'",
          dirName);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<openDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f2 <openDirectory+0xd2>
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x5,%eax
ja     11db <openDirectory+0xbb>
mov    -0x18(%rbp),%rax
lea    0xe96(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x8(%rbp),%rsi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <errors_printMessageAndExit@plt>
mov    -0x8(%rbp),%rsi
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1040 <errors_printMessageAndExit@plt>
lea    0xee7(%rip),%rdi
mov    $0x0,%al
call   1040 <errors_printMessageAndExit@plt>
mov    -0x8(%rbp),%rsi
lea    0xef2(%rip),%rdi
mov    $0x0,%al
call   1040 <errors_printMessageAndExit@plt>
mov    -0x8(%rbp),%rsi
lea    0xeff(%rip),%rdi
mov    $0x0,%al
call   1040 <errors_printMessageAndExit@plt>
mov    -0x8(%rbp),%rsi
lea    0xf20(%rip),%rdi
mov    $0x0,%al
call   1040 <errors_printMessageAndExit@plt>
mov    -0x8(%rbp),%rsi
lea    0xf26(%rip),%rdi
mov    $0x0,%al
call   1040 <errors_printMessageAndExit@plt>
jmp    11f2 <openDirectory+0xd2>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openDirectory>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <opendir@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11cd <openDirectory+0xad>
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x5,%eax
ja     11bc <openDirectory+0x9c>
lea    0xeb0(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xeb8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <errors_printMessageAndExit@plt>
lea    0xecc(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <errors_printMessageAndExit@plt>
lea    0xf03(%rip),%rdi
xor    %eax,%eax
call   1040 <errors_printMessageAndExit@plt>
lea    0xf12(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <errors_printMessageAndExit@plt>
lea    0xf20(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <errors_printMessageAndExit@plt>
lea    0xf42(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <errors_printMessageAndExit@plt>
lea    0xf49(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <errors_printMessageAndExit@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [37 x i8] c"No permission to read directory '%s'\00"
@.str.1 = [72 x i8] c"Too many symbolic links had to be resolved while trying to look up '%s'\00"
@.str.2 = [29 x i8] c"\1B[7mToo many files open\1B[27m\00"
@.str.3 = [31 x i8] c"No directory named '%s' exists\00"
@.str.4 = [51 x i8] c"Not enough memory available to open directory '%s'\00"
@.str.5 = [24 x i8] c"'%s' is not a directory\00"
@.str.6 = [62 x i8] c"An unknown error occurred while trying to open directory '%s'\00"
define i32* @openDirectory(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32* @opendir(i8* %4)
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %31
8: ; preds = %1
%9 = load i32, i32* @errno
switch i32 %9, label %27 [
i32 133, label %10
i32 132, label %13
i32 131, label %16
i32 130, label %18
i32 129, label %21
i32 128, label %24
]
10: ; preds = %8
%11 = load i8*, i8** %2
%12 = call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %8, %10
%14 = load i8*, i8** %2
%15 = call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %8, %13
%17 = call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %8, %16
%19 = load i8*, i8** %2
%20 = call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %8, %18
%22 = load i8*, i8** %2
%23 = call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %8, %21
%25 = load i8*, i8** %2
%26 = call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %8, %24
%28 = load i8*, i8** %2
%29 = call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.6, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27
br label %31
31: ; preds = %30, %1
%32 = load i32*, i32** %3
ret i32* %32
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [37 x i8] c"No permission to read directory '%s'\00"
@.str.1 = [72 x i8] c"Too many symbolic links had to be resolved while trying to look up '%s'\00"
@.str.2 = [29 x i8] c"\1B[7mToo many files open\1B[27m\00"
@.str.3 = [31 x i8] c"No directory named '%s' exists\00"
@.str.4 = [51 x i8] c"Not enough memory available to open directory '%s'\00"
@.str.5 = [24 x i8] c"'%s' is not a directory\00"
@.str.6 = [62 x i8] c"An unknown error occurred while trying to open directory '%s'\00"
define noalias i32* @openDirectory(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 = load i32, i32* @errno
switch i32 %5, label %18 [
i32 133, label %6
i32 132, label %8
i32 131, label %10
i32 130, label %12
i32 129, label %14
i32 128, label %16
]
6: ; preds = %4
%7 = tail call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %4, %6
%9 = tail call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %4, %8
%11 = tail call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %4, %10
%13 = tail call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %14
14: ; preds = %4, %12
%15 = tail call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %16
16: ; preds = %4, %14
%17 = tail call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %4, %16
%19 = tail call i32 (i8*, ...) @errors_printMessageAndExit(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.6, i64 0, i64 0), i8* %0)
br label %20
20: ; preds = %18, %1
ret i32* %2
}