#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;
  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];
  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    1287 <popen2+0x117>
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    1287 <popen2+0x117>
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>
cmpq   $0x0,-0x18(%rbp)
jne    1257 <popen2+0xe7>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
jmp    1260 <popen2+0xf0>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
jne    1278 <popen2+0x108>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
jmp    1281 <popen2+0x111>
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%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,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11a6 <popen2+0x36>
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
je     11b3 <popen2+0x43>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11a6 <popen2+0x36>
je     11e9 <popen2+0x79>
mov    0x14(%rsp),%edi
test   %rbx,%rbx
je     11cf <popen2+0x5f>
mov    %edi,(%rbx)
jmp    11d4 <popen2+0x64>
call   1040 <close@plt>
mov    0x8(%rsp),%edi
test   %r15,%r15
je     11e2 <popen2+0x72>
mov    %edi,(%r15)
jmp    11a6 <popen2+0x36>
call   1040 <close@plt>
jmp    11a6 <popen2+0x36>
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    0xde5(%rip),%rdi
lea    0xde3(%rip),%rsi
lea    0xddf(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1080 <execl@plt>
lea    0xdd0(%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 %69
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 %69
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
%46 = load i32*, i32** %6
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
br label %56
52: ; preds = %45
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%54 = load i32, i32* %53
%55 = load i32*, i32** %6
store i32 %54, i32* %55
br label %56
56: ; preds = %52, %48
%57 = load i32*, i32** %7
%58 = icmp eq i32* %57, null
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
br label %67
63: ; preds = %56
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%65 = load i32, i32* %64
%66 = load i32*, i32** %7
store i32 %65, i32* %66
br label %67
67: ; preds = %63, %59
%68 = load i32, i32* %10
store i32 %68, i32* %4
br label %69
69: ; preds = %67, %23, %18
%70 = load i32, i32* %4
ret i32 %70
}

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 %47
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 %47
15: ; preds = %11
%16 = call i32 (...) @fork()
%17 = icmp slt i32 %16, 0
br i1 %17, label %47, 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 = %44, %46, %15, %3, %11
%48 = phi i32 [ -1, %11 ], [ -1, %3 ], [ %16, %15 ], [ %16, %46 ], [ %16, %44 ]
ret i32 %48
}