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

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *, char const *,
                         int /*<<< orphan*/ *);
scalar_t__ fork();
scalar_t__ pipe(int *);
pid_t bidirectional_popen(const char *cmd, int *in_fd, int *out_fd) {
  int input_fds[2];
  int output_fds[2];
  if (pipe(input_fds) != 0)
    return 0;
  if (pipe(output_fds) != 0)
    return 0;
  pid_t pid = fork();
  if (pid < 0)
    return 0;
  if (pid == 0) {
    close(input_fds[1]);
    close(output_fds[0]);
    dup2(input_fds[0], STDIN_FILENO);
    dup2(output_fds[1], STDOUT_FILENO);
    dup2(output_fds[1], STDERR_FILENO);
    execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
  } else {
    close(input_fds[0]);
    close(output_fds[1]);
    *in_fd = input_fds[1];
    *out_fd = output_fds[0];
  }
  return pid;
}


x86 assembly (clang, -O0):
<bidirectional_popen>:
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
je     1184 <bidirectional_popen+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1263 <bidirectional_popen+0x113>
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11a4 <bidirectional_popen+0x54>
movq   $0x0,-0x8(%rbp)
jmp    1263 <bidirectional_popen+0x113>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11c7 <bidirectional_popen+0x77>
movq   $0x0,-0x8(%rbp)
jmp    1263 <bidirectional_popen+0x113>
cmpq   $0x0,-0x38(%rbp)
jne    1239 <bidirectional_popen+0xe9>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rcx
lea    0xde0(%rip),%rsi
lea    0xde1(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1060 <execl@plt>
jmp    125b <bidirectional_popen+0x10b>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bidirectional_popen>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
je     1179 <bidirectional_popen+0x29>
xor    %ebx,%ebx
jmp    1188 <bidirectional_popen+0x38>
mov    %rsp,%rdi
call   1050 <pipe@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1199 <bidirectional_popen+0x49>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1188 <bidirectional_popen+0x38>
je     11cf <bidirectional_popen+0x7f>
mov    %rax,%r13
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%eax
mov    %eax,(%r12)
mov    (%rsp),%eax
mov    %eax,(%r15)
mov    %r13,%rbx
jmp    1188 <bidirectional_popen+0x38>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x4(%rsp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x4(%rsp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xde3(%rip),%rdi
lea    0xde4(%rip),%rdx
xor    %ebx,%ebx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   1060 <execl@plt>
jmp    1188 <bidirectional_popen+0x38>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
define i64 @bidirectional_popen(i8* %0, i32* %1, i32* %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [2 x i32]
%9 = alloca [2 x i32]
%10 = alloca i64
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 %14, label %15
14: ; preds = %3
store i64 0, i64* %4
br label %64
15: ; preds = %3
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%17 = call i64 @pipe(i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i64 0, i64* %4
br label %64
20: ; preds = %15
%21 = call i64 (...) @fork()
store i64 %21, i64* %10
%22 = load i64, i64* %10
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i64 0, i64* %4
br label %64
25: ; preds = %20
%26 = load i64, i64* %10
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %49
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]* %9, i64 0, i64 0
%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 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]* %9, i64 0, i64 1
%44 = load i32, i32* %43
%45 = load i32, i32* @STDERR_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = load i8*, i8** %5
%48 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), 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* %47, i32* null)
br label %62
49: ; preds = %25
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%57 = load i32, i32* %56
%58 = load i32*, i32** %6
store i32 %57, i32* %58
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%60 = load i32, i32* %59
%61 = load i32*, i32** %7
store i32 %60, i32* %61
br label %62
62: ; preds = %49, %28
%63 = load i64, i64* %10
store i64 %63, i64* %4
br label %64
64: ; preds = %62, %24, %19, %14
%65 = load i64, i64* %4
ret i64 %65
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
define i64 @bidirectional_popen(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 %46
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 %46
15: ; preds = %11
%16 = call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %46, label %18
18: ; preds = %15
%19 = icmp eq i64 %16, 0
br i1 %19, label %20, label %37
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* %12
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %8
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%30 = load i32, i32* %29
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = load i32, i32* %29
%34 = load i32, i32* @STDERR_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
%36 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), 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* %0, i32* null)
br label %46
37: ; preds = %18
%38 = load i32, i32* %8
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%44 = load i32, i32* %43
store i32 %44, i32* %1
%45 = load i32, i32* %12
store i32 %45, i32* %2
br label %46
46: ; preds = %15, %37, %20, %11, %3
%47 = phi i64 [ 0, %3 ], [ 0, %11 ], [ 0, %15 ], [ %16, %37 ], [ 0, %20 ]
ret i64 %47
}