#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*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
pid_t execpipe(char *command, char *args[], int pipefd[3]) {
  pid_t pid;
  int infd[2], outfd[2], errfd[2];
  pipe(infd);
  pipe(outfd);
  pipe(errfd);
  pid = fork();
  if (pid == 0) {
    close(infd[1]);
    close(outfd[0]);
    close(errfd[0]);
    dup2(infd[0], STDIN_FILENO);
    dup2(outfd[1], STDOUT_FILENO);
    dup2(errfd[1], STDERR_FILENO);
    execvp(command, args);
    exit(1);
  } else if (pid < 0) {
    return -1;
  }
  close(infd[0]);
  close(outfd[1]);
  close(errfd[1]);
  pipefd[0] = infd[1];
  pipefd[1] = outfd[0];
  pipefd[2] = errfd[0];
  return pid;
}


x86 assembly (clang, -O0):
<execpipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
lea    -0x38(%rbp),%rdi
call   1050 <pipe@plt>
lea    -0x40(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1207 <execpipe+0xa7>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x40(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x34(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x3c(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <execvp@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jge    121f <execpipe+0xbf>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1261 <execpipe+0x101>
jmp    1224 <execpipe+0xc4>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execpipe>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    %rsp,%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11ed <execpipe+0x8d>
js     11d7 <execpipe+0x77>
mov    %rax,%rbx
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%eax
mov    %eax,(%r12)
mov    0x8(%rsp),%eax
mov    %eax,0x4(%r12)
mov    (%rsp),%eax
mov    %eax,0x8(%r12)
jmp    11de <execpipe+0x7e>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x4(%rsp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i64 @execpipe(i8* %0, i8** %1, i32* %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i64
%9 = alloca [2 x i32]
%10 = alloca [2 x i32]
%11 = alloca [2 x i32]
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = call i64 (...) @fork()
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %47
21: ; preds = %3
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%32 = load i32, i32* %31
%33 = load i32, i32* @STDIN_FILENO
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%36 = load i32, i32* %35
%37 = load i32, i32* @STDOUT_FILENO
%38 = call i32 @dup2(i32 %36, i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%40 = load i32, i32* %39
%41 = load i32, i32* @STDERR_FILENO
%42 = call i32 @dup2(i32 %40, i32 %41)
%43 = load i8*, i8** %5
%44 = load i8**, i8*** %6
%45 = call i32 @execvp(i8* %43, i8** %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %3
%48 = load i64, i64* %8
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %47
store i64 -1, i64* %4
br label %75
51: ; preds = %47
br label %52
52: ; preds = %51
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%63 = load i32, i32* %62
%64 = load i32*, i32** %7
%65 = getelementptr inbounds i32, i32* %64, i64 0
store i32 %63, i32* %65
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%67 = load i32, i32* %66
%68 = load i32*, i32** %7
%69 = getelementptr inbounds i32, i32* %68, i64 1
store i32 %67, i32* %69
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%71 = load i32, i32* %70
%72 = load i32*, i32** %7
%73 = getelementptr inbounds i32, i32* %72, i64 2
store i32 %71, i32* %73
%74 = load i64, i64* %8
store i64 %74, i64* %4
br label %75
75: ; preds = %52, %50
%76 = load i64, i64* %4
ret i64 %76
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i64 @execpipe(i8* %0, i8** %1, i32* nocapture writeonly %2) {
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = alloca [2 x i32]
%7 = bitcast [2 x i32]* %4 to i8*
%8 = bitcast [2 x i32]* %5 to i8*
%9 = bitcast [2 x i32]* %6 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %39
18: ; preds = %3
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %12
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %14
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %10
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%30 = load i32, i32* %29
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i32, i32* @STDERR_FILENO
%36 = call i32 @dup2(i32 %34, i32 %35)
%37 = call i32 @execvp(i8* %0, i8** %1)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %3
%40 = icmp slt i64 %16, 0
br i1 %40, label %56, label %41
41: ; preds = %39
%42 = load i32, i32* %10
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%51 = load i32, i32* %50
store i32 %51, i32* %2
%52 = load i32, i32* %12
%53 = getelementptr inbounds i32, i32* %2, i64 1
store i32 %52, i32* %53
%54 = load i32, i32* %14
%55 = getelementptr inbounds i32, i32* %2, i64 2
store i32 %54, i32* %55
br label %56
56: ; preds = %39, %41
%57 = phi i64 [ %16, %41 ], [ -1, %39 ]
ret i64 %57
}