#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*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
long dirfd(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
long strtol(char *, char **, int);
void close_fds(void) {
  struct dirent *dent;
  DIR *dirp;
  char *endp;
  long fd;
  if ((dirp = opendir("/proc/self/fd")) != NULL) {
    while ((dent = readdir(dirp)) != NULL) {
      fd = strtol(dent->d_name, &endp, 10);
      if (dent->d_name != endp && *endp == '\0' && fd >= 3 &&
          fd != dirfd(dirp)) {
        close((int)fd);
      }
    }
    closedir(dirp);
  }
}


x86 assembly (clang, -O0):
<close_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     121d <close_fds+0xbd>
jmp    1187 <close_fds+0x27>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     1214 <close_fds+0xb4>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
cmp    -0x18(%rbp),%rax
je     120f <close_fds+0xaf>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    120f <close_fds+0xaf>
cmpq   $0x3,-0x20(%rbp)
jl     120f <close_fds+0xaf>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <dirfd@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     120f <close_fds+0xaf>
mov    -0x20(%rbp),%rax
mov    %eax,%edi
call   1040 <close@plt>
jmp    1187 <close_fds+0x27>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_fds>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe91(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1201 <close_fds+0xa1>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11ee <close_fds+0x8e>
mov    %rax,%rbx
mov    %rsp,%r14
jmp    11b0 <close_fds+0x50>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ee <close_fds+0x8e>
mov    (%rbx),%rdi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,%r12
mov    (%rsp),%rax
cmp    %rax,(%rbx)
je     11a0 <close_fds+0x40>
cmpb   $0x0,(%rax)
jne    11a0 <close_fds+0x40>
cmp    $0x3,%r12
jl     11a0 <close_fds+0x40>
mov    %r15,%rdi
call   1060 <dirfd@plt>
cmp    %rax,%r12
je     11a0 <close_fds+0x40>
mov    %r12d,%edi
call   1040 <close@plt>
jmp    11a0 <close_fds+0x40>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <closedir@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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