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

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char getchar();
int pop();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ push(char);
int calculator(void) {
  char operand_1, operand_2, result;
  printf("enter an RPN expression:\n");
  char ch = getchar();
  while (ch != '\n') {
    if (ch > '0' && ch < '9') {
      push(ch);
      while ((ch = getchar()) == ' ') {
        ;
      }
    } else if (ch == '*' || ch == '+' || ch == '-' || ch == '/') {
      operand_1 = pop() - 48;
      operand_2 = pop() - 48;
      switch (ch) {
      case '*':
        result = operand_1 * operand_2;
        push(result);
        break;
      case '/':
        result = operand_1 / operand_2;
        push(result);
        break;
      case '+':
        result = operand_1 + operand_2;
        push(result);
        break;
      case '-':
        result = operand_1 - operand_2;
        push(result);
        break;
      }
      while ((ch = getchar()) == ' ') {
        ;
      }
    } else if (ch == '=') {
      break;
    } else {
      exit(0);
    }
  }
  int outcome = (int)pop();
  return outcome;
}


x86 assembly (clang, -O0):
<calculator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb9(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x4(%rbp)
movsbl -0x4(%rbp),%eax
cmp    $0xa,%eax
je     12ec <calculator+0x19c>
movsbl -0x4(%rbp),%eax
cmp    $0x30,%eax
jle    11be <calculator+0x6e>
movsbl -0x4(%rbp),%eax
cmp    $0x39,%eax
jge    11be <calculator+0x6e>
movsbl -0x4(%rbp),%edi
call   1060 <push@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x4(%rbp)
movsbl %al,%eax
cmp    $0x20,%eax
jne    11b9 <calculator+0x69>
jmp    119e <calculator+0x4e>
jmp    12e7 <calculator+0x197>
movsbl -0x4(%rbp),%eax
cmp    $0x2a,%eax
je     11f2 <calculator+0xa2>
movsbl -0x4(%rbp),%eax
cmp    $0x2b,%eax
je     11f2 <calculator+0xa2>
movsbl -0x4(%rbp),%eax
cmp    $0x2d,%eax
je     11f2 <calculator+0xa2>
movsbl -0x4(%rbp),%eax
cmp    $0x2f,%eax
jne    12c9 <calculator+0x179>
xor    %eax,%eax
mov    %al,-0x11(%rbp)
call   1040 <pop@plt>
mov    %eax,%ecx
mov    -0x11(%rbp),%al
add    $0xd0,%cl
mov    %cl,-0x1(%rbp)
call   1040 <pop@plt>
add    $0xd0,%al
mov    %al,-0x2(%rbp)
movsbl -0x4(%rbp),%eax
add    $0xffffffd6,%eax
mov    %eax,%ecx
mov    %rcx,-0x10(%rbp)
sub    $0x5,%eax
ja     12a4 <calculator+0x154>
mov    -0x10(%rbp),%rax
lea    0xdce(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsbl -0x1(%rbp),%eax
movsbl -0x2(%rbp),%ecx
imul   %ecx,%eax
mov    %al,-0x3(%rbp)
movsbl -0x3(%rbp),%edi
call   1060 <push@plt>
jmp    12a4 <calculator+0x154>
movsbl -0x1(%rbp),%eax
movsbl -0x2(%rbp),%ecx
cltd
idiv   %ecx
mov    %al,-0x3(%rbp)
movsbl -0x3(%rbp),%edi
call   1060 <push@plt>
jmp    12a4 <calculator+0x154>
movsbl -0x1(%rbp),%eax
movsbl -0x2(%rbp),%ecx
add    %ecx,%eax
mov    %al,-0x3(%rbp)
movsbl -0x3(%rbp),%edi
call   1060 <push@plt>
jmp    12a4 <calculator+0x154>
movsbl -0x1(%rbp),%eax
movsbl -0x2(%rbp),%ecx
sub    %ecx,%eax
mov    %al,-0x3(%rbp)
movsbl -0x3(%rbp),%edi
call   1060 <push@plt>
jmp    12a9 <calculator+0x159>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x4(%rbp)
movsbl %al,%eax
cmp    $0x20,%eax
jne    12c4 <calculator+0x174>
jmp    12a9 <calculator+0x159>
jmp    12e2 <calculator+0x192>
movsbl -0x4(%rbp),%eax
cmp    $0x3d,%eax
jne    12db <calculator+0x18b>
jmp    12ec <calculator+0x19c>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    12e7 <calculator+0x197>
jmp    116e <calculator+0x1e>
mov    $0x0,%al
call   1040 <pop@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculator>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xeba(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
jne    117f <calculator+0x2f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <pop@plt>
movabs $0xac0000000000,%r14
lea    0xe70(%rip),%r15
jmp    11a4 <calculator+0x54>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0xa,%al
je     116e <calculator+0x1e>
movsbl %al,%ebx
lea    -0x31(%rbx),%ecx
cmp    $0x7,%cl
ja     11d0 <calculator+0x80>
mov    %ebx,%edi
call   1060 <push@plt>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0x20,%al
je     11c0 <calculator+0x70>
jmp    11a0 <calculator+0x50>
nopl   (%rax)
cmp    $0x3d,%al
ja     124a <calculator+0xfa>
movzbl %bl,%eax
bt     %rax,%r14
jae    1240 <calculator+0xf0>
xor    %eax,%eax
call   1040 <pop@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <pop@plt>
add    $0xffffffd6,%ebx
cmp    $0x5,%ebx
ja     1230 <calculator+0xe0>
add    $0xd0,%bpl
add    $0xd0,%al
movslq (%r15,%rbx,4),%rcx
add    %r15,%rcx
jmp    *%rcx
mul    %bpl
jmp    1223 <calculator+0xd3>
add    %bpl,%al
jmp    1223 <calculator+0xd3>
sub    %al,%bpl
mov    %ebp,%eax
jmp    1223 <calculator+0xd3>
movsbl %bpl,%ecx
movsbl %al,%esi
mov    %ecx,%eax
cwtd
idiv   %si
movsbl %al,%edi
call   1060 <push@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0x20,%al
je     1230 <calculator+0xe0>
jmp    11a0 <calculator+0x50>
cmp    $0x3d,%rax
je     116e <calculator+0x1e>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"enter an RPN expression:\0A\00"
define i32 @calculator() {
%1 = alloca i8
%2 = alloca i8
%3 = alloca i8
%4 = alloca i8
%5 = alloca i32
%6 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%7 = call signext i8 (...) @getchar()
store i8 %7, i8* %4
br label %8
8: ; preds = %105, %0
%9 = load i8, i8* %4
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 10
br i1 %11, label %12, label %106
12: ; preds = %8
%13 = load i8, i8* %4
%14 = sext i8 %13 to i32
%15 = icmp sgt i32 %14, 48
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = load i8, i8* %4
%18 = sext i8 %17 to i32
%19 = icmp slt i32 %18, 57
br i1 %19, label %20, label %29
20: ; preds = %16
%21 = load i8, i8* %4
%22 = call i32 @push(i8 signext %21)
br label %23
23: ; preds = %27, %20
%24 = call signext i8 (...) @getchar()
store i8 %24, i8* %4
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 32
br i1 %26, label %27, label %28
27: ; preds = %23
br label %23
28: ; preds = %23
br label %105
29: ; preds = %16, %12
%30 = load i8, i8* %4
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 42
br i1 %32, label %45, label %33
33: ; preds = %29
%34 = load i8, i8* %4
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 43
br i1 %36, label %45, label %37
37: ; preds = %33
%38 = load i8, i8* %4
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 45
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = load i8, i8* %4
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 47
br i1 %44, label %45, label %97
45: ; preds = %41, %37, %33, %29
%46 = call i32 (...) @pop()
%47 = sub nsw i32 %46, 48
%48 = trunc i32 %47 to i8
store i8 %48, i8* %1
%49 = call i32 (...) @pop()
%50 = sub nsw i32 %49, 48
%51 = trunc i32 %50 to i8
store i8 %51, i8* %2
%52 = load i8, i8* %4
%53 = sext i8 %52 to i32
switch i32 %53, label %90 [
i32 42, label %54
i32 47, label %63
i32 43, label %72
i32 45, label %81
]
54: ; preds = %45
%55 = load i8, i8* %1
%56 = sext i8 %55 to i32
%57 = load i8, i8* %2
%58 = sext i8 %57 to i32
%59 = mul nsw i32 %56, %58
%60 = trunc i32 %59 to i8
store i8 %60, i8* %3
%61 = load i8, i8* %3
%62 = call i32 @push(i8 signext %61)
br label %90
63: ; preds = %45
%64 = load i8, i8* %1
%65 = sext i8 %64 to i32
%66 = load i8, i8* %2
%67 = sext i8 %66 to i32
%68 = sdiv i32 %65, %67
%69 = trunc i32 %68 to i8
store i8 %69, i8* %3
%70 = load i8, i8* %3
%71 = call i32 @push(i8 signext %70)
br label %90
72: ; preds = %45
%73 = load i8, i8* %1
%74 = sext i8 %73 to i32
%75 = load i8, i8* %2
%76 = sext i8 %75 to i32
%77 = add nsw i32 %74, %76
%78 = trunc i32 %77 to i8
store i8 %78, i8* %3
%79 = load i8, i8* %3
%80 = call i32 @push(i8 signext %79)
br label %90
81: ; preds = %45
%82 = load i8, i8* %1
%83 = sext i8 %82 to i32
%84 = load i8, i8* %2
%85 = sext i8 %84 to i32
%86 = sub nsw i32 %83, %85
%87 = trunc i32 %86 to i8
store i8 %87, i8* %3
%88 = load i8, i8* %3
%89 = call i32 @push(i8 signext %88)
br label %90
90: ; preds = %45, %81, %72, %63, %54
br label %91
91: ; preds = %95, %90
%92 = call signext i8 (...) @getchar()
store i8 %92, i8* %4
%93 = sext i8 %92 to i32
%94 = icmp eq i32 %93, 32
br i1 %94, label %95, label %96
95: ; preds = %91
br label %91
96: ; preds = %91
br label %104
97: ; preds = %41
%98 = load i8, i8* %4
%99 = sext i8 %98 to i32
%100 = icmp eq i32 %99, 61
br i1 %100, label %101, label %102
101: ; preds = %97
br label %106
102: ; preds = %97
%103 = call i32 @exit(i32 0)
unreachable
104: ; preds = %96
br label %105
105: ; preds = %104, %28
br label %8
106: ; preds = %101, %8
%107 = call i32 (...) @pop()
store i32 %107, i32* %5
%108 = load i32, i32* %5
ret i32 %108
}

LLVM IR (-O2):
@str = [25 x i8] c"enter an RPN expression:\00"
define i32 @calculator() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%2 = tail call signext i8 (...) @getchar()
%3 = icmp eq i8 %2, 10
br i1 %3, label %45, label %4
4: ; preds = %0, %42
%5 = phi i8 [ %43, %42 ], [ %2, %0 ]
%6 = sext i8 %5 to i32
%7 = add i8 %5, -49
%8 = icmp ult i8 %7, 8
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = tail call i32 @push(i8 signext %5)
br label %11
11: ; preds = %11, %9
%12 = tail call signext i8 (...) @getchar()
%13 = icmp eq i8 %12, 32
br i1 %13, label %11, label %42
14: ; preds = %4
switch i8 %5, label %40 [
i8 47, label %15
i8 45, label %15
i8 43, label %15
i8 42, label %15
i8 61, label %45
]
15: ; preds = %14, %14, %14, %14
%16 = tail call i32 (...) @pop()
%17 = trunc i32 %16 to i8
%18 = add i8 %17, -48
%19 = tail call i32 (...) @pop()
%20 = trunc i32 %19 to i8
%21 = add i8 %20, -48
switch i32 %6, label %36 [
i32 42, label %22
i32 47, label %24
i32 43, label %29
i32 45, label %31
]
22: ; preds = %15
%23 = mul i8 %21, %18
br label %33
24: ; preds = %15
%25 = sext i8 %18 to i16
%26 = sext i8 %21 to i16
%27 = sdiv i16 %25, %26
%28 = trunc i16 %27 to i8
br label %33
29: ; preds = %15
%30 = add i8 %21, %18
br label %33
31: ; preds = %15
%32 = sub i8 %18, %21
br label %33
33: ; preds = %22, %24, %29, %31
%34 = phi i8 [ %32, %31 ], [ %30, %29 ], [ %28, %24 ], [ %23, %22 ]
%35 = tail call i32 @push(i8 signext %34)
br label %36
36: ; preds = %33, %15
br label %37
37: ; preds = %36, %37
%38 = tail call signext i8 (...) @getchar()
%39 = icmp eq i8 %38, 32
br i1 %39, label %37, label %42
40: ; preds = %14
%41 = tail call i32 @exit(i32 0)
unreachable
42: ; preds = %37, %11
%43 = phi i8 [ %12, %11 ], [ %38, %37 ]
%44 = icmp eq i8 %43, 10
br i1 %44, label %45, label %4
45: ; preds = %42, %14, %0
%46 = tail call i32 (...) @pop()
ret i32 %46
}