#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 */
int /*<<< orphan*/ chdir(char *);
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ my_freevect(char **);
int /*<<< orphan*/ my_str(char *);
char **my_str2vect(char *);
int /*<<< orphan*/ my_strcmp(char *, char *);
int read(int /*<<< orphan*/, char *, int);
scalar_t__ wait(void *);
int /*<<< orphan*/ writePrompt(char *, char *);
scalar_t__ xmalloc(int);
int main(int argc, char **argv) {
  pid_t pid;
  char *input = ((void *)0);
  int strLen;
  char **inputVect = ((void *)0);
  char *hostname = ((void *)0);
  char *cwd = ((void *)0);
  cwd = (char *)xmalloc(256 * sizeof(char) + 1);
  hostname = (char *)xmalloc(64 * sizeof(char) + 1);
  input = (char *)xmalloc(256 * sizeof(char));
  while (1) {
    writePrompt(hostname, cwd);
    strLen = read(0, input, 256 * sizeof(char)) - 1;
    input[strLen] = '\0';
    if (strLen >= 2) {
      inputVect = my_str2vect(input);
      if (!my_strcmp(inputVect[0], "exit")) {
        my_str("Exiting minishell. Goodbye!\n");
        free(cwd);
        free(hostname);
        exit(1);
      } else if (!my_strcmp(inputVect[0], "cd"))
        chdir(inputVect[1]);
      else {
        if ((pid = fork()) < 0) {
          my_str("FORK ERROR");
          exit(1);
        } else if (pid > 0) {
          while (wait(((void *)0)) != pid)
            ;
          my_freevect(inputVect);
        } else {
          if (execvp(inputVect[0], inputVect) < 0) {
            my_str("ERROR: execvp failed.\n");
            exit(1);
          }
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x101,%edi
call   1080 <xmalloc@plt>
mov    %rax,-0x40(%rbp)
mov    $0x41,%edi
call   1080 <xmalloc@plt>
mov    %rax,-0x38(%rbp)
mov    $0x100,%edi
call   1080 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <writePrompt@plt>
mov    -0x20(%rbp),%rsi
xor    %edi,%edi
mov    $0x100,%edx
call   1090 <read@plt>
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x2,-0x24(%rbp)
jl     1392 <main+0x1c2>
mov    -0x20(%rbp),%rdi
call   1050 <my_str2vect@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd7c(%rip),%rsi
call   1040 <my_strcmp@plt>
cmp    $0x0,%eax
jne    12ba <main+0xea>
lea    0xd6c(%rip),%rdi
call   10b0 <my_str@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd5a(%rip),%rsi
call   1040 <my_strcmp@plt>
cmp    $0x0,%eax
jne    12e8 <main+0x118>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <chdir@plt>
jmp    1388 <main+0x1b8>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1313 <main+0x143>
lea    0xd21(%rip),%rdi
call   10b0 <my_str@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1349 <main+0x179>
jmp    1323 <main+0x153>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
cmp    -0x18(%rbp),%rax
je     133b <main+0x16b>
jmp    1323 <main+0x153>
mov    -0x30(%rbp),%rdi
call   10a0 <my_freevect@plt>
jmp    137e <main+0x1ae>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   10c0 <execvp@plt>
cmp    $0x0,%rax
jge    1379 <main+0x1a9>
lea    0xcc6(%rip),%rdi
call   10b0 <my_str@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    137e <main+0x1ae>
jmp    1383 <main+0x1b3>
jmp    1388 <main+0x1b8>
jmp    138d <main+0x1bd>
jmp    1392 <main+0x1c2>
jmp    1230 <main+0x60>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x101,%edi
call   1080 <xmalloc@plt>
mov    %rax,%r14
mov    $0x41,%edi
call   1080 <xmalloc@plt>
mov    %rax,%r15
mov    $0x100,%edi
call   1080 <xmalloc@plt>
mov    %rax,%rbx
lea    0xe19(%rip),%r13
jmp    1220 <main+0x50>
mov    0x8(%rbp),%rdi
call   1070 <chdir@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <writePrompt@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1090 <read@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
cmp    $0x3,%eax
jl     1220 <main+0x50>
mov    %rbx,%rdi
call   1050 <my_str2vect@plt>
mov    %rax,%rbp
mov    (%rax),%rdi
lea    0xda5(%rip),%rsi
call   1040 <my_strcmp@plt>
test   %eax,%eax
je     12c7 <main+0xf7>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1040 <my_strcmp@plt>
test   %eax,%eax
je     120b <main+0x3b>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     12ed <main+0x11d>
je     12a9 <main+0xd9>
mov    %rax,%r12
cs nopw 0x0(%rax,%rax,1)
nop
xor    %edi,%edi
call   10e0 <wait@plt>
cmp    %r12,%rax
jne    1290 <main+0xc0>
mov    %rbp,%rdi
call   10a0 <my_freevect@plt>
jmp    1220 <main+0x50>
mov    0x0(%rbp),%rdi
mov    %rbp,%rsi
call   10c0 <execvp@plt>
test   %rax,%rax
jns    1220 <main+0x50>
lea    0xd6b(%rip),%rdi
jmp    12f4 <main+0x124>
lea    0xd37(%rip),%rdi
call   10b0 <my_str@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd31(%rip),%rdi
call   10b0 <my_str@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"exit\00"
@.str.1 = [29 x i8] c"Exiting minishell. Goodbye!\0A\00"
@.str.2 = [3 x i8] c"cd\00"
@.str.3 = [11 x i8] c"FORK ERROR\00"
@.str.4 = [23 x i8] c"ERROR: execvp failed.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i8** null, i8*** %9
store i8* null, i8** %10
store i8* null, i8** %11
%12 = call i64 @xmalloc(i32 257)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %11
%14 = call i64 @xmalloc(i32 65)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %10
%16 = call i64 @xmalloc(i32 256)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %7
br label %18
18: ; preds = %2, %90
%19 = load i8*, i8** %10
%20 = load i8*, i8** %11
%21 = call i32 @writePrompt(i8* %19, i8* %20)
%22 = load i8*, i8** %7
%23 = call i32 @read(i32 0, i8* %22, i32 256)
%24 = sub nsw i32 %23, 1
store i32 %24, i32* %8
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i32, i32* %8
%30 = icmp sge i32 %29, 2
br i1 %30, label %31, label %90
31: ; preds = %18
%32 = load i8*, i8** %7
%33 = call i8** @my_str2vect(i8* %32)
store i8** %33, i8*** %9
%34 = load i8**, i8*** %9
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 @my_strcmp(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%38 = icmp ne i32 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %31
%40 = call i32 @my_str(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%41 = load i8*, i8** %11
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** %10
%44 = call i32 @free(i8* %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %31
%47 = load i8**, i8*** %9
%48 = getelementptr inbounds i8*, i8** %47, i64 0
%49 = load i8*, i8** %48
%50 = call i32 @my_strcmp(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%51 = icmp ne i32 %50, 0
br i1 %51, label %57, label %52
52: ; preds = %46
%53 = load i8**, i8*** %9
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @chdir(i8* %55)
br label %88
57: ; preds = %46
%58 = call i64 (...) @fork()
store i64 %58, i64* %6
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = call i32 @my_str(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %57
%64 = load i64, i64* %6
%65 = icmp sgt i64 %64, 0
br i1 %65, label %66, label %75
66: ; preds = %63
br label %67
67: ; preds = %71, %66
%68 = call i64 @wait(i8* null)
%69 = load i64, i64* %6
%70 = icmp ne i64 %68, %69
br i1 %70, label %71, label %72
71: ; preds = %67
br label %67
72: ; preds = %67
%73 = load i8**, i8*** %9
%74 = call i32 @my_freevect(i8** %73)
br label %86
75: ; preds = %63
%76 = load i8**, i8*** %9
%77 = getelementptr inbounds i8*, i8** %76, i64 0
%78 = load i8*, i8** %77
%79 = load i8**, i8*** %9
%80 = call i64 @execvp(i8* %78, i8** %79)
%81 = icmp slt i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %75
%83 = call i32 @my_str(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %75
br label %86
86: ; preds = %85, %72
br label %87
87: ; preds = %86
br label %88
88: ; preds = %87, %52
br label %89
89: ; preds = %88
br label %90
90: ; preds = %89, %18
br label %18
}

LLVM IR (-O2):
@.str = [5 x i8] c"exit\00"
@.str.1 = [29 x i8] c"Exiting minishell. Goodbye!\0A\00"
@.str.2 = [3 x i8] c"cd\00"
@.str.3 = [11 x i8] c"FORK ERROR\00"
@.str.4 = [23 x i8] c"ERROR: execvp failed.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 @xmalloc(i32 257)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @xmalloc(i32 65)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i64 @xmalloc(i32 256)
%8 = inttoptr i64 %7 to i8*
br label %9
9: ; preds = %51, %2
%10 = tail call i32 @writePrompt(i8* %6, i8* %4)
%11 = tail call i32 @read(i32 0, i8* %8, i32 256)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %8, i64 %13
store i8 0, i8* %14
%15 = icmp sgt i32 %11, 2
br i1 %15, label %16, label %51
16: ; preds = %9
%17 = tail call i8** @my_str2vect(i8* %8)
%18 = load i8*, i8** %17
%19 = tail call i32 @my_strcmp(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = tail call i32 @my_str(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @free(i8* %4)
%24 = tail call i32 @free(i8* %6)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %16
%27 = load i8*, i8** %17
%28 = tail call i32 @my_strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = getelementptr inbounds i8*, i8** %17, i64 1
%32 = load i8*, i8** %31
%33 = tail call i32 @chdir(i8* %32)
br label %51
34: ; preds = %26
%35 = tail call i64 (...) @fork()
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = tail call i32 @my_str(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %34
%41 = icmp eq i64 %35, 0
br i1 %41, label %47, label %42
42: ; preds = %40, %42
%43 = tail call i64 @wait(i8* null)
%44 = icmp eq i64 %43, %35
br i1 %44, label %45, label %42
45: ; preds = %42
%46 = tail call i32 @my_freevect(i8** %17)
br label %51
47: ; preds = %40
%48 = load i8*, i8** %17
%49 = tail call i64 @execvp(i8* %48, i8** %17)
%50 = icmp slt i64 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %47, %45, %30, %9
br label %9
52: ; preds = %47
%53 = tail call i32 @my_str(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%54 = tail call i32 @exit(i32 1)
unreachable
}