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


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    0x2e95(%rip),%rax
or     (%rax),%esi
mov    0x2e74(%rip),%rax
or     (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
call   1030 <_open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1185 <__opendir2+0x55>
movq   $0x0,-0x8(%rbp)
jmp    11c7 <__opendir2+0x97>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <__opendir_common@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bf <__opendir2+0x8f>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <_close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e0b(%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    0x2ea6(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e83(%rip),%rcx
or     (%rcx),%eax
mov    %esi,%ebp
mov    0x2e60(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1030 <_open@plt>
cmp    $0xffffffff,%eax
je     118a <__opendir2+0x5a>
mov    %eax,%ebx
mov    %eax,%edi
mov    %ebp,%esi
call   1050 <__opendir_common@plt>
test   %rax,%rax
jne    118c <__opendir2+0x5c>
mov    0x2e4b(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1040 <_close@plt>
mov    %ebp,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@O_DIRECTORY = 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_NONBLOCK
%12 = or i32 %10, %11
%13 = load i32, i32* @O_DIRECTORY
%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* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
store i32* null, i32** %3
br label %33
20: ; preds = %2
%21 = load i32, i32* %6
%22 = load i32, i32* %5
%23 = call i32* @__opendir_common(i32 %21, i32 %22)
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i32, i32* @errno
store i32 %27, i32* %8
%28 = load i32, i32* %6
%29 = call i32 @_close(i32 %28)
%30 = load i32, i32* %8
store i32 %30, i32* @errno
br label %31
31: ; preds = %26, %20
%32 = load i32*, i32** %7
store i32* %32, i32** %3
br label %33
33: ; preds = %31, %19
%34 = load i32*, i32** %3
ret i32* %34
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@O_DIRECTORY = 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_NONBLOCK
%5 = or i32 %4, %3
%6 = load i32, i32* @O_DIRECTORY
%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, 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
}