#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*/ *fdopendir(int);
int openat(int, char const *, int);
DIR *opendirat(int base_dir_fd, const char *name) {
  if (base_dir_fd < 0 || !name || *name == '/')
    return NULL;
  int new_dir_fd = openat(base_dir_fd, name, O_RDONLY | O_DIRECTORY);
  if (new_dir_fd < 0)
    return NULL;
  return fdopendir(new_dir_fd);
}


x86 assembly (clang, -O0):
<opendirat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1154 <opendirat+0x34>
cmpq   $0x0,-0x18(%rbp)
je     1154 <opendirat+0x34>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1161 <opendirat+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11a5 <opendirat+0x85>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    0x2e60(%rip),%rax
or     (%rax),%edx
call   1030 <openat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1199 <opendirat+0x79>
movq   $0x0,-0x8(%rbp)
jmp    11a5 <opendirat+0x85>
mov    -0x1c(%rbp),%edi
call   1040 <fdopendir@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opendirat>:
push   %rax
test   %edi,%edi
js     1152 <opendirat+0x32>
test   %rsi,%rsi
je     1152 <opendirat+0x32>
cmpb   $0x2f,(%rsi)
je     1152 <opendirat+0x32>
mov    0x2eb2(%rip),%rax
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1030 <openat@plt>
test   %eax,%eax
js     1152 <opendirat+0x32>
mov    %eax,%edi
pop    %rax
jmp    1040 <fdopendir@plt>
xor    %eax,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
define i32* @opendirat(i32 %0, i8* %1) {
%3 = icmp sgt i32 %0, -1
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %17
6: ; preds = %2
%7 = load i8, i8* %1
%8 = icmp eq i8 %7, 47
br i1 %8, label %17, label %9
9: ; preds = %6
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_DIRECTORY
%12 = or i32 %11, %10
%13 = tail call i32 @openat(i32 %0, i8* %1, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = tail call i32* @fdopendir(i32 %13)
br label %17
17: ; preds = %15, %9, %2, %6
%18 = phi i32* [ null, %6 ], [ null, %2 ], [ %16, %15 ], [ null, %9 ]
ret i32* %18
}