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

/* Variables and functions */
int NOFILE;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 2)
    return 1;
  int fd;
  for (fd = 3; fd < NOFILE; ++fd) {
    close(fd);
  }
  umask(0);
  if (chdir("/") != 0) {
    perror("Failed to change to root directory");
    return 1;
  }
  execvp(argv[1], &argv[1]);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    117c <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    11fc <main+0xac>
movl   $0x3,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%eax
jge    11ab <main+0x5b>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1183 <main+0x33>
xor    %edi,%edi
call   1050 <umask@plt>
lea    0xe47(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     11e0 <main+0x90>
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fc <main+0xac>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1070 <execvp@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jl     11a6 <main+0x56>
mov    %rsi,%r14
mov    0x2e75(%rip),%rbx
cmpl   $0x4,(%rbx)
jl     117e <main+0x2e>
mov    $0x3,%ebp
nopl   (%rax)
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    (%rbx),%ebp
jl     1170 <main+0x20>
xor    %edi,%edi
call   1050 <umask@plt>
lea    0xe74(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jne    11b0 <main+0x60>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   1070 <execvp@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
jmp    11a6 <main+0x56>

LLVM IR (-O0):
@NOFILE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [35 x i8] c"Failed to change to root directory\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 1, i32* %3
br label %34
10: ; preds = %2
store i32 3, i32* %6
br label %11
11: ; preds = %18, %10
%12 = load i32, i32* %6
%13 = load i32, i32* @NOFILE
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32, i32* %6
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %15
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
br label %11
21: ; preds = %11
%22 = call i32 @umask(i32 0)
%23 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %34
27: ; preds = %21
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = call i32 @execvp(i8* %30, i8** %32)
store i32 1, i32* %3
br label %34
34: ; preds = %27, %25, %9
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@NOFILE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [35 x i8] c"Failed to change to root directory\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %23, label %4
4: ; preds = %2
%5 = load i32, i32* @NOFILE
%6 = icmp sgt i32 %5, 3
br i1 %6, label %7, label %13
7: ; preds = %4, %7
%8 = phi i32 [ %10, %7 ], [ 3, %4 ]
%9 = tail call i32 @close(i32 %8)
%10 = add nuw nsw i32 %8, 1
%11 = load i32, i32* @NOFILE
%12 = icmp slt i32 %10, %11
br i1 %12, label %7, label %13
13: ; preds = %7, %4
%14 = tail call i32 @umask(i32 0)
%15 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %23
19: ; preds = %13
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @execvp(i8* %21, i8** %20)
br label %23
23: ; preds = %17, %19, %2
ret i32 1
}