#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 */
scalar_t__ EOF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, int *, char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
void be_bc(int todc[2], int fromdc[2]) {
  int num1, num2;
  char op;
  char msg[1024];
  FILE *fpout, *fpin;
  close(todc[0]);
  close(fromdc[1]);
  fpout = fdopen(todc[1], "w");
  fpin = fdopen(fromdc[0], "r");
  if (fpout == NULL || fpin == NULL) {
    perror("fdopen() failed");
    exit(1);
  }
  while (1) {
    printf("tinybc: ");
    if (fgets(msg, 1024, stdin) == NULL)
      break;
    if (sscanf(msg, "%d %[-+*/^] %d", &num1, &op, &num2) != 3) {
      fprintf(stderr, "syntax error\n");
      continue;
    }
    if (fprintf(fpout, "%d\n%d\n%c\np\n", num1, num2, op) == EOF) {
      fprintf(stderr, "fprintf() failed");
      exit(1);
    }
    fflush(fpout);
    if (fgets(msg, 1024, fpin) == NULL)
      break;
    printf("%d %c %d = %s", num1, op, num2, msg);
  }
  fclose(fpout);
  fclose(fpout);
}


x86 assembly (clang, -O0):
<be_bc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%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    0xe28(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x428(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     1219 <be_bc+0x79>
cmpq   $0x0,-0x430(%rbp)
jne    122f <be_bc+0x8f>
lea    0xde4(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1234 <be_bc+0x94>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    0x2d98(%rip),%rax
mov    (%rax),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    126c <be_bc+0xcc>
jmp    135a <be_bc+0x1ba>
lea    -0x420(%rbp),%rdi
lea    0xda3(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x19(%rbp),%rcx
lea    -0x18(%rbp),%r8
call   1090 <sscanf@plt>
cmp    $0x3,%eax
je     12b0 <be_bc+0x110>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1234 <be_bc+0x94>
mov    -0x428(%rbp),%rax
mov    %eax,%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
movsbl -0x19(%rbp),%r8d
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d07(%rip),%rcx
cmp    (%rcx),%rax
jne    1303 <be_bc+0x163>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x428(%rbp),%rdi
call   1080 <fflush@plt>
lea    -0x420(%rbp),%rdi
mov    -0x430(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    1336 <be_bc+0x196>
jmp    135a <be_bc+0x1ba>
mov    -0x14(%rbp),%esi
movsbl -0x19(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    -0x420(%rbp),%r8
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1234 <be_bc+0x94>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<be_bc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    (%rdi),%edi
call   1050 <close@plt>
mov    0x4(%rbx),%edi
call   1050 <close@plt>
mov    0x4(%rbp),%edi
lea    0xe30(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,%r12
mov    (%rbx),%edi
lea    0xe21(%rip),%rsi
call   10a0 <fdopen@plt>
test   %r12,%r12
je     135e <be_bc+0x1be>
test   %rax,%rax
je     135e <be_bc+0x1be>
mov    %rax,0x18(%rsp)
lea    0xe10(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rdx
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     131b <be_bc+0x17b>
lea    0x20(%rsp),%rbx
lea    0x14(%rsp),%r13
lea    0xf(%rsp),%rbp
lea    0x10(%rsp),%r14
lea    0xdcc(%rip),%r15
jmp    1291 <be_bc+0xf1>
nopw   0x0(%rax,%rax,1)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%rdx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     131b <be_bc+0x17b>
mov    %rbx,%rdi
lea    0xd82(%rip),%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
mov    %r14,%r8
call   1090 <sscanf@plt>
cmp    $0x3,%eax
jne    1250 <be_bc+0xb0>
mov    0x14(%rsp),%edx
mov    0x10(%rsp),%ecx
movsbl 0xf(%rsp),%r8d
mov    %r12d,%edi
lea    0xd74(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%rax
je     133d <be_bc+0x19d>
mov    %r12,%rdi
call   1080 <fflush@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    0x18(%rsp),%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     131b <be_bc+0x17b>
mov    0x14(%rsp),%esi
movsbl 0xf(%rsp),%edx
mov    0x10(%rsp),%ecx
lea    0xd4b(%rip),%rdi
mov    %rbx,%r8
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1267 <be_bc+0xc7>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
lea    0xcf9(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xc9f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"fdopen() failed\00"
@.str.3 = [9 x i8] c"tinybc: \00"
@stdin = global i32* null
@.str.4 = [15 x i8] c"%d %[-+*/^] %d\00"
@stderr = global i32 0
@.str.5 = [14 x i8] c"syntax error\0A\00"
@.str.6 = [12 x i8] c"%d\0A%d\0A%c\0Ap\0A\00"
@EOF = global i64 0
@.str.7 = [17 x i8] c"fprintf() failed\00"
@.str.8 = [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 i8
%8 = alloca [1024 x i8]
%9 = alloca i32*
%10 = alloca i32*
store i32* %0, i32** %3
store i32* %1, i32** %4
%11 = load i32*, i32** %3
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = load i32*, i32** %4
%16 = getelementptr inbounds i32, i32* %15, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = load i32*, i32** %3
%20 = getelementptr inbounds i32, i32* %19, i64 1
%21 = load i32, i32* %20
%22 = call i32* @fdopen(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %9
%23 = load i32*, i32** %4
%24 = getelementptr inbounds i32, i32* %23, i64 0
%25 = load i32, i32* %24
%26 = call i32* @fdopen(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %10
%27 = load i32*, i32** %9
%28 = icmp eq i32* %27, null
br i1 %28, label %32, label %29
29: ; preds = %2
%30 = load i32*, i32** %10
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29, %2
%33 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
br label %36
36: ; preds = %35, %47, %72
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%39 = load i32*, i32** @stdin
%40 = call i32* @fgets(i8* %38, i32 1024, i32* %39)
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %43
42: ; preds = %36
br label %79
43: ; preds = %36
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%45 = call i32 @sscanf(i8* %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32* %5, i8* %7, i32* %6)
%46 = icmp ne i32 %45, 3
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = call i64 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %36
50: ; preds = %43
%51 = load i32*, i32** %9
%52 = ptrtoint i32* %51 to i32
%53 = load i32, i32* %5
%54 = load i32, i32* %6
%55 = load i8, i8* %7
%56 = sext i8 %55 to i32
%57 = call i64 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %53, i32 %54, i32 %56)
%58 = load i64, i64* @EOF
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %64
60: ; preds = %50
%61 = load i32, i32* @stderr
%62 = call i64 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %50
%65 = load i32*, i32** %9
%66 = call i32 @fflush(i32* %65)
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%68 = load i32*, i32** %10
%69 = call i32* @fgets(i8* %67, i32 1024, i32* %68)
%70 = icmp eq i32* %69, null
br i1 %70, label %71, label %72
71: ; preds = %64
br label %79
72: ; preds = %64
%73 = load i32, i32* %5
%74 = load i8, i8* %7
%75 = sext i8 %74 to i32
%76 = load i32, i32* %6
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i32 %73, i32 %75, i32 %76, i8* %77)
br label %36
79: ; preds = %71, %42
%80 = load i32*, i32** %9
%81 = call i32 @fclose(i32* %80)
%82 = load i32*, i32** %9
%83 = call i32 @fclose(i32* %82)
ret void
}

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