#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;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int dircheck(char *dir) {
  struct stat dir_stat;
  if (stat(dir, &dir_stat) == -1) {
    if (errno == ENOENT) {
      fprintf(stderr, "No such directory: %s\n", dir);
      return 1;
    } else {
      perror("Stat Error");
      return 1;
    }
  }
  if (S_ISDIR(dir_stat.st_mode))
    return 0;
  else {
    fprintf(stderr, "Not a directory: %s\n", dir);
    return 1;
  }
}


x86 assembly (clang, -O0):
<dircheck>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    11b9 <dircheck+0x79>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
jne    11a1 <dircheck+0x61>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe70(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f7 <dircheck+0xb7>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f7 <dircheck+0xb7>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11d7 <dircheck+0x97>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <dircheck+0xb7>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dircheck>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     117c <dircheck+0x3c>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11af <dircheck+0x6f>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xea8(%rip),%rsi
jmp    11a2 <dircheck+0x62>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    11b5 <dircheck+0x75>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
jmp    11aa <dircheck+0x6a>

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"
@.str.1 = [11 x i8] c"Stat Error\00"
@.str.2 = [21 x i8] c"Not a directory: %s\0A\00"
define i32 @dircheck(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @stat(i8* %5, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %18
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 1, i32* %2
br label %28
16: ; preds = %8
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %28
18: ; preds = %1
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @S_ISDIR(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 0, i32* %2
br label %28
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %3
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %26)
store i32 1, i32* %2
br label %28
28: ; preds = %24, %23, %16, %12
%29 = load i32, i32* %2
ret i32 %29
}

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"
@.str.1 = [11 x i8] c"Stat Error\00"
@.str.2 = [21 x i8] c"Not a directory: %s\0A\00"
define i32 @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 %15
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 %13
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)
br label %23
13: ; preds = %6
%14 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %23
15: ; preds = %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i64 @S_ISDIR(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %15, %20, %13, %10
%24 = phi i32 [ 1, %10 ], [ 1, %13 ], [ 1, %20 ], [ 0, %15 ]
ret i32 %24
}