#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ counting_free(char *);
scalar_t__ counting_malloc(int);
char **create_pipe(char **, int *);
scalar_t__ do_cd(char **);
scalar_t__ do_exit(char **);
int /*<<< orphan*/ execute_command(char **, int *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ get_tokens(char *, char, char **, int);
int num_tokens(char *, char);
int /*<<< orphan*/ perror(char *);
char *read_prompt();
int /*<<< orphan*/ wait(void *);
int main(int argc, char **argv) {
  char *input;
  int fd[2];
  int num_arguments;
  char **arguments;
  char **sink_arguments = NULL;
  while ((input = read_prompt())) {
    num_arguments = num_tokens(input, ' ');
    arguments = (char **)counting_malloc(sizeof(char *) * (num_arguments + 1));
    if (arguments == NULL) {
      perror("Unable to allocate necessary memory");
      exit(EXIT_FAILURE);
    }
    get_tokens(input, ' ', arguments, num_arguments);
    arguments[num_arguments] = (char *)NULL;
    if (do_exit(arguments)) {
      break;
    }
    if (do_cd(arguments)) {
      continue;
    }
    if ((sink_arguments = create_pipe(arguments, fd)) != NULL) {
      execute_command(arguments, fd, 1);
      execute_command(sink_arguments, fd, 2);
      close(fd[0]);
      close(fd[1]);
      wait((void *)NULL);
    } else {
      execute_command(arguments, fd, 0);
    }
    wait((void *)NULL);
    counting_free(input);
  }
  exit(EXIT_SUCCESS);
}


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,-0x38(%rbp)
mov    $0x0,%al
call   1050 <read_prompt@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1328 <main+0x158>
mov    -0x18(%rbp),%rdi
mov    $0x20,%esi
call   10a0 <num_tokens@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1090 <counting_malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1250 <main+0x80>
lea    0xdc3(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x20,%esi
call   10f0 <get_tokens@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x30(%rbp),%rdi
call   1040 <do_exit@plt>
cmp    $0x0,%rax
je     128d <main+0xbd>
jmp    1328 <main+0x158>
mov    -0x30(%rbp),%rdi
call   1070 <do_cd@plt>
cmp    $0x0,%rax
je     12a5 <main+0xd5>
jmp    11ee <main+0x1e>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <create_pipe@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1302 <main+0x132>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   10c0 <execute_command@plt>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   10c0 <execute_command@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
jmp    1311 <main+0x141>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   10c0 <execute_command@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
mov    -0x18(%rbp),%rdi
call   1030 <counting_free@plt>
jmp    11ee <main+0x1e>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <read_prompt@plt>
test   %rax,%rax
jne    11f2 <main+0x22>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    %rax,%r15
mov    %rsp,%r14
jmp    1225 <main+0x55>
mov    %r12,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   10c0 <execute_command@plt>
xor    %edi,%edi
call   10e0 <wait@plt>
mov    %r15,%rdi
call   1030 <counting_free@plt>
xor    %eax,%eax
call   1050 <read_prompt@plt>
mov    %rax,%r15
test   %rax,%rax
je     11e4 <main+0x14>
mov    %r15,%rdi
mov    $0x20,%esi
call   10a0 <num_tokens@plt>
mov    %eax,%ebx
lea    0x8(,%rbx,8),%edi
call   1090 <counting_malloc@plt>
test   %rax,%rax
je     12db <main+0x10b>
mov    %rax,%r12
mov    %r15,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
mov    %ebx,%ecx
call   10f0 <get_tokens@plt>
movslq %ebx,%rax
movq   $0x0,(%r12,%rax,8)
mov    %r12,%rdi
call   1040 <do_exit@plt>
test   %rax,%rax
jne    11e4 <main+0x14>
mov    %r12,%rdi
call   1070 <do_cd@plt>
test   %rax,%rax
jne    1216 <main+0x46>
mov    %r12,%rdi
mov    %r14,%rsi
call   1080 <create_pipe@plt>
test   %rax,%rax
je     11fa <main+0x2a>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   10c0 <execute_command@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x2,%edx
call   10c0 <execute_command@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10e0 <wait@plt>
jmp    1207 <main+0x37>
lea    0xd1e(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Unable to allocate necessary memory\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8** null, i8*** %10
br label %11
11: ; preds = %71, %47, %2
%12 = call i8* (...) @read_prompt()
store i8* %12, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %75
14: ; preds = %11
%15 = load i8*, i8** %6
%16 = call i32 @num_tokens(i8* %15, i8 signext 32)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = mul i64 8, %19
%21 = trunc i64 %20 to i32
%22 = call i64 @counting_malloc(i32 %21)
%23 = inttoptr i64 %22 to i8**
store i8** %23, i8*** %9
%24 = load i8**, i8*** %9
%25 = icmp eq i8** %24, null
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %14
%31 = load i8*, i8** %6
%32 = load i8**, i8*** %9
%33 = load i32, i32* %8
%34 = call i32 @get_tokens(i8* %31, i8 signext 32, i8** %32, i32 %33)
%35 = load i8**, i8*** %9
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %35, i64 %37
store i8* null, i8** %38
%39 = load i8**, i8*** %9
%40 = call i64 @do_exit(i8** %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %30
br label %75
43: ; preds = %30
%44 = load i8**, i8*** %9
%45 = call i64 @do_cd(i8** %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
br label %11
48: ; preds = %43
%49 = load i8**, i8*** %9
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%51 = call i8** @create_pipe(i8** %49, i32* %50)
store i8** %51, i8*** %10
%52 = icmp ne i8** %51, null
br i1 %52, label %53, label %67
53: ; preds = %48
%54 = load i8**, i8*** %9
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%56 = call i32 @execute_command(i8** %54, i32* %55, i32 1)
%57 = load i8**, i8*** %10
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%59 = call i32 @execute_command(i8** %57, i32* %58, i32 2)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = call i32 @wait(i8* null)
br label %71
67: ; preds = %48
%68 = load i8**, i8*** %9
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%70 = call i32 @execute_command(i8** %68, i32* %69, i32 0)
br label %71
71: ; preds = %67, %53
%72 = call i32 @wait(i8* null)
%73 = load i8*, i8** %6
%74 = call i32 @counting_free(i8* %73)
br label %11
75: ; preds = %42, %11
%76 = load i32, i32* @EXIT_SUCCESS
%77 = call i32 @exit(i32 %76)
unreachable
}

LLVM IR (-O2):
@.str = [36 x i8] c"Unable to allocate necessary memory\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = call i8* (...) @read_prompt()
%6 = icmp eq i8* %5, null
br i1 %6, label %50, label %7
7: ; preds = %2
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %10
10: ; preds = %7, %47
%11 = phi i8* [ %5, %7 ], [ %48, %47 ]
%12 = call i32 @num_tokens(i8* %11, i8 signext 32)
%13 = shl i32 %12, 3
%14 = add i32 %13, 8
%15 = call i64 @counting_malloc(i32 %14)
%16 = inttoptr i64 %15 to i8**
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %22
18: ; preds = %10
%19 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %10
%23 = call i32 @get_tokens(i8* %11, i8 signext 32, i8** %16, i32 %12)
%24 = sext i32 %12 to i64
%25 = getelementptr inbounds i8*, i8** %16, i64 %24
store i8* null, i8** %25
%26 = call i64 @do_exit(i8** %16)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %50
28: ; preds = %22
%29 = call i64 @do_cd(i8** %16)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %47
31: ; preds = %28
%32 = call i8** @create_pipe(i8** %16, i32* %8)
%33 = icmp eq i8** %32, null
br i1 %33, label %42, label %34
34: ; preds = %31
%35 = call i32 @execute_command(i8** %16, i32* %8, i32 1)
%36 = call i32 @execute_command(i8** %32, i32* %8, i32 2)
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %9
%40 = call i32 @close(i32 %39)
%41 = call i32 @wait(i8* null)
br label %44
42: ; preds = %31
%43 = call i32 @execute_command(i8** %16, i32* %8, i32 0)
br label %44
44: ; preds = %42, %34
%45 = call i32 @wait(i8* null)
%46 = call i32 @counting_free(i8* %11)
br label %47
47: ; preds = %44, %28
%48 = call i8* (...) @read_prompt()
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %10
50: ; preds = %47, %22, %2
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
}