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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ _SC_OPEN_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
long dirfd(int /*<<< orphan*/ *);
scalar_t__ getpid();
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int snprintf(char *, int, char *, long);
long strtol(char *, char **, int);
long sysconf(int /*<<< orphan*/);
void close_open_fds() {
  long fd, maxfd;
  char fdpath[PATH_MAX], *endp;
  struct dirent *dent;
  DIR *dirp;
  int len;
  len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
  if (len > 0 && (size_t)len <= sizeof(fdpath) && (dirp = opendir(fdpath))) {
    while ((dent = readdir(dirp)) != NULL) {
      fd = strtol(dent->d_name, &endp, 10);
      if (dent->d_name != endp && *endp == '\0' && fd != dirfd(dirp)) {
        (void)close((int)fd);
      }
    }
    (void)closedir(dirp);
  } else {
    maxfd = sysconf(_SC_OPEN_MAX);
    for (fd = 0; fd < maxfd; fd++)
      (void)close((int)fd);
  }
}


x86 assembly (clang, -O0):
<close_open_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%esi
mov    %rax,%rcx
lea    0xe1d(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    12b2 <close_open_fds+0x122>
mov    -0x50(%rbp),%rcx
movslq -0x3c(%rbp),%rax
cmp    %rcx,%rax
ja     12b2 <close_open_fds+0x122>
mov    -0x48(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12b2 <close_open_fds+0x122>
jmp    1222 <close_open_fds+0x92>
mov    -0x38(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12a4 <close_open_fds+0x114>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,-0x8(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
je     129f <close_open_fds+0x10f>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    129f <close_open_fds+0x10f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <dirfd@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
cmp    %rcx,%rax
je     129f <close_open_fds+0x10f>
mov    -0x8(%rbp),%rax
mov    %eax,%edi
call   1060 <close@plt>
jmp    1222 <close_open_fds+0x92>
mov    -0x38(%rbp),%rdi
call   1070 <closedir@plt>
jmp    12fb <close_open_fds+0x16b>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    12f6 <close_open_fds+0x166>
mov    -0x8(%rbp),%rax
mov    %eax,%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    12cc <close_open_fds+0x13c>
jmp    12fb <close_open_fds+0x16b>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_open_fds>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e4a(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe38(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
test   %eax,%eax
jle    1258 <close_open_fds+0xc8>
cltq
cmp    %r14,%rax
ja     1258 <close_open_fds+0xc8>
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1258 <close_open_fds+0xc8>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1282 <close_open_fds+0xf2>
mov    %rax,%rbx
lea    -0x28(%rbp),%r14
jmp    1220 <close_open_fds+0x90>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1282 <close_open_fds+0xf2>
mov    (%rbx),%rdi
mov    %r14,%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %rax,%r12
mov    -0x28(%rbp),%rax
cmp    %rax,(%rbx)
je     1210 <close_open_fds+0x80>
cmpb   $0x0,(%rax)
jne    1210 <close_open_fds+0x80>
mov    %r15,%rdi
call   1080 <dirfd@plt>
cmp    %rax,%r12
je     1210 <close_open_fds+0x80>
mov    %r12d,%edi
call   1060 <close@plt>
jmp    1210 <close_open_fds+0x80>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
test   %rax,%rax
jle    128a <close_open_fds+0xfa>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1270 <close_open_fds+0xe0>
jmp    128a <close_open_fds+0xfa>
mov    %r15,%rdi
call   1070 <closedir@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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