#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ ENOTDIR;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ *__opendir_common(int, char const *, int);
int _open(char const *, int);
int /*<<< orphan*/ errno;
scalar_t__ stat(char const *, struct stat *);
DIR *__opendir2(const char *name, int flags) {
  int fd;
  struct stat statb;
  if (stat(name, &statb) != 0)
    return (NULL);
  if (!S_ISDIR(statb.st_mode)) {
    errno = ENOTDIR;
    return (NULL);
  }
  if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
    return (NULL);
  return __opendir_common(fd, name, flags);
}


x86 assembly (clang, -O0):
<__opendir2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1173 <__opendir2+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11ea <__opendir2+0xaa>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11a3 <__opendir2+0x63>
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e34(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11ea <__opendir2+0xaa>
mov    -0x10(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
call   1050 <_open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11d7 <__opendir2+0x97>
movq   $0x0,-0x8(%rbp)
jmp    11ea <__opendir2+0xaa>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <__opendir_common@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__opendir2>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     115e <__opendir2+0x1e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %eax,%eax
je     1197 <__opendir2+0x57>
mov    0x2e67(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <_open@plt>
cmp    $0xffffffff,%eax
je     1155 <__opendir2+0x15>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <__opendir_common@plt>
jmp    1157 <__opendir2+0x17>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    0x2e21(%rip),%rcx
mov    %eax,(%rcx)
jmp    1155 <__opendir2+0x15>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32* @__opendir2(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %22
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @S_ISDIR(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = load i32, i32* @ENOTDIR
store i32 %13, i32* @errno
br label %22
14: ; preds = %7
%15 = load i32, i32* @O_RDONLY
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %16, %15
%18 = call i32 @_open(i8* %0, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = call i32* @__opendir_common(i32 %18, i8* %0, i32 %1)
br label %22
22: ; preds = %14, %2, %20, %12
%23 = phi i32* [ %21, %20 ], [ null, %12 ], [ null, %2 ], [ null, %14 ]
ret i32* %23
}