#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*/ O_WRONLY;
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 *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
pid_t popen2(char *command[], int *infp, int *outfp) {
  int p_stdin[2], p_stdout[2];
  int p_stderr = open("/dev/null", O_WRONLY);
  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], STDIN_FILENO);
    close(p_stdout[0]);
    dup2(p_stdout[1], STDOUT_FILENO);
    dup2(p_stderr, STDERR_FILENO);
    execvp(command[0], command);
    perror("execvp");
    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_stderr);
  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)
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jne    11d2 <popen2+0x52>
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11de <popen2+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <popen2+0x145>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11fd <popen2+0x7d>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c5 <popen2+0x145>
cmpl   $0x0,-0x38(%rbp)
jne    1270 <popen2+0xf0>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x34(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <execvp@plt>
lea    0xda9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1275 <popen2+0xf5>
cmpq   $0x0,-0x18(%rbp)
jne    128d <popen2+0x10d>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
jmp    1296 <popen2+0x116>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
jne    12ae <popen2+0x12e>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
jmp    12b7 <popen2+0x137>
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%r15
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    120b <popen2+0x8b>
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    120b <popen2+0x8b>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     120b <popen2+0x8b>
je     121a <popen2+0x9a>
mov    0xc(%rsp),%edi
test   %rbp,%rbp
je     11eb <popen2+0x6b>
mov    %edi,0x0(%rbp)
jmp    11f0 <popen2+0x70>
call   1040 <close@plt>
mov    (%rsp),%edi
test   %r12,%r12
je     11fe <popen2+0x7e>
mov    %edi,(%r12)
jmp    1203 <popen2+0x83>
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1030 <dup2@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
call   1080 <execvp@plt>
lea    0xd98(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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