#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 */
int O_CLOEXEC;
int O_DIRECTORY;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ *__opendir_common(int, char const *, int);
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int);
DIR *__opendir2(const char *name, int flags) {
  DIR *dirp;
  int fd;
  if ((fd = open(name, O_RDONLY | O_DIRECTORY | O_NONBLOCK | O_CLOEXEC)) == -1)
    return NULL;
  dirp = __opendir_common(fd, name, flags);
  if (dirp == NULL) {
    int serrno = errno;
    (void)close(fd);
    errno = serrno;
  }
  return dirp;
}


x86 assembly (clang, -O0):
<__opendir2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    0x2e7d(%rip),%rax
or     (%rax),%esi
mov    0x2e8c(%rip),%rax
or     (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1185 <__opendir2+0x55>
movq   $0x0,-0x8(%rbp)
jmp    11cb <__opendir2+0x9b>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <__opendir_common@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <__opendir2+0x93>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%ecx
mov    0x2e07(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__opendir2>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ea5(%rip),%rcx
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e9b(%rip),%rcx
or     (%rcx),%eax
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e5c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1192 <__opendir2+0x62>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <__opendir_common@plt>
test   %rax,%rax
jne    1194 <__opendir2+0x64>
mov    0x2e43(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@O_NONBLOCK = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i32 0
define i32* @__opendir2(i8* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_DIRECTORY
%12 = or i32 %10, %11
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %12, %13
%15 = load i32, i32* @O_CLOEXEC
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %9, i32 %16)
store i32 %17, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
store i32* null, i32** %3
br label %34
20: ; preds = %2
%21 = load i32, i32* %7
%22 = load i8*, i8** %4
%23 = load i32, i32* %5
%24 = call i32* @__opendir_common(i32 %21, i8* %22, i32 %23)
store i32* %24, i32** %6
%25 = load i32*, i32** %6
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i32, i32* @errno
store i32 %28, i32* %8
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %8
store i32 %31, i32* @errno
br label %32
32: ; preds = %27, %20
%33 = load i32*, i32** %6
store i32* %33, i32** %3
br label %34
34: ; preds = %32, %19
%35 = load i32*, i32** %3
ret i32* %35
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@O_NONBLOCK = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i32 0
define i32* @__opendir2(i8* %0, i32 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_DIRECTORY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %5, %6
%8 = load i32, i32* @O_CLOEXEC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = tail call i32* @__opendir_common(i32 %10, i8* %0, i32 %1)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 @close(i32 %10)
store i32 %16, i32* @errno
br label %18
18: ; preds = %12, %15, %2
%19 = phi i32* [ null, %2 ], [ null, %15 ], [ %13, %12 ]
ret i32* %19
}