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

/* Variables and functions */
int STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ stderr;
void init_py(pid_t *pid, FILE **bcin, FILE **bcout) {
  int w_pipe[2];
  int r_pipe[2];
  if (pipe(w_pipe) != 0 || pipe(r_pipe) != 0) {
    fprintf(stderr, "pipe failed to happen");
    exit(1);
  }
  pid_t bc = fork();
  if (bc == 0) {
    dup2(w_pipe[0], STDIN_FILENO);
    dup2(r_pipe[1], STDOUT_FILENO);
    close(w_pipe[1]);
    close(r_pipe[0]);
    close(STDERR_FILENO);
    execlp("python", "python", "-i", NULL);
    _exit(1);
  }
  close(w_pipe[0]);
  close(r_pipe[1]);
  *bcin = fdopen(w_pipe[1], "w");
  *bcout = fdopen(r_pipe[0], "r");
}


x86 assembly (clang, -O0):
<init_py>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jne    11ca <init_py+0x3a>
lea    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11e9 <init_py+0x59>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1263 <init_py+0xd3>
mov    -0x20(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
lea    0xdd0(%rip),%rsi
lea    0xdd0(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10a0 <execlp@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
lea    0xda3(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%edi
lea    0xd8c(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_py>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    1207 <init_py+0x77>
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    1207 <init_py+0x77>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    0x10(%rsp),%edi
test   %rax,%rax
je     1226 <init_py+0x96>
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%edi
lea    0xe3c(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,(%rbx)
mov    0x8(%rsp),%edi
lea    0xe2b(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
lea    0xda9(%rip),%rdi
lea    0xda9(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   10a0 <execlp@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"pipe failed to happen\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [7 x i8] c"python\00"
@.str.2 = [3 x i8] c"-i\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [2 x i8] c"r\00"
define void @init_py(i64* %0, i32** %1, i32** %2) {
%4 = alloca i64*
%5 = alloca i32**
%6 = alloca i32**
%7 = alloca [2 x i32]
%8 = alloca [2 x i32]
%9 = alloca i64
store i64* %0, i64** %4
store i32** %1, i32*** %5
store i32** %2, i32*** %6
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %3
%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 %17, label %21
17: ; preds = %13, %3
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = call i64 (...) @fork()
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %44
25: ; preds = %21
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%27 = load i32, i32* %26
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%31 = load i32, i32* %30
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @STDERR_FILENO
%41 = call i32 @close(i32 %40)
%42 = call i32 @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
%43 = call i32 @_exit(i32 1)
unreachable
44: ; preds = %21
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32* @fdopen(i32 %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32**, i32*** %5
store i32* %53, i32** %54
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32* @fdopen(i32 %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%58 = load i32**, i32*** %6
store i32* %57, i32** %58
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"pipe failed to happen\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.1 = [7 x i8] c"python\00"
@.str.2 = [3 x i8] c"-i\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [2 x i8] c"r\00"
define void @init_py(i64* nocapture readnone %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* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
%22 = load i32, i32* %8
br i1 %21, label %23, label %39
23: ; preds = %19
%24 = load i32, i32* @STDIN_FILENO
%25 = call i32 @dup2(i32 %22, i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%27 = load i32, i32* %26
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %12
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* @STDERR_FILENO
%36 = call i32 @close(i32 %35)
%37 = call i32 @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
%38 = call i32 @_exit(i32 1)
unreachable
39: ; preds = %19
%40 = call i32 @close(i32 %22)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32* @fdopen(i32 %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %46, i32** %1
%47 = load i32, i32* %12
%48 = call i32* @fdopen(i32 %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %48, i32** %2
ret void
}