#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ pipe(int *);
pid_t popen2(char *const *command, int *stdin, int *stdout) {
  int p_stdin[2], p_stdout[2];
  if (pipe(p_stdin) || pipe(p_stdout))
    return -1;
  pid_t pid = fork();
  if (pid < 0)
    return pid;
  if (pid == 0) {
    close(p_stdin[1]);
    close(p_stdout[0]);
    dup2(p_stdin[0], STDIN_FILENO);
    dup2(p_stdout[1], STDOUT_FILENO);
    dup2(p_stdout[1], STDERR_FILENO);
    execvp(*command, command);
    exit(EXIT_FAILURE);
  }
  close(p_stdin[0]);
  close(p_stdout[1]);
  if (stdin)
    *stdin = p_stdin[1];
  else
    close(p_stdin[1]);
  if (stdout)
    *stdout = p_stdout[0];
  else
    close(p_stdout[0]);
  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    $0x0,%rax
jne    119a <popen2+0x3a>
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11a6 <popen2+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <popen2+0x128>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11c5 <popen2+0x65>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <popen2+0x128>
cmpl   $0x0,-0x34(%rbp)
jne    1230 <popen2+0xd0>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execvp@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x18(%rbp)
je     1259 <popen2+0xf9>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1261 <popen2+0x101>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x20(%rbp)
je     127a <popen2+0x11a>
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1282 <popen2+0x122>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
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,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1196 <popen2+0x36>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
je     11a3 <popen2+0x43>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1196 <popen2+0x36>
je     11eb <popen2+0x8b>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
test   %rbx,%rbx
je     11d1 <popen2+0x71>
mov    %edi,(%rbx)
jmp    11d6 <popen2+0x76>
call   1040 <close@plt>
mov    0x8(%rsp),%edi
test   %r15,%r15
je     11e4 <popen2+0x84>
mov    %edi,(%r15)
jmp    1196 <popen2+0x36>
call   1040 <close@plt>
jmp    1196 <popen2+0x36>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
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 i64 @pipe(i32* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%16 = call i64 @pipe(i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14, %3
store i32 -1, i32* %4
br label %83
19: ; preds = %14
%20 = call i32 (...) @fork()
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i32, i32* %10
store i32 %24, i32* %4
br label %83
25: ; preds = %19
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %53
28: ; preds = %25
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%36 = load i32, i32* %35
%37 = load i32, i32* @STDIN_FILENO
%38 = call i32 @dup2(i32 %36, i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%40 = load i32, i32* %39
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i32 @dup2(i32 %40, i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%44 = load i32, i32* %43
%45 = load i32, i32* @STDERR_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = load i8**, i8*** %5
%48 = load i8*, i8** %47
%49 = load i8**, i8*** %5
%50 = call i32 @execvp(i8* %48, i8** %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %25
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = load i32*, i32** %6
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %66
62: ; preds = %53
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%64 = load i32, i32* %63
%65 = load i32*, i32** %6
store i32 %64, i32* %65
br label %70
66: ; preds = %53
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
br label %70
70: ; preds = %66, %62
%71 = load i32*, i32** %7
%72 = icmp ne i32* %71, null
br i1 %72, label %73, label %77
73: ; preds = %70
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%75 = load i32, i32* %74
%76 = load i32*, i32** %7
store i32 %75, i32* %76
br label %81
77: ; preds = %70
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%79 = load i32, i32* %78
%80 = call i32 @close(i32 %79)
br label %81
81: ; preds = %77, %73
%82 = load i32, i32* %10
store i32 %82, i32* %4
br label %83
83: ; preds = %81, %23, %18
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @popen2(i8** %0, i32* writeonly %1, i32* 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 i64 @pipe(i32* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %58
11: ; preds = %3
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%13 = call i64 @pipe(i32* %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %58
15: ; preds = %11
%16 = call i32 (...) @fork()
%17 = icmp slt i32 %16, 0
br i1 %17, label %58, label %18
18: ; preds = %15
%19 = icmp eq i32 %16, 0
br i1 %19, label %20, label %40
20: ; preds = %18
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %12
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %8
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%30 = load i32, i32* %29
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = load i32, i32* %29
%34 = load i32, i32* @STDERR_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
%36 = load i8*, i8** %0
%37 = call i32 @execvp(i8* %36, i8** %0)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %18
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = icmp eq i32* %1, null
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%48 = load i32, i32* %47
br i1 %46, label %50, label %49
49: ; preds = %40
store i32 %48, i32* %1
br label %52
50: ; preds = %40
%51 = call i32 @close(i32 %48)
br label %52
52: ; preds = %50, %49
%53 = icmp eq i32* %2, null
%54 = load i32, i32* %12
br i1 %53, label %56, label %55
55: ; preds = %52
store i32 %54, i32* %2
br label %58
56: ; preds = %52
%57 = call i32 @close(i32 %54)
br label %58
58: ; preds = %15, %56, %55, %3, %11
%59 = phi i32 [ -1, %11 ], [ -1, %3 ], [ %16, %55 ], [ %16, %56 ], [ %16, %15 ]
ret i32 %59
}