#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ handle_pipes(char **, size_t, char **);
char *malloc(size_t);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
size_t process_args(char **, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ wait(scalar_t__);
int main(void) {
  int filedes[2];
  size_t length = 1000;
  char *buffer = malloc(length);
  char *args[length];
  char *dargs[length];
  int status = 0;
  while (1) {
    pipe(filedes);
    printf("$ ");
    fgets(buffer, length, stdin);
    size_t argnum = process_args(args, buffer);
    if (argnum == 0)
      continue;
    if (handle_pipes(args, argnum, dargs)) {
      pid_t pid = fork();
      if (pid == 0) {
        dup2(filedes[1], 1);
        execvp(args[0], args);
      } else {
        close(filedes[1]);
      }
      wait(pid);
    } else {
      int p = 0, o = 0;
      while (p < 1000)
        dargs[p++] = args[o++];
    }
    pid_t pid2 = fork();
    if (pid2 == 0) {
      dup2(filedes[0], 0);
      execvp(dargs[0], dargs);
      close(filedes[1]);
    }
    wait(pid2);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x3e8,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
jmp    1229 <main+0x79>
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
call   1080 <fgets@plt>
mov    -0x70(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10b0 <process_args@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1275 <main+0xc5>
jmp    1224 <main+0x74>
mov    -0x68(%rbp),%rdx
mov    -0x70(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1030 <handle_pipes@plt>
cmp    $0x0,%rax
je     12da <main+0x12a>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    12c4 <main+0x114>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x70(%rbp),%rsi
mov    (%rsi),%rdi
call   10a0 <execvp@plt>
jmp    12cc <main+0x11c>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x50(%rbp),%rdi
call   10c0 <wait@plt>
jmp    132b <main+0x17b>
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
cmpl   $0x3e8,-0x54(%rbp)
jge    1326 <main+0x176>
mov    -0x68(%rbp),%rax
mov    -0x70(%rbp),%rcx
mov    -0x58(%rbp),%edx
mov    %edx,%esi
add    $0x1,%esi
mov    %esi,-0x58(%rbp)
movslq %edx,%rdx
mov    (%rcx,%rdx,8),%rdx
mov    -0x54(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x54(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    12e8 <main+0x138>
jmp    132b <main+0x17b>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    135f <main+0x1af>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x68(%rbp),%rsi
mov    (%rsi),%rdi
call   10a0 <execvp@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x60(%rbp),%rdi
call   10c0 <wait@plt>
jmp    1224 <main+0x74>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3e98,%rsp
mov    $0x3e8,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
lea    0xe1b(%rip),%r15
mov    0x2dfc(%rip),%r14
lea    0x1f50(%rsp),%r12
lea    0x10(%rsp),%r13
jmp    1208 <main+0x48>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   10d0 <wait@plt>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
mov    %r15,%rdi
call   1050 <printf@plt>
mov    (%r14),%edx
mov    $0x3e8,%esi
mov    %rbx,%rdi
call   1080 <fgets@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   10c0 <process_args@plt>
test   %rax,%rax
je     1208 <main+0x48>
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r13,%rdx
call   1030 <handle_pipes@plt>
test   %rax,%rax
je     1270 <main+0xb0>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %rax,%rbp
mov    0xc(%rsp),%edi
test   %rax,%rax
je     1282 <main+0xc2>
call   1060 <close@plt>
jmp    129c <main+0xdc>
nopw   0x0(%rax,%rax,1)
mov    $0x1f40,%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1090 <memcpy@plt>
jmp    12a4 <main+0xe4>
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x1f50(%rsp),%rdi
mov    %r12,%rsi
call   10b0 <execvp@plt>
mov    %rbp,%rdi
call   10d0 <wait@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1200 <main+0x40>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x10(%rsp),%rdi
mov    %r13,%rsi
call   10b0 <execvp@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
jmp    1200 <main+0x40>

LLVM IR (-O0):
@.str = [3 x i8] c"$ \00"
@stdin = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
store i32 0, i32* %1
store i64 1000, i64* %3
%14 = load i64, i64* %3
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %4
%16 = load i64, i64* %3
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %5
%18 = alloca i8*, i64 %16
store i64 %16, i64* %6
%19 = load i64, i64* %3
%20 = alloca i8*, i64 %19
store i64 %19, i64* %7
store i32 0, i32* %8
br label %21
21: ; preds = %86, %34, %0
br label %22
22: ; preds = %21
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = call i32 @pipe(i32* %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%26 = load i8*, i8** %4
%27 = load i64, i64* %3
%28 = load i32, i32* @stdin
%29 = call i32 @fgets(i8* %26, i64 %27, i32 %28)
%30 = load i8*, i8** %4
%31 = call i64 @process_args(i8** %18, i8* %30)
store i64 %31, i64* %9
%32 = load i64, i64* %9
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %22
br label %21
35: ; preds = %22
%36 = load i64, i64* %9
%37 = call i64 @handle_pipes(i8** %18, i64 %36, i8** %20)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %57
39: ; preds = %35
%40 = call i64 (...) @fork()
store i64 %40, i64* %10
%41 = load i64, i64* %10
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %39
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @dup2(i32 %45, i32 1)
%47 = getelementptr inbounds i8*, i8** %18, i64 0
%48 = load i8*, i8** %47
%49 = call i32 @execvp(i8* %48, i8** %18)
br label %54
50: ; preds = %39
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %50, %43
%55 = load i64, i64* %10
%56 = call i32 @wait(i64 %55)
br label %72
57: ; preds = %35
store i32 0, i32* %11
store i32 0, i32* %12
br label %58
58: ; preds = %61, %57
%59 = load i32, i32* %11
%60 = icmp slt i32 %59, 1000
br i1 %60, label %61, label %71
61: ; preds = %58
%62 = load i32, i32* %12
%63 = add nsw i32 %62, 1
store i32 %63, i32* %12
%64 = sext i32 %62 to i64
%65 = getelementptr inbounds i8*, i8** %18, i64 %64
%66 = load i8*, i8** %65
%67 = load i32, i32* %11
%68 = add nsw i32 %67, 1
store i32 %68, i32* %11
%69 = sext i32 %67 to i64
%70 = getelementptr inbounds i8*, i8** %20, i64 %69
store i8* %66, i8** %70
br label %58
71: ; preds = %58
br label %72
72: ; preds = %71, %54
%73 = call i64 (...) @fork()
store i64 %73, i64* %13
%74 = load i64, i64* %13
%75 = icmp eq i64 %74, 0
br i1 %75, label %76, label %86
76: ; preds = %72
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%78 = load i32, i32* %77
%79 = call i32 @dup2(i32 %78, i32 0)
%80 = getelementptr inbounds i8*, i8** %20, i64 0
%81 = load i8*, i8** %80
%82 = call i32 @execvp(i8* %81, i8** %20)
%83 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%84 = load i32, i32* %83
%85 = call i32 @close(i32 %84)
br label %86
86: ; preds = %76, %72
%87 = load i64, i64* %13
%88 = call i32 @wait(i64 %87)
br label %21
}

LLVM IR (-O2):
@.str = [3 x i8] c"$ \00"
@stdin = global i32 0
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = tail call dereferenceable_or_null(1000) i8* @malloc(i64 1000)
%4 = alloca [1000 x i8*]
%5 = bitcast [1000 x i8*]* %4 to i8*
%6 = alloca [1000 x i8*]
%7 = bitcast [1000 x i8*]* %6 to i8*
%8 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %6, i64 0, i64 0
%9 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %4, i64 0, i64 0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %12
12: ; preds = %19, %0
%13 = call i32 @pipe(i32* %10)
%14 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %3, i64 1000, i32 %15)
%17 = call i64 @process_args(i8** %9, i8* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %12, %46
br label %12
20: ; preds = %12
%21 = call i64 @handle_pipes(i8** %9, i64 %17, i8** %8)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
br label %36
24: ; preds = %20
%25 = call i64 (...) @fork()
%26 = icmp eq i64 %25, 0
%27 = load i32, i32* %11
br i1 %26, label %28, label %32
28: ; preds = %24
%29 = call i32 @dup2(i32 %27, i32 1)
%30 = load i8*, i8** %9
%31 = call i32 @execvp(i8* %30, i8** %9)
br label %34
32: ; preds = %24
%33 = call i32 @close(i32 %27)
br label %34
34: ; preds = %32, %28
%35 = call i32 @wait(i64 %25)
br label %36
36: ; preds = %23, %34
%37 = call i64 (...) @fork()
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i32, i32* %10
%41 = call i32 @dup2(i32 %40, i32 0)
%42 = load i8*, i8** %8
%43 = call i32 @execvp(i8* %42, i8** %8)
%44 = load i32, i32* %11
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %39, %36
%47 = call i32 @wait(i64 %37)
br label %19
}