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

/* Variables and functions */
scalar_t__ close(int);
int /*<<< orphan*/ die_errno(char *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ spawn(char const *const *, int, int);
int /*<<< orphan*/ xpipe(int *);
FILE *spawn_pipe(pid_t *pid, const char *const cmd[], int errfd) {
  int pipefd[2];
  FILE *f;
  xpipe(pipefd);
  *pid = spawn(cmd, pipefd[1], errfd);
  if (close(pipefd[1]) || (errfd != -1 && close(errfd)))
    die_errno("cannot close unneeded fd");
  f = fdopen(pipefd[0], "r");
  if (!f)
    die_errno("cannot stream read end of pipe");
  return f;
}


x86 assembly (clang, -O0):
<spawn_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <xpipe@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1060 <spawn@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jne    11b1 <spawn_pipe+0x61>
cmpl   $0xffffffff,-0x14(%rbp)
je     11bd <spawn_pipe+0x6d>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     11bd <spawn_pipe+0x6d>
lea    0xe48(%rip),%rdi
call   1040 <die_errno@plt>
mov    -0x1c(%rbp),%edi
lea    0xe52(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e7 <spawn_pipe+0x97>
lea    0xe39(%rip),%rdi
call   1040 <die_errno@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_pipe>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
call   1070 <xpipe@plt>
mov    0xc(%rsp),%esi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1060 <spawn@plt>
mov    %eax,0x0(%rbp)
mov    0xc(%rsp),%edi
call   1030 <close@plt>
test   %rax,%rax
je     11c7 <spawn_pipe+0x77>
lea    0xe6e(%rip),%rdi
call   1040 <die_errno@plt>
mov    0x8(%rsp),%edi
lea    0xe77(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11bb <spawn_pipe+0x6b>
lea    0xe65(%rip),%rdi
call   1040 <die_errno@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0xffffffff,%r14d
je     1197 <spawn_pipe+0x47>
mov    %r14d,%edi
call   1030 <close@plt>
test   %rax,%rax
jne    118b <spawn_pipe+0x3b>
jmp    1197 <spawn_pipe+0x47>

LLVM IR (-O0):
@.str = [25 x i8] c"cannot close unneeded fd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"cannot stream read end of pipe\00"
define i32* @spawn_pipe(i32* %0, i8** %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca i32*
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%10 = call i32 @xpipe(i32* %9)
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%13 = load i32, i32* %12
%14 = load i32, i32* %6
%15 = call i32 @spawn(i8** %11, i32 %13, i32 %14)
%16 = load i32*, i32** %4
store i32 %15, i32* %16
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i64 @close(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %3
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i32, i32* %6
%26 = call i64 @close(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24, %3
%29 = call i32 @die_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %24, %21
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32* @fdopen(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %33, i32** %8
%34 = load i32*, i32** %8
%35 = icmp ne i32* %34, null
br i1 %35, label %38, label %36
36: ; preds = %30
%37 = call i32 @die_errno(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %30
%39 = load i32*, i32** %8
ret i32* %39
}

LLVM IR (-O2):
@.str = [25 x i8] c"cannot close unneeded fd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"cannot stream read end of pipe\00"
define noalias i32* @spawn_pipe(i32* nocapture writeonly %0, i8** %1, i32 %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 @xpipe(i32* %6)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%9 = load i32, i32* %8
%10 = call i32 @spawn(i8** %1, i32 %9, i32 %2)
store i32 %10, i32* %0
%11 = load i32, i32* %8
%12 = call i64 @close(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = icmp eq i32 %2, -1
br i1 %15, label %21, label %16
16: ; preds = %14
%17 = call i64 @close(i32 %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16, %3
%20 = call i32 @die_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16, %14
%22 = load i32, i32* %6
%23 = call i32* @fdopen(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @die_errno(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
ret i32* %23
}