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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, char *, char const *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fork();
int pipe(int *);
FILE **p2open(const char *cmd, FILE **fpbuf) {
  int ERROR = -1;
  FILE **result = NULL;
  int fd1[2], fd2[2];
  int ispipe1 = ERROR, ispipe2 = ERROR;
  pid_t pid;
  if ((ispipe1 = pipe(fd1)) == -1 || (ispipe2 = pipe(fd2)) == -1) {
    return NULL;
  }
  if ((pid = fork())) {
    if ((fpbuf[0] = fdopen(fd1[0], "r")) == NULL ||
        (fpbuf[1] = fdopen(fd2[1], "w")) == NULL) {
      result = NULL;
    } else {
      result = fpbuf;
    }
  } else {
    dup2(fd2[0], 0);
    dup2(fd1[1], 1);
    close(fd1[0]);
    close(fd1[1]);
    close(fd2[0]);
    close(fd2[1]);
    execlp("/bin/sh", "sh", "-c", cmd, NULL);
  }
  if (ispipe1 != ERROR) {
    close(fd1[1]);
    if (!result) {
      close(fd1[0]);
    }
  }
  if (ispipe2 != ERROR) {
    close(fd2[0]);
    if (!result) {
      close(fd2[1]);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<p2open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
je     11b5 <p2open+0x55>
lea    -0x38(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x40(%rbp)
cmp    $0xffffffff,%eax
jne    11c2 <p2open+0x62>
movq   $0x0,-0x8(%rbp)
jmp    12ea <p2open+0x18a>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1230 <p2open+0xd0>
mov    -0x30(%rbp),%edi
lea    0xe21(%rip),%rsi
call   1060 <fdopen@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
je     1216 <p2open+0xb6>
mov    -0x34(%rbp),%edi
lea    0xe03(%rip),%rsi
call   1060 <fdopen@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,0x8(%rcx)
cmp    $0x0,%rax
jne    1223 <p2open+0xc3>
movq   $0x0,-0x28(%rbp)
jmp    122b <p2open+0xcb>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    128a <p2open+0x12a>
mov    -0x38(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rcx
lea    0xd92(%rip),%rdi
lea    0xd90(%rip),%rsi
lea    0xd8c(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <execlp@plt>
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12b6 <p2open+0x156>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12b1 <p2open+0x151>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
jmp    12b6 <p2open+0x156>
mov    -0x40(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12e2 <p2open+0x182>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12dd <p2open+0x17d>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
jmp    12e2 <p2open+0x182>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p2open>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11dd <p2open+0x7d>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11dd <p2open+0x7d>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11e4 <p2open+0x84>
mov    0x10(%rsp),%edi
lea    0xe5f(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1241 <p2open+0xe1>
mov    0xc(%rsp),%edi
lea    0xe45(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,0x8(%rbx)
test   %rax,%rax
je     1241 <p2open+0xe1>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
jmp    1262 <p2open+0x102>
xor    %ebx,%ebx
jmp    1267 <p2open+0x107>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x14(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xddc(%rip),%rdi
lea    0xdda(%rip),%rsi
lea    0xdd6(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1070 <execlp@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
define i32** @p2open(i8* %0, i32** writeonly %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 i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %62, label %10
10: ; preds = %2
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %62, label %14
14: ; preds = %10
%15 = call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %14
%18 = load i32, i32* %7
%19 = call i32* @fdopen(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %1
%20 = icmp eq i32* %19, null
br i1 %20, label %48, label %21
21: ; preds = %17
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32* @fdopen(i32 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = getelementptr inbounds i32*, i32** %1, i64 1
store i32* %24, i32** %25
%26 = icmp eq i32* %24, null
br i1 %26, label %48, label %43
27: ; preds = %14
%28 = load i32, i32* %11
%29 = call i32 @dup2(i32 %28, i32 0)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @dup2(i32 %31, i32 1)
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %30
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %11
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, 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* %0, i32* null)
br label %48
43: ; preds = %21
%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* %11
br label %58
48: ; preds = %21, %17, %27
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %11
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%57 = load i32, i32* %56
br label %58
58: ; preds = %48, %43
%59 = phi i32 [ %47, %43 ], [ %57, %48 ]
%60 = phi i32** [ %1, %43 ], [ null, %48 ]
%61 = call i32 @close(i32 %59)
br label %62
62: ; preds = %58, %2, %10
%63 = phi i32** [ null, %10 ], [ null, %2 ], [ %60, %58 ]
ret i32** %63
}