#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 /*<<< orphan*/ send_repl(int, char *);
DIR *ensure_dir_exists(int sock, const char *path) {
  DIR *dir = opendir(path);
  if (dir == NULL) {
    printf("Error openning directory \"%s\", error was:\n  ", path);
    send_repl(sock, "550 Requested action not taken.\r\n");
    switch (errno) {
    case EACCES:
      printf("Access denied.\n");
      closedir(dir);
      return NULL;
    case EMFILE:
      printf("Too many file descriptors in use by process.\n");
      closedir(dir);
      return NULL;
    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 NULL;
    case ENOMEM:
      printf("Insufficient memory to complete the operation..\n");
      closedir(dir);
      return NULL;
    default:
    case ENOTDIR:
      printf("\"%s\" is not a directory.\n", path);
      closedir(dir);
      return NULL;
    }
  }
  return dir;
}


x86 assembly (clang, -O0):
<ensure_dir_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1295 <ensure_dir_exists+0x155>
mov    -0x18(%rbp),%rsi
lea    0xea6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    0xec2(%rip),%rsi
call   1060 <send_repl@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x5,%eax
ja     1263 <ensure_dir_exists+0x123>
mov    -0x28(%rbp),%rax
lea    0xe52(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movq   $0x0,-0x8(%rbp)
jmp    129d <ensure_dir_exists+0x15d>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movq   $0x0,-0x8(%rbp)
jmp    129d <ensure_dir_exists+0x15d>
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
jmp    1290 <ensure_dir_exists+0x150>
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movq   $0x0,-0x8(%rbp)
jmp    129d <ensure_dir_exists+0x15d>
lea    0xec3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movq   $0x0,-0x8(%rbp)
jmp    129d <ensure_dir_exists+0x15d>
jmp    1268 <ensure_dir_exists+0x128>
mov    -0x18(%rbp),%rsi
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movq   $0x0,-0x8(%rbp)
jmp    129d <ensure_dir_exists+0x15d>
jmp    1295 <ensure_dir_exists+0x155>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_dir_exists>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <opendir@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11f9 <ensure_dir_exists+0xa9>
lea    0xea0(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xebc(%rip),%rsi
mov    %ebp,%edi
call   1070 <send_repl@plt>
mov    0x2e4d(%rip),%rcx
mov    $0xffffff7f,%eax
add    (%rcx),%eax
cmp    $0x4,%eax
ja     11b8 <ensure_dir_exists+0x68>
lea    0xe5a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xec7(%rip),%rdi
jmp    11ed <ensure_dir_exists+0x9d>
lea    0xea4(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f2 <ensure_dir_exists+0xa2>
lea    0xedb(%rip),%rdi
jmp    11ed <ensure_dir_exists+0x9d>
lea    0xf03(%rip),%rdi
jmp    11ed <ensure_dir_exists+0x9d>
lea    0xf2b(%rip),%rdi
jmp    11ed <ensure_dir_exists+0x9d>
lea    0xf4f(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <closedir@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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