#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_IGN;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int pipe(int *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t pid;
  int tochild[2] = {-1, -1};
  int fromchild[2] = {-1, -1};
  if (argc < 2)
    _exit(111);
  if (!argv[0])
    _exit(111);
  if (!argv[1])
    _exit(111);
  ++argv;
  if (pipe(tochild) == -1)
    _exit(111);
  if (pipe(fromchild) == -1)
    _exit(111);
  pid = fork();
  if (pid == -1)
    _exit(111);
  if (pid == 0) {
    close(tochild[1]);
    close(fromchild[0]);
    close(1);
    if (dup2(tochild[0], 0) == -1)
      _exit(111);
    if (dup2(fromchild[1], 1) == -1)
      _exit(111);
    execvp(*argv, argv);
    _exit(111);
  }
  close(tochild[0]);
  close(fromchild[1]);
  close(0);
  if (dup2(fromchild[0], 0) == -1)
    _exit(111);
  close(1);
  if (dup2(tochild[1], 1) == -1)
    _exit(111);
  signal(SIGPIPE, SIG_IGN);
  _exit(111);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe73(%rip),%rax
mov    %rax,-0x1c(%rbp)
mov    0xe68(%rip),%rax
mov    %rax,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b0 <main+0x40>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11c8 <main+0x58>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11e1 <main+0x71>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1209 <main+0x99>
mov    $0x6f,%edi
call   1030 <_exit@plt>
lea    -0x24(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1225 <main+0xb5>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1243 <main+0xd3>
mov    $0x6f,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    12be <main+0x14e>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1284 <main+0x114>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12a4 <main+0x134>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <execvp@plt>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12f2 <main+0x182>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    131c <main+0x1ac>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
mov    0x2cbc(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x6f,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movq   $0xffffffffffffffff,0x8(%rsp)
ff ff
movq   $0xffffffffffffffff,(%rsp)
ff
cmp    $0x1,%edi
jle    1296 <main+0x126>
mov    %rsi,%rbx
cmpq   $0x0,(%rsi)
je     1296 <main+0x126>
cmpq   $0x0,0x8(%rbx)
je     1296 <main+0x126>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0x126>
mov    %rsp,%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0x126>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1249 <main+0xd9>
cmp    $0xffffffff,%eax
je     1296 <main+0x126>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0x126>
mov    $0x1,%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0x126>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x6f,%edi
call   1030 <_exit@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0x126>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0x126>
add    $0x8,%rbx
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1080 <execvp@plt>
mov    $0x6f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@__const.main.tochild = [2 x i32] [i32 -1, i32 -1]
@__const.main.fromchild = [2 x i32] [i32 -1, i32 -1]
@SIGPIPE = global i32 0
@SIG_IGN = global i32 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 [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [2 x i32]* %7 to i8*
%10 = bitcast [2 x i32]* %8 to i8*
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @_exit(i32 111)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = call i32 @_exit(i32 111)
unreachable
22: ; preds = %15
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = icmp ne i8* %25, null
br i1 %26, label %29, label %27
27: ; preds = %22
%28 = call i32 @_exit(i32 111)
unreachable
29: ; preds = %22
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i32 1
store i8** %31, i8*** %5
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%33 = call i32 @pipe(i32* %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @_exit(i32 111)
unreachable
37: ; preds = %29
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%39 = call i32 @pipe(i32* %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @_exit(i32 111)
unreachable
43: ; preds = %37
%44 = call i32 (...) @fork()
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @_exit(i32 111)
unreachable
49: ; preds = %43
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %79
52: ; preds = %49
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = call i32 @close(i32 1)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @dup2(i32 %61, i32 0)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %52
%65 = call i32 @_exit(i32 111)
unreachable
66: ; preds = %52
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @dup2(i32 %68, i32 1)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 @_exit(i32 111)
unreachable
73: ; preds = %66
%74 = load i8**, i8*** %5
%75 = load i8*, i8** %74
%76 = load i8**, i8*** %5
%77 = call i32 @execvp(i8* %75, i8** %76)
%78 = call i32 @_exit(i32 111)
unreachable
79: ; preds = %49
%80 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%81 = load i32, i32* %80
%82 = call i32 @close(i32 %81)
%83 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%84 = load i32, i32* %83
%85 = call i32 @close(i32 %84)
%86 = call i32 @close(i32 0)
%87 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%88 = load i32, i32* %87
%89 = call i32 @dup2(i32 %88, i32 0)
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %93
91: ; preds = %79
%92 = call i32 @_exit(i32 111)
unreachable
93: ; preds = %79
%94 = call i32 @close(i32 1)
%95 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%96 = load i32, i32* %95
%97 = call i32 @dup2(i32 %96, i32 1)
%98 = icmp eq i32 %97, -1
br i1 %98, label %99, label %101
99: ; preds = %93
%100 = call i32 @_exit(i32 111)
unreachable
101: ; preds = %93
%102 = load i32, i32* @SIGPIPE
%103 = load i32, i32* @SIG_IGN
%104 = call i32 @signal(i32 %102, i32 %103)
%105 = call i32 @_exit(i32 111)
unreachable
}

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