#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 */
scalar_t__ EACCES;
scalar_t__ ENOENT;
scalar_t__ ENOTDIR;
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ strerror(scalar_t__);
DIR *checked_opendir(const char *path) {
  DIR *d = opendir(path);
  if (d == NULL && errno != EACCES && errno != ENOENT && errno != ENOTDIR) {
    errx(1, "Failed to open a directory: %s", strerror(errno));
  }
  return (d);
}


x86 assembly (clang, -O0):
<checked_opendir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c4 <checked_opendir+0x94>
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <checked_opendir+0x94>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <checked_opendir+0x94>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <checked_opendir+0x94>
mov    0x2e27(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe41(%rip),%rsi
call   1040 <errx@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_opendir>:
push   %rbx
call   1030 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1184 <checked_opendir+0x54>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rdi
mov    0x2e91(%rip),%rax
cmp    (%rax),%rdi
je     1184 <checked_opendir+0x54>
mov    0x2e65(%rip),%rax
cmp    (%rax),%rdi
je     1184 <checked_opendir+0x54>
mov    0x2e91(%rip),%rax
cmp    (%rax),%rdi
je     1184 <checked_opendir+0x54>
call   1050 <strerror@plt>
lea    0xe88(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1040 <errx@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@EACCES = global i64 0
@ENOENT = global i64 0
@ENOTDIR = global i64 0
@.str = [31 x i8] c"Failed to open a directory: %s\00"
define i32* @checked_opendir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32* @opendir(i8* %4)
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %24
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @EACCES
%11 = icmp ne i64 %9, %10
br i1 %11, label %12, label %24
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOTDIR
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = call i32 @strerror(i64 %21)
%23 = call i32 @errx(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %20, %16, %12, %8, %1
%25 = load i32*, i32** %3
ret i32* %25
}

LLVM IR (-O2):
@errno = global i64 0
@EACCES = global i64 0
@ENOENT = global i64 0
@ENOTDIR = global i64 0
@.str = [31 x i8] c"Failed to open a directory: %s\00"
define noalias i32* @checked_opendir(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %17
4: ; preds = %1
%5 = load i64, i64* @errno
%6 = load i64, i64* @EACCES
%7 = icmp eq i64 %5, %6
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %5, %8
%10 = select i1 %7, i1 true, i1 %9
%11 = load i64, i64* @ENOTDIR
%12 = icmp eq i64 %5, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %17, label %14
14: ; preds = %4
%15 = tail call i32 @strerror(i64 %5)
%16 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %14, %4, %1
ret i32* %2
}