#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 */
#define ENOENT 128
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getenv(char *);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ readline(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(void) {
  char argstr[16][32];
  char *argv[16];
  char *token;
  char buffer[32];
  char delimiter[] = " |\n\t";
  int idx;
  int w_ptr;
  int i;
  pid_t PID;
  int fdw;
  int dir;
  fprintf(stdout, "\E[0;0H");
  fprintf(stdout, "\E[2J");
  fprintf(stdout, "\E[0;0H");
  fprintf(stdout, "\E[2J");
  while (1) {
    printf("# ");
    readline(buffer, 32);
    idx = 0;
    token = strtok(buffer, delimiter);
    while (token != NULL) {
      strcpy(&(argstr[idx][0]), token);
      argv[idx] = argstr[idx];
      idx++;
      fprintf(stdout, "token %d = %s\n", idx, token);
      token = strtok(NULL, delimiter);
    }
    for (i = 0; i < idx; i++) {
      if (strcmp(argv[i], ">") == 0) {
        w_ptr = i + 1;
        argv[i] = NULL;
        break;
      } else
        w_ptr = 0;
    }
    if (strcmp(argv[0], "logout") == 0 || strcmp(argv[0], "exit") == 0) {
      exit(0);
    } else if (strcmp(argv[0], "cd") == 0) {
      if (argv[1] == NULL) {
        dir = chdir(getenv("HOME"));
      } else {
        dir = chdir(argv[1]);
        switch (dir) {
        case -1:
          fprintf(stdout, "\"%s\" is no valid directory\n", argv[1]);
          break;
        case 0:
          break;
        case ENOENT:
          printf("dir not found\n");
          break;
        default:
          break;
        }
      }
    } else {
      if ((PID = fork()) == 0) {
        if (w_ptr != 0) {
          close(1);
          fdw = open(argv[w_ptr], O_CREAT | O_TRUNC | O_WRONLY, 0644);
          if (fdw < 0) {
            fprintf(stderr, "Could not write to file %s\n", argv[w_ptr]);
            exit(-1);
          }
        }
        strcpy(buffer, "/bin/");
        strcat(buffer, argv[0]);
        execv(buffer, &argv[0]);
        strcpy(buffer, "/usr/bin/");
        strcat(buffer, argv[0]);
        execv(buffer, &argv[0]);
        printf("!!! panic !!!\n");
      } else if (PID < 0) {
        printf("fork failed\n");
        exit(-1);
      } else
        wait(0);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2f0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xdf8(%rip),%eax
mov    %eax,-0x2c5(%rbp)
mov    0xdf0(%rip),%al
mov    %al,-0x2c1(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
lea    0xd94(%rip),%rdi
call   1060 <printf@plt>
lea    -0x2c0(%rbp),%rdi
mov    $0x20,%esi
call   1070 <readline@plt>
movl   $0x0,-0x2cc(%rbp)
lea    -0x2c0(%rbp),%rdi
lea    -0x2c5(%rbp),%rsi
call   10c0 <strtok@plt>
mov    %rax,-0x298(%rbp)
cmpq   $0x0,-0x298(%rbp)
je     1359 <main+0x169>
movslq -0x2cc(%rbp),%rax
lea    -0x210(%rbp),%rdi
shl    $0x5,%rax
add    %rax,%rdi
mov    -0x298(%rbp),%rsi
call   1040 <strcpy@plt>
movslq -0x2cc(%rbp),%rax
lea    -0x210(%rbp),%rcx
shl    $0x5,%rax
add    %rax,%rcx
movslq -0x2cc(%rbp),%rax
mov    %rcx,-0x290(%rbp,%rax,8)
ff
mov    -0x2cc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2cc(%rbp)
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
mov    -0x2cc(%rbp),%edx
mov    -0x298(%rbp),%rcx
lea    0xcde(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
lea    -0x2c5(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <strtok@plt>
mov    %rax,-0x298(%rbp)
jmp    12b7 <main+0xc7>
movl   $0x0,-0x2d4(%rbp)
mov    -0x2d4(%rbp),%eax
cmp    -0x2cc(%rbp),%eax
jge    13e4 <main+0x1f4>
movslq -0x2d4(%rbp),%rax
mov    -0x290(%rbp,%rax,8),%rdi
ff
lea    0xc98(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    13c1 <main+0x1d1>
mov    -0x2d4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2d0(%rbp)
movslq -0x2d4(%rbp),%rax
movq   $0x0,-0x290(%rbp,%rax,8)
jmp    13e4 <main+0x1f4>
movl   $0x0,-0x2d0(%rbp)
jmp    13d0 <main+0x1e0>
mov    -0x2d4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2d4(%rbp)
jmp    1363 <main+0x173>
mov    -0x290(%rbp),%rdi
lea    0xc33(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     141e <main+0x22e>
mov    -0x290(%rbp),%rdi
lea    0xc1d(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1425 <main+0x235>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0x290(%rbp),%rdi
lea    0xbfe(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    150c <main+0x31c>
cmpq   $0x0,-0x288(%rbp)
jne    146f <main+0x27f>
lea    0xbdd(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1050 <chdir@plt>
mov    %eax,-0x2e8(%rbp)
jmp    1507 <main+0x317>
mov    -0x288(%rbp),%rdi
call   1050 <chdir@plt>
mov    %eax,-0x2e8(%rbp)
mov    -0x2e8(%rbp),%eax
mov    %eax,-0x2ec(%rbp)
sub    $0xffffffff,%eax
je     14c4 <main+0x2d4>
jmp    149b <main+0x2ab>
mov    -0x2ec(%rbp),%eax
test   %eax,%eax
je     14e7 <main+0x2f7>
jmp    14ae <main+0x2be>
mov    -0x2ec(%rbp),%eax
sub    $0x80,%eax
je     14ec <main+0x2fc>
jmp    14fd <main+0x30d>
mov    0x2afd(%rip),%rax
mov    (%rax),%edi
mov    -0x288(%rbp),%rdx
lea    0xb5e(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    1502 <main+0x312>
jmp    1502 <main+0x312>
lea    0xb62(%rip),%rdi
call   1060 <printf@plt>
jmp    1502 <main+0x312>
jmp    1502 <main+0x312>
jmp    1507 <main+0x317>
jmp    166f <main+0x47f>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %rax,-0x2e0(%rbp)
cmp    $0x0,%rax
jne    163a <main+0x44a>
cmpl   $0x0,-0x2d0(%rbp)
je     15b7 <main+0x3c7>
mov    $0x1,%edi
call   1080 <close@plt>
movslq -0x2d0(%rbp),%rax
mov    -0x290(%rbp,%rax,8),%rdi
ff
mov    0x2aa7(%rip),%rax
mov    (%rax),%esi
mov    0x2a96(%rip),%rax
or     (%rax),%esi
mov    0x2a55(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
mov    %eax,-0x2e4(%rbp)
cmpl   $0x0,-0x2e4(%rbp)
jge    15b2 <main+0x3c2>
mov    0x2a47(%rip),%rax
mov    (%rax),%edi
movslq -0x2d0(%rbp),%rax
mov    -0x290(%rbp,%rax,8),%rdx
ff
lea    0xac3(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
jmp    15b7 <main+0x3c7>
lea    -0x2c0(%rbp),%rdi
lea    0xabf(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x2c0(%rbp),%rdi
mov    -0x290(%rbp),%rsi
call   10d0 <strcat@plt>
lea    -0x2c0(%rbp),%rdi
lea    -0x290(%rbp),%rsi
call   1100 <execv@plt>
lea    -0x2c0(%rbp),%rdi
lea    0xa82(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x2c0(%rbp),%rdi
mov    -0x290(%rbp),%rsi
call   10d0 <strcat@plt>
lea    -0x2c0(%rbp),%rdi
lea    -0x290(%rbp),%rsi
call   1100 <execv@plt>
lea    0xa5a(%rip),%rdi
call   1060 <printf@plt>
jmp    166a <main+0x47a>
cmpq   $0x0,-0x2e0(%rbp)
jge    165e <main+0x46e>
lea    0xa4a(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   10f0 <wait@plt>
jmp    166a <main+0x47a>
jmp    166f <main+0x47f>
jmp    1674 <main+0x484>
jmp    1276 <main+0x86>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2b8,%rsp
movb   $0x0,0xc(%rsp)
movl   $0x90a7c20,0x8(%rsp)
09
mov    0x2da3(%rip),%rbx
mov    (%rbx),%edi
lea    0xdd7(%rip),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdcb(%rip),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    (%rbx),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    (%rbx),%edi
mov    %rbp,%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
lea    0x8(%rsp),%r13
lea    0xda5(%rip),%r14
jmp    1290 <main+0x90>
lea    0xdbc(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1060 <chdir@plt>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xd7a(%rip),%rdi
call   1070 <printf@plt>
lea    0x90(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x20,%esi
call   1080 <readline@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   10d0 <strtok@plt>
test   %rax,%rax
je     138c <main+0x18c>
mov    %rax,%r15
mov    $0x1,%eax
lea    0xb0(%rsp),%rbp
mov    %r14,%rbx
mov    %r13,%r14
xor    %r13d,%r13d
xchg   %ax,%ax
mov    %eax,%r12d
mov    %rbp,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %rbp,0x10(%rsp,%r13,8)
add    $0x1,%r13
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r13d,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   10d0 <strtok@plt>
mov    %rax,%r15
add    $0x20,%rbp
lea    0x1(%r12),%eax
test   %r15,%r15
jne    12e0 <main+0xe0>
test   %r13d,%r13d
mov    %r14,%r13
mov    %rbx,%r14
lea    0xce8(%rip),%r15
mov    0x2c86(%rip),%rbx
je     138c <main+0x18c>
mov    %r12d,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    0x10(%rsp,%rbp,8),%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1375 <main+0x175>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1350 <main+0x150>
movl   $0x0,0x4(%rsp)
jmp    1385 <main+0x185>
lea    0x1(%rbp),%eax
mov    %eax,0x4(%rsp)
movq   $0x0,0x10(%rsp,%rbp,8)
mov    0x2c3c(%rip),%rbx
mov    0x10(%rsp),%rdi
lea    0xc8d(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     151b <main+0x31b>
mov    0x10(%rsp),%rdi
lea    0xc7a(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     151b <main+0x31b>
mov    0x10(%rsp),%rdi
lea    0xc65(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1400 <main+0x200>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
je     1450 <main+0x250>
js     1522 <main+0x322>
xor    %edi,%edi
call   1100 <wait@plt>
jmp    1290 <main+0x90>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x18(%rsp),%rdi
test   %rdi,%rdi
je     1271 <main+0x71>
call   1060 <chdir@plt>
cmp    $0x80,%eax
je     150a <main+0x30a>
cmp    $0xffffffff,%eax
jne    1290 <main+0x90>
mov    (%rbx),%edi
mov    0x18(%rsp),%rdx
lea    0xc04(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    1290 <main+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%ebx
test   %ebx,%ebx
je     1497 <main+0x297>
mov    $0x1,%edi
call   1090 <close@plt>
movslq %ebx,%rbx
mov    0x10(%rsp,%rbx,8),%rdi
mov    0x2b7f(%rip),%rax
mov    (%rax),%esi
mov    0x2b7e(%rip),%rax
or     (%rax),%esi
mov    0x2b35(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10c0 <open@plt>
test   %eax,%eax
js     1538 <main+0x338>
lea    0x90(%rsp),%rbx
mov    %rbx,%rdi
lea    0xbcc(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   10e0 <strcat@plt>
mov    %rbx,%rdi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
call   1110 <execv@plt>
mov    %rbx,%rdi
lea    0xb9c(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   10e0 <strcat@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1110 <execv@plt>
lea    0xb8e(%rip),%rdi
call   1050 <puts@plt>
mov    0x2ac3(%rip),%rbx
jmp    1290 <main+0x90>
lea    0xb84(%rip),%rdi
call   1050 <puts@plt>
jmp    1290 <main+0x90>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    0xb52(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>
mov    0x2a91(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp,%rbx,8),%rdx
lea    0xb08(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@__const.main.delimiter = [5 x i8] c" |\0A\09\00"
@stdout = global i32 0
@.str = [7 x i8] c"\1B[0;0H\00"
@.str.1 = [5 x i8] c"\1B[2J\00"
@.str.2 = [3 x i8] c"# \00"
@.str.3 = [15 x i8] c"token %d = %s\0A\00"
@.str.4 = [2 x i8] c">\00"
@.str.5 = [7 x i8] c"logout\00"
@.str.6 = [5 x i8] c"exit\00"
@.str.7 = [3 x i8] c"cd\00"
@.str.8 = [5 x i8] c"HOME\00"
@.str.9 = [28 x i8] c"\22%s\22 is no valid directory\0A\00"
@.str.10 = [15 x i8] c"dir not found\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.11 = [28 x i8] c"Could not write to file %s\0A\00"
@.str.12 = [6 x i8] c"/bin/\00"
@.str.13 = [10 x i8] c"/usr/bin/\00"
@.str.14 = [15 x i8] c"!!! panic !!!\0A\00"
@.str.15 = [13 x i8] c"fork failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [16 x [32 x i8]]
%3 = alloca [16 x i8*]
%4 = alloca i8*
%5 = alloca [32 x i8]
%6 = alloca [5 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %1
%13 = bitcast [5 x i8]* %6 to i8*
%14 = load i32, i32* @stdout
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @stdout
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @stdout
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @stdout
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %0, %177
%23 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%25 = call i32 @readline(i8* %24, i32 32)
store i32 0, i32* %7
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%27 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%28 = call i8* @strtok(i8* %26, i8* %27)
store i8* %28, i8** %4
br label %29
29: ; preds = %32, %22
%30 = load i8*, i8** %4
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %54
32: ; preds = %29
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [16 x [32 x i8]], [16 x [32 x i8]]* %2, i64 0, i64 %34
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %35, i64 0, i64 0
%37 = load i8*, i8** %4
%38 = call i32 @strcpy(i8* %36, i8* %37)
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [16 x [32 x i8]], [16 x [32 x i8]]* %2, i64 0, i64 %40
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %41, i64 0, i64 0
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 %44
store i8* %42, i8** %45
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
%48 = load i32, i32* @stdout
%49 = load i32, i32* %7
%50 = load i8*, i8** %4
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %49, i8* %50)
%52 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%53 = call i8* @strtok(i8* null, i8* %52)
store i8* %53, i8** %4
br label %29
54: ; preds = %29
store i32 0, i32* %9
br label %55
55: ; preds = %74, %54
%56 = load i32, i32* %9
%57 = load i32, i32* %7
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %77
59: ; preds = %55
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 %61
%63 = load i8*, i8** %62
%64 = call i64 @strcmp(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %59
%67 = load i32, i32* %9
%68 = add nsw i32 %67, 1
store i32 %68, i32* %8
%69 = load i32, i32* %9
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 %70
store i8* null, i8** %71
br label %77
72: ; preds = %59
store i32 0, i32* %8
br label %73
73: ; preds = %72
br label %74
74: ; preds = %73
%75 = load i32, i32* %9
%76 = add nsw i32 %75, 1
store i32 %76, i32* %9
br label %55
77: ; preds = %66, %55
%78 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%79 = load i8*, i8** %78
%80 = call i64 @strcmp(i8* %79, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%81 = icmp eq i64 %80, 0
br i1 %81, label %87, label %82
82: ; preds = %77
%83 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%84 = load i8*, i8** %83
%85 = call i64 @strcmp(i8* %84, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%86 = icmp eq i64 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %82, %77
%88 = call i32 @exit(i32 0)
unreachable
89: ; preds = %82
%90 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%91 = load i8*, i8** %90
%92 = call i64 @strcmp(i8* %91, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%93 = icmp eq i64 %92, 0
br i1 %93, label %94, label %117
94: ; preds = %89
%95 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 1
%96 = load i8*, i8** %95
%97 = icmp eq i8* %96, null
br i1 %97, label %98, label %101
98: ; preds = %94
%99 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%100 = call i32 @chdir(i8* %99)
store i32 %100, i32* %12
br label %116
101: ; preds = %94
%102 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 1
%103 = load i8*, i8** %102
%104 = call i32 @chdir(i8* %103)
store i32 %104, i32* %12
%105 = load i32, i32* %12
switch i32 %105, label %114 [
i32 -1, label %106
i32 0, label %111
i32 128, label %112
]
106: ; preds = %101
%107 = load i32, i32* @stdout
%108 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 1
%109 = load i8*, i8** %108
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8* %109)
br label %115
111: ; preds = %101
br label %115
112: ; preds = %101
%113 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
br label %115
114: ; preds = %101
br label %115
115: ; preds = %114, %112, %111, %106
br label %116
116: ; preds = %115, %98
br label %176
117: ; preds = %89
%118 = call i64 (...) @fork()
store i64 %118, i64* %10
%119 = icmp eq i64 %118, 0
br i1 %119, label %120, label %166
120: ; preds = %117
%121 = load i32, i32* %8
%122 = icmp ne i32 %121, 0
br i1 %122, label %123, label %146
123: ; preds = %120
%124 = call i32 @close(i32 1)
%125 = load i32, i32* %8
%126 = sext i32 %125 to i64
%127 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 %126
%128 = load i8*, i8** %127
%129 = load i32, i32* @O_CREAT
%130 = load i32, i32* @O_TRUNC
%131 = or i32 %129, %130
%132 = load i32, i32* @O_WRONLY
%133 = or i32 %131, %132
%134 = call i32 @open(i8* %128, i32 %133, i32 420)
store i32 %134, i32* %11
%135 = load i32, i32* %11
%136 = icmp slt i32 %135, 0
br i1 %136, label %137, label %145
137: ; preds = %123
%138 = load i32, i32* @stderr
%139 = load i32, i32* %8
%140 = sext i32 %139 to i64
%141 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 %140
%142 = load i8*, i8** %141
%143 = call i32 (i32, i8*, ...) @fprintf(i32 %138, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0), i8* %142)
%144 = call i32 @exit(i32 -1)
unreachable
145: ; preds = %123
br label %146
146: ; preds = %145, %120
%147 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%148 = call i32 @strcpy(i8* %147, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
%149 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%150 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%151 = load i8*, i8** %150
%152 = call i32 @strcat(i8* %149, i8* %151)
%153 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%154 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%155 = call i32 @execv(i8* %153, i8** %154)
%156 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%157 = call i32 @strcpy(i8* %156, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.13, i64 0, i64 0))
%158 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%159 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%160 = load i8*, i8** %159
%161 = call i32 @strcat(i8* %158, i8* %160)
%162 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%163 = getelementptr inbounds [16 x i8*], [16 x i8*]* %3, i64 0, i64 0
%164 = call i32 @execv(i8* %162, i8** %163)
%165 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.14, i64 0, i64 0))
br label %175
166: ; preds = %117
%167 = load i64, i64* %10
%168 = icmp slt i64 %167, 0
br i1 %168, label %169, label %172
169: ; preds = %166
%170 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.15, i64 0, i64 0))
%171 = call i32 @exit(i32 -1)
unreachable
172: ; preds = %166
%173 = call i32 @wait(i32 0)
br label %174
174: ; preds = %172
br label %175
175: ; preds = %174, %146
br label %176
176: ; preds = %175, %116
br label %177
177: ; preds = %176
br label %22
}

LLVM IR (-O2):
@__const.main.delimiter = [5 x i8] c" |\0A\09\00"
@stdout = global i32 0
@.str = [7 x i8] c"\1B[0;0H\00"
@.str.1 = [5 x i8] c"\1B[2J\00"
@.str.2 = [3 x i8] c"# \00"
@.str.3 = [15 x i8] c"token %d = %s\0A\00"
@.str.4 = [2 x i8] c">\00"
@.str.5 = [7 x i8] c"logout\00"
@.str.6 = [5 x i8] c"exit\00"
@.str.7 = [3 x i8] c"cd\00"
@.str.8 = [5 x i8] c"HOME\00"
@.str.9 = [28 x i8] c"\22%s\22 is no valid directory\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.11 = [28 x i8] c"Could not write to file %s\0A\00"
@.str.12 = [6 x i8] c"/bin/\00"
@.str.13 = [10 x i8] c"/usr/bin/\00"
@str = [12 x i8] c"fork failed\00"
@str.16 = [14 x i8] c"!!! panic !!!\00"
@str.17 = [14 x i8] c"dir not found\00"
define i32 @main() {
%1 = alloca [16 x [32 x i8]]
%2 = alloca [16 x i8*]
%3 = alloca [32 x i8]
%4 = alloca [5 x i8]
%5 = getelementptr inbounds [16 x [32 x i8]], [16 x [32 x i8]]* %1, i64 0, i64 0, i64 0
%6 = bitcast [16 x i8*]* %2 to i8*
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @stdout
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @stdout
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @stdout
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 0
%18 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 1
br label %19
19: ; preds = %83, %0
%20 = phi i32 [ undef, %0 ], [ %57, %83 ]
%21 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @readline(i8* %7, i32 32)
%23 = call i8* @strtok(i8* %7, i8* %8)
%24 = icmp eq i8* %23, null
br i1 %24, label %56, label %30
25: ; preds = %30
%26 = trunc i64 %36 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %56, label %28
28: ; preds = %25
%29 = and i64 %36, 4294967295
br label %42
30: ; preds = %19, %30
%31 = phi i64 [ %36, %30 ], [ 0, %19 ]
%32 = phi i8* [ %40, %30 ], [ %23, %19 ]
%33 = getelementptr inbounds [16 x [32 x i8]], [16 x [32 x i8]]* %1, i64 0, i64 %31, i64 0
%34 = call i32 @strcpy(i8* %33, i8* %32)
%35 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 %31
store i8* %33, i8** %35
%36 = add nuw i64 %31, 1
%37 = load i32, i32* @stdout
%38 = trunc i64 %36 to i32
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %38, i8* %32)
%40 = call i8* @strtok(i8* null, i8* %8)
%41 = icmp eq i8* %40, null
br i1 %41, label %25, label %30
42: ; preds = %28, %53
%43 = phi i64 [ 0, %28 ], [ %54, %53 ]
%44 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 %43
%45 = load i8*, i8** %44
%46 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = trunc i64 %43 to i32
%50 = and i64 %43, 4294967295
%51 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 %50
%52 = add nuw nsw i32 %49, 1
store i8* null, i8** %51
br label %56
53: ; preds = %42
%54 = add nuw nsw i64 %43, 1
%55 = icmp eq i64 %54, %29
br i1 %55, label %56, label %42
56: ; preds = %53, %19, %25, %48
%57 = phi i32 [ %52, %48 ], [ %20, %25 ], [ %20, %19 ], [ 0, %53 ]
%58 = load i8*, i8** %17
%59 = call i64 @strcmp(i8* %58, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%60 = icmp eq i64 %59, 0
br i1 %60, label %65, label %61
61: ; preds = %56
%62 = load i8*, i8** %17
%63 = call i64 @strcmp(i8* %62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %61, %56
%66 = call i32 @exit(i32 0)
unreachable
67: ; preds = %61
%68 = load i8*, i8** %17
%69 = call i64 @strcmp(i8* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %86
71: ; preds = %67
%72 = load i8*, i8** %18
%73 = icmp eq i8* %72, null
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%76 = call i32 @chdir(i8* %75)
br label %83
77: ; preds = %71
%78 = call i32 @chdir(i8* %72)
switch i32 %78, label %83 [
i32 -1, label %79
i32 128, label %84
]
79: ; preds = %77
%80 = load i32, i32* @stdout
%81 = load i8*, i8** %18
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8* %81)
br label %83
83: ; preds = %79, %84, %77, %74, %124, %109
br label %19
84: ; preds = %77
%85 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.17, i64 0, i64 0))
br label %83
86: ; preds = %67
%87 = call i64 (...) @fork()
%88 = icmp eq i64 %87, 0
br i1 %88, label %89, label %119
89: ; preds = %86
%90 = icmp eq i32 %57, 0
br i1 %90, label %109, label %91
91: ; preds = %89
%92 = call i32 @close(i32 1)
%93 = sext i32 %57 to i64
%94 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 %93
%95 = load i8*, i8** %94
%96 = load i32, i32* @O_CREAT
%97 = load i32, i32* @O_TRUNC
%98 = or i32 %97, %96
%99 = load i32, i32* @O_WRONLY
%100 = or i32 %98, %99
%101 = call i32 @open(i8* %95, i32 %100, i32 420)
%102 = icmp slt i32 %101, 0
br i1 %102, label %103, label %109
103: ; preds = %91
%104 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 %93
%105 = load i32, i32* @stderr
%106 = load i8*, i8** %104
%107 = call i32 (i32, i8*, ...) @fprintf(i32 %105, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0), i8* %106)
%108 = call i32 @exit(i32 -1)
unreachable
109: ; preds = %91, %89
%110 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
%111 = load i8*, i8** %17
%112 = call i32 @strcat(i8* %7, i8* %111)
%113 = call i32 @execv(i8* %7, i8** %17)
%114 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.13, i64 0, i64 0))
%115 = load i8*, i8** %17
%116 = call i32 @strcat(i8* %7, i8* %115)
%117 = call i32 @execv(i8* %7, i8** %17)
%118 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.16, i64 0, i64 0))
br label %83
119: ; preds = %86
%120 = icmp slt i64 %87, 0
br i1 %120, label %121, label %124
121: ; preds = %119
%122 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%123 = call i32 @exit(i32 -1)
unreachable
124: ; preds = %119
%125 = call i32 @wait(i32 0)
br label %83
}