#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 O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *, char const *, ...);
int /*<<< orphan*/ execlp(char const *, char const *, char *);
int fork();
int open(char const *, int, int);
scalar_t__ pipe(int *);
int pipe_open(const char *executable, const char *filename, int pipe_input,
              pid_t *child_pid) {
  int file_fd = -1;
  pid_t pid;
  int parent_pipe[2];
  if (pipe(parent_pipe) != 0)
    err(1, "pipe (for '%s') failed", executable);
  if (filename != NULL) {
    file_fd =
        open(filename, pipe_input ? (O_RDONLY) : (O_CREAT | O_WRONLY), 0666);
    if (file_fd == -1)
      err(1, "failed to %s file '%s'", pipe_input ? "open" : "create",
          filename);
  }
  pid = fork();
  if (pid == -1)
    err(1, "Fork failed for '%s'", executable);
  if (pid > 0) {
    *child_pid = pid;
    close(file_fd);
    close(parent_pipe[pipe_input ? 1 : 0]);
    return (parent_pipe[pipe_input ? 0 : 1]);
  }
  if (pipe_input) {
    dup2(parent_pipe[1], STDOUT_FILENO);
    close(parent_pipe[0]);
    if (file_fd != -1)
      dup2(file_fd, STDIN_FILENO);
  } else {
    dup2(parent_pipe[0], STDIN_FILENO);
    close(parent_pipe[1]);
    if (file_fd != -1)
      dup2(file_fd, STDOUT_FILENO);
  }
  execlp(executable, executable, (char *)NULL);
  err(1, "execlp(%s) failed", executable);
  return 0;
}


