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

/* Variables and functions */
int asprintf(char **, char *, char *);
int atoi(char *);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *getpid();
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int const);
scalar_t__ strcmp(char *, char *);
int close_nonstd_fds(void) {
  const int procpidfdmax = 32;
  int rv = -1;
  int fd = -1;
  char *cmp = NULL;
  char *sym = NULL;
  char phys[procpidfdmax];
  DIR *dir = NULL;
  struct dirent *ent = NULL;
  dir = opendir("/proc/self/fd");
  if (!dir) {
    goto out;
  }
  while ((ent = readdir(dir)) != NULL) {
    free(cmp);
    free(sym);
    if (strcmp(ent->d_name, ".") == 0)
      continue;
    if (strcmp(ent->d_name, "..") == 0)
      continue;
    if (asprintf(&sym, "/proc/self/fd/%s", ent->d_name) == -1)
      goto out;
    if (asprintf(&cmp, "/proc/%d/fd", getpid()) == -1)
      goto out;
    bzero(phys, sizeof(phys));
    if (readlink(sym, phys, procpidfdmax - 1) == -1)
      goto out;
    if (strcmp(cmp, phys) == 0)
      continue;
    fd = atoi(ent->d_name);
    switch (fd) {
    case 0:
    case 1:
    case 2:
      break;
    default:
      close(fd);
    }
  }
  rv = 0;
out:
  closedir(dir);
  dir = NULL;
  ent = NULL;
  free(cmp);
  free(sym);
  cmp = sym = NULL;
  return rv;
}


