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

/* Variables and functions */
#define S_IFDIR 128
int S_IFMT;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
void check_dir(const char *dir) {
  struct stat st;
  if (stat(dir, &st) < 0) {
    perror("stat error");
    exit(1);
  }
  switch (st.st_mode & S_IFMT) {
  case S_IFDIR:
    if (st.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) {
      return;
    } else {
      fprintf(stderr, "directory is not readable\n");
      exit(1);
    }
  default:
    fprintf(stderr, "not a valid path\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<check_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    1179 <check_dir+0x39>
lea    0xe96(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%eax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
sub    $0x80,%eax
jne    11e5 <check_dir+0xa5>
jmp    1197 <check_dir+0x57>
mov    -0x10(%rbp),%eax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e26(%rip),%rdx
or     (%rdx),%ecx
mov    0x2e15(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11c6 <check_dir+0x86>
add    $0x10,%rsp
pop    %rbp
ret
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<check_dir>:
push   %rax
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     1185 <check_dir+0x45>
mov    (%rsp),%eax
mov    0x2e88(%rip),%rcx
mov    (%rcx),%ecx
and    %eax,%ecx
cmp    $0x80,%ecx
jne    119b <check_dir+0x5b>
mov    0x2e55(%rip),%rcx
mov    0x2e5e(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e4b(%rip),%rcx
or     (%rcx),%edx
test   %eax,%edx
je     11ad <check_dir+0x6d>
pop    %rax
ret
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
jmp    11bd <check_dir+0x7d>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"stat error\00"
@S_IFMT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"directory is not readable\0A\00"
@.str.2 = [18 x i8] c"not a valid path\0A\00"
define void @check_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @stat(i8* %4, %struct.stat* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @S_IFMT
%14 = and i32 %12, %13
switch i32 %14, label %30 [
i32 128, label %15
]
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IROTH
%22 = or i32 %20, %21
%23 = and i32 %17, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %15
ret void
26: ; preds = %15
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %10
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"stat error\00"
@S_IFMT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"directory is not readable\0A\00"
@.str.2 = [18 x i8] c"not a valid path\0A\00"
define void @check_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @S_IFMT
%13 = and i32 %12, %11
%14 = icmp eq i32 %13, 128
br i1 %14, label %15, label %28
15: ; preds = %9
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IRGRP
%18 = or i32 %17, %16
%19 = load i32, i32* @S_IROTH
%20 = or i32 %18, %19
%21 = and i32 %20, %11
%22 = icmp eq i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %15
ret void
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %9
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
}