#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;
typedef int /*<<< orphan*/ input;

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strsep(char **, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  char *input;
  char *input_copy;
  char *input_copy_pointer;
  char *token;
  char *command[255 + 1];
  int status;
  int token_counter;
  pid_t child_pid;
  do {
    printf("> ");
    input = (char *)malloc((1024 * sizeof(char)));
    fgets(input, (1024 * sizeof(char)), stdin);
    input_copy = (char *)malloc(sizeof(input));
    strcpy(input_copy, input);
    input_copy_pointer = input_copy;
    if (input_copy[strlen(input_copy) - 1] == '\n')
      input_copy[strlen(input_copy) - 1] = '\0';
    for (token_counter = 0; (token = strsep(&input_copy_pointer, " "));
         token_counter++) {
      command[token_counter] = token;
    }
    command[token_counter] = NULL;
    if (!*input || !strcmp(command[0], "exit"))
      exit(0);
    if (!strcmp(command[0], "cd")) {
      chdir(command[1]);
    }
    child_pid = fork();
    if (child_pid) {
      waitpid(child_pid, &status, 0);
      if (status) {
        printf("Non-zero exit status: %d\n", status);
      }
    } else {
      if (execvp(command[0], command)) {
        printf("Command not found: %s\n", command[0]);
      }
      break;
    }
    free(input);
    free(input_copy);
  } while (*input);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x400,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1080 <fgets@plt>
mov    $0x8,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x848(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x848(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    128f <main+0xbf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x850(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x850(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x838(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xd5d(%rip),%rsi
call   10b0 <strsep@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12de <main+0x10e>
mov    -0x28(%rbp),%rcx
movslq -0x838(%rbp),%rax
mov    %rcx,-0x830(%rbp,%rax,8)
ff
mov    -0x838(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x838(%rbp)
jmp    1299 <main+0xc9>
movslq -0x838(%rbp),%rax
movq   $0x0,-0x830(%rbp,%rax,8)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     131a <main+0x14a>
mov    -0x830(%rbp),%rdi
lea    0xcf7(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1321 <main+0x151>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0x830(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1349 <main+0x179>
mov    -0x828(%rbp),%rdi
call   1060 <chdir@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x840(%rbp)
cmpq   $0x0,-0x840(%rbp)
je     13a0 <main+0x1d0>
mov    -0x840(%rbp),%rdi
lea    -0x834(%rbp),%rsi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmpl   $0x0,-0x834(%rbp)
je     139b <main+0x1cb>
mov    -0x834(%rbp),%esi
lea    0xc77(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13d7 <main+0x207>
mov    -0x830(%rbp),%rdi
lea    -0x830(%rbp),%rsi
call   10d0 <execvp@plt>
cmp    $0x0,%rax
je     13d2 <main+0x202>
mov    -0x830(%rbp),%rsi
lea    0xc5a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13f6 <main+0x226>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11e2 <main+0x12>
xor    %eax,%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
lea    0xe19(%rip),%r15
lea    0x8(%rsp),%rbx
jmp    120a <main+0x3a>
nop
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
cmpb   $0x0,(%r14)
je     1356 <main+0x186>
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x400,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
mov    $0x8,%edi
call   10a0 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r13,0x8(%rsp)
mov    %r13,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r13,1)
jne    127a <main+0xaa>
mov    %r13,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r13,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   10b0 <strsep@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     12a9 <main+0xd9>
nopl   0x0(%rax)
mov    %rax,0x10(%rsp,%rbp,8)
add    $0x1,%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
call   10b0 <strsep@plt>
test   %rax,%rax
jne    1290 <main+0xc0>
mov    %ebp,%eax
movq   $0x0,0x10(%rsp,%rax,8)
cmpb   $0x0,(%r14)
je     136a <main+0x19a>
mov    0x10(%rsp),%r12
mov    %r12,%rdi
lea    0xd36(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     136a <main+0x19a>
mov    %r12,%rdi
lea    0xd24(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
jne    12f7 <main+0x127>
mov    0x18(%rsp),%rdi
call   1060 <chdir@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     1331 <main+0x161>
mov    %rax,%rdi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   10c0 <waitpid@plt>
mov    0x4(%rsp),%esi
test   %esi,%esi
je     11f0 <main+0x20>
lea    0xce6(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    11f0 <main+0x20>
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
call   10d0 <execvp@plt>
test   %rax,%rax
je     1356 <main+0x186>
mov    0x10(%rsp),%rsi
lea    0xcd6(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   10e0 <exit@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"> \00"
@stdin = global i32 0
@.str.1 = [2 x i8] c" \00"
@.str.2 = [5 x i8] c"exit\00"
@.str.3 = [3 x i8] c"cd\00"
@.str.4 = [26 x i8] c"Non-zero exit status: %d\0A\00"
@.str.5 = [23 x i8] c"Command not found: %s\0A\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca [256 x i8*]
%3 = alloca i32
%4 = bitcast i8** %1 to i8*
%5 = bitcast [256 x i8*]* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [256 x i8*], [256 x i8*]* %2, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8*], [256 x i8*]* %2, i64 0, i64 1
br label %9
9: ; preds = %72, %0
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = call i64 @malloc(i32 1024)
%12 = inttoptr i64 %11 to i8*
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %12, i32 1024, i32 %13)
%15 = call i64 @malloc(i32 8)
%16 = inttoptr i64 %15 to i8*
%17 = call i32 @strcpy(i8* %16, i8* %12)
store i8* %16, i8** %1
%18 = call i32 @strlen(i8* %16)
%19 = add nsw i32 %18, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %16, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %29
24: ; preds = %9
%25 = call i32 @strlen(i8* %16)
%26 = add nsw i32 %25, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %16, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %24, %9
%30 = call i8* @strsep(i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i8* %30, null
br i1 %31, label %39, label %32
32: ; preds = %29, %32
%33 = phi i64 [ %36, %32 ], [ 0, %29 ]
%34 = phi i8* [ %37, %32 ], [ %30, %29 ]
%35 = getelementptr inbounds [256 x i8*], [256 x i8*]* %2, i64 0, i64 %33
store i8* %34, i8** %35
%36 = add nuw i64 %33, 1
%37 = call i8* @strsep(i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %32
39: ; preds = %32, %29
%40 = phi i64 [ 0, %29 ], [ %36, %32 ]
%41 = and i64 %40, 4294967295
%42 = getelementptr inbounds [256 x i8*], [256 x i8*]* %2, i64 0, i64 %41
store i8* null, i8** %42
%43 = load i8, i8* %12
%44 = icmp eq i8 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %39
%46 = load i8*, i8** %7
%47 = call i32 @strcmp(i8* dereferenceable(1) %46, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45, %39
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %45
%52 = call i32 @strcmp(i8* dereferenceable(1) %46, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i8*, i8** %8
%56 = call i32 @chdir(i8* %55)
br label %57
57: ; preds = %54, %51
%58 = call i64 (...) @fork()
%59 = icmp eq i64 %58, 0
br i1 %59, label %66, label %60
60: ; preds = %57
%61 = call i32 @waitpid(i64 %58, i32* %3, i32 0)
%62 = load i32, i32* %3
%63 = icmp eq i32 %62, 0
br i1 %63, label %72, label %64
64: ; preds = %60
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %62)
br label %72
66: ; preds = %57
%67 = call i64 @execvp(i8* %46, i8** %7)
%68 = icmp eq i64 %67, 0
br i1 %68, label %77, label %69
69: ; preds = %66
%70 = load i8*, i8** %7
%71 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %70)
br label %77
72: ; preds = %60, %64
%73 = call i32 @free(i8* %12)
%74 = call i32 @free(i8* %16)
%75 = load i8, i8* %12
%76 = icmp eq i8 %75, 0
br i1 %76, label %77, label %9
77: ; preds = %72, %66, %69
ret i32 0
}