#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_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 ((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     1300 <main+0x140>
mov    -0x18(%rbp),%rdi
mov    $0x20,%esi
call   1090 <num_tokens@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <counting_malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1240 <main+0x80>
lea    0xdd3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x20,%esi
call   10e0 <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     127d <main+0xbd>
jmp    1300 <main+0x140>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <create_pipe@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12da <main+0x11a>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <execute_command@plt>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   10b0 <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   10d0 <wait@plt>
jmp    12e9 <main+0x129>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   10b0 <execute_command@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10d0 <wait@plt>
mov    -0x18(%rbp),%rdi
call   1030 <counting_free@plt>
jmp    11de <main+0x1e>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   10c0 <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    11e2 <main+0x22>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    %rax,%r15
mov    %rsp,%r14
jmp    121b <main+0x5b>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   10b0 <execute_command@plt>
xor    %edi,%edi
call   10d0 <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     11d4 <main+0x14>
mov    %r15,%rdi
mov    $0x20,%esi
call   1090 <num_tokens@plt>
mov    %eax,%r12d
lea    0x8(,%r12,8),%edi
call   1080 <counting_malloc@plt>
test   %rax,%rax
je     12c7 <main+0x107>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
mov    %r12d,%ecx
call   10e0 <get_tokens@plt>
movslq %r12d,%rax
movq   $0x0,(%rbx,%rax,8)
mov    %rbx,%rdi
call   1040 <do_exit@plt>
test   %rax,%rax
jne    11d4 <main+0x14>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <create_pipe@plt>
test   %rax,%rax
je     11f0 <main+0x30>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   10b0 <execute_command@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x2,%edx
call   10b0 <execute_command@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
jmp    11fd <main+0x3d>
lea    0xd32(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   10c0 <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 = %66, %2
%12 = call i8* (...) @read_prompt()
store i8* %12, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %70
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 %70
43: ; preds = %30
%44 = load i8**, i8*** %9
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%46 = call i8** @create_pipe(i8** %44, i32* %45)
store i8** %46, i8*** %10
%47 = icmp ne i8** %46, null
br i1 %47, label %48, label %62
48: ; preds = %43
%49 = load i8**, i8*** %9
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%51 = call i32 @execute_command(i8** %49, i32* %50, i32 1)
%52 = load i8**, i8*** %10
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%54 = call i32 @execute_command(i8** %52, i32* %53, i32 2)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = call i32 @wait(i8* null)
br label %66
62: ; preds = %43
%63 = load i8**, i8*** %9
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%65 = call i32 @execute_command(i8** %63, i32* %64, i32 0)
br label %66
66: ; preds = %62, %48
%67 = call i32 @wait(i8* null)
%68 = load i8*, i8** %6
%69 = call i32 @counting_free(i8* %68)
br label %11
70: ; preds = %42, %11
%71 = load i32, i32* @EXIT_SUCCESS
%72 = call i32 @exit(i32 %71)
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 %46, 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, %41
%11 = phi i8* [ %5, %7 ], [ %44, %41 ]
%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 %46
28: ; preds = %22
%29 = call i8** @create_pipe(i8** %16, i32* %8)
%30 = icmp eq i8** %29, null
br i1 %30, label %39, label %31
31: ; preds = %28
%32 = call i32 @execute_command(i8** %16, i32* %8, i32 1)
%33 = call i32 @execute_command(i8** %29, i32* %8, i32 2)
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %9
%37 = call i32 @close(i32 %36)
%38 = call i32 @wait(i8* null)
br label %41
39: ; preds = %28
%40 = call i32 @execute_command(i8** %16, i32* %8, i32 0)
br label %41
41: ; preds = %39, %31
%42 = call i32 @wait(i8* null)
%43 = call i32 @counting_free(i8* %11)
%44 = call i8* (...) @read_prompt()
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %10
46: ; preds = %41, %22, %2
%47 = load i32, i32* @EXIT_SUCCESS
%48 = call i32 @exit(i32 %47)
unreachable
}