#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 atol(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ *opendir(char *);
scalar_t__ readdir_r(int /*<<< orphan*/ *, struct dirent *, struct dirent **);
int /*<<< orphan*/ sprintf(char *, char *, int);
void close_all_fd(void) {
  DIR *dirp;
  struct dirent ent, *res;
  char path[128];
  int fd;
  sprintf(path, "/proc/%u/fd", getpid());
  dirp = opendir(path);
  if (!dirp)
    return;
  while (1) {
    if (readdir_r(dirp, &ent, &res))
      return;
    if (!res)
      break;
    fd = atol(ent.d_name);
    if (fd > 2)
      close(fd);
  }
  closedir(dirp);
}


x86 assembly (clang, -O0):
<close_all_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0xb0(%rbp),%rdi
mov    %eax,%edx
lea    0xe60(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c5 <close_all_fd+0x55>
jmp    122e <close_all_fd+0xbe>
jmp    11ca <close_all_fd+0x5a>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <readdir_r@plt>
cmp    $0x0,%rax
je     11ea <close_all_fd+0x7a>
jmp    122e <close_all_fd+0xbe>
cmpq   $0x0,-0x18(%rbp)
jne    11fa <close_all_fd+0x8a>
jmp    1225 <close_all_fd+0xb5>
mov    -0x10(%rbp),%edi
call   1080 <atol@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x2,-0xa4(%rbp)
jle    1220 <close_all_fd+0xb0>
mov    -0xa4(%rbp),%edi
call   1060 <close@plt>
jmp    11ca <close_all_fd+0x5a>
mov    -0x8(%rbp),%rdi
call   1070 <closedir@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_all_fd>:
push   %r15
push   %r14
push   %rbx
sub    $0x90,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe76(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1209 <close_all_fd+0x99>
mov    %rax,%rbx
mov    %rsp,%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <readdir_r@plt>
test   %rax,%rax
jne    1209 <close_all_fd+0x99>
mov    %rsp,%r14
lea    0x8(%rsp),%r15
jmp    11e3 <close_all_fd+0x73>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <readdir_r@plt>
test   %rax,%rax
jne    1209 <close_all_fd+0x99>
cmpq   $0x0,0x8(%rsp)
je     1201 <close_all_fd+0x91>
mov    (%rsp),%edi
call   1080 <atol@plt>
cmp    $0x3,%eax
jl     11d0 <close_all_fd+0x60>
mov    %eax,%edi
call   1060 <close@plt>
jmp    11d0 <close_all_fd+0x60>
mov    %rbx,%rdi
call   1070 <closedir@plt>
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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