#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;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
pid_t bpopen(char *argv[], int *fdr, int *fdw) {
  int p2c[2], c2p[2];
  pid_t pid;
  if (pipe(p2c) || pipe(c2p)) {
    printf("pipe: %s\n", strerror(errno));
    return (0);
  }
  if ((pid = fork()) < 0) {
    printf("fork: %s\n", strerror(errno));
    return (0);
  }
  if (!pid) {
    close(c2p[0]);
    close(p2c[1]);
    dup2(c2p[1], STDOUT_FILENO);
    dup2(p2c[0], STDIN_FILENO);
    execv(argv[0], argv);
    fprintf(stderr, "execv: %s: %s\n", argv[0], strerror(errno));
    exit(1);
  }
  close(c2p[1]);
  close(p2c[0]);
  *fdw = p2c[1];
  *fdr = c2p[0];
  return (pid);
}


x86 assembly (clang, -O0):
<bpopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jne    11ca <bpopen+0x3a>
lea    -0x30(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11f3 <bpopen+0x63>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <bpopen+0x158>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    122f <bpopen+0x9f>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <bpopen+0x158>
cmpl   $0x0,-0x34(%rbp)
jne    12c0 <bpopen+0x130>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x28(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execv@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd63(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpopen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    11c1 <bpopen+0x31>
mov    %rsp,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
je     11ef <bpopen+0x5f>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2a(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1224 <bpopen+0x94>
je     1243 <bpopen+0xb3>
mov    %eax,%r12d
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%eax
mov    %eax,(%rbx)
mov    (%rsp),%eax
mov    %eax,(%r15)
mov    %r12d,%ebp
jmp    11e0 <bpopen+0x50>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd1(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    11e0 <bpopen+0x50>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10a0 <execv@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%rbp
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd70(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"pipe: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"fork: %s\0A\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str.2 = [15 x i8] c"execv: %s: %s\0A\00"
define i32 @bpopen(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 %22
18: ; preds = %14, %3
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 0, i32* %4
br label %74
22: ; preds = %14
%23 = call i32 (...) @fork()
store i32 %23, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 0, i32* %4
br label %74
29: ; preds = %22
%30 = load i32, i32* %10
%31 = icmp ne i32 %30, 0
br i1 %31, label %60, label %32
32: ; preds = %29
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%40 = load i32, i32* %39
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i32 @dup2(i32 %40, i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%44 = load i32, i32* %43
%45 = load i32, i32* @STDIN_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 0
%49 = load i8*, i8** %48
%50 = load i8**, i8*** %5
%51 = call i32 @execv(i8* %49, i8** %50)
%52 = load i32, i32* @stderr
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 0
%55 = load i8*, i8** %54
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %55, i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %29
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%68 = load i32, i32* %67
%69 = load i32*, i32** %7
store i32 %68, i32* %69
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%71 = load i32, i32* %70
%72 = load i32*, i32** %6
store i32 %71, i32* %72
%73 = load i32, i32* %10
store i32 %73, i32* %4
br label %74
74: ; preds = %60, %25, %18
%75 = load i32, i32* %4
ret i32 %75
}

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