#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 /*<<< 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 *, 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*/ setenv(char *, char *, int);
int /*<<< orphan*/ *stderr;
void init_bc(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]);
    setenv("BC_LINE_LENGTH", "1000000", 1);
    execlp("bc", "bc", NULL);
    _exit(1);
  }
  close(w_pipe[0]);
  close(r_pipe[1]);
  *bcin = fdopen(w_pipe[1], "w");
  *bcout = fdopen(r_pipe[0], "r");
  fprintf(*bcin, "ibase=16;obase=10;\n");
}


x86 assembly (clang, -O0):
<init_bc>:
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   1070 <pipe@plt>
cmp    $0x0,%rax
jne    11da <init_bc+0x3a>
lea    -0x28(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
je     11fa <init_bc+0x5a>
mov    0x2df7(%rip),%rax
mov    (%rax),%rdi
lea    0xe15(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1277 <init_bc+0xd7>
mov    -0x20(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
lea    0xdcd(%rip),%rdi
lea    0xdd5(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    0xdcc(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10b0 <execlp@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
lea    0xd9f(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%edi
lea    0xd88(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xd6d(%rip),%rsi
call   1080 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_bc>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jne    1230 <init_bc+0x90>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jne    1230 <init_bc+0x90>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    0x10(%rsp),%edi
test   %rax,%rax
je     125a <init_bc+0xba>
call   1060 <close@plt>
mov    0xc(%rsp),%edi
call   1060 <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)
mov    (%rbx),%rcx
lea    0xe1b(%rip),%rdi
mov    $0x13,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2da1(%rip),%rax
mov    (%rax),%rcx
lea    0xdbf(%rip),%rdi
mov    $0x15,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xd83(%rip),%rdi
lea    0xd8b(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    0xd82(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10b0 <execlp@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@stderr = global i32* null
@.str = [22 x i8] c"pipe failed to happen\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [15 x i8] c"BC_LINE_LENGTH\00"
@.str.2 = [8 x i8] c"1000000\00"
@.str.3 = [3 x i8] c"bc\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [20 x i8] c"ibase=16;obase=10;\0A\00"
define void @init_bc(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 %43
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 = call i32 @setenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 1)
%41 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
%42 = call i32 @_exit(i32 1)
unreachable
43: ; preds = %21
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32* @fdopen(i32 %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32**, i32*** %5
store i32* %52, i32** %53
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%55 = load i32, i32* %54
%56 = call i32* @fdopen(i32 %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%57 = load i32**, i32*** %6
store i32* %56, i32** %57
%58 = load i32**, i32*** %5
%59 = load i32*, i32** %58
%60 = call i32 @fprintf(i32* %59, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@stderr = global i32* null
@.str = [22 x i8] c"pipe failed to happen\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [15 x i8] c"BC_LINE_LENGTH\00"
@.str.2 = [8 x i8] c"1000000\00"
@.str.3 = [3 x i8] c"bc\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [20 x i8] c"ibase=16;obase=10;\0A\00"
define void @init_bc(i64* nocapture readnone %0, i32** nocapture %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 i64 @fwrite(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 21, i64 1, i32* %16)
%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 %38
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 = call i32 @setenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 1)
%36 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
%37 = call i32 @_exit(i32 1)
unreachable
38: ; preds = %19
%39 = call i32 @close(i32 %22)
%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
%45 = call i32* @fdopen(i32 %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %45, i32** %1
%46 = load i32, i32* %12
%47 = call i32* @fdopen(i32 %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %47, i32** %2
%48 = load i32*, i32** %1
%49 = call i64 @fwrite(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 19, i64 1, i32* %48)
ret void
}