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

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
int parseline(char *, char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
scalar_t__ strncmp(char *, char *, 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
        printf("%s: Command not found.\n", cmdline);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e2d(%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    0xe24(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x110(%rbp)
mov    -0x110(%rbp),%eax
cmp    -0x104(%rbp),%eax
jge    1244 <main+0xb4>
mov    -0x110(%rbp),%esi
movslq -0x110(%rbp),%rax
mov    -0x100(%rbp,%rax,8),%rdx
ff
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x110(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x110(%rbp)
jmp    11fb <main+0x6b>
cmpl   $0x0,-0x104(%rbp)
jne    1256 <main+0xc6>
jmp    11a2 <main+0x12>
mov    -0x100(%rbp),%rdi
lea    0xdbd(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1286 <main+0xf6>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x100(%rbp),%rdi
lea    0xd92(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12d3 <main+0x143>
lea    -0x150(%rbp),%rdi
mov    $0x32,%esi
call   1080 <getcwd@plt>
lea    -0x150(%rbp),%rsi
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11a2 <main+0x12>
mov    -0x100(%rbp),%rdi
lea    0xd4d(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    134f <main+0x1bf>
cmpl   $0x1,-0x104(%rbp)
jne    131a <main+0x18a>
lea    0xd28(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x158(%rbp)
jmp    1328 <main+0x198>
mov    -0xf8(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    -0x158(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     134a <main+0x1ba>
lea    0xcf1(%rip),%rdi
call   10a0 <perror@plt>
jmp    11a2 <main+0x12>
lea    -0x60(%rbp),%rsi
lea    0xcf5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1366 <main+0x1d6>
jmp    136b <main+0x1db>
jmp    1370 <main+0x1e0>
jmp    11a2 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
lea    0xe0(%rsp),%r13
lea    0xe4e(%rip),%r12
lea    0xe4b(%rip),%rbx
jmp    11ed <main+0x4d>
nopl   0x0(%rax)
lea    0xa0(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x32,%esi
call   1090 <getcwd@plt>
mov    %rbp,%rdi
call   1050 <puts@plt>
lea    0xe0c(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    $0x50,%esi
call   10a0 <fgets@plt>
mov    %r13,%rdi
mov    %rsp,%rsi
call   1080 <parseline@plt>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebp,%ebp
jle    125a <main+0xba>
mov    %ebp,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rsp,%r14,8),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%r14
cmp    %r14,%r15
jne    1240 <main+0xa0>
test   %ebp,%ebp
je     11ed <main+0x4d>
mov    (%rsp),%rdi
lea    0xdb8(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1304 <main+0x164>
mov    (%rsp),%rdi
lea    0xd9f(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11d0 <main+0x30>
mov    (%rsp),%rdi
lea    0xd85(%rip),%rsi
mov    $0x1e,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     12ca <main+0x12a>
lea    0xd90(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    11ed <main+0x4d>
cmp    $0x1,%ebp
jne    12e0 <main+0x140>
lea    0xd57(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
jmp    12e5 <main+0x145>
mov    0x8(%rsp),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
je     11ed <main+0x4d>
lea    0xd38(%rip),%rdi
call   10b0 <perror@plt>
jmp    11ed <main+0x4d>
mov    0x2ccd(%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 = [24 x i8] c"%s: Command not found.\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, %82
%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 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0), i8* %77)
br label %79
79: ; preds = %76
br label %80
80: ; preds = %79
br label %81
81: ; preds = %80
br label %82
82: ; preds = %81
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 = [24 x i8] c"%s: Command not found.\0A\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = alloca [20 x i8*]
%3 = alloca [50 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%5 = bitcast [20 x i8*]* %2 to i8*
%6 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 0
%7 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 1
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
br label %9
9: ; preds = %56, %0
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @stdin
%12 = call i32 @fgets(i8* %4, i32 80, i32 %11)
%13 = call i32 @parseline(i8* %4, i8** %6)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp sgt i32 %13, 0
br i1 %15, label %16, label %26
16: ; preds = %9
%17 = zext i32 %13 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ 0, %16 ], [ %24, %18 ]
%20 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %19
%21 = load i8*, i8** %20
%22 = trunc i64 %19 to i32
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %22, i8* %21)
%24 = add nuw nsw i64 %19, 1
%25 = icmp eq i64 %24, %17
br i1 %25, label %26, label %18
26: ; preds = %18, %9
%27 = icmp eq i32 %13, 0
br i1 %27, label %56, label %28
28: ; preds = %26
%29 = load i8*, i8** %6
%30 = call i64 @strncmp(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 30)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %28
%36 = load i8*, i8** %6
%37 = call i64 @strncmp(i8* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 30)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @getcwd(i8* %8, i32 50)
%41 = call i32 @puts(i8* %8)
br label %56
42: ; preds = %35
%43 = load i8*, i8** %6
%44 = call i64 @strncmp(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 30)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %59
46: ; preds = %42
%47 = icmp eq i32 %13, 1
br i1 %47, label %48, label %50
48: ; preds = %46
%49 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %52
50: ; preds = %46
%51 = load i8*, i8** %7
br label %52
52: ; preds = %50, %48
%53 = phi i8* [ %49, %48 ], [ %51, %50 ]
%54 = call i64 @chdir(i8* %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %52, %57, %26, %59, %39
br label %9
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %56
59: ; preds = %42
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0), i8* %4)
br label %56
}