#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]);
    if (infp == NULL)
      close(p_stdin[0]);
    else
      dup2(p_stdin[0], 0);
    close(p_stdout[0]);
    if (outfp == NULL)
      close(p_stdout[1]);
    else
      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    12cc <popen2+0x15c>
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    12cc <popen2+0x15c>
cmpl   $0x0,-0x34(%rbp)
jne    126f <popen2+0xff>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11ff <popen2+0x8f>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
jmp    1209 <popen2+0x99>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1229 <popen2+0xb9>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
jmp    1236 <popen2+0xc6>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rcx
lea    0xdbf(%rip),%rdi
lea    0xdbd(%rip),%rsi
lea    0xdb9(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
lea    0xdab(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1274 <popen2+0x104>
cmpq   $0x0,-0x18(%rbp)
jne    128c <popen2+0x11c>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
jmp    1295 <popen2+0x125>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
jne    12ad <popen2+0x13d>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
jmp    12b6 <popen2+0x146>
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
test   %rbx,%rbx
jne    1212 <popen2+0xa2>
call   1040 <close@plt>
jmp    1219 <popen2+0xa9>
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
test   %r15,%r15
jne    1232 <popen2+0xc2>
call   1040 <close@plt>
jmp    123c <popen2+0xcc>
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xdbd(%rip),%rdi
lea    0xdbb(%rip),%rsi
lea    0xdb7(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
lea    0xda8(%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 %92
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 %92
25: ; preds = %19
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %61
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 = load i32*, i32** %6
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
br label %42
38: ; preds = %28
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @dup2(i32 %40, i32 0)
br label %42
42: ; preds = %38, %34
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = load i32*, i32** %7
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
br label %56
52: ; preds = %42
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @dup2(i32 %54, i32 1)
br label %56
56: ; preds = %52, %48
%57 = load i8*, i8** %5
%58 = 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* %57, i32* null)
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %25
br label %62
62: ; preds = %61
%63 = load i32*, i32** %6
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %69
65: ; preds = %62
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
br label %73
69: ; preds = %62
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%71 = load i32, i32* %70
%72 = load i32*, i32** %6
store i32 %71, i32* %72
br label %73
73: ; preds = %69, %65
%74 = load i32*, i32** %7
%75 = icmp eq i32* %74, null
br i1 %75, label %76, label %80
76: ; preds = %73
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%78 = load i32, i32* %77
%79 = call i32 @close(i32 %78)
br label %84
80: ; preds = %73
%81 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%82 = load i32, i32* %81
%83 = load i32*, i32** %7
store i32 %82, i32* %83
br label %84
84: ; preds = %80, %76
%85 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%86 = load i32, i32* %85
%87 = call i32 @close(i32 %86)
%88 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%89 = load i32, i32* %88
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* %10
store i32 %91, i32* %4
br label %92
92: ; preds = %84, %23, %18
%93 = load i32, i32* %4
ret i32 %93
}

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