#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
void executePipedCommand(char *argvA[], char *argvB[], char *nameA,
                         char *nameB) {
  int pipefd[2];
  if (pipe(pipefd)) {
    perror("pipe");
    exit(127);
  }
  switch (fork()) {
  case -1:
    perror("fork()");
    exit(127);
  case 0:
    close(pipefd[0]);
    dup2(pipefd[1], 1);
    close(pipefd[1]);
    execve(nameA, argvA, 0);
    perror(nameA);
    exit(126);
  default:
    close(pipefd[1]);
    dup2(pipefd[0], 0);
    close(pipefd[0]);
    execve(nameB, argvB, 0);
    perror(nameB);
    exit(125);
  }
}


x86 assembly (clang, -O0):
<executePipedCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11b1 <executePipedCommand+0x41>
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     11db <executePipedCommand+0x6b>
jmp    11cb <executePipedCommand+0x5b>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     11f1 <executePipedCommand+0x81>
jmp    1230 <executePipedCommand+0xc0>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1060 <execve@plt>
mov    -0x18(%rbp),%rdi
call   1070 <perror@plt>
mov    $0x7e,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1060 <execve@plt>
mov    -0x20(%rbp),%rdi
call   1070 <perror@plt>
mov    $0x7d,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<executePipedCommand>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    11aa <executePipedCommand+0x3a>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11c0 <executePipedCommand+0x50>
cmp    $0xffffffff,%eax
jne    11fe <executePipedCommand+0x8e>
lea    0xe5d(%rip),%rdi
jmp    11b1 <executePipedCommand+0x41>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x7f,%edi
call   1080 <exit@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <execve@plt>
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    $0x7e,%edi
call   1080 <exit@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1060 <execve@plt>
mov    %r14,%rdi
call   1070 <perror@plt>
mov    $0x7d,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [7 x i8] c"fork()\00"
define void @executePipedCommand(i8** %0, i8** %1, i8* %2, i8* %3) {
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [2 x i32]
store i8** %0, i8*** %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 127)
unreachable
16: ; preds = %4
%17 = call i32 (...) @fork()
switch i32 %17, label %37 [
i32 -1, label %18
i32 0, label %21
]
18: ; preds = %16
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 127)
unreachable
21: ; preds = %16
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %7
%32 = load i8**, i8*** %5
%33 = call i32 @execve(i8* %31, i8** %32, i32 0)
%34 = load i8*, i8** %7
%35 = call i32 @perror(i8* %34)
%36 = call i32 @exit(i32 126)
unreachable
37: ; preds = %16
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @dup2(i32 %42, i32 0)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = load i8*, i8** %8
%48 = load i8**, i8*** %6
%49 = call i32 @execve(i8* %47, i8** %48, i32 0)
%50 = load i8*, i8** %8
%51 = call i32 @perror(i8* %50)
%52 = call i32 @exit(i32 125)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [7 x i8] c"fork()\00"
define void @executePipedCommand(i8** %0, i8** %1, i8* %2, i8* %3) {
%5 = alloca [2 x i32]
%6 = bitcast [2 x i32]* %5 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 127)
unreachable
13: ; preds = %4
%14 = call i32 (...) @fork()
switch i32 %14, label %29 [
i32 -1, label %15
i32 0, label %18
]
15: ; preds = %13
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 127)
unreachable
18: ; preds = %13
%19 = load i32, i32* %7
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @dup2(i32 %22, i32 1)
%24 = load i32, i32* %21
%25 = call i32 @close(i32 %24)
%26 = call i32 @execve(i8* %2, i8** %0, i32 0)
%27 = call i32 @perror(i8* %2)
%28 = call i32 @exit(i32 126)
unreachable
29: ; preds = %13
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %7
%34 = call i32 @dup2(i32 %33, i32 0)
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
%37 = call i32 @execve(i8* %3, i8** %1, i32 0)
%38 = call i32 @perror(i8* %3)
%39 = call i32 @exit(i32 125)
unreachable
}