#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*/ path;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ EINVAL;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int dirfd(int /*<<< orphan*/ *);
scalar_t__ errno;
int getpid();
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strtol(int /*<<< orphan*/, char **, int);
int close_from(int minfd) {
  int i, dfd, fd, ret = -1;
  DIR *dir;
  char path[PATH_MAX], *end;
  for (i = minfd; i < 1024; i++)
    close(i);
  snprintf(path, sizeof(path), "/proc/%d/fd/", getpid());
  dir = opendir(path);
  if (!dir)
    return -1;
  dfd = dirfd(dir);
  for (;;) {
    struct dirent *ent;
    errno = 0;
    ent = readdir(dir);
    if (!ent && errno)
      goto end;
    errno = 0;
    fd = strtol(ent->d_name, &end, 10);
    if (*end || errno) {
      errno = EINVAL;
      goto end;
    }
    if (fd < minfd || fd == dfd)
      continue;
    close(fd);
  }
  ret = 0;
end:
  closedir(dir);
  return ret;
}


x86 assembly (clang, -O0):
<close_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x400,-0xc(%rbp)
jge    11eb <close_from+0x6b>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c8 <close_from+0x48>
mov    -0x58(%rbp),%rax
mov    %eax,-0x5c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x5c(%rbp),%esi
mov    -0x50(%rbp),%rdi
mov    %eax,%ecx
lea    0xdf7(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1239 <close_from+0xb9>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1320 <close_from+0x1a0>
mov    -0x20(%rbp),%rdi
call   1080 <dirfd@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d8c(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1281 <close_from+0x101>
mov    0x2d66(%rip),%rax
cmpq   $0x0,(%rax)
je     1281 <close_from+0x101>
jmp    130a <close_from+0x18a>
mov    0x2d50(%rip),%rax
movq   $0x0,(%rax)
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12c7 <close_from+0x147>
mov    0x2d1b(%rip),%rax
cmpq   $0x0,(%rax)
je     12e0 <close_from+0x160>
mov    0x2d02(%rip),%rax
mov    (%rax),%rcx
mov    0x2d00(%rip),%rax
mov    %rcx,(%rax)
jmp    130a <close_from+0x18a>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     12f8 <close_from+0x178>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    12fd <close_from+0x17d>
jmp    1245 <close_from+0xc5>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    1245 <close_from+0xc5>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_from>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e58(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
cmp    $0x3ff,%edi
jg     11d2 <close_from+0x52>
mov    %r14d,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    $0x400,%ebx
jne    11c0 <close_from+0x40>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe20(%rip),%rdx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12a7 <close_from+0x127>
mov    %rax,%r13
mov    %rax,%rdi
call   1080 <dirfd@plt>
mov    %eax,%r15d
mov    0x2dc5(%rip),%rbx
movq   $0x0,(%rbx)
mov    %r13,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
jne    122d <close_from+0xad>
cmpq   $0x0,(%rbx)
jne    129f <close_from+0x11f>
lea    -0x30(%rbp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movq   $0x0,(%rbx)
mov    (%rax),%edi
mov    %r12,%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    -0x30(%rbp),%rcx
cmpb   $0x0,(%rcx)
jne    1292 <close_from+0x112>
cmpq   $0x0,(%rbx)
jne    1292 <close_from+0x112>
cmp    %r14d,%eax
jl     1276 <close_from+0xf6>
cmp    %r15d,%eax
je     1276 <close_from+0xf6>
mov    %eax,%edi
call   1060 <close@plt>
movq   $0x0,(%rbx)
mov    %r13,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
jne    1240 <close_from+0xc0>
cmpq   $0x0,(%rbx)
je     1240 <close_from+0xc0>
jmp    129f <close_from+0x11f>
mov    0x2d37(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rbx)
mov    %r13,%rdi
call   1070 <closedir@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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