#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_DIRECTORY;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ *fdopendir(int);
int open(char const *, int, int /*<<< orphan*/);
DIR *opendir(const char *name) {
  int fd, err;
  DIR *dp;
  fd = open(name, O_DIRECTORY | O_RDONLY, 0);
  if (fd < 0)
    return NULL;
  dp = fdopendir(fd);
  if (!dp) {
    err = errno;
    close(fd);
    errno = err;
  }
  return dp;
}


x86 assembly (clang, -O0):
<opendir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e98(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1173 <opendir+0x43>
movq   $0x0,-0x8(%rbp)
jmp    11b2 <opendir+0x82>
mov    -0x14(%rbp),%edi
call   1050 <fdopendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11aa <opendir+0x7a>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%ecx
mov    0x2e28(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opendir>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e9d(%rip),%rax
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1040 <open@plt>
test   %eax,%eax
js     117a <opendir+0x4a>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <fdopendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    117a <opendir+0x4a>
mov    0x2e65(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,(%r14)
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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