#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*/ SIGKILL;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
void execpiped(char **cmdfrom, char **cmdto, int *frompid, int *topid) {
  int pipefd[2];
  pipe(pipefd);
  *frompid = fork();
  if (*frompid == 0) {
    signal(SIGPIPE, SIG_DFL);
    close(pipefd[0]);
    dup2(pipefd[1], 1);
    close(pipefd[1]);
    execv(cmdfrom[0], cmdfrom);
    exit(1);
  }
  close(pipefd[1]);
  if (*frompid <= 0) {
    *frompid = 0;
    return;
  }
  *topid = fork();
  if (*topid == 0) {
    signal(SIGPIPE, SIG_DFL);
    dup2(pipefd[0], 0);
    close(pipefd[0]);
    execv(cmdto[0], cmdto);
    exit(1);
  }
  close(pipefd[0]);
  if (*topid <= 0) {
    kill(*frompid, SIGKILL);
    *frompid = 0;
    *topid = 0;
    return;
  }
}


x86 assembly (clang, -O0):
<execpiped>:
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>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    120b <execpiped+0x8b>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@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    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1090 <execv@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jg     122f <execpiped+0xaf>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
jmp    12cb <execpiped+0x14b>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    128e <execpiped+0x10e>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <execv@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jg     12cb <execpiped+0x14b>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execpiped>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsp,%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,(%rbx)
test   %eax,%eax
je     1201 <execpiped+0x81>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
cmpl   $0x0,(%rbx)
jle    11eb <execpiped+0x6b>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,(%r15)
test   %eax,%eax
je     1240 <execpiped+0xc0>
mov    (%rsp),%edi
call   1040 <close@plt>
cmpl   $0x0,(%r15)
jg     11f5 <execpiped+0x75>
mov    (%rbx),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
movl   $0x0,(%rbx)
jmp    11ee <execpiped+0x6e>
mov    %rbx,%r15
movl   $0x0,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@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    (%r12),%rdi
mov    %r12,%rsi
jmp    126f <execpiped+0xef>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1090 <execv@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@SIGPIPE = global i32 0
@SIG_DFL = global i32 0
@SIGKILL = global i32 0
define void @execpiped(i8** %0, i8** %1, i32* nocapture %2, i32* nocapture %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 i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %4
%12 = load i32, i32* @SIGPIPE
%13 = load i32, i32* @SIG_DFL
%14 = call i32 @signal(i32 %12, i32 %13)
%15 = load i32, i32* %7
%16 = call i32 @close(i32 %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @dup2(i32 %18, i32 1)
%20 = load i32, i32* %17
%21 = call i32 @close(i32 %20)
%22 = load i8*, i8** %0
%23 = call i32 @execv(i8* %22, i8** %0)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %4
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %2
%30 = icmp slt i32 %29, 1
br i1 %30, label %54, label %31
31: ; preds = %25
%32 = call i32 (...) @fork()
store i32 %32, i32* %3
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %45
34: ; preds = %31
%35 = load i32, i32* @SIGPIPE
%36 = load i32, i32* @SIG_DFL
%37 = call i32 @signal(i32 %35, i32 %36)
%38 = load i32, i32* %7
%39 = call i32 @dup2(i32 %38, i32 0)
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
%42 = load i8*, i8** %1
%43 = call i32 @execv(i8* %42, i8** %1)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %31
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %3
%49 = icmp slt i32 %48, 1
br i1 %49, label %50, label %56
50: ; preds = %45
%51 = load i32, i32* %2
%52 = load i32, i32* @SIGKILL
%53 = call i32 @kill(i32 %51, i32 %52)
store i32 0, i32* %2
br label %54
54: ; preds = %25, %50
%55 = phi i32* [ %3, %50 ], [ %2, %25 ]
store i32 0, i32* %55
br label %56
56: ; preds = %54, %45
ret void
}