#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*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execvp(char const *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int pipe(int *);
int popenRWE(int *rwepipe, const char *path) {
  int in[2];
  int out[2];
  int err[2];
  int pid;
  int rc;
  const char *argv[4] = {"sh", "-c", NULL, NULL};
  argv[2] = path;
  rc = pipe(in);
  if (rc < 0)
    goto error_in;
  rc = pipe(out);
  if (rc < 0)
    goto error_out;
  rc = pipe(err);
  if (rc < 0)
    goto error_err;
  pid = fork();
  if (pid > 0) {
    close(in[0]);
    close(out[1]);
    close(err[1]);
    rwepipe[0] = in[1];
    rwepipe[1] = out[0];
    rwepipe[2] = err[0];
    return pid;
  } else if (pid == 0) {
    close(in[1]);
    close(out[0]);
    close(err[0]);
    close(0);
    dup(in[0]);
    close(1);
    dup(out[1]);
    close(2);
    dup(err[1]);
    execvp(argv[0], (char **)argv);
    exit(1);
  } else
    goto error_fork;
  return pid;
error_fork:
  close(err[0]);
  close(err[1]);
error_err:
  close(out[0]);
  close(out[1]);
error_out:
  close(in[0]);
  close(in[1]);
error_in:
  return -1;
}


x86 assembly (clang, -O0):
<popenRWE>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2c89(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2c86(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    0x2c83(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c80(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11bf <popenRWE+0x5f>
jmp    12ea <popenRWE+0x18a>
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11da <popenRWE+0x7a>
jmp    12da <popenRWE+0x17a>
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11f5 <popenRWE+0x95>
jmp    12ca <popenRWE+0x16a>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    1249 <popenRWE+0xe9>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f1 <popenRWE+0x191>
cmpl   $0x0,-0x34(%rbp)
jne    12b5 <popenRWE+0x155>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1030 <dup@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1030 <dup@plt>
mov    -0x60(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1060 <execvp@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    12ba <popenRWE+0x15a>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popenRWE>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%r14
movaps 0x2c9e(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0x2c82(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
mov    %rsi,0x30(%rsp)
lea    0x18(%rsp),%rdi
call   1050 <pipe@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1236 <popenRWE+0xd6>
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
test   %eax,%eax
js     1224 <popenRWE+0xc4>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %eax,%eax
js     1212 <popenRWE+0xb2>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jle    11fe <popenRWE+0x9e>
mov    %eax,%ebp
mov    0x18(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x1c(%rsp),%eax
mov    %eax,(%r14)
mov    0x10(%rsp),%eax
mov    %eax,0x4(%r14)
mov    0x8(%rsp),%eax
mov    %eax,0x8(%r14)
mov    %ebp,%ebx
jmp    1236 <popenRWE+0xd6>
je     1241 <popenRWE+0xe1>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x18(%rsp),%edi
call   1040 <close@plt>
mov    0x1c(%rsp),%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x1c(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x18(%rsp),%edi
call   1030 <dup@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1030 <dup@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1030 <dup@plt>
lea    0xd67(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1060 <execvp@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

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