#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ WUR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getfd(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printcmdline(char **);
int read(int, char *, int);
int /*<<< orphan*/ waitchild();
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 4)
    die("Usage: %s (+|-) (filename|fd/) command [args]\n", argv[0]);
  if ((argv[1][0] != '-' && argv[1][0] != '+') || argv[1][1] != '\0')
    die("arg '%s' not '-' nor '+'\n", argv[1]);
  int fd = getfd(argv[2]);
  int pipefd[2];
  if (pipe(pipefd) < 0)
    die("pipe:");
  switch (fork()) {
  case -1:
    die("fork:");
  case 0:
    close(fd);
    close(pipefd[0]);
    dup2(pipefd[1], 2);
    if (pipefd[1] != 2)
      close(pipefd[1]);
    if (argv[1][0] == '+')
      printcmdline(&argv[3]);
    execvp(argv[3], &argv[3]);
    die("Cannot execute '%s':", argv[3]);
  }
  close(pipefd[1]);
  char buf[4096];
  while (1) {
    int l = read(pipefd[0], buf, sizeof buf);
    if (l > 0) {
      WUR(write(fd, buf, l));
      WUR(write(2, buf, l));
      continue;
    }
    if (l < 0)
      if (errno == EINTR)
        continue;
    exit(waitchild());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1208 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     1230 <main+0x60>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
jne    1245 <main+0x75>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     125b <main+0x8b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <getfd@plt>
mov    %eax,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    128c <main+0xbc>
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1028(%rbp)
sub    $0xffffffff,%eax
je     12bc <main+0xec>
jmp    12a9 <main+0xd9>
mov    -0x1028(%rbp),%eax
test   %eax,%eax
je     12ca <main+0xfa>
jmp    1345 <main+0x175>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x18(%rbp)
je     12f9 <main+0x129>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2b,%eax
jne    131a <main+0x14a>
mov    -0x10(%rbp),%rdi
add    $0x18,%rdi
call   10d0 <printcmdline@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x18,%rsi
call   10b0 <execvp@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
jle    13b3 <main+0x1e3>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1040 <write@plt>
mov    %eax,%edi
call   1030 <WUR@plt>
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
mov    $0x2,%edi
call   1040 <write@plt>
mov    %eax,%edi
call   1030 <WUR@plt>
jmp    134d <main+0x17d>
cmpl   $0x0,-0x1024(%rbp)
jge    13e4 <main+0x214>
mov    0x2c19(%rip),%rax
mov    (%rax),%rax
mov    0x2c07(%rip),%rcx
cmp    (%rcx),%rax
jne    13df <main+0x20f>
jmp    134d <main+0x17d>
jmp    13e4 <main+0x214>
mov    $0x0,%al
call   1090 <waitchild@plt>
mov    %eax,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jg     11f8 <main+0x28>
mov    (%r14),%rsi
lea    0xe0f(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x8(%r14),%rsi
mov    (%rsi),%al
cmp    $0x2d,%al
je     1206 <main+0x36>
cmp    $0x2b,%al
jne    120c <main+0x3c>
cmpb   $0x0,0x1(%rsi)
je     121a <main+0x4a>
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0x10(%r14),%rdi
call   10e0 <getfd@plt>
mov    %eax,%r15d
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jns    1243 <main+0x73>
lea    0xe0d(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1261 <main+0x91>
cmp    $0xffffffff,%eax
jne    12bf <main+0xef>
lea    0xdf5(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
cmp    $0x2,%edi
je     128e <main+0xbe>
call   1060 <close@plt>
mov    0x8(%r14),%rax
cmpb   $0x2b,(%rax)
jne    12a0 <main+0xd0>
lea    0x18(%r14),%rdi
call   10d0 <printcmdline@plt>
lea    0x18(%r14),%rsi
mov    0x18(%r14),%rdi
call   10b0 <execvp@plt>
mov    0x18(%r14),%rsi
lea    0xd9d(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rbx
mov    0x2d0c(%rip),%r14
mov    0x2cfd(%rip),%r12
jmp    130c <main+0x13c>
nopl   (%rax)
mov    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%edi
call   1030 <WUR@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %eax,%edi
call   1030 <WUR@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     12e0 <main+0x110>
jns    132c <main+0x15c>
mov    (%r14),%rax
cmp    (%r12),%rax
je     130c <main+0x13c>
xor    %eax,%eax
call   1090 <waitchild@plt>
mov    %eax,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"Usage: %s (+|-) (filename|fd/) command [args]\0A\00"
@.str.1 = [26 x i8] c"arg '%s' not '-' nor '+'\0A\00"
@.str.2 = [6 x i8] c"pipe:\00"
@.str.3 = [6 x i8] c"fork:\00"
@.str.4 = [21 x i8] c"Cannot execute '%s':\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca [4096 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 4
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %12, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 45
br i1 %24, label %25, label %33
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 43
br i1 %32, label %41, label %33
33: ; preds = %25, %17
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %33, %25
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %41, %33
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 2
%49 = load i8*, i8** %48
%50 = call i32 @getfd(i8* %49)
store i32 %50, i32* %6
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%52 = call i64 @pipe(i32* %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %46
%55 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %54, %46
%57 = call i32 (...) @fork()
switch i32 %57, label %99 [
i32 -1, label %58
i32 0, label %60
]
58: ; preds = %56
%59 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %56, %58
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%67 = load i32, i32* %66
%68 = call i32 @dup2(i32 %67, i32 2)
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%70 = load i32, i32* %69
%71 = icmp ne i32 %70, 2
br i1 %71, label %72, label %76
72: ; preds = %60
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%74 = load i32, i32* %73
%75 = call i32 @close(i32 %74)
br label %76
76: ; preds = %72, %60
%77 = load i8**, i8*** %5
%78 = getelementptr inbounds i8*, i8** %77, i64 1
%79 = load i8*, i8** %78
%80 = getelementptr inbounds i8, i8* %79, i64 0
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = icmp eq i32 %82, 43
br i1 %83, label %84, label %88
84: ; preds = %76
%85 = load i8**, i8*** %5
%86 = getelementptr inbounds i8*, i8** %85, i64 3
%87 = call i32 @printcmdline(i8** %86)
br label %88
88: ; preds = %84, %76
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 3
%91 = load i8*, i8** %90
%92 = load i8**, i8*** %5
%93 = getelementptr inbounds i8*, i8** %92, i64 3
%94 = call i32 @execvp(i8* %91, i8** %93)
%95 = load i8**, i8*** %5
%96 = getelementptr inbounds i8*, i8** %95, i64 3
%97 = load i8*, i8** %96
%98 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %97)
br label %99
99: ; preds = %88, %56
%100 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%101 = load i32, i32* %100
%102 = call i32 @close(i32 %101)
br label %103
103: ; preds = %99, %110, %127
%104 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%105 = load i32, i32* %104
%106 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%107 = call i32 @read(i32 %105, i8* %106, i32 4096)
store i32 %107, i32* %9
%108 = load i32, i32* %9
%109 = icmp sgt i32 %108, 0
br i1 %109, label %110, label %120
110: ; preds = %103
%111 = load i32, i32* %6
%112 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%113 = load i32, i32* %9
%114 = call i32 @write(i32 %111, i8* %112, i32 %113)
%115 = call i32 @WUR(i32 %114)
%116 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%117 = load i32, i32* %9
%118 = call i32 @write(i32 2, i8* %116, i32 %117)
%119 = call i32 @WUR(i32 %118)
br label %103
120: ; preds = %103
%121 = load i32, i32* %9
%122 = icmp slt i32 %121, 0
br i1 %122, label %123, label %129
123: ; preds = %120
%124 = load i64, i64* @errno
%125 = load i64, i64* @EINTR
%126 = icmp eq i64 %124, %125
br i1 %126, label %127, label %128
127: ; preds = %123
br label %103
128: ; preds = %123
br label %129
129: ; preds = %128, %120
%130 = call i32 (...) @waitchild()
%131 = call i32 @exit(i32 %130)
unreachable
}

LLVM IR (-O2):
@.str = [47 x i8] c"Usage: %s (+|-) (filename|fd/) command [args]\0A\00"
@.str.1 = [26 x i8] c"arg '%s' not '-' nor '+'\0A\00"
@.str.2 = [6 x i8] c"pipe:\00"
@.str.3 = [6 x i8] c"fork:\00"
@.str.4 = [21 x i8] c"Cannot execute '%s':\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [2 x i32]
%4 = alloca [4096 x i8]
%5 = icmp slt i32 %0, 4
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %9
9: ; preds = %6, %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i8, i8* %11
switch i8 %12, label %17 [
i8 45, label %13
i8 43, label %13
]
13: ; preds = %9, %9
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %9, %13
%18 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %19
19: ; preds = %17, %13
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32 @getfd(i8* %21)
%23 = bitcast [2 x i32]* %3 to i8*
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%25 = call i64 @pipe(i32* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %19
%30 = call i32 (...) @fork()
switch i32 %30, label %57 [
i32 -1, label %31
i32 0, label %33
]
31: ; preds = %29
%32 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %29, %31
%34 = call i32 @close(i32 %22)
%35 = load i32, i32* %24
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @dup2(i32 %38, i32 2)
%40 = load i32, i32* %37
%41 = icmp eq i32 %40, 2
br i1 %41, label %44, label %42
42: ; preds = %33
%43 = call i32 @close(i32 %40)
br label %44
44: ; preds = %42, %33
%45 = load i8*, i8** %10
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 43
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = getelementptr inbounds i8*, i8** %1, i64 3
%50 = call i32 @printcmdline(i8** %49)
br label %51
51: ; preds = %48, %44
%52 = getelementptr inbounds i8*, i8** %1, i64 3
%53 = load i8*, i8** %52
%54 = call i32 @execvp(i8* %53, i8** %52)
%55 = load i8*, i8** %52
%56 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %51, %29
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
br label %62
62: ; preds = %77, %57
%63 = load i32, i32* %24
%64 = call i32 @read(i32 %63, i8* %61, i32 4096)
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %62
%67 = call i32 @write(i32 %22, i8* %61, i32 %64)
%68 = call i32 @WUR(i32 %67)
%69 = call i32 @write(i32 2, i8* %61, i32 %64)
%70 = call i32 @WUR(i32 %69)
br label %77
71: ; preds = %62
%72 = icmp slt i32 %64, 0
br i1 %72, label %73, label %78
73: ; preds = %71
%74 = load i64, i64* @errno
%75 = load i64, i64* @EINTR
%76 = icmp eq i64 %74, %75
br i1 %76, label %77, label %78
77: ; preds = %73, %66
br label %62
78: ; preds = %73, %71
%79 = call i32 (...) @waitchild()
%80 = call i32 @exit(i32 %79)
unreachable
}