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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int, char);
int printf(char *, ...);
int sscanf(char *, char *, int *, char *, int *);
int /*<<< orphan*/ *stdin;
extern void be_bc(int in[2], int out[2]) {
  close(in[0]);
  close(out[1]);
  FILE *fpin = fdopen(in[1], "w");
  FILE *fpout = fdopen(out[0], "r");
  int num1, num2;
  char operation[BUFSIZ], message[BUFSIZ];
  while (printf("tinybc: "), fgets(message, BUFSIZ, stdin) != NULL) {
    if (sscanf(message, "%d%[-+*/^]%d", &num1, operation, &num2) != 3) {
      printf("syntax error\n");
      continue;
    }
    fprintf(fpin, "%d\n%d\n%c\np\n", num1, num2, *operation);
    fflush(fpin);
    if (fgets(message, BUFSIZ, fpout) == NULL)
      break;
    printf("%d %c %d = %s", num1, *operation, num2, message);
  }
  fclose(fpin);
  fclose(fpout);
}


x86 assembly (clang, -O0):
<be_bc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
lea    0xe4b(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x30(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x38(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rdi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12fa <be_bc+0x17a>
mov    -0x50(%rbp),%rcx
mov    -0x48(%rbp),%rdi
lea    0xdac(%rip),%rsi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%r8
call   1090 <sscanf@plt>
cmp    $0x3,%eax
je     128a <be_bc+0x10a>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121e <be_bc+0x9e>
mov    -0x50(%rbp),%rax
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xd89(%rip),%rsi
movsbl (%rax),%r8d
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x48(%rbp),%rdi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    12d6 <be_bc+0x156>
jmp    12fa <be_bc+0x17a>
mov    -0x48(%rbp),%r8
mov    -0x50(%rbp),%rax
mov    -0x24(%rbp),%esi
movsbl (%rax),%edx
mov    -0x28(%rbp),%ecx
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121e <be_bc+0x9e>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<be_bc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1060 <close@plt>
mov    0x4(%r14),%edi
call   1060 <close@plt>
mov    0x4(%rbx),%edi
lea    0xe3f(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,%r15
mov    (%r14),%edi
lea    0xe6a(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,-0x38(%rbp)
mov    %rsp,-0x40(%rbp)
mov    0x2df1(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xdea(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%r13),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%rdx
mov    %rbx,%rdi
call   1070 <fgets@plt>
test   %rax,%rax
je     12f8 <be_bc+0x168>
lea    0xdbd(%rip),%r14
jmp    1281 <be_bc+0xf1>
nopw   0x0(%rax,%rax,1)
lea    0xddb(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%r13),%esi
mov    0x2d77(%rip),%rax
mov    (%rax),%rdx
mov    %rbx,%rdi
call   1070 <fgets@plt>
test   %rax,%rax
je     12f8 <be_bc+0x168>
mov    %rbx,%rdi
lea    0xd80(%rip),%rsi
lea    -0x30(%rbp),%rdx
mov    %r12,%rcx
lea    -0x2c(%rbp),%r8
call   10a0 <sscanf@plt>
cmp    $0x3,%eax
jne    1250 <be_bc+0xc0>
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
movsbl (%r12),%r8d
mov    %r15,%rdi
lea    0xd63(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r15,%rdi
call   1090 <fflush@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
mov    -0x38(%rbp),%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12f8 <be_bc+0x168>
mov    -0x30(%rbp),%esi
movsbl (%r12),%edx
mov    -0x2c(%rbp),%ecx
lea    0xd3b(%rip),%rdi
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <printf@plt>
jmp    125c <be_bc+0xcc>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@BUFSIZ = global i32 0
@.str.2 = [9 x i8] c"tinybc: \00"
@stdin = global i32* null
@.str.3 = [13 x i8] c"%d%[-+*/^]%d\00"
@.str.4 = [14 x i8] c"syntax error\0A\00"
@.str.5 = [12 x i8] c"%d\0A%d\0A%c\0Ap\0A\00"
@.str.6 = [14 x i8] c"%d %c %d = %s\00"
define void @be_bc(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i32* %0, i32** %3
store i32* %1, i32** %4
%12 = load i32*, i32** %3
%13 = getelementptr inbounds i32, i32* %12, i64 0
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
%16 = load i32*, i32** %4
%17 = getelementptr inbounds i32, i32* %16, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = load i32*, i32** %3
%21 = getelementptr inbounds i32, i32* %20, i64 1
%22 = load i32, i32* %21
%23 = call i32* @fdopen(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %5
%24 = load i32*, i32** %4
%25 = getelementptr inbounds i32, i32* %24, i64 0
%26 = load i32, i32* %25
%27 = call i32* @fdopen(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %6
%28 = load i32, i32* @BUFSIZ
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
store i8* %30, i8** %9
%31 = alloca i8, i64 %29
store i64 %29, i64* %10
%32 = load i32, i32* @BUFSIZ
%33 = zext i32 %32 to i64
%34 = alloca i8, i64 %33
store i64 %33, i64* %11
br label %35
35: ; preds = %59, %44, %2
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @BUFSIZ
%38 = load i32*, i32** @stdin
%39 = call i32* @fgets(i8* %34, i32 %37, i32* %38)
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %65
41: ; preds = %35
%42 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32* %7, i8* %31, i32* %8)
%43 = icmp ne i32 %42, 3
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %35
46: ; preds = %41
%47 = load i32*, i32** %5
%48 = load i32, i32* %7
%49 = load i32, i32* %8
%50 = load i8, i8* %31
%51 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %48, i32 %49, i8 signext %50)
%52 = load i32*, i32** %5
%53 = call i32 @fflush(i32* %52)
%54 = load i32, i32* @BUFSIZ
%55 = load i32*, i32** %6
%56 = call i32* @fgets(i8* %34, i32 %54, i32* %55)
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %59
58: ; preds = %46
br label %65
59: ; preds = %46
%60 = load i32, i32* %7
%61 = load i8, i8* %31
%62 = sext i8 %61 to i32
%63 = load i32, i32* %8
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %60, i32 %62, i32 %63, i8* %34)
br label %35
65: ; preds = %58, %35
%66 = load i32*, i32** %5
%67 = call i32 @fclose(i32* %66)
%68 = load i32*, i32** %6
%69 = call i32 @fclose(i32* %68)
%70 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@BUFSIZ = global i32 0
@.str.2 = [9 x i8] c"tinybc: \00"
@stdin = global i32* null
@.str.3 = [13 x i8] c"%d%[-+*/^]%d\00"
@.str.5 = [12 x i8] c"%d\0A%d\0A%c\0Ap\0A\00"
@.str.6 = [14 x i8] c"%d %c %d = %s\00"
@str = [13 x i8] c"syntax error\00"
define void @be_bc(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* %0
%6 = tail call i32 @close(i32 %5)
%7 = getelementptr inbounds i32, i32* %1, i64 1
%8 = load i32, i32* %7
%9 = tail call i32 @close(i32 %8)
%10 = getelementptr inbounds i32, i32* %0, i64 1
%11 = load i32, i32* %10
%12 = tail call i32* @fdopen(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %1
%14 = tail call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = bitcast i32* %3 to i8*
%16 = bitcast i32* %4 to i8*
%17 = load i32, i32* @BUFSIZ
%18 = zext i32 %17 to i64
%19 = tail call i8* @llvm.stacksave()
%20 = alloca i8, i64 %18
%21 = load i32, i32* @BUFSIZ
%22 = zext i32 %21 to i64
%23 = alloca i8, i64 %22
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @BUFSIZ
%26 = load i32*, i32** @stdin
%27 = call i32* @fgets(i8* %23, i32 %25, i32* %26)
%28 = icmp eq i32* %27, null
br i1 %28, label %55, label %29
29: ; preds = %2, %34
%30 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32* %3, i8* %20, i32* %4)
%31 = icmp eq i32 %30, 3
br i1 %31, label %40, label %32
32: ; preds = %29
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %32, %49
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @BUFSIZ
%37 = load i32*, i32** @stdin
%38 = call i32* @fgets(i8* %23, i32 %36, i32* %37)
%39 = icmp eq i32* %38, null
br i1 %39, label %55, label %29
40: ; preds = %29
%41 = load i32, i32* %3
%42 = load i32, i32* %4
%43 = load i8, i8* %20
%44 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %41, i32 %42, i8 signext %43)
%45 = call i32 @fflush(i32* %12)
%46 = load i32, i32* @BUFSIZ
%47 = call i32* @fgets(i8* %23, i32 %46, i32* %14)
%48 = icmp eq i32* %47, null
br i1 %48, label %55, label %49
49: ; preds = %40
%50 = load i32, i32* %3
%51 = load i8, i8* %20
%52 = sext i8 %51 to i32
%53 = load i32, i32* %4
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %50, i32 %52, i32 %53, i8* %23)
br label %34
55: ; preds = %34, %40, %2
%56 = call i32 @fclose(i32* %12)
%57 = call i32 @fclose(i32* %14)
ret void
}