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

/* Variables and functions */
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int dirfd(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
long strtol(char *, char **, int);
int sysconf(int /*<<< orphan*/);
void closeall(int min) {
  char *endp;
  long n;
  DIR *dir = opendir("/proc/self/fd");
  if (dir != NULL) {
    int dfd = dirfd(dir);
    struct dirent *d;
    while ((d = readdir(dir)) != NULL) {
      errno = 0;
      n = strtol(d->d_name, &endp, 10);
      if (!errno && *endp == '\0' && endp != d->d_name && n >= min && n != dfd)
        (void)close(n);
    }
    closedir(dir);
  } else {
    int fd = sysconf(_SC_OPEN_MAX);
    while (--fd >= min)
      (void)close(fd);
  }
}


x86 assembly (clang, -O0):
<closeall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe7e(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1252 <closeall+0xe2>
mov    -0x20(%rbp),%rdi
call   1060 <dirfd@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1244 <closeall+0xd4>
mov    0x2e18(%rip),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    0x2df1(%rip),%rax
cmpq   $0x0,(%rax)
jne    123f <closeall+0xcf>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    123f <closeall+0xcf>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
cmp    (%rcx),%rax
je     123f <closeall+0xcf>
mov    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
cmp    %rcx,%rax
jl     123f <closeall+0xcf>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
je     123f <closeall+0xcf>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
call   1040 <close@plt>
jmp    11a2 <closeall+0x32>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
jmp    1287 <closeall+0x117>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
cmp    -0x4(%rbp),%eax
jl     1282 <closeall+0x112>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
jmp    1263 <closeall+0xf3>
jmp    1287 <closeall+0x117>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<closeall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0xe7b(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1236 <closeall+0xc6>
mov    %rax,%r13
mov    %rax,%rdi
call   1060 <dirfd@plt>
mov    %eax,%r15d
mov    %r13,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1220 <closeall+0xb0>
mov    %rax,%rbp
movslq %r14d,%r12
movslq %r15d,%r15
mov    0x2e1a(%rip),%rbx
mov    %rsp,%r14
jmp    11e0 <closeall+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1220 <closeall+0xb0>
movq   $0x0,(%rbx)
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
cmpq   $0x0,(%rbx)
jne    11d0 <closeall+0x60>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    11d0 <closeall+0x60>
cmp    0x0(%rbp),%rcx
je     11d0 <closeall+0x60>
cmp    %r12,%rax
jl     11d0 <closeall+0x60>
cmp    %r15,%rax
je     11d0 <closeall+0x60>
mov    %eax,%edi
call   1040 <close@plt>
jmp    11d0 <closeall+0x60>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <closedir@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
cmp    %r14d,%eax
jle    125f <closeall+0xef>
mov    %eax,%ebx
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
mov    %ebx,%edi
call   1040 <close@plt>
cmp    %r14d,%ebx
jg     1250 <closeall+0xe0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"/proc/self/fd\00"
@errno = global i64 0
@_SC_OPEN_MAX = global i32 0
define void @closeall(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.dirent*
%8 = alloca i32
store i32 %0, i32* %2
%9 = call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %55
12: ; preds = %1
%13 = load i32*, i32** %5
%14 = call i32 @dirfd(i32* %13)
store i32 %14, i32* %6
br label %15
15: ; preds = %51, %12
%16 = load i32*, i32** %5
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %7
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %52
19: ; preds = %15
store i64 0, i64* @errno
%20 = load %struct.dirent*, %struct.dirent** %7
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strtol(i8* %22, i8** %3, i32 10)
store i64 %23, i64* %4
%24 = load i64, i64* @errno
%25 = icmp ne i64 %24, 0
br i1 %25, label %51, label %26
26: ; preds = %19
%27 = load i8*, i8** %3
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %51
31: ; preds = %26
%32 = load i8*, i8** %3
%33 = load %struct.dirent*, %struct.dirent** %7
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = icmp ne i8* %32, %35
br i1 %36, label %37, label %51
37: ; preds = %31
%38 = load i64, i64* %4
%39 = load i32, i32* %2
%40 = sext i32 %39 to i64
%41 = icmp sge i64 %38, %40
br i1 %41, label %42, label %51
42: ; preds = %37
%43 = load i64, i64* %4
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = icmp ne i64 %43, %45
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i64, i64* %4
%49 = trunc i64 %48 to i32
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %47, %42, %37, %31, %26, %19
br label %15
52: ; preds = %15
%53 = load i32*, i32** %5
%54 = call i32 @closedir(i32* %53)
br label %67
55: ; preds = %1
%56 = load i32, i32* @_SC_OPEN_MAX
%57 = call i32 @sysconf(i32 %56)
store i32 %57, i32* %8
br label %58
58: ; preds = %63, %55
%59 = load i32, i32* %8
%60 = add nsw i32 %59, -1
store i32 %60, i32* %8
%61 = load i32, i32* %2
%62 = icmp sge i32 %60, %61
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
br label %58
66: ; preds = %58
br label %67
67: ; preds = %66, %52
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"/proc/self/fd\00"
@errno = global i64 0
@_SC_OPEN_MAX = global i32 0
define void @closeall(i32 %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %39, label %6
6: ; preds = %1
%7 = tail call i32 @dirfd(i32* %4)
%8 = tail call %struct.dirent* @readdir(i32* %4)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %37, label %10
10: ; preds = %6
%11 = sext i32 %0 to i64
%12 = sext i32 %7 to i64
br label %13
13: ; preds = %10, %34
%14 = phi %struct.dirent* [ %8, %10 ], [ %35, %34 ]
store i64 0, i64* @errno
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i64 @strtol(i8* %16, i8** %2, i32 10)
%18 = load i64, i64* @errno
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %13
%21 = load i8*, i8** %2
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %20
%25 = load i8*, i8** %15
%26 = icmp eq i8* %21, %25
%27 = icmp slt i64 %17, %11
%28 = select i1 %26, i1 true, i1 %27
%29 = icmp eq i64 %17, %12
%30 = select i1 %28, i1 true, i1 %29
br i1 %30, label %34, label %31
31: ; preds = %24
%32 = trunc i64 %17 to i32
%33 = tail call i32 @close(i32 %32)
br label %34
34: ; preds = %31, %24, %20, %13
%35 = tail call %struct.dirent* @readdir(i32* %4)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %13
37: ; preds = %34, %6
%38 = tail call i32 @closedir(i32* %4)
br label %48
39: ; preds = %1
%40 = load i32, i32* @_SC_OPEN_MAX
%41 = tail call i32 @sysconf(i32 %40)
%42 = icmp sgt i32 %41, %0
br i1 %42, label %43, label %48
43: ; preds = %39, %43
%44 = phi i32 [ %45, %43 ], [ %41, %39 ]
%45 = add nsw i32 %44, -1
%46 = tail call i32 @close(i32 %45)
%47 = icmp sgt i32 %45, %0
br i1 %47, label %43, label %48
48: ; preds = %43, %39, %37
ret void
}