#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fatal(char *);
int fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ report(char *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
pid_t filter(int fd, char **command) {
  pid_t pid;
  int pfd[2];
  if (pipe(pfd) < 0) {
    report("pipe()");
    return -1;
  }
  switch ((pid = fork())) {
  case -1:
    report("fork()");
    return -1;
  case 0:
    dup2(pfd[0], 0);
    dup2(fd, 1);
    close(pfd[0]);
    close(pfd[1]);
    close(fd);
    signal(SIGPIPE, SIG_DFL);
    execvp(command[0], command);
    fatal(command[0]);
  }
  dup2(pfd[1], fd);
  close(pfd[0]);
  close(pfd[1]);
  return pid;
}


x86 assembly (clang, -O0):
<filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11ba <filter+0x3a>
lea    0xe57(%rip),%rdi
call   1080 <report@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <filter+0x102>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     11e7 <filter+0x67>
jmp    11d7 <filter+0x57>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     11ff <filter+0x7f>
jmp    1261 <filter+0xe1>
lea    0xe19(%rip),%rdi
call   1080 <report@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <filter+0x102>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <fatal@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filter>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     11b8 <filter+0x38>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11cb <filter+0x4b>
cmp    $0xffffffff,%ebx
jne    1229 <filter+0xa9>
lea    0xe51(%rip),%rdi
jmp    11bf <filter+0x3f>
lea    0xe41(%rip),%rdi
call   1080 <report@plt>
mov    $0xffffffff,%ebx
jmp    1247 <filter+0xc7>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %r14d,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x0(%rbp),%rdi
mov    %rbp,%rsi
call   1070 <execvp@plt>
mov    0x0(%rbp),%rdi
call   1090 <fatal@plt>
mov    0xc(%rsp),%edi
mov    %r14d,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"pipe()\00"
@.str.1 = [7 x i8] c"fork()\00"
@SIGPIPE = global i32 0
@SIG_DFL = global i32 0
define i32 @filter(i32 %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @report(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %39
10: ; preds = %2
%11 = call i32 (...) @fork()
switch i32 %11, label %31 [
i32 -1, label %12
i32 0, label %14
]
12: ; preds = %10
%13 = call i32 @report(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %39
14: ; preds = %10
%15 = load i32, i32* %5
%16 = call i32 @dup2(i32 %15, i32 0)
%17 = call i32 @dup2(i32 %0, i32 1)
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = call i32 @close(i32 %0)
%24 = load i32, i32* @SIGPIPE
%25 = load i32, i32* @SIG_DFL
%26 = call i32 @signal(i32 %24, i32 %25)
%27 = load i8*, i8** %1
%28 = call i32 @execvp(i8* %27, i8** %1)
%29 = load i8*, i8** %1
%30 = call i32 @fatal(i8* %29)
br label %31
31: ; preds = %14, %10
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @dup2(i32 %33, i32 %0)
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %32
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %31, %12, %8
%40 = phi i32 [ -1, %8 ], [ %11, %31 ], [ -1, %12 ]
ret i32 %40
}