#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 */
#define EACCES 132
int EIO;
#define EMFILE 131
#define ENAMETOOLONG 130
#define ENFILE 129
#define ENOENT 128
int ENOTDIR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
DIR *fdopendir(int fd) {
  struct stat st;
  if (fstat(fd, &st))
    return NULL;
  if (!S_ISDIR(st.st_mode)) {
    errno = ENOTDIR;
    return NULL;
  }
  char path[sizeof("/proc/self/fd/") + 3 * sizeof(int)];
  sprintf(path, "/proc/self/fd/%u", fd);
  DIR *dir = opendir(path);
  if (dir != NULL) {
    close(fd);
    return dir;
  }
  switch (errno) {
  case EACCES:
  case ENAMETOOLONG:
  case ENOENT:
  case EMFILE:
  case ENFILE:
    errno = EIO;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<fdopendir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     117e <fdopendir+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    1222 <fdopendir+0xd2>
mov    -0x10(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11ae <fdopendir+0x5e>
mov    0x2e62(%rip),%rax
mov    (%rax),%ecx
mov    0x2e31(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1222 <fdopendir+0xd2>
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe44(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11ee <fdopendir+0x9e>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1222 <fdopendir+0xd2>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x4,%eax
ja     121a <fdopendir+0xca>
jmp    1208 <fdopendir+0xb8>
mov    0x2dc9(%rip),%rax
mov    (%rax),%ecx
mov    0x2db8(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdopendir>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1070 <fstat@plt>
test   %rax,%rax
je     1173 <fdopendir+0x23>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     11af <fdopendir+0x5f>
lea    0xe79(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c3 <fdopendir+0x73>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1169 <fdopendir+0x19>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    0x2e11(%rip),%rcx
mov    %eax,(%rcx)
jmp    1167 <fdopendir+0x17>
mov    0x2e06(%rip),%rax
mov    (%rax),%ecx
add    $0xffffff80,%ecx
cmp    $0x4,%ecx
ja     1169 <fdopendir+0x19>
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
jmp    1169 <fdopendir+0x19>

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%u\00"
@EIO = global i32 0
define i32* @fdopendir(i32 %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca [27 x i8]
%6 = alloca i32*
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i64 @fstat(i32 %7, %struct.stat* %4)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32* null, i32** %2
br label %35
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @S_ISDIR(i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = load i32, i32* @ENOTDIR
store i32 %17, i32* @errno
store i32* null, i32** %2
br label %35
18: ; preds = %11
%19 = getelementptr inbounds [27 x i8], [27 x i8]* %5, i64 0, i64 0
%20 = load i32, i32* %3
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [27 x i8], [27 x i8]* %5, i64 0, i64 0
%23 = call i32* @opendir(i8* %22)
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = load i32*, i32** %6
store i32* %29, i32** %2
br label %35
30: ; preds = %18
%31 = load i32, i32* @errno
switch i32 %31, label %34 [
i32 132, label %32
i32 130, label %32
i32 128, label %32
i32 131, label %32
i32 129, label %32
]
32: ; preds = %30, %30, %30, %30, %30
%33 = load i32, i32* @EIO
store i32 %33, i32* @errno
br label %34
34: ; preds = %32, %30
store i32* null, i32** %2
br label %35
35: ; preds = %34, %26, %16, %10
%36 = load i32*, i32** %2
ret i32* %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%u\00"
@EIO = global i32 0
define noalias i32* @fdopendir(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca [27 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i64 @fstat(i32 %0, %struct.stat* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %28
7: ; preds = %1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail 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 %28
14: ; preds = %7
%15 = getelementptr inbounds [27 x i8], [27 x i8]* %3, i64 0, i64 0
%16 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = call i32* @opendir(i8* %15)
%18 = icmp eq i32* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = tail call i32 @close(i32 %0)
br label %27
21: ; preds = %14
%22 = load i32, i32* @errno
%23 = add i32 %22, -128
%24 = icmp ult i32 %23, 5
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* @EIO
store i32 %26, i32* @errno
br label %27
27: ; preds = %25, %21, %19
br label %28
28: ; preds = %1, %27, %12
%29 = phi i32* [ %17, %27 ], [ null, %12 ], [ null, %1 ]
ret i32* %29
}