#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 */
int ENOENT;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ debug(char *, char const *, char *);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int);
bool verify_optiondir(const char *directory) {
  struct stat buf;
  if (stat(directory, &buf) < 0) {
    int errcode = errno;
    if (errcode == ENOENT) {
      debug("verify_optiondir: stat of %s failed: %s\n", directory,
            strerror(errno));
      return false;
    } else {
      fprintf(stderr, "ERROR: stat of %s failed: %s\n", directory,
              strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  if (!S_ISDIR(buf.st_mode)) {
    fprintf(stderr, "ERROR: %s exists but is not a directory\n", directory);
    exit(EXIT_FAILURE);
  }
  return true;
}


x86 assembly (clang, -O0):
<verify_optiondir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    121e <verify_optiondir+0xbe>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%eax
jne    11d6 <verify_optiondir+0x76>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe3b(%rip),%rdi
call   1030 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    125f <verify_optiondir+0xff>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1258 <verify_optiondir+0xf8>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_optiondir>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11a2 <verify_optiondir+0x42>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    11cf <verify_optiondir+0x6f>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    0xeb1(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11fa <verify_optiondir+0x9a>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
cmp    (%rax),%edi
jne    11d6 <verify_optiondir+0x76>
call   1080 <strerror@plt>
lea    0xe3e(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <debug@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e03(%rip),%rax
mov    (%rax),%ebp
call   1080 <strerror@plt>
lea    0xe3e(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@ENOENT = global i32 0
@.str = [41 x i8] c"verify_optiondir: stat of %s failed: %s\0A\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"ERROR: stat of %s failed: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [41 x i8] c"ERROR: %s exists but is not a directory\0A\00"
define i32 @verify_optiondir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @stat(i8* %6, %struct.stat* %4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %1
%10 = load i32, i32* @errno
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = load i32, i32* @ENOENT
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @debug(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
store i32 0, i32* %2
br label %39
19: ; preds = %9
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %3
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %1
%28 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @S_ISDIR(i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %38, label %32
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %3
%35 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %27
store i32 1, i32* %2
br label %39
39: ; preds = %38, %14
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@ENOENT = global i32 0
@.str = [41 x i8] c"verify_optiondir: stat of %s failed: %s\0A\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"ERROR: stat of %s failed: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [41 x i8] c"ERROR: %s exists but is not a directory\0A\00"
define i32 @verify_optiondir(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 %19
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = load i32, i32* @ENOENT
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = call i8* @strerror(i32 %7)
%12 = call i32 @debug(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11)
br label %29
13: ; preds = %6
%14 = load i32, i32* @stderr
%15 = call i8* @strerror(i32 %7)
%16 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = 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 = call i32 @S_ISDIR(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19, %10
%30 = phi i32 [ 0, %10 ], [ 1, %19 ]
ret i32 %30
}