#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, char *, char const *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
pid_t popen2(const char *command, int *infp, int *outfp) {
  int p_stdin[2], p_stdout[2];
  pid_t pid;
  if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
    return -1;
  pid = fork();
  if (pid < 0)
    return pid;
  else if (pid == 0) {
    close(p_stdin[1]);
    dup2(p_stdin[0], 0);
    close(p_stdout[0]);
    dup2(p_stdout[1], 1);
    execl("/bin/sh", "sh", "-c", command, NULL);
    perror("execl");
    exit(1);
  }
  if (infp == NULL)
    close(p_stdin[1]);
  else
    *infp = p_stdin[1];
  if (outfp == NULL)
    close(p_stdout[0]);
  else
    *outfp = p_stdout[0];
  close(p_stdin[0]);
  close(p_stdout[1]);
  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    11aa <popen2+0x3a>
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11b6 <popen2+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <popen2+0x12c>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11d5 <popen2+0x65>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129c <popen2+0x12c>
cmpl   $0x0,-0x34(%rbp)
jne    123f <popen2+0xcf>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rcx
lea    0xdef(%rip),%rdi
lea    0xded(%rip),%rsi
lea    0xde9(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
lea    0xddb(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1244 <popen2+0xd4>
cmpq   $0x0,-0x18(%rbp)
jne    125c <popen2+0xec>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
jmp    1265 <popen2+0xf5>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
jne    127d <popen2+0x10d>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
jmp    1286 <popen2+0x116>
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%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    11ec <popen2+0x7c>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    11ec <popen2+0x7c>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11ec <popen2+0x7c>
je     11f9 <popen2+0x89>
mov    0x14(%rsp),%edi
test   %rbx,%rbx
je     11c2 <popen2+0x52>
mov    %edi,(%rbx)
jmp    11c7 <popen2+0x57>
call   1040 <close@plt>
mov    0x8(%rsp),%edi
test   %r15,%r15
je     11d5 <popen2+0x65>
mov    %edi,(%r15)
jmp    11da <popen2+0x6a>
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xdd5(%rip),%rdi
lea    0xdd3(%rip),%rsi
lea    0xdcf(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
lea    0xdc0(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@.str.3 = [6 x i8] c"execl\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 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 %76
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 %76
25: ; preds = %19
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %45
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]* %8, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @dup2(i32 %33, i32 0)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @dup2(i32 %39, i32 1)
%41 = load i8*, i8** %5
%42 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32* null)
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %25
br label %46
46: ; preds = %45
%47 = load i32*, i32** %6
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
br label %57
53: ; preds = %46
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%55 = load i32, i32* %54
%56 = load i32*, i32** %6
store i32 %55, i32* %56
br label %57
57: ; preds = %53, %49
%58 = load i32*, i32** %7
%59 = icmp eq i32* %58, null
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
br label %68
64: ; preds = %57
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%66 = load i32, i32* %65
%67 = load i32*, i32** %7
store i32 %66, i32* %67
br label %68
68: ; preds = %64, %60
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %10
store i32 %75, i32* %4
br label %76
76: ; preds = %68, %23, %18
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@.str.3 = [6 x i8] c"execl\00"
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 %53
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 %53
15: ; preds = %11
%16 = call i32 (...) @fork()
%17 = icmp slt i32 %16, 0
br i1 %17, label %53, label %18
18: ; preds = %15
%19 = icmp eq i32 %16, 0
br i1 %19, label %20, label %34
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* %8
%25 = call i32 @dup2(i32 %24, i32 0)
%26 = load i32, i32* %12
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @dup2(i32 %29, i32 1)
%31 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32* null)
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %18
%35 = icmp eq i32* %1, null
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%37 = load i32, i32* %36
br i1 %35, label %38, label %40
38: ; preds = %34
%39 = call i32 @close(i32 %37)
br label %41
40: ; preds = %34
store i32 %37, i32* %1
br label %41
41: ; preds = %40, %38
%42 = icmp eq i32* %2, null
%43 = load i32, i32* %12
br i1 %42, label %44, label %46
44: ; preds = %41
%45 = call i32 @close(i32 %43)
br label %47
46: ; preds = %41
store i32 %43, i32* %2
br label %47
47: ; preds = %46, %44
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %15, %3, %11, %47
%54 = phi i32 [ %16, %47 ], [ -1, %11 ], [ -1, %3 ], [ %16, %15 ]
ret i32 %54
}