#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 atoi(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
void close_fds(void) {
  DIR *dir;
  struct dirent *d;
  dir = opendir("/proc/self/fd");
  while ((d = readdir(dir)) != NULL) {
    if (atoi(d->d_name) < 3)
      continue;
    close(atoi(d->d_name));
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<close_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11af <close_fds+0x5f>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
cmp    $0x3,%eax
jge    1198 <close_fds+0x48>
jmp    1168 <close_fds+0x18>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1040 <close@plt>
jmp    1168 <close_fds+0x18>
mov    -0x8(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_fds>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11ac <close_fds+0x5c>
mov    %rax,%rbx
jmp    1190 <close_fds+0x40>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ac <close_fds+0x5c>
mov    (%rbx),%edi
call   1070 <atoi@plt>
cmp    $0x3,%eax
jl     1180 <close_fds+0x30>
mov    (%rbx),%edi
call   1070 <atoi@plt>
mov    %eax,%edi
call   1040 <close@plt>
jmp    1180 <close_fds+0x30>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <closedir@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [14 x i8] c"/proc/self/fd\00"
define void @close_fds() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32* %3, i32** %1
br label %4
4: ; preds = %15, %14, %0
%5 = load i32*, i32** %1
%6 = call %struct.dirent* @readdir(i32* %5)
store %struct.dirent* %6, %struct.dirent** %2
%7 = icmp ne %struct.dirent* %6, null
br i1 %7, label %8, label %21
8: ; preds = %4
%9 = load %struct.dirent*, %struct.dirent** %2
%10 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @atoi(i32 %11)
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %15
14: ; preds = %8
br label %4
15: ; preds = %8
%16 = load %struct.dirent*, %struct.dirent** %2
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @atoi(i32 %18)
%20 = call i32 @close(i32 %19)
br label %4
21: ; preds = %4
%22 = load i32*, i32** %1
%23 = call i32 @closedir(i32* %22)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [14 x i8] c"/proc/self/fd\00"
define void @close_fds() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%2 = tail call %struct.dirent* @readdir(i32* %1)
%3 = icmp eq %struct.dirent* %2, null
br i1 %3, label %17, label %4
4: ; preds = %0, %14
%5 = phi %struct.dirent* [ %15, %14 ], [ %2, %0 ]
%6 = getelementptr inbounds %struct.dirent, %struct.dirent* %5, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i32 @atoi(i32 %7)
%9 = icmp slt i32 %8, 3
br i1 %9, label %14, label %10
10: ; preds = %4
%11 = load i32, i32* %6
%12 = tail call i32 @atoi(i32 %11)
%13 = tail call i32 @close(i32 %12)
br label %14
14: ; preds = %10, %4
%15 = tail call %struct.dirent* @readdir(i32* %1)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %17, label %4
17: ; preds = %14, %0
%18 = tail call i32 @closedir(i32* %1)
ret void
}