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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
void dircheck(char *dir) {
  struct stat dir_stat;
  if (stat(dir, &dir_stat) == -1) {
    if (errno == ENOENT) {
      fprintf(stderr, "No such directory: %s\n", dir);
      exit(EXIT_FAILURE);
    } else {
      perror("Stat Error");
      exit(EXIT_FAILURE);
    }
  }
  if (S_ISDIR(dir_stat.st_mode))
    exit(EXIT_SUCCESS);
  else {
    fprintf(stderr, "Not a directory: %s\n", dir);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<dircheck>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    11cd <dircheck+0x7d>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
jne    11b3 <dircheck+0x63>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe60(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe5d(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11ed <dircheck+0x9d>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe21(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<dircheck>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    118f <dircheck+0x3f>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    11b7 <dircheck+0x67>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
jmp    11ad <dircheck+0x5d>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
jne    11d1 <dircheck+0x81>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
jmp    11c3 <dircheck+0x73>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"No such directory: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"Stat Error\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [21 x i8] c"Not a directory: %s\0A\00"
define void @dircheck(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @stat(i8* %4, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %21
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %2
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %7
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %1
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @S_ISDIR(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %2
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"No such directory: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"Stat Error\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [21 x i8] c"Not a directory: %s\0A\00"
define void @dircheck(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %19
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %6
%16 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %1
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i64 @S_ISDIR(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* @EXIT_SUCCESS
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
}