#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_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopendir(int);
int open(char const *, int);
DIR *opendir(const char *path) {
  int fd = open(path, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
  if (fd < 0)
    return NULL;
  DIR *result = fdopendir(fd);
  if (!result) {
    close(fd);
  }
  return result;
}


x86 assembly (clang, -O0):
<opendir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
or     (%rax),%esi
mov    0x2e7f(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117a <opendir+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <opendir+0x71>
mov    -0x14(%rbp),%edi
call   1050 <fdopendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1199 <opendir+0x69>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opendir>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e8e(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
test   %eax,%eax
js     116c <opendir+0x3c>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <fdopendir@plt>
test   %rax,%rax
je     1165 <opendir+0x35>
pop    %rbx
ret
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_DIRECTORY = global i32 0
define i32* @opendir(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_CLOEXEC
%9 = or i32 %7, %8
%10 = load i32, i32* @O_DIRECTORY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32* null, i32** %2
br label %26
16: ; preds = %1
%17 = load i32, i32* %4
%18 = call i32* @fdopendir(i32 %17)
store i32* %18, i32** %5
%19 = load i32*, i32** %5
%20 = icmp ne i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
br label %24
24: ; preds = %21, %16
%25 = load i32*, i32** %5
store i32* %25, i32** %2
br label %26
26: ; preds = %24, %15
%27 = load i32*, i32** %2
ret i32* %27
}

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