#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execvp(char const *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int pipe(int *);
FILE *popenNoStderr(const char *exe, const char *const argv[], int *retPid) {
  int out[2];
  int pid;
  int rc;
  rc = pipe(out);
  if (rc < 0)
    goto error_out;
  pid = fork();
  if (pid > 0) {
    close(out[1]);
    *retPid = pid;
    return fdopen(out[0], "r");
  } else if (pid == 0) {
    close(out[0]);
    close(1);
    dup(out[1]);
    close(0);
    close(2);
    execvp(exe, (char **)argv);
    exit(1);
  } else
    goto error_fork;
  return 0;
error_fork:
  close(out[0]);
  close(out[1]);
error_out:
  return 0;
}


x86 assembly (clang, -O0):
<popenNoStderr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    119f <popenNoStderr+0x2f>
jmp    123d <popenNoStderr+0xcd>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11dc <popenNoStderr+0x6c>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%edi
lea    0xe32(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x8(%rbp)
jmp    1245 <popenNoStderr+0xd5>
cmpl   $0x0,-0x2c(%rbp)
jne    1228 <popenNoStderr+0xb8>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1030 <dup@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <execvp@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    122d <popenNoStderr+0xbd>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popenNoStderr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %eax,%eax
js     11ca <popenNoStderr+0x5a>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11b5 <popenNoStderr+0x45>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    %ebp,(%rbx)
mov    (%rsp),%edi
lea    0xe52(%rip),%rsi
call   1060 <fdopen@plt>
jmp    11cc <popenNoStderr+0x5c>
mov    (%rsp),%edi
call   1040 <close@plt>
test   %ebp,%ebp
je     11d7 <popenNoStderr+0x67>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1030 <dup@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <execvp@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32* @popenNoStderr(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca [2 x i32]
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
br label %54
16: ; preds = %3
%17 = call i32 (...) @fork()
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %16
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %9
%25 = load i32*, i32** %7
store i32 %24, i32* %25
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32* @fdopen(i32 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %28, i32** %4
br label %55
29: ; preds = %16
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %46
32: ; preds = %29
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = call i32 @close(i32 1)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @dup(i32 %38)
%40 = call i32 @close(i32 0)
%41 = call i32 @close(i32 2)
%42 = load i8*, i8** %5
%43 = load i8**, i8*** %6
%44 = call i32 @execvp(i8* %42, i8** %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %29
br label %47
47: ; preds = %46
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %47, %15
store i32* null, i32** %4
br label %55
55: ; preds = %54, %20
%56 = load i32*, i32** %4
ret i32* %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define noalias i32* @popenNoStderr(i8* %0, i8** %1, i32* nocapture writeonly %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %3
%10 = call i32 (...) @fork()
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
store i32 %10, i32* %2
%16 = load i32, i32* %6
%17 = call i32* @fdopen(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %35
18: ; preds = %9
%19 = icmp eq i32 %10, 0
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
br i1 %19, label %22, label %31
22: ; preds = %18
%23 = call i32 @close(i32 1)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @dup(i32 %25)
%27 = call i32 @close(i32 0)
%28 = call i32 @close(i32 2)
%29 = call i32 @execvp(i8* %0, i8** %1)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %18
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %31, %3, %12
%36 = phi i32* [ %17, %12 ], [ null, %3 ], [ null, %31 ]
ret i32* %36
}