x86 assembly (clang, -O0):
<close_nonstd_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x20,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xe05(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1214 <close_nonstd_fds+0x64>
jmp    1356 <close_nonstd_fds+0x1a6>
jmp    1219 <close_nonstd_fds+0x69>
mov    -0x30(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     134f <close_nonstd_fds+0x19f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xdbf(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1264 <close_nonstd_fds+0xb4>
jmp    1219 <close_nonstd_fds+0x69>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    0xd9c(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1286 <close_nonstd_fds+0xd6>
jmp    1219 <close_nonstd_fds+0x69>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdx
lea    -0x20(%rbp),%rdi
lea    0xd79(%rip),%rsi
call   10b0 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    12ab <close_nonstd_fds+0xfb>
jmp    1356 <close_nonstd_fds+0x1a6>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,%rdx
lea    -0x18(%rbp),%rdi
lea    0xd62(%rip),%rsi
call   10b0 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    12d3 <close_nonstd_fds+0x123>
jmp    1356 <close_nonstd_fds+0x1a6>
lea    -0x60(%rbp),%rdi
mov    $0x20,%esi
call   10c0 <bzero@plt>
mov    -0x20(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x1f,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
jne    1301 <close_nonstd_fds+0x151>
jmp    1356 <close_nonstd_fds+0x1a6>
mov    -0x18(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    131d <close_nonstd_fds+0x16d>
jmp    1219 <close_nonstd_fds+0x69>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
ja     1342 <close_nonstd_fds+0x192>
jmp    133d <close_nonstd_fds+0x18d>
jmp    134a <close_nonstd_fds+0x19a>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    1219 <close_nonstd_fds+0x69>
movl   $0x0,-0x8(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <closedir@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_nonstd_fds>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsp,%rbp
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe26(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,%rbx
mov    $0xffffffff,%r14d
test   %rax,%rax
je     1317 <close_nonstd_fds+0x167>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     130a <close_nonstd_fds+0x15a>
mov    %rax,%r13
mov    %rbp,0x18(%rsp)
lea    0xdfe(%rip),%rbp
lea    0xdf6(%rip),%r15
lea    0x20(%rsp),%r12
xor    %edi,%edi
nop
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x0(%r13),%rdi
mov    %rbp,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12f0 <close_nonstd_fds+0x140>
mov    0x0(%r13),%rdi
mov    %r15,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12f0 <close_nonstd_fds+0x140>
mov    0x0(%r13),%rdx
lea    0x8(%rsp),%rdi
lea    0xda8(%rip),%rsi
call   10b0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1312 <close_nonstd_fds+0x162>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0x10(%rsp),%rdi
lea    0xd98(%rip),%rsi
mov    %rax,%rdx
call   10b0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1312 <close_nonstd_fds+0x162>
mov    %r12,%rdi
mov    $0x20,%esi
call   10c0 <bzero@plt>
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
mov    $0x1f,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     1312 <close_nonstd_fds+0x162>
mov    0x10(%rsp),%rdi
mov    %r12,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12f0 <close_nonstd_fds+0x140>
mov    0x0(%r13),%rdi
call   10d0 <atoi@plt>
cmp    $0x3,%eax
jb     12f0 <close_nonstd_fds+0x140>
mov    %eax,%edi
call   1070 <close@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     130f <close_nonstd_fds+0x15f>
mov    %rax,%r13
mov    0x10(%rsp),%rdi
jmp    1220 <close_nonstd_fds+0x70>
xor    %r14d,%r14d
jmp    1317 <close_nonstd_fds+0x167>
xor    %r14d,%r14d
mov    0x18(%rsp),%rbp
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rbp,%rsp
mov    %r14d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"/proc/self/fd\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [17 x i8] c"/proc/self/fd/%s\00"
@.str.4 = [12 x i8] c"/proc/%d/fd\00"
define i32 @close_nonstd_fds() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.dirent*
store i32 32, i32* %1
store i32 -1, i32* %2
store i32 -1, i32* %3
store i8* null, i8** %4
store i8* null, i8** %5
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %6
%10 = alloca i8, i64 32
store i32* null, i32** %7
store %struct.dirent* null, %struct.dirent** %8
%11 = call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %0
br label %73
15: ; preds = %0
br label %16
16: ; preds = %71, %60, %37, %30, %15
%17 = load i32*, i32** %7
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %8
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %72
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = call i32 @free(i8* %21)
%23 = load i8*, i8** %5
%24 = call i32 @free(i8* %23)
%25 = load %struct.dirent*, %struct.dirent** %8
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %20
br label %16
31: ; preds = %20
%32 = load %struct.dirent*, %struct.dirent** %8
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
br label %16
38: ; preds = %31
%39 = load %struct.dirent*, %struct.dirent** %8
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %38
br label %73
45: ; preds = %38
%46 = call i8* (...) @getpid()
%47 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %45
br label %73
50: ; preds = %45
%51 = call i32 @bzero(i8* %10, i32 32)
%52 = load i8*, i8** %5
%53 = call i32 @readlink(i8* %52, i8* %10, i32 31)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %56
55: ; preds = %50
br label %73
56: ; preds = %50
%57 = load i8*, i8** %4
%58 = call i64 @strcmp(i8* %57, i8* %10)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %56
br label %16
61: ; preds = %56
%62 = load %struct.dirent*, %struct.dirent** %8
%63 = getelementptr inbounds %struct.dirent, %struct.dirent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = call i32 @atoi(i8* %64)
store i32 %65, i32* %3
%66 = load i32, i32* %3
switch i32 %66, label %68 [
i32 0, label %67
i32 1, label %67
i32 2, label %67
]
67: ; preds = %61, %61, %61
br label %71
68: ; preds = %61
%69 = load i32, i32* %3
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68, %67
br label %16
72: ; preds = %16
store i32 0, i32* %2
br label %73
73: ; preds = %72, %55, %49, %44, %14
%74 = load i32*, i32** %7
%75 = call i32 @closedir(i32* %74)
store i32* null, i32** %7
store %struct.dirent* null, %struct.dirent** %8
%76 = load i8*, i8** %4
%77 = call i32 @free(i8* %76)
%78 = load i8*, i8** %5
%79 = call i32 @free(i8* %78)
store i8* null, i8** %5
store i8* null, i8** %4
%80 = load i32, i32* %2
%81 = load i8*, i8** %6
ret i32 %80
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [14 x i8] c"/proc/self/fd\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [17 x i8] c"/proc/self/fd/%s\00"
@.str.4 = [12 x i8] c"/proc/%d/fd\00"
define i32 @close_nonstd_fds() {
%1 = alloca i8*
%2 = alloca i8*
%3 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = tail call i8* @llvm.stacksave()
%6 = alloca [32 x i8]
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %55, label %10
10: ; preds = %0
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %55, label %13
13: ; preds = %10, %53
%14 = phi i8* [ %54, %53 ], [ null, %10 ]
%15 = phi %struct.dirent* [ %51, %53 ], [ %11, %10 ]
%16 = call i32 @free(i8* %14)
%17 = load i8*, i8** %2
%18 = call i32 @free(i8* %17)
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %50, label %23
23: ; preds = %13
%24 = load i8*, i8** %19
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %50, label %27
27: ; preds = %23
%28 = load i8*, i8** %19
%29 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %55, label %31
31: ; preds = %27
%32 = call i8* (...) @getpid()
%33 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %55, label %35
35: ; preds = %31
%36 = call i32 @bzero(i8* %7, i32 32)
%37 = load i8*, i8** %2
%38 = call i32 @readlink(i8* %37, i8* %7, i32 31)
%39 = icmp eq i32 %38, -1
br i1 %39, label %55, label %40
40: ; preds = %35
%41 = load i8*, i8** %1
%42 = call i64 @strcmp(i8* %41, i8* %7)
%43 = icmp eq i64 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %40
%45 = load i8*, i8** %19
%46 = call i32 @atoi(i8* %45)
%47 = icmp ult i32 %46, 3
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = call i32 @close(i32 %46)
br label %50
50: ; preds = %48, %44, %13, %23, %40
%51 = call %struct.dirent* @readdir(i32* %8)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %55, label %53
53: ; preds = %50
%54 = load i8*, i8** %1
br label %13
55: ; preds = %27, %31, %35, %50, %10, %0
%56 = phi i32 [ -1, %0 ], [ 0, %10 ], [ -1, %27 ], [ -1, %31 ], [ -1, %35 ], [ 0, %50 ]
%57 = call i32 @closedir(i32* %8)
%58 = load i8*, i8** %1
%59 = call i32 @free(i8* %58)
%60 = load i8*, i8** %2
%61 = call i32 @free(i8* %60)
store i8* null, i8** %2
store i8* null, i8** %1
ret i32 %56
}