#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 p[2]) {
  FILE *fpin = fdopen(p[1], "w");
  FILE *fpout = fdopen(p[1], "r");
  close(p[0]);
  close(p[1]);
  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    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
lea    0xe66(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
lea    0xe51(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12f7 <be_bc+0x177>
mov    -0x48(%rbp),%rcx
mov    -0x40(%rbp),%rdi
lea    0xdaf(%rip),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%r8
call   1090 <sscanf@plt>
cmp    $0x3,%eax
je     1287 <be_bc+0x107>
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121b <be_bc+0x9b>
mov    -0x48(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xd8c(%rip),%rsi
movsbl (%rax),%r8d
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x40(%rbp),%rdi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    12d3 <be_bc+0x153>
jmp    12f7 <be_bc+0x177>
mov    -0x40(%rbp),%r8
mov    -0x48(%rbp),%rax
mov    -0x1c(%rbp),%esi
movsbl (%rax),%edx
mov    -0x20(%rbp),%ecx
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121b <be_bc+0x9b>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%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    %rdi,%rbx
mov    0x4(%rdi),%edi
lea    0xe52(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,%r15
mov    0x4(%rbx),%edi
lea    0xe7d(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,-0x38(%rbp)
mov    (%rbx),%edi
call   1060 <close@plt>
mov    0x4(%rbx),%edi
call   1060 <close@plt>
mov    %rsp,-0x40(%rbp)
mov    0x2df5(%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    0xdee(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%r13),%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%rdx
mov    %rbx,%rdi
call   1070 <fgets@plt>
test   %rax,%rax
je     12f8 <be_bc+0x168>
lea    0xdc1(%rip),%r14
jmp    1281 <be_bc+0xf1>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
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) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %2
%10 = load i32*, i32** %2
%11 = getelementptr inbounds i32, i32* %10, i64 1
%12 = load i32, i32* %11
%13 = call i32* @fdopen(i32 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %3
%14 = load i32*, i32** %2
%15 = getelementptr inbounds i32, i32* %14, i64 1
%16 = load i32, i32* %15
%17 = call i32* @fdopen(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %4
%18 = load i32*, i32** %2
%19 = getelementptr inbounds i32, i32* %18, i64 0
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = load i32*, i32** %2
%23 = getelementptr inbounds i32, i32* %22, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @BUFSIZ
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
store i8* %28, i8** %7
%29 = alloca i8, i64 %27
store i64 %27, i64* %8
%30 = load i32, i32* @BUFSIZ
%31 = zext i32 %30 to i64
%32 = alloca i8, i64 %31
store i64 %31, i64* %9
br label %33
33: ; preds = %57, %42, %1
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @BUFSIZ
%36 = load i32*, i32** @stdin
%37 = call i32* @fgets(i8* %32, i32 %35, i32* %36)
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %63
39: ; preds = %33
%40 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32* %5, i8* %29, i32* %6)
%41 = icmp ne i32 %40, 3
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %33
44: ; preds = %39
%45 = load i32*, i32** %3
%46 = load i32, i32* %5
%47 = load i32, i32* %6
%48 = load i8, i8* %29
%49 = call i32 @fprintf(i32* %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %46, i32 %47, i8 signext %48)
%50 = load i32*, i32** %3
%51 = call i32 @fflush(i32* %50)
%52 = load i32, i32* @BUFSIZ
%53 = load i32*, i32** %4
%54 = call i32* @fgets(i8* %32, i32 %52, i32* %53)
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %57
56: ; preds = %44
br label %63
57: ; preds = %44
%58 = load i32, i32* %5
%59 = load i8, i8* %29
%60 = sext i8 %59 to i32
%61 = load i32, i32* %6
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %58, i32 %60, i32 %61, i8* %32)
br label %33
63: ; preds = %56, %33
%64 = load i32*, i32** %3
%65 = call i32 @fclose(i32* %64)
%66 = load i32*, i32** %4
%67 = call i32 @fclose(i32* %66)
%68 = load i8*, i8** %7
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) {
%2 = alloca i32
%3 = alloca i32
%4 = getelementptr inbounds i32, i32* %0, i64 1
%5 = load i32, i32* %4
%6 = tail call i32* @fdopen(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* %4
%8 = tail call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* %0
%10 = tail call i32 @close(i32 %9)
%11 = load i32, i32* %4
%12 = tail call i32 @close(i32 %11)
%13 = bitcast i32* %2 to i8*
%14 = bitcast i32* %3 to i8*
%15 = load i32, i32* @BUFSIZ
%16 = zext i32 %15 to i64
%17 = tail call i8* @llvm.stacksave()
%18 = alloca i8, i64 %16
%19 = load i32, i32* @BUFSIZ
%20 = zext i32 %19 to i64
%21 = alloca i8, i64 %20
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @BUFSIZ
%24 = load i32*, i32** @stdin
%25 = call i32* @fgets(i8* %21, i32 %23, i32* %24)
%26 = icmp eq i32* %25, null
br i1 %26, label %53, label %27
27: ; preds = %1, %32
%28 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32* %2, i8* %18, i32* %3)
%29 = icmp eq i32 %28, 3
br i1 %29, label %38, label %30
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %30, %47
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @BUFSIZ
%35 = load i32*, i32** @stdin
%36 = call i32* @fgets(i8* %21, i32 %34, i32* %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %53, label %27
38: ; preds = %27
%39 = load i32, i32* %2
%40 = load i32, i32* %3
%41 = load i8, i8* %18
%42 = call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %39, i32 %40, i8 signext %41)
%43 = call i32 @fflush(i32* %6)
%44 = load i32, i32* @BUFSIZ
%45 = call i32* @fgets(i8* %21, i32 %44, i32* %8)
%46 = icmp eq i32* %45, null
br i1 %46, label %53, label %47
47: ; preds = %38
%48 = load i32, i32* %2
%49 = load i8, i8* %18
%50 = sext i8 %49 to i32
%51 = load i32, i32* %3
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %48, i32 %50, i32 %51, i8* %21)
br label %32
53: ; preds = %32, %38, %1
%54 = call i32 @fclose(i32* %6)
%55 = call i32 @fclose(i32* %8)
ret void
}