#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 */
double add(double, double);
double divide(double, double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ gets(char *);
double multiply(double, double);
double pow(double, double);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, double *, char *, double *);
int /*<<< orphan*/ stderr;
double subtract(double, double);
main() {
  int valid_input = 1;
  double x, y, result;
  char op;
  char inbuf[133];
  printf("Simple calculator - format is number operator number\n");
  printf("Supported operations:\n");
  printf("    + (add)  - (subt)  * (mult)  - (div)  ^ (power)\n\n");
  printf("Enter equation: ");
  gets(inbuf);
  if (sscanf(inbuf, "%lf %c %lf", &x, &op, &y) != 3)
    valid_input = 0;
  switch (op) {
  case '+':
    result = add(x, y);
    break;
  case '-':
    result = subtract(x, y);
    break;
  case '*':
    result = multiply(x, y);
    break;
  case '/':
    result = divide(x, y);
    break;
  case '^':
    result = pow(x, y);
    break;
  default:
    valid_input = 0;
    break;
  }
  if (valid_input)
    printf("%lf %c %lf = %lf\n", x, op, y, result);
  else
    fprintf(stderr, "Error.\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0xb0(%rbp),%rdi
call   1080 <gets@plt>
lea    -0xb0(%rbp),%rdi
lea    0xe99(%rip),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x21(%rbp),%rcx
lea    -0x18(%rbp),%r8
call   1090 <sscanf@plt>
cmp    $0x3,%eax
je     121c <main+0x8c>
movl   $0x0,-0x8(%rbp)
movsbl -0x21(%rbp),%eax
mov    %eax,-0xb4(%rbp)
sub    $0x2a,%eax
je     12b6 <main+0x126>
jmp    1234 <main+0xa4>
mov    -0xb4(%rbp),%eax
sub    $0x2b,%eax
je     1284 <main+0xf4>
jmp    1248 <main+0xb8>
mov    -0xb4(%rbp),%eax
sub    $0x2d,%eax
je     129d <main+0x10d>
jmp    125c <main+0xcc>
mov    -0xb4(%rbp),%eax
sub    $0x2f,%eax
je     12cf <main+0x13f>
jmp    1270 <main+0xe0>
mov    -0xb4(%rbp),%eax
sub    $0x5e,%eax
je     12e8 <main+0x158>
jmp    1301 <main+0x171>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
call   1030 <add@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    1308 <main+0x178>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
call   10a0 <subtract@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    1308 <main+0x178>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
call   1040 <multiply@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    1308 <main+0x178>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
call   1050 <divide@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    1308 <main+0x178>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
call   10b0 <pow@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    1308 <main+0x178>
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1338 <main+0x1a8>
movsd  -0x10(%rbp),%xmm0
movsbl -0x21(%rbp),%esi
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
lea    0xd73(%rip),%rdi
mov    $0x3,%al
call   1060 <printf@plt>
jmp    134d <main+0x1bd>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0xb0,%rsp
lea    0xe9f(%rip),%rdi
call   1050 <puts@plt>
lea    0xec8(%rip),%rdi
call   1050 <puts@plt>
lea    0xed2(%rip),%rdi
call   1050 <puts@plt>
lea    0xe45(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <gets@plt>
lea    0xe3b(%rip),%rsi
lea    0x18(%rsp),%rdx
lea    0xf(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
call   10a0 <sscanf@plt>
mov    %eax,%ebx
movsbl 0xf(%rsp),%ecx
lea    -0x2a(%rcx),%eax
cmp    $0x5,%eax
ja     1237 <main+0x97>
lea    0xde5(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsd  0x18(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
call   1040 <multiply@plt>
jmp    1286 <main+0xe6>
cmp    $0x5e,%ecx
jne    12af <main+0x10f>
movsd  0x18(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
call   10c0 <pow@plt>
jmp    1286 <main+0xe6>
movsd  0x18(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
call   1030 <add@plt>
jmp    1286 <main+0xe6>
movsd  0x18(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
call   10b0 <subtract@plt>
jmp    1286 <main+0xe6>
movsd  0x18(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
call   1060 <divide@plt>
movaps %xmm0,%xmm2
cmp    $0x3,%ebx
jne    12af <main+0x10f>
movsd  0x18(%rsp),%xmm0
movsbl 0xf(%rsp),%esi
movsd  0x10(%rsp),%xmm1
lea    0xd8e(%rip),%rdi
mov    $0x3,%al
call   1070 <printf@plt>
jmp    12c4 <main+0x124>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
call   1080 <fprintf@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [54 x i8] c"Simple calculator - format is number operator number\0A\00"
@.str.1 = [23 x i8] c"Supported operations:\0A\00"
@.str.2 = [54 x i8] c" + (add) - (subt) * (mult) - (div) ^ (power)\0A\0A\00"
@.str.3 = [17 x i8] c"Enter equation: \00"
@.str.4 = [11 x i8] c"%lf %c %lf\00"
@.str.5 = [18 x i8] c"%lf %c %lf = %lf\0A\00"
@stderr = global i32 0
@.str.6 = [8 x i8] c"Error.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca i8
%7 = alloca [133 x i8]
store i32 0, i32* %1
store i32 1, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%12 = getelementptr inbounds [133 x i8], [133 x i8]* %7, i64 0, i64 0
%13 = call i32 @gets(i8* %12)
%14 = getelementptr inbounds [133 x i8], [133 x i8]* %7, i64 0, i64 0
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), double* %3, i8* %6, double* %4)
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %18
17: ; preds = %0
store i32 0, i32* %2
br label %18
18: ; preds = %17, %0
%19 = load i8, i8* %6
%20 = sext i8 %19 to i32
switch i32 %20, label %41 [
i32 43, label %21
i32 45, label %25
i32 42, label %29
i32 47, label %33
i32 94, label %37
]
21: ; preds = %18
%22 = load double, double* %3
%23 = load double, double* %4
%24 = call double @add(double %22, double %23)
store double %24, double* %5
br label %42
25: ; preds = %18
%26 = load double, double* %3
%27 = load double, double* %4
%28 = call double @subtract(double %26, double %27)
store double %28, double* %5
br label %42
29: ; preds = %18
%30 = load double, double* %3
%31 = load double, double* %4
%32 = call double @multiply(double %30, double %31)
store double %32, double* %5
br label %42
33: ; preds = %18
%34 = load double, double* %3
%35 = load double, double* %4
%36 = call double @divide(double %34, double %35)
store double %36, double* %5
br label %42
37: ; preds = %18
%38 = load double, double* %3
%39 = load double, double* %4
%40 = call double @pow(double %38, double %39)
store double %40, double* %5
br label %42
41: ; preds = %18
store i32 0, i32* %2
br label %42
42: ; preds = %41, %37, %33, %29, %25, %21
%43 = load i32, i32* %2
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %42
%46 = load double, double* %3
%47 = load i8, i8* %6
%48 = sext i8 %47 to i32
%49 = load double, double* %4
%50 = load double, double* %5
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), double %46, i32 %48, double %49, double %50)
br label %55
52: ; preds = %42
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %52, %45
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str.3 = [17 x i8] c"Enter equation: \00"
@.str.4 = [11 x i8] c"%lf %c %lf\00"
@.str.5 = [18 x i8] c"%lf %c %lf = %lf\0A\00"
@stderr = global i32 0
@.str.6 = [8 x i8] c"Error.\0A\00"
@str = [53 x i8] c"Simple calculator - format is number operator number\00"
@str.7 = [22 x i8] c"Supported operations:\00"
@str.8 = [53 x i8] c" + (add) - (subt) * (mult) - (div) ^ (power)\0A\00"
define i32 @main() {
%1 = alloca double
%2 = alloca double
%3 = alloca i8
%4 = alloca [133 x i8]
%5 = bitcast double* %1 to i8*
%6 = bitcast double* %2 to i8*
%7 = getelementptr inbounds [133 x i8], [133 x i8]* %4, i64 0, i64 0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.7, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.8, i64 0, i64 0))
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%12 = call i32 @gets(i8* %7)
%13 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), double* %1, i8* %3, double* %2)
%14 = icmp eq i32 %13, 3
%15 = load i8, i8* %3
%16 = sext i8 %15 to i32
switch i32 %16, label %45 [
i32 43, label %17
i32 45, label %21
i32 42, label %25
i32 47, label %29
i32 94, label %33
]
17: ; preds = %0
%18 = load double, double* %1
%19 = load double, double* %2
%20 = call double @add(double %18, double %19)
br label %37
21: ; preds = %0
%22 = load double, double* %1
%23 = load double, double* %2
%24 = call double @subtract(double %22, double %23)
br label %37
25: ; preds = %0
%26 = load double, double* %1
%27 = load double, double* %2
%28 = call double @multiply(double %26, double %27)
br label %37
29: ; preds = %0
%30 = load double, double* %1
%31 = load double, double* %2
%32 = call double @divide(double %30, double %31)
br label %37
33: ; preds = %0
%34 = load double, double* %1
%35 = load double, double* %2
%36 = call double @pow(double %34, double %35)
br label %37
37: ; preds = %33, %29, %25, %21, %17
%38 = phi double [ %36, %33 ], [ %32, %29 ], [ %28, %25 ], [ %24, %21 ], [ %20, %17 ]
br i1 %14, label %39, label %45
39: ; preds = %37
%40 = load double, double* %1
%41 = load i8, i8* %3
%42 = sext i8 %41 to i32
%43 = load double, double* %2
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), double %40, i32 %42, double %43, double %38)
br label %48
45: ; preds = %0, %37
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %45, %39
ret i32 0
}