#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int popen2(char **args, FILE **fpin, FILE **fpout) {
  int pin[2], pout[2];
  pid_t pid;
  if (pipe(pin) == -1) {
    perror("pipe");
    return (-1);
  }
  if (pipe(pout) == -1) {
    perror("pipe");
    return (-1);
  }
  pid = fork();
  if (pid == -1) {
    perror("fork");
    return (-1);
  }
  if (pid == 0) {
    dup2(pin[0], STDIN_FILENO);
    close(pin[0]);
    close(pin[1]);
    dup2(pout[1], STDOUT_FILENO);
    close(pout[1]);
    close(pout[0]);
    execvp(args[0], args);
    return (-1);
  }
  close(pin[0]);
  close(pout[1]);
  *fpin = fdopen(pin[1], "w");
  *fpout = fdopen(pout[0], "r");
  return (pid);
}


x86 assembly (clang, -O0):
<popen2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11ae <popen2+0x3e>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <popen2+0x144>
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d8 <popen2+0x68>
lea    0xe39(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <popen2+0x144>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1204 <popen2+0x94>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <popen2+0x144>
cmpl   $0x0,-0x34(%rbp)
jne    126c <popen2+0xfc>
mov    -0x28(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <execvp@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <popen2+0x144>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
lea    0xd84(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%edi
lea    0xd6d(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popen2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     124e <popen2+0xde>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     124e <popen2+0xde>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11f9 <popen2+0x89>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1257 <popen2+0xe7>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
lea    0xe2e(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,(%r15)
mov    0x8(%rsp),%edi
lea    0xe1d(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,(%r14)
jmp    1268 <popen2+0xf8>
mov    0x10(%rsp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1080 <execvp@plt>
jmp    1263 <popen2+0xf3>
lea    0xdab(%rip),%rdi
jmp    125e <popen2+0xee>
lea    0xda7(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [2 x i8] c"r\00"
define i32 @popen2(i8** %0, i32** nocapture writeonly %1, i32** nocapture writeonly %2) {
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = bitcast [2 x i32]* %4 to i8*
%7 = bitcast [2 x i32]* %5 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %53
13: ; preds = %3
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %53
19: ; preds = %13
%20 = call i32 (...) @fork()
switch i32 %20, label %42 [
i32 -1, label %21
i32 0, label %23
]
21: ; preds = %19
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %53
23: ; preds = %19
%24 = load i32, i32* %8
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = load i32, i32* %8
%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 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
%36 = load i32, i32* %32
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %14
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %0
%41 = call i32 @execvp(i8* %40, i8** %0)
br label %53
42: ; preds = %19
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32* @fdopen(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %50, i32** %1
%51 = load i32, i32* %14
%52 = call i32* @fdopen(i32 %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %52, i32** %2
br label %53
53: ; preds = %42, %23, %21, %17, %11
%54 = phi i32 [ -1, %11 ], [ -1, %17 ], [ -1, %21 ], [ -1, %23 ], [ %20, %42 ]
ret i32 %54
}