int sh(int f, int interactive) {
  char *line, **newargv, *tmpstr;
  for (;;) {
    if (interactive)
      writestr(1, "$ ");
    if ((line = readline(f)) == 0)
      return 0;
    if ((newargv = tokenize(line, strlen(line))) == 0)
      writestr(2, "sh: Syntax error\n");
    else if (*newargv != 0) {
      if (!strcmp(newargv[0], "exit")) {
        if (newargv[1] == 0)
          return 0;
        else
          return atoi(newargv[1]);
      } else if (!strcmp(newargv[0], "cd")) {
        if (newargv[1] == 0) {
          if ((tmpstr = getenv("HOME")) == 0)
            chdir("/");
          else
            chdir(tmpstr);
        } else
          chdir(newargv[1]);
      } else {
        if (fork())
          wait(0);
        else {
          execvp(newargv[0], newargv);
          writestr(2, "sh: ");
          writestr(2, newargv[0]);
          writestr(2, ": Not found\n");
          _exit(255);
        }
      }
    }
    free(line);
    line = 0;
    free(newargv);
    newargv = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sh>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11fb <sh+0x2b>
mov    $0x1,%edi
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   10b0 <writestr@plt>
mov    -0x8(%rbp),%edi
mov    $0x0,%al
call   1080 <readline@plt>
cltq
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1221 <sh+0x51>
movl   $0x0,-0x4(%rbp)
jmp    13f2 <sh+0x222>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rsi
mov    $0x0,%al
call   10a0 <tokenize@plt>
cltq
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1268 <sh+0x98>
mov    $0x2,%edi
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   10b0 <writestr@plt>
jmp    13cb <sh+0x1fb>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     13c6 <sh+0x1f6>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd91(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    12c4 <sh+0xf4>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    12ad <sh+0xdd>
movl   $0x0,-0x4(%rbp)
jmp    13f2 <sh+0x222>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x0,%al
call   10d0 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    13f2 <sh+0x222>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xd48(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1344 <sh+0x174>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1330 <sh+0x160>
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1030 <getenv@plt>
cltq
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1320 <sh+0x150>
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1070 <chdir@plt>
jmp    132b <sh+0x15b>
mov    -0x28(%rbp),%rdi
mov    $0x0,%al
call   1070 <chdir@plt>
jmp    133f <sh+0x16f>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x0,%al
call   1070 <chdir@plt>
jmp    13bc <sh+0x1ec>
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%eax
je     1362 <sh+0x192>
xor    %edi,%edi
mov    $0x0,%al
call   10e0 <wait@plt>
jmp    13b7 <sh+0x1e7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x0,%al
call   10c0 <execvp@plt>
mov    $0x2,%edi
lea    0xca4(%rip),%rsi
mov    $0x0,%al
call   10b0 <writestr@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    $0x2,%edi
mov    $0x0,%al
call   10b0 <writestr@plt>
mov    $0x2,%edi
lea    0xc83(%rip),%rsi
mov    $0x0,%al
call   10b0 <writestr@plt>
mov    $0xff,%edi
call   1050 <_exit@plt>
jmp    13bc <sh+0x1ec>
jmp    13c1 <sh+0x1f1>
jmp    13c6 <sh+0x1f6>
jmp    13cb <sh+0x1fb>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x20(%rbp)
jmp    11de <sh+0xe>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sh>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xe1a(%rip),%r13
jmp    1213 <sh+0x43>
nopl   0x0(%rax,%rax,1)
mov    $0x2,%edi
lea    0xe07(%rip),%rsi
xor    %eax,%eax
call   10b0 <writestr@plt>
mov    %r12,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1040 <free@plt>
test   %ebx,%ebx
je     1226 <sh+0x56>
mov    $0x1,%edi
mov    %r13,%rsi
xor    %eax,%eax
call   10b0 <writestr@plt>
mov    %ebp,%edi
xor    %eax,%eax
call   1080 <readline@plt>
test   %eax,%eax
je     12ef <sh+0x11f>
movslq %eax,%r12
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %r12,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   10a0 <tokenize@plt>
movslq %eax,%r14
test   %r14d,%r14d
je     11f0 <sh+0x20>
mov    (%r14),%r15
test   %r15,%r15
je     1203 <sh+0x33>
mov    %r15,%rdi
lea    0xdac(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     12d1 <sh+0x101>
mov    %r15,%rdi
lea    0xd9e(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     129e <sh+0xce>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1300 <sh+0x130>
xor    %edi,%edi
xor    %eax,%eax
call   10e0 <wait@plt>
jmp    1203 <sh+0x33>
mov    0x8(%r14),%rdi
test   %rdi,%rdi
jne    12c5 <sh+0xf5>
lea    0xd6f(%rip),%rdi
xor    %eax,%eax
call   1030 <getenv@plt>
test   %eax,%eax
je     12be <sh+0xee>
movslq %eax,%rdi
jmp    12c5 <sh+0xf5>
lea    0xd5d(%rip),%rdi
xor    %eax,%eax
call   1070 <chdir@plt>
jmp    1203 <sh+0x33>
mov    0x8(%r14),%rdi
test   %rdi,%rdi
je     12ef <sh+0x11f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10d0 <atoi@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10c0 <execvp@plt>
lea    0xd10(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   10b0 <writestr@plt>
mov    (%r14),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   10b0 <writestr@plt>
lea    0xcf3(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   10b0 <writestr@plt>
mov    $0xff,%edi
call   1050 <_exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"$ \00"
@.str.1 = [18 x i8] c"sh: Syntax error\0A\00"
@.str.2 = [5 x i8] c"exit\00"
@.str.3 = [3 x i8] c"cd\00"
@.str.4 = [5 x i8] c"HOME\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [5 x i8] c"sh: \00"
@.str.7 = [13 x i8] c": Not found\0A\00"
define i32 @sh(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %9
9: ; preds = %101, %2
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i32, i32* %4
%16 = call i32 (i32, ...) bitcast (i32 (...)* @readline to i32 (i32, ...)*)(i32 %15)
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 0, i32* %3
br label %105
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = load i8*, i8** %6
%24 = call i64 @strlen(i8* %23)
%25 = call i32 (i8*, i64, ...) bitcast (i32 (...)* @tokenize to i32 (i8*, i64, ...)*)(i8* %22, i64 %24)
%26 = sext i32 %25 to i64
%27 = inttoptr i64 %26 to i8**
store i8** %27, i8*** %7
%28 = icmp eq i8** %27, null
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %101
31: ; preds = %21
%32 = load i8**, i8*** %7
%33 = load i8*, i8** %32
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %100
35: ; preds = %31
%36 = load i8**, i8*** %7
%37 = getelementptr inbounds i8*, i8** %36, i64 0
%38 = load i8*, i8** %37
%39 = call i32 @strcmp(i8* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp ne i32 %39, 0
br i1 %40, label %52, label %41
41: ; preds = %35
%42 = load i8**, i8*** %7
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 0, i32* %3
br label %105
47: ; preds = %41
%48 = load i8**, i8*** %7
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 (i8*, ...) bitcast (i32 (...)* @atoi to i32 (i8*, ...)*)(i8* %50)
store i32 %51, i32* %3
br label %105
52: ; preds = %35
%53 = load i8**, i8*** %7
%54 = getelementptr inbounds i8*, i8** %53, i64 0
%55 = load i8*, i8** %54
%56 = call i32 @strcmp(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%57 = icmp ne i32 %56, 0
br i1 %57, label %80, label %58
58: ; preds = %52
%59 = load i8**, i8*** %7
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = load i8*, i8** %60
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %74
63: ; preds = %58
%64 = call i32 (i8*, ...) bitcast (i32 (...)* @getenv to i32 (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%65 = sext i32 %64 to i64
%66 = inttoptr i64 %65 to i8*
store i8* %66, i8** %8
%67 = icmp eq i8* %66, null
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %73
70: ; preds = %63
%71 = load i8*, i8** %8
%72 = call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %71)
br label %73
73: ; preds = %70, %68
br label %79
74: ; preds = %58
%75 = load i8**, i8*** %7
%76 = getelementptr inbounds i8*, i8** %75, i64 1
%77 = load i8*, i8** %76
%78 = call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %77)
br label %79
79: ; preds = %74, %73
br label %98
80: ; preds = %52
%81 = call i32 (...) @fork()
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 (i32, ...) bitcast (i32 (...)* @wait to i32 (i32, ...)*)(i32 0)
br label %97
85: ; preds = %80
%86 = load i8**, i8*** %7
%87 = getelementptr inbounds i8*, i8** %86, i64 0
%88 = load i8*, i8** %87
%89 = load i8**, i8*** %7
%90 = call i32 (i8*, i8**, ...) bitcast (i32 (...)* @execvp to i32 (i8*, i8**, ...)*)(i8* %88, i8** %89)
%91 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%92 = load i8**, i8*** %7
%93 = getelementptr inbounds i8*, i8** %92, i64 0
%94 = load i8*, i8** %93
%95 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 2, i8* %94)
%96 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
unreachable
97: ; preds = %83
br label %98
98: ; preds = %97, %79
br label %99
99: ; preds = %98
br label %100
100: ; preds = %99, %31
br label %101
101: ; preds = %100, %29
%102 = load i8*, i8** %6
store i8* null, i8** %6
%103 = load i8**, i8*** %7
%104 = bitcast i8** %103 to i8*
store i8** null, i8*** %7
br label %9
105: ; preds = %47, %46, %20
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
@.str = [3 x i8] c"$ \00"
@.str.1 = [18 x i8] c"sh: Syntax error\0A\00"
@.str.2 = [5 x i8] c"exit\00"
@.str.3 = [3 x i8] c"cd\00"
@.str.4 = [5 x i8] c"HOME\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [5 x i8] c"sh: \00"
@.str.7 = [13 x i8] c": Not found\0A\00"
define i32 @sh(i32 %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br label %4
4: ; preds = %64, %2
br i1 %3, label %7, label %5
5: ; preds = %4
%6 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %4
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @readline to i32 (i32, ...)*)(i32 %0)
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i32 %8, 0
br i1 %11, label %66, label %12
12: ; preds = %7
%13 = tail call i64 @strlen(i8* %10)
%14 = tail call i32 (i8*, i64, ...) bitcast (i32 (...)* @tokenize to i32 (i8*, i64, ...)*)(i8* %10, i64 %13)
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i8**
%17 = icmp eq i32 %14, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %64
20: ; preds = %12
%21 = load i8*, i8** %16
%22 = icmp eq i8* %21, null
br i1 %22, label %64, label %23
23: ; preds = %20
%24 = tail call i32 @strcmp(i8* %21, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = inttoptr i64 %15 to i8**
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %66, label %31
31: ; preds = %26
%32 = tail call i32 (i8*, ...) bitcast (i32 (...)* @atoi to i32 (i8*, ...)*)(i8* %29)
br label %66
33: ; preds = %23
%34 = tail call i32 @strcmp(i8* %21, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %33
%37 = getelementptr inbounds i8*, i8** %16, i64 1
%38 = load i8*, i8** %37
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %49
40: ; preds = %36
%41 = tail call i32 (i8*, ...) bitcast (i32 (...)* @getenv to i32 (i8*, ...)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = tail call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %64
45: ; preds = %40
%46 = sext i32 %41 to i64
%47 = inttoptr i64 %46 to i8*
%48 = tail call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %47)
br label %64
49: ; preds = %36
%50 = tail call i32 (i8*, ...) bitcast (i32 (...)* @chdir to i32 (i8*, ...)*)(i8* %38)
br label %64
51: ; preds = %33
%52 = tail call i32 (...) @fork()
%53 = icmp eq i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %51
%55 = tail call i32 (i32, ...) bitcast (i32 (...)* @wait to i32 (i32, ...)*)(i32 0)
br label %64
56: ; preds = %51
%57 = inttoptr i64 %15 to i8**
%58 = load i8*, i8** %57
%59 = tail call i32 (i8*, i8**, ...) bitcast (i32 (...)* @execvp to i32 (i8*, i8**, ...)*)(i8* %58, i8** %57)
%60 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%61 = load i8*, i8** %57
%62 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 2, i8* %61)
%63 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @writestr to i32 (i32, i8*, ...)*)(i32 2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
tail call void @_exit(i32 255)
unreachable
64: ; preds = %20, %54, %43, %45, %49, %18
tail call void @free(i8* %10)
%65 = inttoptr i64 %15 to i8*
tail call void @free(i8* %65)
br label %4
66: ; preds = %7, %26, %31
%67 = phi i32 [ %32, %31 ], [ 0, %26 ], [ 0, %7 ]
ret i32 %67
}