#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 /*<<< orphan*/ asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *get_current_dir_name();
int /*<<< orphan*/ printf(char *);
scalar_t__ setenv(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unsetenv(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int cid;
  int status;
  char *cwd;
  char *args[2] = {"bash", NULL};
  char *env;
  cwd = get_current_dir_name();
  if (!cwd) {
    return 1;
  }
  asprintf(&env, "%s/libpurelibc.so:%s/libjail.so", cwd, cwd);
  if (setenv("LD_PRELOAD", env, 1) < 0) {
    fprintf(stderr, "%s", strerror(errno));
    free(cwd);
    free(env);
    return 1;
  }
  setenv("JAIL", cwd, 1);
  cid = fork();
  if (!cid) {
    execvp("bash", args);
    return 0;
  } else {
    wait(&status);
  }
  printf("All over now.\n");
  free(cwd);
  free(env);
  unsetenv("LD_PRELOAD");
  return 0;
}


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)
mov    0x2c33(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c30(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <get_current_dir_name@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fe <main+0x4e>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x14e>
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%rdi
lea    0xdf4(%rip),%rsi
call   1080 <asprintf@plt>
mov    -0x38(%rbp),%rsi
lea    0xe04(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0x0,%rax
jge    127f <main+0xcf>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd4(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x14e>
mov    -0x20(%rbp),%rsi
lea    0xda9(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    12c4 <main+0x114>
lea    -0x30(%rbp),%rsi
lea    0xd4d(%rip),%rdi
call   10a0 <execvp@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fe <main+0x14e>
lea    -0x18(%rbp),%rdi
call   10c0 <wait@plt>
lea    0xd64(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
lea    0xd33(%rip),%rdi
call   1090 <unsetenv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movaps 0x2c41(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1070 <get_current_dir_name@plt>
test   %rax,%rax
je     1261 <main+0xb1>
mov    %rax,%rbx
lea    0xe27(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
mov    %rax,%rcx
call   1080 <asprintf@plt>
mov    (%rsp),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x1,%ebp
mov    $0x1,%edx
call   1050 <setenv@plt>
test   %rax,%rax
js     1268 <main+0xb8>
lea    0xe21(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12a5 <main+0xf5>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
lea    0xdfb(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
lea    0xdcb(%rip),%rdi
call   1090 <unsetenv@plt>
jmp    12b6 <main+0x106>
mov    $0x1,%ebp
jmp    12b6 <main+0x106>
mov    0x2d71(%rip),%rax
mov    (%rax),%r14d
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xda9(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    12b6 <main+0x106>
lea    0xd54(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <execvp@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"bash\00"
@__const.main.args = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [32 x i8] c"%s/libpurelibc.so:%s/libjail.so\00"
@.str.2 = [11 x i8] c"LD_PRELOAD\00"
@stderr = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@errno = global i32 0
@.str.4 = [5 x i8] c"JAIL\00"
@.str.5 = [15 x i8] c"All over now.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [2 x i8*]
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [2 x i8*]* %9 to i8*
%12 = call i8* (...) @get_current_dir_name()
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 1, i32* %3
br label %50
16: ; preds = %2
%17 = load i8*, i8** %8
%18 = load i8*, i8** %8
%19 = call i32 @asprintf(i8** %10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %18)
%20 = load i8*, i8** %10
%21 = call i64 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %20, i32 1)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %8
%29 = call i32 @free(i8* %28)
%30 = load i8*, i8** %10
%31 = call i32 @free(i8* %30)
store i32 1, i32* %3
br label %50
32: ; preds = %16
%33 = load i8*, i8** %8
%34 = call i64 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %33, i32 1)
%35 = call i32 (...) @fork()
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %32
%39 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 0
%40 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %39)
store i32 0, i32* %3
br label %50
41: ; preds = %32
%42 = call i32 @wait(i32* %7)
br label %43
43: ; preds = %41
%44 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%45 = load i8*, i8** %8
%46 = call i32 @free(i8* %45)
%47 = load i8*, i8** %10
%48 = call i32 @free(i8* %47)
%49 = call i32 @unsetenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %50
50: ; preds = %43, %38, %23, %15
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [5 x i8] c"bash\00"
@__const.main.args = [2 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [32 x i8] c"%s/libpurelibc.so:%s/libjail.so\00"
@.str.2 = [11 x i8] c"LD_PRELOAD\00"
@stderr = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@errno = global i32 0
@.str.4 = [5 x i8] c"JAIL\00"
@str = [14 x i8] c"All over now.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [2 x i8*]
%5 = alloca i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast [2 x i8*]* %4 to i8*
%8 = bitcast i8** %5 to i8*
%9 = tail call i8* (...) @get_current_dir_name()
%10 = icmp eq i8* %9, null
br i1 %10, label %38, label %11
11: ; preds = %2
%12 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %9)
%13 = load i8*, i8** %5
%14 = call i64 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 1)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = call i32 @free(i8* %9)
%22 = load i8*, i8** %5
%23 = call i32 @free(i8* %22)
br label %38
24: ; preds = %11
%25 = call i64 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %9, i32 1)
%26 = call i32 (...) @fork()
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
%30 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %29)
br label %38
31: ; preds = %24
%32 = call i32 @wait(i32* %3)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%34 = call i32 @free(i8* %9)
%35 = load i8*, i8** %5
%36 = call i32 @free(i8* %35)
%37 = call i32 @unsetenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %2, %31, %28, %16
%39 = phi i32 [ 1, %16 ], [ 0, %31 ], [ 0, %28 ], [ 1, %2 ]
ret i32 %39
}