#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*/ DIR;

/* Variables and functions */
int asprintf(char **, char *, int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
void close_fds() {
  char *fd_dir_path;
  DIR *dir_fd;
  struct dirent *fd_file;
  assert(asprintf(&fd_dir_path, "/proc/%d/fd", getpid()) != -1);
  dir_fd = opendir(fd_dir_path);
  assert(dir_fd != NULL);
  free(fd_dir_path);
  while ((fd_file = readdir(dir_fd)) != NULL) {
    if (atoi(fd_file->d_name) > 2)
      close(atoi(fd_file->d_name));
  }
  closedir(dir_fd);
}


x86 assembly (clang, -O0):
<close_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edx
lea    -0x8(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   10a0 <asprintf@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     122b <close_fds+0x9b>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   10b0 <atoi@plt>
cmp    $0x2,%eax
jle    1226 <close_fds+0x96>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1060 <close@plt>
jmp    11e9 <close_fds+0x59>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_fds>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5e(%rip),%rsi
mov    %rsp,%rdi
mov    %eax,%edx
call   10a0 <asprintf@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1080 <assert@plt>
mov    (%rsp),%rdi
call   1050 <opendir@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1080 <assert@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     121c <close_fds+0x8c>
mov    %rax,%rbx
jmp    1200 <close_fds+0x70>
nop
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     121c <close_fds+0x8c>
mov    (%rbx),%edi
call   10b0 <atoi@plt>
cmp    $0x3,%eax
jl     11f0 <close_fds+0x60>
mov    (%rbx),%edi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1060 <close@plt>
jmp    11f0 <close_fds+0x60>
mov    %r14,%rdi
call   1070 <closedir@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [12 x i8] c"/proc/%d/fd\00"
define void @close_fds() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call i32 (...) @getpid()
%4 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp ne i32 %4, -1
%6 = zext i1 %5 to i32
%7 = call i32 @assert(i32 %6)
%8 = load i8*, i8** %1
%9 = call i32* @opendir(i8* %8)
%10 = icmp ne i32* %9, null
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8*, i8** %1
%14 = call i32 @free(i8* %13)
%15 = call %struct.dirent* @readdir(i32* %9)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %30, label %17
17: ; preds = %0, %27
%18 = phi %struct.dirent* [ %28, %27 ], [ %15, %0 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @atoi(i32 %20)
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* %19
%25 = call i32 @atoi(i32 %24)
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %23, %17
%28 = call %struct.dirent* @readdir(i32* %9)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %30, label %17
30: ; preds = %27, %0
%31 = call i32 @closedir(i32* %9)
ret void
}