#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 /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ *__opendir_common(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int fstat(int, struct stat *);
DIR *_fdopendir(int fd) {
  struct stat sb;
  if (fstat(fd, &sb) == -1)
    return NULL;
  if (!S_ISDIR(sb.st_mode)) {
    errno = ENOTDIR;
    return NULL;
  }
  if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
    return NULL;
  return __opendir_common(fd, NULL, 0x0001 | 0x0002);
}


x86 assembly (clang, -O0):
<_fdopendir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    116d <_fdopendir+0x2d>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <_fdopendir+0xa2>
mov    -0x10(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    119d <_fdopendir+0x5d>
mov    0x2e73(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3a(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11e2 <_fdopendir+0xa2>
mov    -0xc(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11cd <_fdopendir+0x8d>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <_fdopendir+0xa2>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x3,%edx
call   1050 <__opendir_common@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fdopendir>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11a6 <_fdopendir+0x66>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     1194 <_fdopendir+0x54>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11a6 <_fdopendir+0x66>
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x3,%edx
add    $0x10,%rsp
pop    %rbx
jmp    1050 <__opendir_common@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    0x2e24(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32* @_fdopendir(i32 %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca %struct.stat
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @fstat(i32 %5, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %1
store i32* null, i32** %2
br label %26
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @S_ISDIR(i32 %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = load i32, i32* @ENOTDIR
store i32 %15, i32* @errno
store i32* null, i32** %2
br label %26
16: ; preds = %9
%17 = load i32, i32* %3
%18 = load i32, i32* @F_SETFD
%19 = load i32, i32* @FD_CLOEXEC
%20 = call i32 @fcntl(i32 %17, i32 %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %16
store i32* null, i32** %2
br label %26
23: ; preds = %16
%24 = load i32, i32* %3
%25 = call i32* @__opendir_common(i32 %24, i32* null, i32 3)
store i32* %25, i32** %2
br label %26
26: ; preds = %23, %22, %14, %8
%27 = load i32*, i32** %2
ret i32* %27
}

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