#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 */
scalar_t__ EINTR;
int close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dirfd(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int strtol(int /*<<< orphan*/, char **, int);
int main(int argc, char **argv) {
  DIR *dirp;
  struct dirent *entry;
  int fd;
  char *eptr;
  dirp = opendir("/proc/self/fd");
  if (dirp == NULL) {
    perror("opendir");
    exit(255);
  }
  while ((entry = readdir(dirp)) != NULL) {
    fd = strtol(entry->d_name, &eptr, 10);
    if (eptr[0] == '\0') {
      if (fd > 2 && fd != !dirfd(dirp)) {
        while (close(fd) == -1 && errno == EINTR)
          ;
      }
    }
  }
  closedir(dirp);
  execvp(argv[1], &argv[1]);
  perror("execvp");
  exit(255);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d7 <main+0x47>
lea    0xe46(%rip),%rdi
call   1090 <perror@plt>
mov    $0xff,%edi
call   10b0 <exit@plt>
jmp    11dc <main+0x4c>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12aa <main+0x11a>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12a5 <main+0x115>
cmpl   $0x2,-0x24(%rbp)
jle    12a0 <main+0x110>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <dirfd@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    $0x0,%ecx
setne  %cl
xor    $0xff,%cl
and    $0x1,%cl
movzbl %cl,%ecx
cmp    %ecx,%eax
je     12a0 <main+0x110>
jmp    1254 <main+0xc4>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x35(%rbp)
jne    1286 <main+0xf6>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    1296 <main+0x106>
jmp    129b <main+0x10b>
jmp    1254 <main+0xc4>
jmp    12a0 <main+0x110>
jmp    12a5 <main+0x115>
jmp    11dc <main+0x4c>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10a0 <execvp@plt>
lea    0xd47(%rip),%rdi
call   1090 <perror@plt>
mov    $0xff,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe5b(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
jne    11b8 <main+0x28>
lea    0xe58(%rip),%rdi
jmp    11e7 <main+0x57>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11f6 <main+0x66>
mov    %r12,%rdi
call   1050 <closedir@plt>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   10a0 <execvp@plt>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
mov    $0xff,%edi
call   10b0 <exit@plt>
mov    %rsp,%r15
mov    0x2de0(%rip),%r13
mov    0x2dd1(%rip),%rbx
jmp    121d <main+0x8d>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11c8 <main+0x38>
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,%ebp
mov    (%rsp),%rax
cmpb   $0x0,(%rax)
jne    1210 <main+0x80>
cmp    $0x3,%ebp
jl     1210 <main+0x80>
mov    %r12,%rdi
call   1060 <dirfd@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1210 <main+0x80>
mov    (%rbx),%rax
cmp    %rax,0x0(%r13)
je     1250 <main+0xc0>
jmp    1210 <main+0x80>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [14 x i8] c"/proc/self/fd\00"
@.str.1 = [8 x i8] c"opendir\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 255)
unreachable
16: ; preds = %2
br label %17
17: ; preds = %56, %16
%18 = load i32*, i32** %6
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %7
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %57
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %7
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @strtol(i32 %24, i8** %9, i32 10)
store i32 %25, i32* %8
%26 = load i8*, i8** %9
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %56
31: ; preds = %21
%32 = load i32, i32* %8
%33 = icmp sgt i32 %32, 2
br i1 %33, label %34, label %55
34: ; preds = %31
%35 = load i32, i32* %8
%36 = load i32*, i32** %6
%37 = call i32 @dirfd(i32* %36)
%38 = icmp ne i32 %37, 0
%39 = xor i1 %38, true
%40 = zext i1 %39 to i32
%41 = icmp ne i32 %35, %40
br i1 %41, label %42, label %55
42: ; preds = %34
br label %43
43: ; preds = %53, %42
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i64, i64* @errno
%49 = load i64, i64* @EINTR
%50 = icmp eq i64 %48, %49
br label %51
51: ; preds = %47, %43
%52 = phi i1 [ false, %43 ], [ %50, %47 ]
br i1 %52, label %53, label %54
53: ; preds = %51
br label %43
54: ; preds = %51
br label %55
55: ; preds = %54, %34, %31
br label %56
56: ; preds = %55, %21
br label %17
57: ; preds = %17
%58 = load i32*, i32** %6
%59 = call i32 @closedir(i32* %58)
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 1
%62 = load i8*, i8** %61
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 1
%65 = call i32 @execvp(i8* %62, i8** %64)
%66 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%67 = call i32 @exit(i32 255)
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [14 x i8] c"/proc/self/fd\00"
@.str.1 = [8 x i8] c"opendir\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %35, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 255)
unreachable
13: ; preds = %7, %32
%14 = phi %struct.dirent* [ %33, %32 ], [ %8, %7 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strtol(i32 %16, i8** %3, i32 10)
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
%21 = icmp sgt i32 %17, 2
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %23, label %32
23: ; preds = %13
%24 = call i32 @dirfd(i32* %5)
br label %25
25: ; preds = %23, %25
%26 = call i32 @close(i32 %17)
%27 = icmp eq i32 %26, -1
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
%31 = select i1 %27, i1 %30, i1 false
br i1 %31, label %25, label %32
32: ; preds = %25, %13
%33 = call %struct.dirent* @readdir(i32* %5)
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %35, label %13
35: ; preds = %32, %7
%36 = call i32 @closedir(i32* %5)
%37 = getelementptr inbounds i8*, i8** %1, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @execvp(i8* %38, i8** %37)
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 255)
unreachable
}