x86 assembly (clang, -O0):
<pipe_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
lea    -0x38(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11b8 <pipe_open+0x48>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
cmpq   $0x0,-0x18(%rbp)
je     124d <pipe_open+0xdd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11e6 <pipe_open+0x76>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    11fb <pipe_open+0x8b>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    0x2dc2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1248 <pipe_open+0xd8>
mov    -0x1c(%rbp),%ecx
lea    0xe10(%rip),%rdx
lea    0xe04(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
jmp    124d <pipe_open+0xdd>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1278 <pipe_open+0x108>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
cmpl   $0x0,-0x30(%rbp)
jle    12cc <pipe_open+0x15c>
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
cltq
mov    -0x38(%rbp,%rax,4),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
cltq
mov    -0x38(%rbp,%rax,4),%eax
mov    %eax,-0x4(%rbp)
jmp    1377 <pipe_open+0x207>
cmpl   $0x0,-0x1c(%rbp)
je     130f <pipe_open+0x19f>
mov    -0x34(%rbp),%edi
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
je     130a <pipe_open+0x19a>
mov    -0x2c(%rbp),%edi
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
jmp    1348 <pipe_open+0x1d8>
mov    -0x38(%rbp),%edi
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
je     1343 <pipe_open+0x1d3>
mov    -0x2c(%rbp),%edi
mov    0x2cac(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
jmp    1348 <pipe_open+0x1d8>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xce6(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
je     11ad <pipe_open+0x3d>
lea    0xe62(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    $0xffffffff,%ebx
test   %rbp,%rbp
je     121d <pipe_open+0xad>
test   %r12d,%r12d
je     11c7 <pipe_open+0x57>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
jmp    11d9 <pipe_open+0x69>
mov    0x2e2a(%rip),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    121d <pipe_open+0xad>
test   %r12d,%r12d
lea    0xe3c(%rip),%rax
lea    0xe30(%rip),%rdx
cmove  %rax,%rdx
lea    0xe0e(%rip),%rsi
mov    $0x1,%edi
mov    %rbp,%rcx
xor    %eax,%eax
call   1060 <err@plt>
mov    $0xffffffff,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     1256 <pipe_open+0xe6>
test   %eax,%eax
jle    126c <pipe_open+0xfc>
mov    %eax,(%r14)
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
xor    %eax,%eax
test   %r12d,%r12d
sete   %bl
setne  %al
mov    0x8(%rsp,%rax,4),%edi
call   1040 <close@plt>
mov    0x8(%rsp,%rbx,4),%ebx
jmp    12ef <pipe_open+0x17f>
lea    0xddd(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <err@plt>
test   %r12d,%r12d
je     129a <pipe_open+0x12a>
mov    0xc(%rsp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
je     12ca <pipe_open+0x15a>
mov    0x2d30(%rip),%rax
jmp    12c1 <pipe_open+0x151>
mov    0x8(%rsp),%edi
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
je     12ca <pipe_open+0x15a>
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
lea    0xd6f(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"pipe (for '%s') failed\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [23 x i8] c"failed to %s file '%s'\00"
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [7 x i8] c"create\00"
@.str.4 = [21 x i8] c"Fork failed for '%s'\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str.5 = [18 x i8] c"execlp(%s) failed\00"
define i32 @pipe_open(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2 x i32]
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 -1, i32* %10
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = call i32 (i32, i8*, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %16, %4
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %45
22: ; preds = %19
%23 = load i8*, i8** %7
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = load i32, i32* @O_RDONLY
br label %32
28: ; preds = %22
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_WRONLY
%31 = or i32 %29, %30
br label %32
32: ; preds = %28, %26
%33 = phi i32 [ %27, %26 ], [ %31, %28 ]
%34 = call i32 @open(i8* %23, i32 %33, i32 438)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i32, i32* %8
%39 = icmp ne i32 %38, 0
%40 = zext i1 %39 to i64
%41 = select i1 %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0)
%42 = load i8*, i8** %7
%43 = call i32 (i32, i8*, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %41, i8* %42)
br label %44
44: ; preds = %37, %32
br label %45
45: ; preds = %44, %19
%46 = call i32 (...) @fork()
store i32 %46, i32* %11
%47 = load i32, i32* %11
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i8*, i8** %6
%51 = call i32 (i32, i8*, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %45
%53 = load i32, i32* %11
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %75
55: ; preds = %52
%56 = load i32, i32* %11
%57 = load i32*, i32** %9
store i32 %56, i32* %57
%58 = load i32, i32* %10
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
%62 = zext i1 %61 to i64
%63 = select i1 %61, i32 1, i32 0
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 %64
%66 = load i32, i32* %65
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %8
%69 = icmp ne i32 %68, 0
%70 = zext i1 %69 to i64
%71 = select i1 %69, i32 0, i32 1
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 %72
%74 = load i32, i32* %73
store i32 %74, i32* %5
br label %114
75: ; preds = %52
%76 = load i32, i32* %8
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %93
78: ; preds = %75
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%80 = load i32, i32* %79
%81 = load i32, i32* @STDOUT_FILENO
%82 = call i32 @dup2(i32 %80, i32 %81)
%83 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%84 = load i32, i32* %83
%85 = call i32 @close(i32 %84)
%86 = load i32, i32* %10
%87 = icmp ne i32 %86, -1
br i1 %87, label %88, label %92
88: ; preds = %78
%89 = load i32, i32* %10
%90 = load i32, i32* @STDIN_FILENO
%91 = call i32 @dup2(i32 %89, i32 %90)
br label %92
92: ; preds = %88, %78
br label %108
93: ; preds = %75
%94 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%95 = load i32, i32* %94
%96 = load i32, i32* @STDIN_FILENO
%97 = call i32 @dup2(i32 %95, i32 %96)
%98 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%99 = load i32, i32* %98
%100 = call i32 @close(i32 %99)
%101 = load i32, i32* %10
%102 = icmp ne i32 %101, -1
br i1 %102, label %103, label %107
103: ; preds = %93
%104 = load i32, i32* %10
%105 = load i32, i32* @STDOUT_FILENO
%106 = call i32 @dup2(i32 %104, i32 %105)
br label %107
107: ; preds = %103, %93
br label %108
108: ; preds = %107, %92
%109 = load i8*, i8** %6
%110 = load i8*, i8** %6
%111 = call i32 @execlp(i8* %109, i8* %110, i8* null)
%112 = load i8*, i8** %6
%113 = call i32 (i32, i8*, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %112)
store i32 0, i32* %5
br label %114
114: ; preds = %108, %55
%115 = load i32, i32* %5
ret i32 %115
}

LLVM IR (-O2):
@.str = [23 x i8] c"pipe (for '%s') failed\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [23 x i8] c"failed to %s file '%s'\00"
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [7 x i8] c"create\00"
@.str.4 = [21 x i8] c"Fork failed for '%s'\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str.5 = [18 x i8] c"execlp(%s) failed\00"
define i32 @pipe_open(i8* %0, i8* %1, i32 %2, i32* nocapture writeonly %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 i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %4
%11 = call i32 (i32, i8*, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %4
%13 = icmp eq i8* %1, null
br i1 %13, label %26, label %14
14: ; preds = %12
%15 = icmp eq i32 %2, 0
%16 = load i32, i32* @O_RDONLY
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_WRONLY
%19 = or i32 %18, %17
%20 = select i1 %15, i32 %19, i32 %16
%21 = call i32 @open(i8* %1, i32 %20, i32 438)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = select i1 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%25 = call i32 (i32, i8*, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %1)
br label %26
26: ; preds = %14, %23, %12
%27 = phi i32 [ -1, %23 ], [ %21, %14 ], [ -1, %12 ]
%28 = call i32 (...) @fork()
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i32, i8*, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %45
32: ; preds = %26
%33 = icmp sgt i32 %28, 0
br i1 %33, label %34, label %45
34: ; preds = %32
store i32 %28, i32* %3
%35 = call i32 @close(i32 %27)
%36 = icmp eq i32 %2, 0
%37 = xor i1 %36, true
%38 = zext i1 %37 to i64
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %38
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = zext i1 %36 to i64
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %42
%44 = load i32, i32* %43
br label %70
45: ; preds = %30, %32
%46 = icmp eq i32 %2, 0
br i1 %46, label %55, label %47
47: ; preds = %45
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%49 = load i32, i32* %48
%50 = load i32, i32* @STDOUT_FILENO
%51 = call i32 @dup2(i32 %49, i32 %50)
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
%54 = icmp eq i32 %27, -1
br i1 %54, label %67, label %63
55: ; preds = %45
%56 = load i32, i32* %7
%57 = load i32, i32* @STDIN_FILENO
%58 = call i32 @dup2(i32 %56, i32 %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = icmp eq i32 %27, -1
br i1 %62, label %67, label %63
63: ; preds = %55, %47
%64 = phi i32* [ @STDIN_FILENO, %47 ], [ @STDOUT_FILENO, %55 ]
%65 = load i32, i32* %64
%66 = call i32 @dup2(i32 %27, i32 %65)
br label %67
67: ; preds = %63, %55, %47
%68 = call i32 @execlp(i8* %0, i8* %0, i8* null)
%69 = call i32 (i32, i8*, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %70
70: ; preds = %67, %34
%71 = phi i32 [ %44, %34 ], [ 0, %67 ]
ret i32 %71
}