#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ close(int);
int dup2(int, int);
scalar_t__ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
scalar_t__ setvbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/, int /*<<< orphan*/);
pid_t cpopen(FILE **fp, char *prog) {
  int fd1[2], fd2[2];
  pid_t pid;
  if (pipe(fd1) < 0 || pipe(fd2) < 0)
    return -1;
  if ((pid = fork()) < 0)
    return -2;
  else if (pid > 0) {
    close(fd1[0]);
    close(fd2[1]);
    if (!(fp[0] = fdopen(fd2[0], "r")))
      return -4;
    if (!(fp[1] = fdopen(fd1[1], "w")))
      return -4;
    if (setvbuf(fp[0], NULL, _IONBF, 0) != 0)
      return -3;
    if (setvbuf(fp[1], NULL, _IONBF, 0) != 0)
      return -3;
    return pid;
  } else {
    close(fd1[1]);
    close(fd2[0]);
    if (fd1[0] != STDIN_FILENO) {
      if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
        perror("dup2");
      close(fd1[0]);
    }
    if (fd2[1] != STDOUT_FILENO) {
      if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
        perror("dup2");
      close(fd2[1]);
    }
    if (execlp("sh", "sh", "-c", prog, NULL) < 0) {
      perror("exec");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<cpopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jl     11b6 <cpopen+0x36>
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11c2 <cpopen+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <cpopen+0x224>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11e1 <cpopen+0x61>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13a4 <cpopen+0x224>
cmpl   $0x0,-0x2c(%rbp)
jle    12c2 <cpopen+0x142>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
lea    0xdfb(%rip),%rsi
call   1060 <fdopen@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1227 <cpopen+0xa7>
movl   $0xfffffffc,-0x4(%rbp)
jmp    13a4 <cpopen+0x224>
mov    -0x1c(%rbp),%edi
lea    0xdd1(%rip),%rsi
call   1060 <fdopen@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,0x8(%rcx)
cmp    $0x0,%rax
jne    1254 <cpopen+0xd4>
movl   $0xfffffffc,-0x4(%rbp)
jmp    13a4 <cpopen+0x224>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1070 <setvbuf@plt>
cmp    $0x0,%rax
je     1285 <cpopen+0x105>
movl   $0xfffffffd,-0x4(%rbp)
jmp    13a4 <cpopen+0x224>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d4c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1070 <setvbuf@plt>
cmp    $0x0,%rax
je     12b7 <cpopen+0x137>
movl   $0xfffffffd,-0x4(%rbp)
jmp    13a4 <cpopen+0x224>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13a4 <cpopen+0x224>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
mov    0x2cf4(%rip),%rcx
cmp    (%rcx),%eax
je     1318 <cpopen+0x198>
mov    -0x20(%rbp),%edi
mov    0x2ce2(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x2cd4(%rip),%rcx
cmp    (%rcx),%eax
je     1310 <cpopen+0x190>
lea    0xcf9(%rip),%rdi
call   1080 <perror@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
mov    0x2cce(%rip),%rcx
cmp    (%rcx),%eax
je     135e <cpopen+0x1de>
mov    -0x24(%rbp),%edi
mov    0x2cbc(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x2cae(%rip),%rcx
cmp    (%rcx),%eax
je     1356 <cpopen+0x1d6>
lea    0xcb3(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rcx
lea    0xca0(%rip),%rsi
lea    0xc9c(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1090 <execlp@plt>
cmp    $0x0,%rax
jge    1393 <cpopen+0x213>
lea    0xc81(%rip),%rdi
call   1080 <perror@plt>
jmp    1398 <cpopen+0x218>
jmp    139d <cpopen+0x21d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     12ea <cpopen+0x16a>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     12ea <cpopen+0x16a>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     1268 <cpopen+0xe8>
je     126f <cpopen+0xef>
mov    %eax,%r15d
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
lea    0xe10(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,(%rbx)
mov    $0xfffffffc,%ebp
test   %rax,%rax
je     12ea <cpopen+0x16a>
mov    0x14(%rsp),%edi
lea    0xdf1(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,0x8(%rbx)
test   %rax,%rax
je     12ea <cpopen+0x16a>
mov    (%rbx),%rdi
mov    0x2db3(%rip),%r14
mov    (%r14),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1070 <setvbuf@plt>
mov    $0xfffffffd,%ebp
test   %rax,%rax
jne    12ea <cpopen+0x16a>
mov    0x8(%rbx),%rdi
mov    (%r14),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1070 <setvbuf@plt>
test   %rax,%rax
mov    $0xfffffffd,%ebp
cmove  %r15d,%ebp
jmp    12ea <cpopen+0x16a>
mov    $0xfffffffe,%ebp
jmp    12ea <cpopen+0x16a>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
mov    0x2d44(%rip),%rbx
mov    (%rbx),%esi
cmp    %esi,%edi
je     12a4 <cpopen+0x124>
call   1030 <dup2@plt>
cmp    (%rbx),%eax
jne    1305 <cpopen+0x185>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2d41(%rip),%rbx
mov    (%rbx),%esi
cmp    %esi,%edi
je     12c7 <cpopen+0x147>
call   1030 <dup2@plt>
cmp    (%rbx),%eax
jne    1313 <cpopen+0x193>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xd3b(%rip),%rdi
lea    0xd37(%rip),%rdx
xor    %ebp,%ebp
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   1090 <execlp@plt>
test   %rax,%rax
js     12f7 <cpopen+0x177>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd11(%rip),%rdi
call   1080 <perror@plt>
jmp    12ea <cpopen+0x16a>
lea    0xcf8(%rip),%rdi
call   1080 <perror@plt>
jmp    129b <cpopen+0x11b>
lea    0xcea(%rip),%rdi
call   1080 <perror@plt>
jmp    12be <cpopen+0x13e>

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

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