#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
int parseline(char *, char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ process_input(int, char **);
int /*<<< orphan*/ stdin;
scalar_t__ strncmp(char *, char *, int);
int wait(int *);
int main(void) {
  char cmdline[80];
  char *argv[20];
  int argc;
  int status;
  pid_t pid;
  while (1) {
    printf("CSC60mshell> ");
    fgets(cmdline, 80, stdin);
    argc = parseline(cmdline, argv);
    printf("%i\n", argc);
    int i;
    for (i = 0; i < argc; i++) {
      printf("Argv %i = %s \n", i, argv[i]);
    }
    if (argc == 0)
      continue;
    else {
      if (strncmp(argv[0], "exit", 30) == 0)
        exit(EXIT_SUCCESS);
      else if (strncmp(argv[0], "pwd", 30) == 0) {
        char cwd[50];
        getcwd(cwd, 50);
        printf("%s\n", cwd);
        continue;
      } else if (strncmp(argv[0], "cd", 30) == 0) {
        char *dir;
        if (argc == 1) {
          dir = getenv("HOME");
        } else
          dir = argv[1];
        if (chdir(dir) != 0) {
          perror("Error changing directory");
        }
        continue;
      } else {
        pid = fork();
        switch (pid) {
        case -1:
          perror("Shell Program fork error");
          exit(EXIT_FAILURE);
        case 0:
          process_input(argc, argv);
          break;
        default:
          if (wait(&status) == -1)
            perror("Parent Process error");
          else
            printf("Child returned status: %d\n", status);
          break;
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1090 <fgets@plt>
lea    -0x60(%rbp),%rdi
lea    -0x100(%rbp),%rsi
call   1070 <parseline@plt>
mov    %eax,-0x104(%rbp)
mov    -0x104(%rbp),%esi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x110(%rbp)
mov    -0x110(%rbp),%eax
cmp    -0x104(%rbp),%eax
jge    1274 <main+0xb4>
mov    -0x110(%rbp),%esi
movslq -0x110(%rbp),%rax
mov    -0x100(%rbp,%rax,8),%rdx
ff
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x110(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x110(%rbp)
jmp    122b <main+0x6b>
cmpl   $0x0,-0x104(%rbp)
jne    1286 <main+0xc6>
jmp    11d2 <main+0x12>
mov    -0x100(%rbp),%rdi
lea    0xd8d(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12b6 <main+0xf6>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x100(%rbp),%rdi
lea    0xd62(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1303 <main+0x143>
lea    -0x150(%rbp),%rdi
mov    $0x32,%esi
call   1080 <getcwd@plt>
lea    -0x150(%rbp),%rsi
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11d2 <main+0x12>
mov    -0x100(%rbp),%rdi
lea    0xd1d(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    137f <main+0x1bf>
cmpl   $0x1,-0x104(%rbp)
jne    134a <main+0x18a>
lea    0xcf8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x158(%rbp)
jmp    1358 <main+0x198>
mov    -0xf8(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    -0x158(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     137a <main+0x1ba>
lea    0xcc1(%rip),%rdi
call   10a0 <perror@plt>
jmp    11d2 <main+0x12>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,-0x10c(%rbp)
mov    -0x10c(%rbp),%eax
mov    %eax,-0x15c(%rbp)
sub    $0xffffffff,%eax
je     13b9 <main+0x1f9>
jmp    13a6 <main+0x1e6>
mov    -0x15c(%rbp),%eax
test   %eax,%eax
je     13d3 <main+0x213>
jmp    13ea <main+0x22a>
lea    0xc8f(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x104(%rbp),%edi
lea    -0x100(%rbp),%rsi
call   10e0 <process_input@plt>
jmp    1429 <main+0x269>
lea    -0x108(%rbp),%rdi
call   10c0 <wait@plt>
cmp    $0xffffffff,%eax
jne    1410 <main+0x250>
lea    0xc62(%rip),%rdi
call   10a0 <perror@plt>
jmp    1424 <main+0x264>
mov    -0x108(%rbp),%esi
lea    0xc60(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1429 <main+0x269>
jmp    142e <main+0x26e>
jmp    1433 <main+0x273>
jmp    1438 <main+0x278>
jmp    143d <main+0x27d>
jmp    11d2 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
lea    0xf0(%rsp),%r13
lea    0x10(%rsp),%r15
lea    0xe1d(%rip),%rbx
jmp    121d <main+0x4d>
nopw   0x0(%rax,%rax,1)
lea    0xb0(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x32,%esi
call   1090 <getcwd@plt>
mov    %rbp,%rdi
call   1050 <puts@plt>
lea    0xddc(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    $0x50,%esi
call   10a0 <fgets@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1080 <parseline@plt>
mov    %eax,%ebp
lea    0xdb9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebp,%ebp
jle    128b <main+0xbb>
mov    %ebp,%r12d
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp,%r14,8),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%r14
cmp    %r14,%r12
jne    1270 <main+0xa0>
test   %ebp,%ebp
je     121d <main+0x4d>
mov    0x10(%rsp),%rdi
lea    0xd86(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     137c <main+0x1ac>
mov    0x10(%rsp),%rdi
lea    0xd6c(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1200 <main+0x30>
mov    0x10(%rsp),%rdi
lea    0xd51(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1322 <main+0x152>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1338 <main+0x168>
cmp    $0xffffffff,%eax
je     138a <main+0x1ba>
lea    0xc(%rsp),%rdi
call   10d0 <wait@plt>
cmp    $0xffffffff,%eax
je     136b <main+0x19b>
mov    0xc(%rsp),%esi
lea    0xd63(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    121d <main+0x4d>
cmp    $0x1,%ebp
jne    1347 <main+0x177>
lea    0xcff(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
jmp    134c <main+0x17c>
mov    %ebp,%edi
mov    %r15,%rsi
call   10f0 <process_input@plt>
jmp    121d <main+0x4d>
mov    0x18(%rsp),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
je     121d <main+0x4d>
lea    0xcd1(%rip),%rdi
call   10b0 <perror@plt>
jmp    121d <main+0x4d>
lea    0xcf2(%rip),%rdi
call   10b0 <perror@plt>
jmp    121d <main+0x4d>
mov    0x2c4d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xcba(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c3b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"CSC60mshell> \00"
@stdin = global i32 0
@.str.1 = [4 x i8] c"%i\0A\00"
@.str.2 = [15 x i8] c"Argv %i = %s \0A\00"
@.str.3 = [5 x i8] c"exit\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [4 x i8] c"pwd\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [3 x i8] c"cd\00"
@.str.7 = [5 x i8] c"HOME\00"
@.str.8 = [25 x i8] c"Error changing directory\00"
@.str.9 = [25 x i8] c"Shell Program fork error\00"
@EXIT_FAILURE = global i32 0
@.str.10 = [21 x i8] c"Parent Process error\00"
@.str.11 = [27 x i8] c"Child returned status: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [80 x i8]
%3 = alloca [20 x i8*]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [50 x i8]
%9 = alloca i8*
store i32 0, i32* %1
br label %10
10: ; preds = %0, %37, %51, %75, %100
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %12, i32 80, i32 %13)
%15 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%16 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%17 = call i32 @parseline(i8* %15, i8** %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 0, i32* %7
br label %20
20: ; preds = %31, %10
%21 = load i32, i32* %7
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %34
24: ; preds = %20
%25 = load i32, i32* %7
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 %27
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %25, i8* %29)
br label %31
31: ; preds = %24
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %20
34: ; preds = %20
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
br label %10
38: ; preds = %34
%39 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%40 = load i8*, i8** %39
%41 = call i64 @strncmp(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 30)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %38
%47 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%48 = load i8*, i8** %47
%49 = call i64 @strncmp(i8* %48, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 30)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %46
%52 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%53 = call i32 @getcwd(i8* %52, i32 50)
%54 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %54)
br label %10
56: ; preds = %46
%57 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%58 = load i8*, i8** %57
%59 = call i64 @strncmp(i8* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 30)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %76
61: ; preds = %56
%62 = load i32, i32* %4
%63 = icmp eq i32 %62, 1
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
store i8* %65, i8** %9
br label %69
66: ; preds = %61
%67 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 1
%68 = load i8*, i8** %67
store i8* %68, i8** %9
br label %69
69: ; preds = %66, %64
%70 = load i8*, i8** %9
%71 = call i64 @chdir(i8* %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %69
%74 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %75
75: ; preds = %73, %69
br label %10
76: ; preds = %56
%77 = call i32 (...) @fork()
store i32 %77, i32* %6
%78 = load i32, i32* %6
switch i32 %78, label %87 [
i32 -1, label %79
i32 0, label %83
]
79: ; preds = %76
%80 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
%81 = load i32, i32* @EXIT_FAILURE
%82 = call i32 @exit(i32 %81)
unreachable
83: ; preds = %76
%84 = load i32, i32* %4
%85 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%86 = call i32 @process_input(i32 %84, i8** %85)
br label %96
87: ; preds = %76
%88 = call i32 @wait(i32* %5)
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %92
90: ; preds = %87
%91 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
br label %95
92: ; preds = %87
%93 = load i32, i32* %5
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0), i32 %93)
br label %95
95: ; preds = %92, %90
br label %96
96: ; preds = %95, %83
br label %97
97: ; preds = %96
br label %98
98: ; preds = %97
br label %99
99: ; preds = %98
br label %100
100: ; preds = %99
br label %10
}

LLVM IR (-O2):
@.str = [14 x i8] c"CSC60mshell> \00"
@stdin = global i32 0
@.str.1 = [4 x i8] c"%i\0A\00"
@.str.2 = [15 x i8] c"Argv %i = %s \0A\00"
@.str.3 = [5 x i8] c"exit\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [4 x i8] c"pwd\00"
@.str.6 = [3 x i8] c"cd\00"
@.str.7 = [5 x i8] c"HOME\00"
@.str.8 = [25 x i8] c"Error changing directory\00"
@.str.9 = [25 x i8] c"Shell Program fork error\00"
@EXIT_FAILURE = global i32 0
@.str.10 = [21 x i8] c"Parent Process error\00"
@.str.11 = [27 x i8] c"Child returned status: %d\0A\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = alloca [20 x i8*]
%3 = alloca i32
%4 = alloca [50 x i8]
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%6 = bitcast [20 x i8*]* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 0
%9 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 1
%10 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
br label %11
11: ; preds = %68, %0
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %5, i32 80, i32 %13)
%15 = call i32 @parseline(i8* %5, i8** %8)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp sgt i32 %15, 0
br i1 %17, label %18, label %28
18: ; preds = %11
%19 = zext i32 %15 to i64
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %26, %20 ]
%22 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %21
%23 = load i8*, i8** %22
%24 = trunc i64 %21 to i32
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %24, i8* %23)
%26 = add nuw nsw i64 %21, 1
%27 = icmp eq i64 %26, %19
br i1 %27, label %28, label %20
28: ; preds = %20, %11
%29 = icmp eq i32 %15, 0
br i1 %29, label %68, label %30
30: ; preds = %28
%31 = load i8*, i8** %8
%32 = call i64 @strncmp(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 30)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @EXIT_SUCCESS
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %30
%38 = load i8*, i8** %8
%39 = call i64 @strncmp(i8* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 30)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = call i32 @getcwd(i8* %10, i32 50)
%43 = call i32 @puts(i8* %10)
br label %68
44: ; preds = %37
%45 = load i8*, i8** %8
%46 = call i64 @strncmp(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 30)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %60
48: ; preds = %44
%49 = icmp eq i32 %15, 1
br i1 %49, label %50, label %52
50: ; preds = %48
%51 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %54
52: ; preds = %48
%53 = load i8*, i8** %9
br label %54
54: ; preds = %52, %50
%55 = phi i8* [ %51, %50 ], [ %53, %52 ]
%56 = call i64 @chdir(i8* %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %68, label %58
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %68
60: ; preds = %44
%61 = call i32 (...) @fork()
switch i32 %61, label %69 [
i32 -1, label %62
i32 0, label %66
]
62: ; preds = %60
%63 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %60
%67 = call i32 @process_input(i32 %15, i8** %8)
br label %68
68: ; preds = %66, %74, %72, %54, %58, %28, %41
br label %11
69: ; preds = %60
%70 = call i32 @wait(i32* %3)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %74
72: ; preds = %69
%73 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
br label %68
74: ; preds = %69
%75 = load i32, i32* %3
%76 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0), i32 %75)
br label %68
}