#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 dirent {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(scalar_t__);
struct dirent *checked_readdir(DIR *d) {
  struct dirent *e;
  errno = 0;
  e = readdir(d);
  if (e == NULL && errno != 0) {
    errx(1, "Failed to read a directory: %s", strerror(errno));
  }
  return (e);
}


x86 assembly (clang, -O0):
<checked_readdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1195 <checked_readdir+0x65>
mov    0x2e77(%rip),%rax
cmpq   $0x0,(%rax)
je     1195 <checked_readdir+0x65>
mov    0x2e66(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe70(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_readdir>:
push   %r14
push   %rbx
push   %rax
mov    0x2ea5(%rip),%r14
movq   $0x0,(%r14)
call   1040 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116f <checked_readdir+0x3f>
mov    (%r14),%rdi
test   %rdi,%rdi
je     116f <checked_readdir+0x3f>
call   1050 <strerror@plt>
lea    0xe9d(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <errx@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@errno = global i64 0
@.str = [31 x i8] c"Failed to read a directory: %s\00"
define %struct.dirent* @checked_readdir(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.dirent*
store i32* %0, i32** %2
store i64 0, i64* @errno
%4 = load i32*, i32** %2
%5 = call %struct.dirent* @readdir(i32* %4)
store %struct.dirent* %5, %struct.dirent** %3
%6 = load %struct.dirent*, %struct.dirent** %3
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %8, label %15
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = call i32 @strerror(i64 %12)
%14 = call i32 @errx(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %8, %1
%16 = load %struct.dirent*, %struct.dirent** %3
ret %struct.dirent* %16
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@errno = global i64 0
@.str = [31 x i8] c"Failed to read a directory: %s\00"
define %struct.dirent* @checked_readdir(i32* %0) {
store i64 0, i64* @errno
%2 = tail call %struct.dirent* @readdir(i32* %0)
%3 = icmp eq %struct.dirent* %2, null
%4 = load i64, i64* @errno
%5 = icmp ne i64 %4, 0
%6 = select i1 %3, i1 %5, i1 false
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @strerror(i64 %4)
%9 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %7, %1
ret %struct.dirent* %2
}