#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ X_OK;
int access(char *, int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ basename(char *, char *);
int /*<<< orphan*/ chdir(char *);
scalar_t__ errno;
int execv(char *, char **);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ join(char *, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
size_t split(char **, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ starts_with(char *, char *);
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int /*<<< orphan*/ wait(int *);
int main() {
  int exec_result;
  int exit_status;
  int child_status;
  int access_status;
  bool file_found = false;
  size_t argc;
  size_t i;
  size_t path_length;
  pid_t pid;
  char raw_input[256];
  char *command_line[256];
  char *path[256];
  char *env_path;
  char program_location[256];
  char temp_filename[256];
  char *cwd;
  char *home;
  do {
    cwd = getcwd(NULL, 0);
    if (cwd == NULL) {
      perror("jsh");
    }
    home = getenv("HOME");
    if (home != NULL && starts_with(cwd, home)) {
      sprintf(cwd, "~%s", cwd + strlen(home));
    }
    printf("JSH:%s$ ", cwd);
    fgets(raw_input, 256, stdin);
    argc = split(command_line, raw_input, " \t\n\v\f\r");
    command_line[argc] = NULL;
    if (argc == 0) {
      continue;
    }
    if (strcmp(command_line[0], "exit") == 0) {
      exit_status = 0;
      if (argc > 1) {
        exit_status = atoi(command_line[1]);
      }
      return exit_status;
    }
    if (strcmp(command_line[0], "cd") == 0) {
      if (argc < 2) {
        chdir(home);
      } else {
        chdir(command_line[1]);
      }
      continue;
    }
    strncpy(program_location, command_line[0], strlen(command_line[0]));
    program_location[strlen(command_line[0])] = '\0';
    basename(command_line[0], command_line[0]);
    pid = fork();
    if (pid < 0) {
      perror("Failed to fork");
    } else if (pid == 0) {
      access_status = access(program_location, X_OK);
      if (access_status == 0) {
        file_found = true;
      } else {
        if (errno == EACCES) {
          perror("File is not executable");
          return 0;
        }
        env_path = getenv("PATH");
        if (env_path != NULL) {
          path_length = split(path, env_path, ":");
          for (i = 0; i < path_length; i++) {
            join(temp_filename, path[i], command_line[0]);
            access_status = access(temp_filename, X_OK);
            if (access_status == 0) {
              strncpy(program_location, temp_filename, strlen(temp_filename));
              program_location[strlen(temp_filename)] = '\0';
              file_found = true;
              break;
            } else {
              if (errno == EACCES) {
                perror("File is not executable");
                break;
              }
            }
          }
        }
      }
      if (file_found) {
        exec_result = execv(program_location, command_line);
        if (exec_result < 0) {
          perror("Failed to exec");
          return 0;
        }
      } else {
        printf("%s: command not found.\n", command_line[0]);
        return -1;
      }
    } else {
      wait(&child_status);
    }
  } while (true);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1390,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1080 <getcwd@plt>
mov    %rax,-0x1358(%rbp)
cmpq   $0x0,-0x1358(%rbp)
jne    1275 <main+0x45>
lea    0xd90(%rip),%rdi
call   10f0 <perror@plt>
lea    0xd88(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1360(%rbp)
cmpq   $0x0,-0x1360(%rbp)
je     12f8 <main+0xc8>
mov    -0x1358(%rbp),%rdi
mov    -0x1360(%rbp),%rsi
call   10d0 <starts_with@plt>
cmp    $0x0,%rax
je     12f8 <main+0xc8>
mov    -0x1358(%rbp),%rax
mov    %rax,-0x1368(%rbp)
mov    -0x1358(%rbp),%rax
mov    %rax,-0x1370(%rbp)
mov    -0x1360(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1370(%rbp),%rdx
mov    -0x1368(%rbp),%rdi
add    %rax,%rdx
lea    0xd16(%rip),%rsi
call   1110 <sprintf@plt>
mov    -0x1358(%rbp),%rsi
lea    0xd07(%rip),%rdi
call   1070 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    0x2ccf(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   10a0 <fgets@plt>
lea    -0x940(%rbp),%rdi
lea    -0x140(%rbp),%rsi
lea    0xcdc(%rip),%rdx
call   1090 <split@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,-0x940(%rbp,%rax,8)
cmpq   $0x0,-0x20(%rbp)
jne    1363 <main+0x133>
jmp    16af <main+0x47f>
mov    -0x940(%rbp),%rdi
lea    0xcac(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    13ac <main+0x17c>
movl   $0x0,-0xc(%rbp)
cmpq   $0x1,-0x20(%rbp)
jbe    13a1 <main+0x171>
mov    -0x938(%rbp),%rdi
call   1100 <atoi@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    16c5 <main+0x495>
mov    -0x940(%rbp),%rdi
lea    0xc68(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    13f6 <main+0x1c6>
cmpq   $0x2,-0x20(%rbp)
jae    13e5 <main+0x1b5>
mov    -0x1360(%rbp),%rdi
call   1060 <chdir@plt>
jmp    13f1 <main+0x1c1>
mov    -0x938(%rbp),%rdi
call   1060 <chdir@plt>
jmp    16af <main+0x47f>
lea    -0x1250(%rbp),%rax
mov    %rax,-0x1380(%rbp)
mov    -0x940(%rbp),%rax
mov    %rax,-0x1378(%rbp)
mov    -0x940(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1380(%rbp),%rdi
mov    -0x1378(%rbp),%rsi
mov    %rax,%rdx
call   1040 <strncpy@plt>
mov    -0x940(%rbp),%rdi
call   1050 <strlen@plt>
movb   $0x0,-0x1250(%rbp,%rax,1)
mov    -0x940(%rbp),%rdi
mov    -0x940(%rbp),%rsi
call   10c0 <basename@plt>
mov    $0x0,%al
call   1150 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1482 <main+0x252>
lea    0xbad(%rip),%rdi
call   10f0 <perror@plt>
jmp    16aa <main+0x47a>
cmpq   $0x0,-0x38(%rbp)
jne    169c <main+0x46c>
lea    -0x1250(%rbp),%rdi
mov    0x2b45(%rip),%rax
mov    (%rax),%esi
call   10e0 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    14bb <main+0x28b>
movl   $0x1,-0x18(%rbp)
jmp    1631 <main+0x401>
mov    0x2b06(%rip),%rax
mov    (%rax),%rax
mov    0x2b0c(%rip),%rcx
cmp    (%rcx),%rax
jne    14ed <main+0x2bd>
lea    0xb58(%rip),%rdi
call   10f0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    16c5 <main+0x495>
lea    0xb57(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1148(%rbp)
cmpq   $0x0,-0x1148(%rbp)
je     162c <main+0x3fc>
lea    -0x1140(%rbp),%rdi
mov    -0x1148(%rbp),%rsi
lea    0xb2d(%rip),%rdx
call   1090 <split@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1627 <main+0x3f7>
lea    -0x1350(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x1140(%rbp,%rax,8),%rsi
ff
mov    -0x940(%rbp),%rdx
call   1120 <join@plt>
lea    -0x1350(%rbp),%rdi
mov    0x2a71(%rip),%rax
mov    (%rax),%esi
call   10e0 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    15e1 <main+0x3b1>
lea    -0x1250(%rbp),%rax
mov    %rax,-0x1390(%rbp)
lea    -0x1350(%rbp),%rax
mov    %rax,-0x1388(%rbp)
lea    -0x1350(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1390(%rbp),%rdi
mov    -0x1388(%rbp),%rsi
mov    %rax,%rdx
call   1040 <strncpy@plt>
lea    -0x1350(%rbp),%rdi
call   1050 <strlen@plt>
movb   $0x0,-0x1250(%rbp,%rax,1)
movl   $0x1,-0x18(%rbp)
jmp    1627 <main+0x3f7>
mov    0x29e0(%rip),%rax
mov    (%rax),%rax
mov    0x29e6(%rip),%rcx
cmp    (%rcx),%rax
jne    160c <main+0x3dc>
lea    0xa32(%rip),%rdi
call   10f0 <perror@plt>
jmp    1627 <main+0x3f7>
jmp    1611 <main+0x3e1>
jmp    1616 <main+0x3e6>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1534 <main+0x304>
jmp    162c <main+0x3fc>
jmp    1631 <main+0x401>
cmpl   $0x0,-0x18(%rbp)
je     1678 <main+0x448>
lea    -0x1250(%rbp),%rdi
lea    -0x940(%rbp),%rsi
call   1140 <execv@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1673 <main+0x443>
lea    0x9f0(%rip),%rdi
call   10f0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    16c5 <main+0x495>
jmp    1697 <main+0x467>
mov    -0x940(%rbp),%rsi
lea    0x9db(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16c5 <main+0x495>
jmp    16a5 <main+0x475>
lea    -0x10(%rbp),%rdi
call   1130 <wait@plt>
jmp    16aa <main+0x47a>
jmp    16af <main+0x47f>
mov    $0x1,%al
test   $0x1,%al
jne    1249 <main+0x19>
jmp    16be <main+0x48e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1390,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1318,%rsp
mov    $0x1,%r15b
lea    0xa10(%rsp),%rbp
lea    0xdc3(%rip),%r13
lea    0x210(%rsp),%r14
mov    %r15d,0x8(%rsp)
jmp    1270 <main+0x40>
mov    0x218(%rsp),%rdi
call   1060 <chdir@plt>
nop
xor    %edi,%edi
xor    %esi,%esi
call   1080 <getcwd@plt>
mov    %rax,%r15
test   %rax,%rax
je     13c7 <main+0x197>
lea    0xd78(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r12
test   %rax,%rax
je     12c6 <main+0x96>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <starts_with@plt>
test   %rax,%rax
je     12c6 <main+0x96>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdx
add    %rax,%rdx
mov    %r15,%rdi
lea    0xd48(%rip),%rsi
call   1110 <sprintf@plt>
lea    0xd40(%rip),%rdi
mov    %r15,%rsi
call   1070 <printf@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0x100,%esi
call   10a0 <fgets@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1090 <split@plt>
movq   $0x0,0x210(%rsp,%rax,8)
test   %rax,%rax
je     1270 <main+0x40>
mov    %rax,%rbx
mov    0x210(%rsp),%rdi
lea    0xcfd(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     1536 <main+0x306>
mov    0x210(%rsp),%rdi
lea    0xce5(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     13b0 <main+0x180>
mov    0x210(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <strncpy@plt>
mov    0x210(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   10c0 <basename@plt>
xor    %eax,%eax
call   1150 <fork@plt>
test   %rax,%rax
js     13d8 <main+0x1a8>
je     13f0 <main+0x1c0>
lea    0xc(%rsp),%rdi
call   1130 <wait@plt>
jmp    1270 <main+0x40>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x1,%rbx
ja     1262 <main+0x32>
mov    %r12,%rdi
call   1060 <chdir@plt>
jmp    1270 <main+0x40>
lea    0xc32(%rip),%rdi
call   10f0 <perror@plt>
jmp    1285 <main+0x55>
lea    0xc46(%rip),%rdi
call   10f0 <perror@plt>
jmp    1270 <main+0x40>
nopl   0x0(%rax)
mov    0x2be9(%rip),%r12
mov    (%r12),%esi
lea    0x10(%rsp),%rdi
call   10e0 <access@plt>
test   %eax,%eax
je     14bb <main+0x28b>
mov    0x2bb4(%rip),%rax
mov    (%rax),%rax
mov    0x2bba(%rip),%rcx
cmp    (%rcx),%rax
je     1572 <main+0x342>
lea    0xc1d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     14b4 <main+0x284>
lea    0xb10(%rsp),%rdi
mov    %rax,%rsi
lea    0xc02(%rip),%rdx
call   1090 <split@plt>
test   %rax,%rax
je     14b4 <main+0x284>
mov    %rax,%r12
xor    %ebx,%ebx
nopl   (%rax)
mov    0xb10(%rsp,%rbx,8),%rsi
mov    0x210(%rsp),%rdx
lea    0x110(%rsp),%r15
mov    %r15,%rdi
call   1120 <join@plt>
mov    0x2b59(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10e0 <access@plt>
test   %eax,%eax
je     14d8 <main+0x2a8>
mov    0x2b2c(%rip),%rax
mov    (%rax),%rax
mov    0x2b32(%rip),%rcx
cmp    (%rcx),%rax
je     1507 <main+0x2d7>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1460 <main+0x230>
testb  $0x1,0x8(%rsp)
jne    151a <main+0x2ea>
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
call   1140 <execv@plt>
xor    %r15d,%r15d
test   %eax,%eax
jns    125b <main+0x2b>
jmp    1564 <main+0x334>
lea    0x110(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
movb   $0x0,0x10(%rsp,%rax,1)
jmp    14bb <main+0x28b>
lea    0xb26(%rip),%rdi
call   10f0 <perror@plt>
testb  $0x1,0x8(%rsp)
je     14bb <main+0x28b>
mov    0x210(%rsp),%rsi
lea    0xb38(%rip),%rdi
call   1070 <printf@plt>
mov    $0xffffffff,%r15d
jmp    154f <main+0x31f>
xor    %r15d,%r15d
cmp    $0x2,%rbx
jb     154f <main+0x31f>
mov    0x218(%rsp),%rdi
call   1100 <atoi@plt>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x1318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xae7(%rip),%rdi
call   10f0 <perror@plt>
jmp    154f <main+0x31f>
lea    0xabb(%rip),%rdi
call   10f0 <perror@plt>
xor    %r15d,%r15d
jmp    154f <main+0x31f>

LLVM IR (-O0):
@.str = [4 x i8] c"jsh\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [4 x i8] c"~%s\00"
@.str.3 = [9 x i8] c"JSH:%s$ \00"
@stdin = global i32 0
@.str.4 = [7 x i8] c" \09\0A\0B\0C\0D\00"
@.str.5 = [5 x i8] c"exit\00"
@.str.6 = [3 x i8] c"cd\00"
@.str.7 = [15 x i8] c"Failed to fork\00"
@X_OK = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@.str.8 = [23 x i8] c"File is not executable\00"
@.str.9 = [5 x i8] c"PATH\00"
@.str.10 = [2 x i8] c":\00"
@.str.11 = [15 x i8] c"Failed to exec\00"
@.str.12 = [24 x i8] c"%s: command not found.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca [256 x i8]
%12 = alloca [256 x i8*]
%13 = alloca [256 x i8*]
%14 = alloca i8*
%15 = alloca [256 x i8]
%16 = alloca [256 x i8]
%17 = alloca i8*
%18 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %6
br label %19
19: ; preds = %191, %0
%20 = call i8* @getcwd(i32* null, i32 0)
store i8* %20, i8** %17
%21 = load i8*, i8** %17
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %26, i8** %18
%27 = load i8*, i8** %18
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %41
29: ; preds = %25
%30 = load i8*, i8** %17
%31 = load i8*, i8** %18
%32 = call i64 @starts_with(i8* %30, i8* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %29
%35 = load i8*, i8** %17
%36 = load i8*, i8** %17
%37 = load i8*, i8** %18
%38 = call i64 @strlen(i8* %37)
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %34, %29, %25
%42 = load i8*, i8** %17
%43 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%45 = load i32, i32* @stdin
%46 = call i32 @fgets(i8* %44, i32 256, i32 %45)
%47 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%49 = call i64 @split(i8** %47, i8* %48, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i64 %49, i64* %7
%50 = load i64, i64* %7
%51 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 %50
store i8* null, i8** %51
%52 = load i64, i64* %7
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %41
br label %191
55: ; preds = %41
%56 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%57 = load i8*, i8** %56
%58 = call i64 @strcmp(i8* %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %69
60: ; preds = %55
store i32 0, i32* %3
%61 = load i64, i64* %7
%62 = icmp ugt i64 %61, 1
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 1
%65 = load i8*, i8** %64
%66 = call i32 @atoi(i8* %65)
store i32 %66, i32* %3
br label %67
67: ; preds = %63, %60
%68 = load i32, i32* %3
store i32 %68, i32* %1
br label %193
69: ; preds = %55
%70 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%71 = load i8*, i8** %70
%72 = call i64 @strcmp(i8* %71, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %85
74: ; preds = %69
%75 = load i64, i64* %7
%76 = icmp ult i64 %75, 2
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = load i8*, i8** %18
%79 = call i32 @chdir(i8* %78)
br label %84
80: ; preds = %74
%81 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 1
%82 = load i8*, i8** %81
%83 = call i32 @chdir(i8* %82)
br label %84
84: ; preds = %80, %77
br label %191
85: ; preds = %69
%86 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%87 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%88 = load i8*, i8** %87
%89 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%90 = load i8*, i8** %89
%91 = call i64 @strlen(i8* %90)
%92 = call i32 @strncpy(i8* %86, i8* %88, i64 %91)
%93 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%94 = load i8*, i8** %93
%95 = call i64 @strlen(i8* %94)
%96 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 %95
store i8 0, i8* %96
%97 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%98 = load i8*, i8** %97
%99 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%100 = load i8*, i8** %99
%101 = call i32 @basename(i8* %98, i8* %100)
%102 = call i64 (...) @fork()
store i64 %102, i64* %10
%103 = load i64, i64* %10
%104 = icmp slt i64 %103, 0
br i1 %104, label %105, label %107
105: ; preds = %85
%106 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %190
107: ; preds = %85
%108 = load i64, i64* %10
%109 = icmp eq i64 %108, 0
br i1 %109, label %110, label %187
110: ; preds = %107
%111 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%112 = load i32, i32* @X_OK
%113 = call i32 @access(i8* %111, i32 %112)
store i32 %113, i32* %5
%114 = load i32, i32* %5
%115 = icmp eq i32 %114, 0
br i1 %115, label %116, label %117
116: ; preds = %110
store i32 1, i32* %6
br label %170
117: ; preds = %110
%118 = load i64, i64* @errno
%119 = load i64, i64* @EACCES
%120 = icmp eq i64 %118, %119
br i1 %120, label %121, label %123
121: ; preds = %117
%122 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %193
123: ; preds = %117
%124 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
store i8* %124, i8** %14
%125 = load i8*, i8** %14
%126 = icmp ne i8* %125, null
br i1 %126, label %127, label %169
127: ; preds = %123
%128 = getelementptr inbounds [256 x i8*], [256 x i8*]* %13, i64 0, i64 0
%129 = load i8*, i8** %14
%130 = call i64 @split(i8** %128, i8* %129, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
store i64 %130, i64* %9
store i64 0, i64* %8
br label %131
131: ; preds = %165, %127
%132 = load i64, i64* %8
%133 = load i64, i64* %9
%134 = icmp ult i64 %132, %133
br i1 %134, label %135, label %168
135: ; preds = %131
%136 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%137 = load i64, i64* %8
%138 = getelementptr inbounds [256 x i8*], [256 x i8*]* %13, i64 0, i64 %137
%139 = load i8*, i8** %138
%140 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%141 = load i8*, i8** %140
%142 = call i32 @join(i8* %136, i8* %139, i8* %141)
%143 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%144 = load i32, i32* @X_OK
%145 = call i32 @access(i8* %143, i32 %144)
store i32 %145, i32* %5
%146 = load i32, i32* %5
%147 = icmp eq i32 %146, 0
br i1 %147, label %148, label %157
148: ; preds = %135
%149 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%150 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%151 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%152 = call i64 @strlen(i8* %151)
%153 = call i32 @strncpy(i8* %149, i8* %150, i64 %152)
%154 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%155 = call i64 @strlen(i8* %154)
%156 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 %155
store i8 0, i8* %156
store i32 1, i32* %6
br label %168
157: ; preds = %135
%158 = load i64, i64* @errno
%159 = load i64, i64* @EACCES
%160 = icmp eq i64 %158, %159
br i1 %160, label %161, label %163
161: ; preds = %157
%162 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
br label %168
163: ; preds = %157
br label %164
164: ; preds = %163
br label %165
165: ; preds = %164
%166 = load i64, i64* %8
%167 = add i64 %166, 1
store i64 %167, i64* %8
br label %131
168: ; preds = %161, %148, %131
br label %169
169: ; preds = %168, %123
br label %170
170: ; preds = %169, %116
%171 = load i32, i32* %6
%172 = icmp ne i32 %171, 0
br i1 %172, label %173, label %182
173: ; preds = %170
%174 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%175 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%176 = call i32 @execv(i8* %174, i8** %175)
store i32 %176, i32* %2
%177 = load i32, i32* %2
%178 = icmp slt i32 %177, 0
br i1 %178, label %179, label %181
179: ; preds = %173
%180 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
store i32 0, i32* %1
br label %193
181: ; preds = %173
br label %186
182: ; preds = %170
%183 = getelementptr inbounds [256 x i8*], [256 x i8*]* %12, i64 0, i64 0
%184 = load i8*, i8** %183
%185 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0), i8* %184)
store i32 -1, i32* %1
br label %193
186: ; preds = %181
br label %189
187: ; preds = %107
%188 = call i32 @wait(i32* %4)
br label %189
189: ; preds = %187, %186
br label %190
190: ; preds = %189, %105
br label %191
191: ; preds = %190, %84, %54
br i1 true, label %19, label %192
192: ; preds = %191
store i32 0, i32* %1
br label %193
193: ; preds = %192, %182, %179, %121, %67
%194 = load i32, i32* %1
ret i32 %194
}

LLVM IR (-O2):
@.str = [4 x i8] c"jsh\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [4 x i8] c"~%s\00"
@.str.3 = [9 x i8] c"JSH:%s$ \00"
@stdin = global i32 0
@.str.4 = [7 x i8] c" \09\0A\0B\0C\0D\00"
@.str.5 = [5 x i8] c"exit\00"
@.str.6 = [3 x i8] c"cd\00"
@.str.7 = [15 x i8] c"Failed to fork\00"
@X_OK = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@.str.8 = [23 x i8] c"File is not executable\00"
@.str.9 = [5 x i8] c"PATH\00"
@.str.10 = [2 x i8] c":\00"
@.str.11 = [15 x i8] c"Failed to exec\00"
@.str.12 = [24 x i8] c"%s: command not found.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [256 x i8*]
%4 = alloca [256 x i8*]
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = bitcast i32* %1 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%9 = bitcast [256 x i8*]* %3 to i8*
%10 = bitcast [256 x i8*]* %4 to i8*
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [256 x i8*], [256 x i8*]* %3, i64 0, i64 0
%14 = getelementptr inbounds [256 x i8*], [256 x i8*]* %4, i64 0, i64 0
%15 = getelementptr inbounds [256 x i8*], [256 x i8*]* %3, i64 0, i64 1
br label %16
16: ; preds = %115, %0
%17 = phi i1 [ false, %115 ], [ true, %0 ]
br label %18
18: ; preds = %72, %16
%19 = call i8* @getcwd(i32* null, i32 0)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = call i64 @starts_with(i8* %19, i8* %24)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = call i64 @strlen(i8* %24)
%31 = getelementptr inbounds i8, i8* %19, i64 %30
%32 = call i32 @sprintf(i8* dereferenceable(1) %19, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %29, %26, %23
%34 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%35 = load i32, i32* @stdin
%36 = call i32 @fgets(i8* %8, i32 256, i32 %35)
%37 = call i64 @split(i8** %13, i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%38 = getelementptr inbounds [256 x i8*], [256 x i8*]* %3, i64 0, i64 %37
store i8* null, i8** %38
%39 = icmp eq i64 %37, 0
br i1 %39, label %72, label %40
40: ; preds = %33
%41 = load i8*, i8** %13
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = icmp ugt i64 %37, 1
br i1 %45, label %46, label %125
46: ; preds = %44
%47 = load i8*, i8** %15
%48 = call i32 @atoi(i8* %47)
br label %125
49: ; preds = %40
%50 = load i8*, i8** %13
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = icmp ult i64 %37, 2
br i1 %54, label %55, label %57
55: ; preds = %53
%56 = call i32 @chdir(i8* %24)
br label %72
57: ; preds = %53
%58 = load i8*, i8** %15
%59 = call i32 @chdir(i8* %58)
br label %72
60: ; preds = %49
%61 = load i8*, i8** %13
%62 = call i64 @strlen(i8* dereferenceable(1) %61)
%63 = call i32 @strncpy(i8* %11, i8* %61, i64 %62)
%64 = load i8*, i8** %13
%65 = call i64 @strlen(i8* dereferenceable(1) %64)
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %65
store i8 0, i8* %66
%67 = call i32 @basename(i8* %64, i8* %64)
%68 = call i64 (...) @fork()
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %60
%71 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %72
72: ; preds = %70, %123, %55, %57, %33
br label %18
73: ; preds = %60
%74 = icmp eq i64 %68, 0
br i1 %74, label %75, label %123
75: ; preds = %73
%76 = load i32, i32* @X_OK
%77 = call i32 @access(i8* %11, i32 %76)
%78 = icmp eq i32 %77, 0
br i1 %78, label %115, label %79
79: ; preds = %75
%80 = load i64, i64* @errno
%81 = load i64, i64* @EACCES
%82 = icmp eq i64 %80, %81
br i1 %82, label %83, label %85
83: ; preds = %79
%84 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
br label %125
85: ; preds = %79
%86 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%87 = icmp eq i8* %86, null
br i1 %87, label %114, label %88
88: ; preds = %85
%89 = call i64 @split(i8** %14, i8* %86, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%90 = icmp eq i64 %89, 0
br i1 %90, label %114, label %93
91: ; preds = %107
%92 = icmp eq i64 %111, %89
br i1 %92, label %114, label %93
93: ; preds = %88, %91
%94 = phi i64 [ %111, %91 ], [ 0, %88 ]
%95 = getelementptr inbounds [256 x i8*], [256 x i8*]* %4, i64 0, i64 %94
%96 = load i8*, i8** %95
%97 = load i8*, i8** %13
%98 = call i32 @join(i8* %12, i8* %96, i8* %97)
%99 = load i32, i32* @X_OK
%100 = call i32 @access(i8* %12, i32 %99)
%101 = icmp eq i32 %100, 0
br i1 %101, label %102, label %107
102: ; preds = %93
%103 = call i64 @strlen(i8* %12)
%104 = call i32 @strncpy(i8* %11, i8* %12, i64 %103)
%105 = call i64 @strlen(i8* %12)
%106 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %105
store i8 0, i8* %106
br label %115
107: ; preds = %93
%108 = load i64, i64* @errno
%109 = load i64, i64* @EACCES
%110 = icmp eq i64 %108, %109
%111 = add nuw i64 %94, 1
br i1 %110, label %112, label %91
112: ; preds = %107
%113 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
br label %114
114: ; preds = %91, %88, %85, %112
br i1 %17, label %120, label %115
115: ; preds = %75, %102, %114
%116 = call i32 @execv(i8* %11, i8** %13)
%117 = icmp slt i32 %116, 0
br i1 %117, label %118, label %16
118: ; preds = %115
%119 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
br label %125
120: ; preds = %114
%121 = load i8*, i8** %13
%122 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0), i8* %121)
br label %125
123: ; preds = %73
%124 = call i32 @wait(i32* %1)
br label %72
125: ; preds = %44, %46, %120, %118, %83
%126 = phi i32 [ 0, %118 ], [ -1, %120 ], [ 0, %83 ], [ %48, %46 ], [ 0, %44 ]
ret i32 %126
}