#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ execl(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ fchdir(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ is_valid_username(char *);
scalar_t__ mkdir(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int main(int argc, char *argv[]) {
  int i;
  int dir_fd;
  struct stat sbuf;
  if (stat("foo", &sbuf) < 0) {
    if (errno == ENOENT) {
      if (mkdir("foo", 0755) < 0) {
        fprintf(stderr, "Failed to create %s - %s\n", "foo", strerror(errno));
        exit(1);
      }
    } else {
      fprintf(stderr, "Failed to stat %s - %s\n", "foo", strerror(errno));
      exit(1);
    }
  } else if (!S_ISDIR(sbuf.st_mode)) {
    fprintf(stderr, "Error - %s is not a directory!\n", "foo");
    exit(1);
  }
  if ((dir_fd = open(".", O_RDONLY)) < 0) {
    fprintf(stderr, "Failed to open \".\" for reading - %s\n", strerror(errno));
    exit(1);
  }
  if (chroot("foo") < 0) {
    fprintf(stderr, "Failed to chroot to %s - %s\n", "foo", strerror(errno));
    exit(1);
  }
  if (fchdir(dir_fd) < 0) {
    fprintf(stderr, "Failed to fchdir - %s\n", strerror(errno));
    exit(1);
  }
  close(dir_fd);
  for (i = 0; i < 1024; i++) {
    chdir("..");
  }
  chroot(".");
  if (argc > 1) {
    if (!is_valid_username(argv[1])) {
      fprintf(stderr, "Exiting...\n");
      exit(1);
    }
    char switch_user_command[256];
    snprintf(switch_user_command, sizeof switch_user_command,
             "su --login -- %s", argv[1]);
    if (execl("/bin/bash", "-i", "-c", switch_user_command, NULL) < 0) {
      fprintf(stderr, "Failed to exec - %s\n", strerror(errno));
      exit(1);
    } else {
      exit(0);
    }
  } else if (argc <= 1) {
    if (execl("/bin/bash", "-i", NULL) < 0) {
      fprintf(stderr, "Failed to exec - %s\n", strerror(errno));
      exit(1);
    } else {
      exit(0);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe00(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jge    12de <main+0xfe>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
jne    1293 <main+0xb3>
lea    0xdcc(%rip),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    128e <main+0xae>
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x124(%rbp),%edi
mov    %rax,%rcx
lea    0xd8e(%rip),%rsi
lea    0xd83(%rip),%rdx
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12d9 <main+0xf9>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x128(%rbp),%edi
mov    %rax,%rcx
lea    0xd5d(%rip),%rsi
lea    0xd38(%rip),%rdx
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    131c <main+0x13c>
mov    -0x20(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1317 <main+0x137>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
lea    0xd37(%rip),%rsi
lea    0xcfa(%rip),%rdx
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    131c <main+0x13c>
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
lea    0xd84(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    137c <main+0x19c>
mov    0x2c94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    0x2c7d(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x12c(%rbp),%edi
mov    %rax,%rdx
lea    0xceb(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xc7d(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
jge    13d8 <main+0x1f8>
mov    0x2c3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x130(%rbp)
mov    0x2c28(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x130(%rbp),%edi
mov    %rax,%rcx
lea    0xcbb(%rip),%rsi
lea    0xc39(%rip),%rdx
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
call   10c0 <fchdir@plt>
cmp    $0x0,%rax
jge    1429 <main+0x249>
mov    0x2be7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x134(%rbp)
mov    0x2bd0(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x134(%rbp),%edi
mov    %rax,%rdx
lea    0xc80(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x400,-0x14(%rbp)
jge    145f <main+0x27f>
lea    0xc63(%rip),%rdi
call   1050 <chdir@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1438 <main+0x258>
lea    0xc4a(%rip),%rdi
call   1030 <chroot@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1544 <main+0x364>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <is_valid_username@plt>
cmp    $0x0,%eax
jne    14ac <main+0x2cc>
mov    0x2b46(%rip),%rax
mov    (%rax),%edi
lea    0xc17(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x100,%esi
lea    0xbf7(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x120(%rbp),%rcx
lea    0xbf5(%rip),%rdi
lea    0xbf8(%rip),%rsi
lea    0xbf4(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x0,%al
call   1100 <execl@plt>
cmp    $0x0,%rax
jge    153d <main+0x35d>
mov    0x2ad3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x138(%rbp)
mov    0x2abc(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x138(%rbp),%edi
mov    %rax,%rdx
lea    0xbb3(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jg     15b7 <main+0x3d7>
lea    0xb7a(%rip),%rdi
lea    0xb7d(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   1100 <execl@plt>
cmp    $0x0,%rax
jge    15b0 <main+0x3d0>
mov    0x2a60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x13c(%rbp)
mov    0x2a49(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x13c(%rbp),%edi
mov    %rax,%rdx
lea    0xb40(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
jmp    15bc <main+0x3dc>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe06(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
test   %rax,%rax
jns    125f <main+0x7f>
mov    0x2dc0(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2da6(%rip),%rax
cmp    (%rax),%rdi
jne    12d3 <main+0xf3>
lea    0xdd6(%rip),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jns    1285 <main+0xa5>
mov    0x2d98(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rdi
call   10f0 <strerror@plt>
lea    0xdb3(%rip),%rsi
lea    0xda8(%rip),%rdx
mov    %ebp,%edi
jmp    1325 <main+0x145>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
jne    1285 <main+0xa5>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
lea    0xd7d(%rip),%rdx
jmp    12c2 <main+0xe2>
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
lea    0xe1b(%rip),%rdi
call   10d0 <open@plt>
test   %eax,%eax
jns    12ea <main+0x10a>
mov    0x2d33(%rip),%rax
mov    (%rax),%ebx
mov    0x2d22(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xd99(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%ebx
call   10f0 <strerror@plt>
lea    0xd36(%rip),%rsi
jmp    131c <main+0x13c>
mov    %eax,%ebx
lea    0xd0d(%rip),%rdi
call   1030 <chroot@plt>
test   %rax,%rax
jns    1339 <main+0x159>
mov    0x2cd4(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc3(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xd5f(%rip),%rsi
lea    0xcdd(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %ebx,%edi
call   10c0 <fchdir@plt>
test   %rax,%rax
jns    1369 <main+0x189>
mov    0x2c8c(%rip),%rax
mov    (%rax),%ebx
mov    0x2c7b(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xd34(%rip),%rsi
jmp    12bd <main+0xdd>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0x400,%ebp
lea    0xd33(%rip),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <chdir@plt>
add    $0xffffffff,%ebp
jne    1380 <main+0x1a0>
lea    0xd1c(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x2,%r15d
jl     13cd <main+0x1ed>
mov    0x8(%r14),%rdi
call   1090 <is_valid_username@plt>
test   %eax,%eax
jne    140d <main+0x22d>
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
lea    0xcf6(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xcfb(%rip),%rdi
lea    0xcfe(%rip),%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1100 <execl@plt>
test   %rax,%rax
jns    1451 <main+0x271>
mov    0x2be8(%rip),%rax
mov    (%rax),%ebx
mov    0x2bd7(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xcd7(%rip),%rsi
jmp    12bd <main+0xdd>
mov    0x8(%r14),%rcx
lea    0xca6(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <snprintf@plt>
lea    0xc9e(%rip),%rdi
lea    0xca1(%rip),%rsi
lea    0xc9d(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
xor    %eax,%eax
call   1100 <execl@plt>
test   %rax,%rax
js     13e9 <main+0x209>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [4 x i8] c"foo\00"
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"Failed to create %s - %s\0A\00"
@.str.2 = [24 x i8] c"Failed to stat %s - %s\0A\00"
@.str.3 = [32 x i8] c"Error - %s is not a directory!\0A\00"
@.str.4 = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@.str.5 = [37 x i8] c"Failed to open \22.\22 for reading - %s\0A\00"
@.str.6 = [29 x i8] c"Failed to chroot to %s - %s\0A\00"
@.str.7 = [23 x i8] c"Failed to fchdir - %s\0A\00"
@.str.8 = [3 x i8] c"..\00"
@.str.9 = [12 x i8] c"Exiting...\0A\00"
@.str.10 = [17 x i8] c"su --login -- %s\00"
@.str.11 = [10 x i8] c"/bin/bash\00"
@.str.12 = [3 x i8] c"-i\00"
@.str.13 = [3 x i8] c"-c\00"
@.str.14 = [21 x i8] c"Failed to exec - %s\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 %struct.stat
%9 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 @stat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %struct.stat* %8)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %2
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = call i64 @mkdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 493)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i64, i64* @errno
%22 = call i8* @strerror(i64 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
br label %32
26: ; preds = %12
%27 = load i32, i32* @stderr
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
br label %43
33: ; preds = %2
%34 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @S_ISDIR(i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
br label %43
43: ; preds = %42, %32
%44 = load i32, i32* @O_RDONLY
%45 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %44)
store i32 %45, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = load i64, i64* @errno
%50 = call i8* @strerror(i64 %49)
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %43
%54 = call i64 @chroot(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %53
%57 = load i32, i32* @stderr
%58 = load i64, i64* @errno
%59 = call i8* @strerror(i64 %58)
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %53
%63 = load i32, i32* %7
%64 = call i64 @fchdir(i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = load i64, i64* @errno
%69 = call i8* @strerror(i64 %68)
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %62
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
store i32 0, i32* %6
br label %75
75: ; preds = %80, %72
%76 = load i32, i32* %6
%77 = icmp slt i32 %76, 1024
br i1 %77, label %78, label %83
78: ; preds = %75
%79 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
br label %80
80: ; preds = %78
%81 = load i32, i32* %6
%82 = add nsw i32 %81, 1
store i32 %82, i32* %6
br label %75
83: ; preds = %75
%84 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%85 = load i32, i32* %4
%86 = icmp sgt i32 %85, 1
br i1 %86, label %87, label %114
87: ; preds = %83
%88 = load i8**, i8*** %5
%89 = getelementptr inbounds i8*, i8** %88, i64 1
%90 = load i8*, i8** %89
%91 = call i32 @is_valid_username(i8* %90)
%92 = icmp ne i32 %91, 0
br i1 %92, label %97, label %93
93: ; preds = %87
%94 = load i32, i32* @stderr
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
%96 = call i32 @exit(i32 1)
unreachable
97: ; preds = %87
%98 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%99 = load i8**, i8*** %5
%100 = getelementptr inbounds i8*, i8** %99, i64 1
%101 = load i8*, i8** %100
%102 = call i32 @snprintf(i8* %98, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i8* %101)
%103 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%104 = call i64 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.13, i64 0, i64 0), i8* %103, i8* null)
%105 = icmp slt i64 %104, 0
br i1 %105, label %106, label %112
106: ; preds = %97
%107 = load i32, i32* @stderr
%108 = load i64, i64* @errno
%109 = call i8* @strerror(i64 %108)
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.14, i64 0, i64 0), i8* %109)
%111 = call i32 @exit(i32 1)
unreachable
112: ; preds = %97
%113 = call i32 @exit(i32 0)
unreachable
114: ; preds = %83
%115 = load i32, i32* %4
%116 = icmp sle i32 %115, 1
br i1 %116, label %117, label %128
117: ; preds = %114
%118 = call i64 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i64 0, i64 0), i8* null)
%119 = icmp slt i64 %118, 0
br i1 %119, label %120, label %126
120: ; preds = %117
%121 = load i32, i32* @stderr
%122 = load i64, i64* @errno
%123 = call i8* @strerror(i64 %122)
%124 = call i32 (i32, i8*, ...) @fprintf(i32 %121, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.14, i64 0, i64 0), i8* %123)
%125 = call i32 @exit(i32 1)
unreachable
126: ; preds = %117
%127 = call i32 @exit(i32 0)
unreachable
128: ; preds = %114
br label %129
129: ; preds = %128
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [4 x i8] c"foo\00"
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"Failed to create %s - %s\0A\00"
@.str.2 = [24 x i8] c"Failed to stat %s - %s\0A\00"
@.str.3 = [32 x i8] c"Error - %s is not a directory!\0A\00"
@.str.4 = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@.str.5 = [37 x i8] c"Failed to open \22.\22 for reading - %s\0A\00"
@.str.6 = [29 x i8] c"Failed to chroot to %s - %s\0A\00"
@.str.7 = [23 x i8] c"Failed to fchdir - %s\0A\00"
@.str.8 = [3 x i8] c"..\00"
@.str.9 = [12 x i8] c"Exiting...\0A\00"
@.str.10 = [17 x i8] c"su --login -- %s\00"
@.str.11 = [10 x i8] c"/bin/bash\00"
@.str.12 = [3 x i8] c"-i\00"
@.str.13 = [3 x i8] c"-c\00"
@.str.14 = [21 x i8] c"Failed to exec - %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [256 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i64 @stat(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %26
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %21
12: ; preds = %8
%13 = call i64 @mkdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 493)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %35
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i64, i64* @errno
%18 = call i8* @strerror(i64 %17)
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %8
%22 = load i32, i32* @stderr
%23 = call i8* @strerror(i64 %9)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @S_ISDIR(i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %26, %12
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = load i64, i64* @errno
%42 = call i8* @strerror(i64 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %35
%46 = call i64 @chroot(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = load i64, i64* @errno
%51 = call i8* @strerror(i64 %50)
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %45
%55 = call i64 @fchdir(i32 %37)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = load i32, i32* @stderr
%59 = load i64, i64* @errno
%60 = call i8* @strerror(i64 %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %60)
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %54
%64 = call i32 @close(i32 %37)
br label %65
65: ; preds = %63, %65
%66 = phi i32 [ 0, %63 ], [ %68, %65 ]
%67 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%68 = add nuw nsw i32 %66, 1
%69 = icmp eq i32 %68, 1024
br i1 %69, label %70, label %65
70: ; preds = %65
%71 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%72 = icmp sgt i32 %0, 1
br i1 %72, label %73, label %96
73: ; preds = %70
%74 = getelementptr inbounds i8*, i8** %1, i64 1
%75 = load i8*, i8** %74
%76 = call i32 @is_valid_username(i8* %75)
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %73
%79 = load i32, i32* @stderr
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %73
%83 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%84 = load i8*, i8** %74
%85 = call i32 @snprintf(i8* %83, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i8* %84)
%86 = call i64 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.13, i64 0, i64 0), i8* %83, i8* null)
%87 = icmp slt i64 %86, 0
br i1 %87, label %88, label %94
88: ; preds = %82
%89 = load i32, i32* @stderr
%90 = load i64, i64* @errno
%91 = call i8* @strerror(i64 %90)
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.14, i64 0, i64 0), i8* %91)
%93 = call i32 @exit(i32 1)
unreachable
94: ; preds = %82
%95 = call i32 @exit(i32 0)
unreachable
96: ; preds = %70
%97 = call i64 (i8*, i8*, i8*, ...) @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i64 0, i64 0), i8* null)
%98 = icmp slt i64 %97, 0
br i1 %98, label %99, label %105
99: ; preds = %96
%100 = load i32, i32* @stderr
%101 = load i64, i64* @errno
%102 = call i8* @strerror(i64 %101)
%103 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.14, i64 0, i64 0), i8* %102)
%104 = call i32 @exit(i32 1)
unreachable
105: ; preds = %96
%106 = call i32 @exit(i32 0)
unreachable
}