#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *get_shell();
char **malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putenv(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct stat buf;
  char *dir;
  char *program;
  char **real_argv;
  int i, j;
  int res;
  int dir_fd;
  if (argc < 2) {
    fprintf(stderr, "Usage: glick-shell {directory} [program [args]]\n");
    return 1;
  }
  dir = argv[1];
  res = stat(dir, &buf);
  if (res != 0 || !S_ISDIR(buf.st_mode)) {
    fprintf(stderr, "Error: Specified location not a directory.\n");
    return 1;
  }
  if (argc >= 3)
    program = argv[2];
  else
    program = get_shell();
  real_argv = malloc(sizeof(char *) * (argc + 1));
  i = 0;
  real_argv[i++] = program;
  for (j = 3; j < argc; j++) {
    real_argv[i++] = argv[j];
  }
  real_argv[i] = NULL;
  dir_fd = open(dir, O_RDONLY);
  if (dir_fd == -1) {
    perror("Error opening directory: ");
    return 1;
  }
  res = dup2(dir_fd, 1023);
  if (res == -1) {
    perror("dup2 error: ");
    return 1;
  }
  close(dir_fd);
  putenv("GLICKROOT=/proc/self/fd/1023");
  execvp(program, real_argv);
  perror("Error starting program");
  return 1;
}


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)
cmpl   $0x2,-0x8(%rbp)
jge    11f1 <main+0x41>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <main+0x1e7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1228 <main+0x78>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1249 <main+0x99>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <main+0x1e7>
cmpl   $0x3,-0x8(%rbp)
jl     1264 <main+0xb4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    126f <main+0xbf>
mov    $0x0,%al
call   10d0 <get_shell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
movl   $0x3,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12ea <main+0x13a>
mov    -0x10(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12ae <main+0xfe>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rdi
mov    0x2ce3(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1331 <main+0x181>
lea    0xd3d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <main+0x1e7>
mov    -0x40(%rbp),%edi
mov    $0x3ff,%esi
call   1030 <dup2@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1363 <main+0x1b3>
lea    0xd25(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <main+0x1e7>
mov    -0x40(%rbp),%edi
call   1050 <close@plt>
lea    0xd12(%rip),%rdi
call   1060 <putenv@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10c0 <execvp@plt>
lea    0xd16(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     11e4 <main+0x24>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
jmp    121b <main+0x5b>
mov    %rsi,%r12
mov    %edi,%ebx
mov    0x8(%rsi),%r14
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1080 <stat@plt>
test   %eax,%eax
jne    120b <main+0x4b>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
jne    1233 <main+0x73>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
cmp    $0x3,%ebx
jb     123f <main+0x7f>
mov    0x10(%r12),%r15
jmp    1249 <main+0x89>
xor    %eax,%eax
call   10e0 <get_shell@plt>
mov    %rax,%r15
lea    0x8(,%rbx,8),%edi
call   10a0 <malloc@plt>
mov    %rax,%r13
mov    %r15,(%rax)
mov    $0x1,%eax
cmp    $0x4,%ebx
jb     1285 <main+0xc5>
mov    %r13,%rdi
add    $0x8,%rdi
add    $0x18,%r12
lea    -0x3(%rbx),%edx
shl    $0x3,%rdx
mov    %r12,%rsi
call   1090 <memcpy@plt>
add    $0xfffffffe,%ebx
mov    %rbx,%rax
movq   $0x0,0x0(%r13,%rax,8)
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
jne    12ad <main+0xed>
lea    0xdb2(%rip),%rdi
jmp    12ee <main+0x12e>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x3ff,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12c9 <main+0x109>
lea    0xdb0(%rip),%rdi
jmp    12ee <main+0x12e>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xdad(%rip),%rdi
call   1060 <putenv@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   10d0 <execvp@plt>
lea    0xdb3(%rip),%rdi
call   10c0 <perror@plt>
jmp    1220 <main+0x60>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [49 x i8] c"Usage: glick-shell {directory} [program [args]]\0A\00"
@.str.1 = [44 x i8] c"Error: Specified location not a directory.\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [26 x i8] c"Error opening directory: \00"
@.str.3 = [13 x i8] c"dup2 error: \00"
@.str.4 = [29 x i8] c"GLICKROOT=/proc/self/fd/1023\00"
@.str.5 = [23 x i8] c"Error starting program\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %102
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = call i32 @stat(i8* %23, %struct.stat* %6)
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp ne i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %19
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @S_ISDIR(i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %27, %19
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %102
35: ; preds = %27
%36 = load i32, i32* %4
%37 = icmp sge i32 %36, 3
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
store i8* %41, i8** %8
br label %44
42: ; preds = %35
%43 = call i8* (...) @get_shell()
store i8* %43, i8** %8
br label %44
44: ; preds = %42, %38
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = mul i64 8, %47
%49 = trunc i64 %48 to i32
%50 = call i8** @malloc(i32 %49)
store i8** %50, i8*** %9
store i32 0, i32* %10
%51 = load i8*, i8** %8
%52 = load i8**, i8*** %9
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i8*, i8** %52, i64 %55
store i8* %51, i8** %56
store i32 3, i32* %11
br label %57
57: ; preds = %72, %44
%58 = load i32, i32* %11
%59 = load i32, i32* %4
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %75
61: ; preds = %57
%62 = load i8**, i8*** %5
%63 = load i32, i32* %11
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8*, i8** %62, i64 %64
%66 = load i8*, i8** %65
%67 = load i8**, i8*** %9
%68 = load i32, i32* %10
%69 = add nsw i32 %68, 1
store i32 %69, i32* %10
%70 = sext i32 %68 to i64
%71 = getelementptr inbounds i8*, i8** %67, i64 %70
store i8* %66, i8** %71
br label %72
72: ; preds = %61
%73 = load i32, i32* %11
%74 = add nsw i32 %73, 1
store i32 %74, i32* %11
br label %57
75: ; preds = %57
%76 = load i8**, i8*** %9
%77 = load i32, i32* %10
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8*, i8** %76, i64 %78
store i8* null, i8** %79
%80 = load i8*, i8** %7
%81 = load i32, i32* @O_RDONLY
%82 = call i32 @open(i8* %80, i32 %81)
store i32 %82, i32* %13
%83 = load i32, i32* %13
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %87
85: ; preds = %75
%86 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %102
87: ; preds = %75
%88 = load i32, i32* %13
%89 = call i32 @dup2(i32 %88, i32 1023)
store i32 %89, i32* %12
%90 = load i32, i32* %12
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %94
92: ; preds = %87
%93 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %102
94: ; preds = %87
%95 = load i32, i32* %13
%96 = call i32 @close(i32 %95)
%97 = call i32 @putenv(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%98 = load i8*, i8** %8
%99 = load i8**, i8*** %9
%100 = call i32 @execvp(i8* %98, i8** %99)
%101 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %102
102: ; preds = %94, %92, %85, %32, %16
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [49 x i8] c"Usage: glick-shell {directory} [program [args]]\0A\00"
@.str.1 = [44 x i8] c"Error: Specified location not a directory.\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [26 x i8] c"Error opening directory: \00"
@.str.3 = [13 x i8] c"dup2 error: \00"
@.str.4 = [29 x i8] c"GLICKROOT=/proc/self/fd/1023\00"
@.str.5 = [23 x i8] c"Error starting program\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %63
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @stat(i8* %11, %struct.stat* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i32 @S_ISDIR(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14, %9
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %63
22: ; preds = %14
%23 = icmp ugt i32 %0, 2
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
br label %29
27: ; preds = %22
%28 = tail call i8* (...) @get_shell()
br label %29
29: ; preds = %27, %24
%30 = phi i8* [ %26, %24 ], [ %28, %27 ]
%31 = shl i32 %0, 3
%32 = add i32 %31, 8
%33 = tail call i8** @malloc(i32 %32)
store i8* %30, i8** %33
%34 = icmp ugt i32 %0, 3
br i1 %34, label %35, label %45
35: ; preds = %29
%36 = getelementptr i8*, i8** %33, i64 1
%37 = bitcast i8** %36 to i8*
%38 = getelementptr i8*, i8** %1, i64 3
%39 = bitcast i8** %38 to i8*
%40 = add nsw i32 %0, -3
%41 = zext i32 %40 to i64
%42 = shl nuw nsw i64 %41, 3
%43 = add nsw i32 %0, -2
%44 = zext i32 %43 to i64
br label %45
45: ; preds = %35, %29
%46 = phi i64 [ 1, %29 ], [ %44, %35 ]
%47 = getelementptr inbounds i8*, i8** %33, i64 %46
store i8* null, i8** %47
%48 = load i32, i32* @O_RDONLY
%49 = tail call i32 @open(i8* %11, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %63
53: ; preds = %45
%54 = tail call i32 @dup2(i32 %49, i32 1023)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %63
58: ; preds = %53
%59 = tail call i32 @close(i32 %49)
%60 = tail call i32 @putenv(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%61 = tail call i32 @execvp(i8* %30, i8** %33)
%62 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %63
63: ; preds = %58, %56, %51, %19, %6
ret i32 1
}