#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 dirent {
  int /*<<< orphan*/ *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ isAsciiDigit(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int strtol(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int closeDescriptors(void) {
  DIR *dp;
  struct dirent *dirp;
  int from_fd = (2 + 1) + 1;
  close(from_fd);
  close(from_fd + 1);
  if ((dp = opendir("/proc/self/fd")) == NULL)
    return 0;
  while ((dirp = readdir(dp)) != NULL) {
    int fd;
    if (isAsciiDigit(dirp->d_name[0]) &&
        (fd = strtol(dirp->d_name, NULL, 10)) >= from_fd + 2)
      close(fd);
  }
  closedir(dp);
  return 1;
}


x86 assembly (clang, -O0):
<closeDescriptors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x4,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1040 <close@plt>
lea    0xe77(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a8 <closeDescriptors+0x48>
movl   $0x0,-0x4(%rbp)
jmp    121f <closeDescriptors+0xbf>
jmp    11ad <closeDescriptors+0x4d>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     120f <closeDescriptors+0xaf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1070 <isAsciiDigit@plt>
cmp    $0x0,%rax
je     120a <closeDescriptors+0xaa>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jl     120a <closeDescriptors+0xaa>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
jmp    11ad <closeDescriptors+0x4d>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<closeDescriptors>:
push   %r14
push   %rbx
push   %rax
mov    $0x4,%edi
call   1040 <close@plt>
mov    $0x5,%edi
call   1040 <close@plt>
lea    0xe81(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11eb <closeDescriptors+0x8b>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11dc <closeDescriptors+0x7c>
mov    %rax,%rbx
jmp    11b0 <closeDescriptors+0x50>
xchg   %ax,%ax
mov    %r14,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11dc <closeDescriptors+0x7c>
mov    (%rbx),%rax
mov    (%rax),%edi
call   1070 <isAsciiDigit@plt>
test   %rax,%rax
je     11a0 <closeDescriptors+0x40>
mov    (%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
cmp    $0x6,%eax
jl     11a0 <closeDescriptors+0x40>
mov    %eax,%edi
call   1040 <close@plt>
jmp    11a0 <closeDescriptors+0x40>
mov    %r14,%rdi
call   1050 <closedir@plt>
mov    $0x1,%eax
jmp    11ed <closeDescriptors+0x8d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i32* }
@.str = [14 x i8] c"/proc/self/fd\00"
define i32 @closeDescriptors() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
%5 = alloca i32
store i32 4, i32* %4
%6 = load i32, i32* %4
%7 = call i32 @close(i32 %6)
%8 = load i32, i32* %4
%9 = add nsw i32 %8, 1
%10 = call i32 @close(i32 %9)
%11 = call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %2
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 0, i32* %1
br label %42
14: ; preds = %0
br label %15
15: ; preds = %38, %14
%16 = load i32*, i32** %2
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %3
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %39
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %3
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i32*, i32** %21
%23 = getelementptr inbounds i32, i32* %22, i64 0
%24 = load i32, i32* %23
%25 = call i64 @isAsciiDigit(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %19
%28 = load %struct.dirent*, %struct.dirent** %3
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 0
%30 = load i32*, i32** %29
%31 = call i32 @strtol(i32* %30, i32* null, i32 10)
store i32 %31, i32* %5
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 2
%34 = icmp sge i32 %31, %33
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %27, %19
br label %15
39: ; preds = %15
%40 = load i32*, i32** %2
%41 = call i32 @closedir(i32* %40)
store i32 1, i32* %1
br label %42
42: ; preds = %39, %13
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
%struct.dirent = type { i32* }
@.str = [14 x i8] c"/proc/self/fd\00"
define i32 @closeDescriptors() {
%1 = tail call i32 @close(i32 4)
%2 = tail call i32 @close(i32 5)
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %26, label %5
5: ; preds = %0
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %24, label %8
8: ; preds = %5, %21
%9 = phi %struct.dirent* [ %22, %21 ], [ %6, %5 ]
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%11 = load i32*, i32** %10
%12 = load i32, i32* %11
%13 = tail call i64 @isAsciiDigit(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %8
%16 = load i32*, i32** %10
%17 = tail call i32 @strtol(i32* nocapture %16, i32* null, i32 10)
%18 = icmp sgt i32 %17, 5
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @close(i32 %17)
br label %21
21: ; preds = %19, %15, %8
%22 = tail call %struct.dirent* @readdir(i32* %3)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %24, label %8
24: ; preds = %21, %5
%25 = tail call i32 @closedir(i32* %3)
br label %26
26: ; preds = %0, %24
%27 = phi i32 [ 1, %24 ], [ 0, %0 ]
ret i32 %27
}