#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__ EBADF;
scalar_t__ ENOENT;
scalar_t__ close(int /*<<< orphan*/);
int /*<<< orphan*/ dirfd(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(void) {
  DIR *dirp;
  struct dirent *ent;
  dirp = opendir("/tmp");
  if (dirp == NULL) {
    if (errno == ENOENT)
      exit(0);
    perror("opendir");
    exit(1);
  }
  if (close(dirfd(dirp)) != 0) {
    puts("could not close directory file descriptor");
    return 0;
  }
  ent = readdir(dirp);
  return ent != NULL || errno != EBADF;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <main+0x61>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    11bb <main+0x4b>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe43(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1060 <dirfd@plt>
mov    %eax,%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     1203 <main+0x93>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <main+0xd5>
mov    -0x10(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
mov    $0x1,%al
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jne    123a <main+0xca>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe88(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11d2 <main+0x62>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <dirfd@plt>
mov    %eax,%edi
call   1050 <close@plt>
test   %rax,%rax
je     11a9 <main+0x39>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    %rbx,%rdi
call   1070 <readdir@plt>
mov    0x2e28(%rip),%rcx
mov    (%rcx),%rdx
mov    0x2e16(%rip),%rcx
xor    (%rcx),%rdx
mov    %rax,%rcx
xor    %eax,%eax
or     %rcx,%rdx
setne  %al
pop    %rbx
ret
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    11ef <main+0x7f>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [5 x i8] c"/tmp\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [8 x i8] c"opendir\00"
@.str.2 = [42 x i8] c"could not close directory file descriptor\00"
@EBADF = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
store i32 0, i32* %1
%4 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %16
7: ; preds = %0
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %7
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32*, i32** %2
%18 = call i32 @dirfd(i32* %17)
%19 = call i64 @close(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @puts(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %35
23: ; preds = %16
%24 = load i32*, i32** %2
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %3
%26 = load %struct.dirent*, %struct.dirent** %3
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %32, label %28
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = load i64, i64* @EBADF
%31 = icmp ne i64 %29, %30
br label %32
32: ; preds = %28, %23
%33 = phi i1 [ true, %23 ], [ %31, %28 ]
%34 = zext i1 %33 to i32
store i32 %34, i32* %1
br label %35
35: ; preds = %32, %21
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [5 x i8] c"/tmp\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [8 x i8] c"opendir\00"
@.str.2 = [42 x i8] c"could not close directory file descriptor\00"
@EBADF = global i64 0
define i32 @main() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %12
3: ; preds = %0
%4 = load i64, i64* @errno
%5 = load i64, i64* @ENOENT
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = tail call i32 @dirfd(i32* %1)
%14 = tail call i64 @close(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %26
18: ; preds = %12
%19 = tail call %struct.dirent* @readdir(i32* %1)
%20 = icmp ne %struct.dirent* %19, null
%21 = load i64, i64* @errno
%22 = load i64, i64* @EBADF
%23 = icmp ne i64 %21, %22
%24 = select i1 %20, i1 true, i1 %23
%25 = zext i1 %24 to i32
br label %26
26: ; preds = %18, %16
%27 = phi i32 [ 0, %16 ], [ %25, %18 ]
ret i32 %27
}