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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int dirfd(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int strtol(scalar_t__ *, char **, int);
int closefrom(int minfd) {
  DIR *dir = opendir("/proc/self/fd");
  if (dir) {
    int dfd = dirfd(dir);
    struct dirent *ent;
    while ((ent = readdir(dir))) {
      char *end;
      int fd = strtol(ent->d_name, &end, 10);
      if (ent->d_name[0] && !end[0] && fd >= minfd && fd != dfd)
        close(fd);
    }
    closedir(dir);
  }
  return 0;
}


x86 assembly (clang, -O0):
<closefrom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe8e(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     120d <closefrom+0xad>
mov    -0x10(%rbp),%rdi
call   1060 <dirfd@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1204 <closefrom+0xa4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmpq   $0x0,(%rax)
je     11ff <closefrom+0x9f>
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11ff <closefrom+0x9f>
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     11ff <closefrom+0x9f>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11ff <closefrom+0x9f>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
jmp    1192 <closefrom+0x32>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<closefrom>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r15d
lea    0xe8a(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1200 <closefrom+0xa0>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <dirfd@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11f8 <closefrom+0x98>
mov    %rax,%rbp
lea    0x8(%rsp),%r12
jmp    11c0 <closefrom+0x60>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11f8 <closefrom+0x98>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    0x0(%rbp),%rcx
cmpq   $0x0,(%rcx)
je     11b0 <closefrom+0x50>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    11b0 <closefrom+0x50>
cmp    %r15d,%eax
jl     11b0 <closefrom+0x50>
cmp    %r14d,%eax
je     11b0 <closefrom+0x50>
mov    %eax,%edi
call   1040 <close@plt>
jmp    11b0 <closefrom+0x50>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i64* }
@.str = [14 x i8] c"/proc/self/fd\00"
define i32 @closefrom(i32 %0) {
%2 = alloca i8*
%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 %34, label %5
5: ; preds = %1
%6 = tail call i32 @dirfd(i32* %3)
%7 = tail call %struct.dirent* @readdir(i32* %3)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %32, label %9
9: ; preds = %5
%10 = bitcast i8** %2 to i8*
br label %11
11: ; preds = %9, %29
%12 = phi %struct.dirent* [ %7, %9 ], [ %30, %29 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i64*, i64** %13
%15 = call i32 @strtol(i64* %14, i8** %2, i32 10)
%16 = load i64*, i64** %13
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %11
%20 = load i8*, i8** %2
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
%23 = icmp slt i32 %15, %0
%24 = select i1 %22, i1 true, i1 %23
%25 = icmp eq i32 %15, %6
%26 = select i1 %24, i1 true, i1 %25
br i1 %26, label %29, label %27
27: ; preds = %19
%28 = tail call i32 @close(i32 %15)
br label %29
29: ; preds = %27, %19, %11
%30 = tail call %struct.dirent* @readdir(i32* %3)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %32, label %11
32: ; preds = %29, %5
%33 = tail call i32 @closedir(i32* %3)
br label %34
34: ; preds = %32, %1
ret i32 0
}