#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 int /*<<< orphan*/ strSubCmd;
typedef int /*<<< orphan*/ strCmd;
typedef scalar_t__ pid_t;

/* Variables and functions */
int BUFSIZ;
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
scalar_t__ close(int);
scalar_t__ dup2(int, scalar_t__);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ executeCmd(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
void distribute(const char *pCmd) {
  char strCmd[BUFSIZ];
  char *pPipe = NULL;
  char strSubCmd[BUFSIZ];
  strncpy(strCmd, pCmd, sizeof(strCmd));
  pPipe = strrchr(strCmd, '|');
  if (pPipe == NULL) {
    strncpy(strSubCmd, strCmd, sizeof(strSubCmd));
  } else {
    strncpy(strSubCmd, pPipe + 1, sizeof(strSubCmd));
  };
  if (pPipe != NULL) {
    int iPipeFds[2];
    pid_t childPid = 0;
    *pPipe = '\0';
    if (pipe(iPipeFds) != 0) {
      perror("pipe() failed.");
      exit(errno);
    }
    childPid = fork();
    if (childPid == 0) {
      if (close(iPipeFds[0]) != 0) {
        perror("close() failed.");
        exit(errno);
      }
      if (dup2(iPipeFds[1], STDOUT_FILENO) != STDOUT_FILENO) {
        perror("dup2() failed.");
        exit(errno);
      }
      distribute(strCmd);
      exit(0);
    }
    if (close(iPipeFds[1]) != 0) {
      perror("close() failed.");
      exit(errno);
    }
    if (dup2(iPipeFds[0], STDIN_FILENO) != STDIN_FILENO) {
      perror("dup2() failed.");
      exit(errno);
    }
  }
  executeCmd(strSubCmd);
}


x86 assembly (clang, -O0):
<distribute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e25(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    (%rcx),%ecx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
mov    $0x7c,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1242 <distribute+0xa2>
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
jmp    1259 <distribute+0xb9>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x20(%rbp)
je     139b <distribute+0x1fb>
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x30(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
je     12a0 <distribute+0x100>
lea    0xd73(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    132e <distribute+0x18e>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     12e2 <distribute+0x142>
lea    0xd40(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d04(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2cf5(%rip),%rcx
cmp    (%rcx),%rax
je     131e <distribute+0x17e>
lea    0xd14(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <distribute@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     135a <distribute+0x1ba>
lea    0xcc8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%edi
mov    0x2c64(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2c55(%rip),%rcx
cmp    (%rcx),%rax
je     1396 <distribute+0x1f6>
lea    0xc9c(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c41(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    139b <distribute+0x1fb>
mov    -0x48(%rbp),%rdi
call   1090 <executeCmd@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<distribute>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rsi
mov    0x2e1f(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %r12,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    %r12,%rdi
mov    $0x7c,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1255 <distribute+0xb5>
mov    %rax,%rbx
mov    %rax,%rsi
add    $0x1,%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx)
lea    -0x28(%rbp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jne    1278 <distribute+0xd8>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     1281 <distribute+0xe1>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
test   %rax,%rax
jne    12b4 <distribute+0x114>
mov    -0x28(%rbp),%edi
mov    0x2d89(%rip),%rbx
mov    (%rbx),%rsi
call   1040 <dup2@plt>
cmp    (%rbx),%rax
je     1263 <distribute+0xc3>
lea    0xdcc(%rip),%rdi
jmp    12bb <distribute+0x11b>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    %r14,%rdi
call   1090 <executeCmd@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd81(%rip),%rdi
jmp    12bb <distribute+0x11b>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
test   %rax,%rax
jne    12b4 <distribute+0x114>
mov    -0x24(%rbp),%edi
mov    0x2d58(%rip),%rbx
mov    (%rbx),%rsi
call   1040 <dup2@plt>
cmp    (%rbx),%rax
jne    124c <distribute+0xac>
mov    %r12,%rdi
call   10b0 <distribute@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd54(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [15 x i8] c"pipe() failed.\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"close() failed.\00"
@STDOUT_FILENO = global i64 0
@.str.2 = [15 x i8] c"dup2() failed.\00"
@STDIN_FILENO = global i64 0
define void @distribute(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [2 x i32]
%8 = alloca i64
store i8* %0, i8** %2
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
store i8* null, i8** %5
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = alloca i8, i64 %14
store i64 %14, i64* %6
%16 = load i8*, i8** %2
%17 = trunc i64 %10 to i32
%18 = call i32 @strncpy(i8* %12, i8* %16, i32 %17)
%19 = call i8* @strrchr(i8* %12, i8 signext 124)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %1
%23 = trunc i64 %14 to i32
%24 = call i32 @strncpy(i8* %15, i8* %12, i32 %23)
br label %30
25: ; preds = %1
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = trunc i64 %14 to i32
%29 = call i32 @strncpy(i8* %15, i8* %27, i32 %28)
br label %30
30: ; preds = %25, %22
%31 = load i8*, i8** %5
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %89
33: ; preds = %30
store i64 0, i64* %8
%34 = load i8*, i8** %5
store i8 0, i8* %34
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%36 = call i64 @pipe(i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @errno
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
%43 = call i64 (...) @fork()
store i64 %43, i64* %8
%44 = load i64, i64* %8
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %68
46: ; preds = %42
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%48 = load i32, i32* %47
%49 = call i64 @close(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* @errno
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %46
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%57 = load i32, i32* %56
%58 = load i64, i64* @STDOUT_FILENO
%59 = call i64 @dup2(i32 %57, i64 %58)
%60 = load i64, i64* @STDOUT_FILENO
%61 = icmp ne i64 %59, %60
br i1 %61, label %62, label %66
62: ; preds = %55
%63 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%64 = load i32, i32* @errno
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %55
%67 = call i32 @exit(i32 0)
unreachable
68: ; preds = %42
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%70 = load i32, i32* %69
%71 = call i64 @close(i32 %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %68
%74 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%75 = load i32, i32* @errno
%76 = call i32 @exit(i32 %75)
unreachable
77: ; preds = %68
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%79 = load i32, i32* %78
%80 = load i64, i64* @STDIN_FILENO
%81 = call i64 @dup2(i32 %79, i64 %80)
%82 = load i64, i64* @STDIN_FILENO
%83 = icmp ne i64 %81, %82
br i1 %83, label %84, label %88
84: ; preds = %77
%85 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%86 = load i32, i32* @errno
%87 = call i32 @exit(i32 %86)
unreachable
88: ; preds = %77
br label %89
89: ; preds = %88, %30
%90 = call i32 @executeCmd(i8* %15)
%91 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [15 x i8] c"pipe() failed.\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"close() failed.\00"
@STDOUT_FILENO = global i64 0
@.str.2 = [15 x i8] c"dup2() failed.\00"
@STDIN_FILENO = global i64 0
define void @distribute(i8* %0) {
%2 = alloca [2 x i32]
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = call i32 @strncpy(i8* %5, i8* %0, i32 %3)
%8 = call i8* @strrchr(i8* %5, i8 signext 124)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @strncpy(i8* %6, i8* %5, i32 %3)
br label %67
12: ; preds = %1
%13 = getelementptr inbounds i8, i8* %8, i64 1
%14 = call i32 @strncpy(i8* %6, i8* %13, i32 %3)
%15 = bitcast [2 x i32]* %2 to i8*
store i8 0, i8* %8
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%17 = call i64 @pipe(i32* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @errno
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %12
%24 = call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %47
26: ; preds = %23
%27 = load i32, i32* %16
%28 = call i64 @close(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @errno
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %26
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = load i64, i64* @STDOUT_FILENO
%38 = call i64 @dup2(i32 %36, i64 %37)
%39 = load i64, i64* @STDOUT_FILENO
%40 = icmp eq i64 %38, %39
br i1 %40, label %45, label %41
41: ; preds = %34
%42 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @errno
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %34
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %23
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i64 @close(i32 %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %56, label %52
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%54 = load i32, i32* @errno
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %47
%57 = load i32, i32* %16
%58 = load i64, i64* @STDIN_FILENO
%59 = call i64 @dup2(i32 %57, i64 %58)
%60 = load i64, i64* @STDIN_FILENO
%61 = icmp eq i64 %59, %60
br i1 %61, label %66, label %62
62: ; preds = %56
%63 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%64 = load i32, i32* @errno
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %56
br label %67
67: ; preds = %10, %66
%68 = call i32 @executeCmd(i8* %6)
ret void
}