#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*/ do_math(char *, int);
int /*<<< orphan*/ reset_string(char *, int);
char uart_getc();
int /*<<< orphan*/ uart_putc(char);
int /*<<< orphan*/ uart_puts(char *);
int calc_init() {
  int res = 0;
  int calc_on = 1;
  int i = 0;
  uart_puts(">>Welcome to the calculator application.\r\n");
  uart_puts("> ");
  int str_len = 80;
  char stringin[str_len];
  while (calc_on) {
    char x = uart_getc();
    if (i > str_len - 1) {
      uart_puts(stringin);
      reset_string(stringin, i);
      uart_puts("Max string len reached\r\n");
      i = 0;
    } else if (x == '\r') {
      if (stringin[0] == 113) {
        calc_on = 0;
        uart_puts("\r\n");
        uart_puts(">>Exiting calculator. Goodbye!\r\n");
        return 1;
      }
      do_math(stringin, i);
      reset_string(stringin, i);
      uart_puts("> ");
      i = 0;
    } else if (x == 127 || x == 8) {
      if (i > 0) {
        uart_putc('\b');
        uart_putc(' ');
        uart_putc('\b');
        stringin[i - 1] = 0x00;
        i--;
      }
    } else {
      stringin[i] = x;
      uart_putc(stringin[i]);
      i++;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<calc_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xe8c(%rip),%rdi
call   1070 <uart_puts@plt>
lea    0xeab(%rip),%rdi
call   1070 <uart_puts@plt>
movl   $0x50,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1317 <calc_init+0x1c7>
mov    $0x0,%al
call   1050 <uart_getc@plt>
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1204 <calc_init+0xb4>
mov    -0x38(%rbp),%rdi
call   1070 <uart_puts@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <reset_string@plt>
lea    0xe3b(%rip),%rdi
call   1070 <uart_puts@plt>
movl   $0x0,-0x10(%rbp)
jmp    1312 <calc_init+0x1c2>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
jne    1283 <calc_init+0x133>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x71,%eax
jne    1253 <calc_init+0x103>
movl   $0x0,-0xc(%rbp)
lea    0xe36(%rip),%rdi
call   1070 <uart_puts@plt>
lea    0xe0c(%rip),%rdi
call   1070 <uart_puts@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1325 <calc_init+0x1d5>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <do_math@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <reset_string@plt>
lea    0xdb9(%rip),%rdi
call   1070 <uart_puts@plt>
movl   $0x0,-0x10(%rbp)
jmp    130d <calc_init+0x1bd>
movsbl -0x29(%rbp),%eax
cmp    $0x7f,%eax
je     129d <calc_init+0x14d>
movsbl -0x29(%rbp),%eax
cmp    $0x8,%eax
jne    12e4 <calc_init+0x194>
cmpl   $0x0,-0x10(%rbp)
jle    12df <calc_init+0x18f>
mov    $0x8,%edi
call   1040 <uart_putc@plt>
mov    $0x20,%edi
call   1040 <uart_putc@plt>
mov    $0x8,%edi
call   1040 <uart_putc@plt>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1308 <calc_init+0x1b8>
mov    -0x38(%rbp),%rax
mov    -0x29(%rbp),%dl
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <uart_putc@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    130d <calc_init+0x1bd>
jmp    1312 <calc_init+0x1c2>
jmp    11b2 <calc_init+0x62>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_init>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
lea    0xe9e(%rip),%rdi
call   1070 <uart_puts@plt>
lea    0xebd(%rip),%r14
mov    %r14,%rdi
call   1070 <uart_puts@plt>
mov    %rsp,%r12
lea    0xeae(%rip),%r15
jmp    11aa <calc_init+0x5a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   1070 <uart_puts@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1060 <reset_string@plt>
mov    %r15,%rdi
call   1070 <uart_puts@plt>
xor    %ebx,%ebx
jmp    11b4 <calc_init+0x64>
xchg   %ax,%ax
test   %ebx,%ebx
jg     11df <calc_init+0x8f>
xor    %eax,%eax
call   1050 <uart_getc@plt>
cmp    $0x50,%ebx
jge    1190 <calc_init+0x40>
cmp    $0x8,%al
je     11b0 <calc_init+0x60>
cmp    $0x7f,%al
je     11b0 <calc_init+0x60>
cmp    $0xd,%al
je     1210 <calc_init+0xc0>
movslq %ebx,%rcx
mov    %al,(%rsp,%rcx,1)
movsbl %al,%edi
call   1040 <uart_putc@plt>
add    $0x1,%ebx
jmp    11b4 <calc_init+0x64>
mov    $0x8,%edi
call   1040 <uart_putc@plt>
mov    $0x20,%edi
call   1040 <uart_putc@plt>
mov    $0x8,%edi
call   1040 <uart_putc@plt>
add    $0xffffffff,%ebx
movb   $0x0,(%rsp,%rbx,1)
jmp    11b4 <calc_init+0x64>
cs nopw 0x0(%rax,%rax,1)
cmpb   $0x71,(%rsp)
je     1237 <calc_init+0xe7>
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <do_math@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1060 <reset_string@plt>
mov    %r14,%rdi
call   1070 <uart_puts@plt>
jmp    11aa <calc_init+0x5a>
lea    0xe27(%rip),%rdi
call   1070 <uart_puts@plt>
lea    0xdfd(%rip),%rdi
call   1070 <uart_puts@plt>
mov    $0x1,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [43 x i8] c">>Welcome to the calculator application.\0D\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [25 x i8] c"Max string len reached\0D\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
@.str.4 = [33 x i8] c">>Exiting calculator. Goodbye!\0D\0A\00"
define i32 @calc_init() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
store i32 0, i32* %2
store i32 1, i32* %3
store i32 0, i32* %4
%10 = call i32 @uart_puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 80, i32* %5
%12 = load i32, i32* %5
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
br label %16
16: ; preds = %84, %0
%17 = load i32, i32* %3
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %85
19: ; preds = %16
%20 = call signext i8 (...) @uart_getc()
store i8 %20, i8* %8
%21 = load i32, i32* %4
%22 = load i32, i32* %5
%23 = sub nsw i32 %22, 1
%24 = icmp sgt i32 %21, %23
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = call i32 @uart_puts(i8* %15)
%27 = load i32, i32* %4
%28 = call i32 @reset_string(i8* %15, i32 %27)
%29 = call i32 @uart_puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %84
30: ; preds = %19
%31 = load i8, i8* %8
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 13
br i1 %33, label %34, label %48
34: ; preds = %30
%35 = getelementptr inbounds i8, i8* %15, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 113
br i1 %38, label %39, label %42
39: ; preds = %34
store i32 0, i32* %3
%40 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @uart_puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
store i32 1, i32* %9
br label %86
42: ; preds = %34
%43 = load i32, i32* %4
%44 = call i32 @do_math(i8* %15, i32 %43)
%45 = load i32, i32* %4
%46 = call i32 @reset_string(i8* %15, i32 %45)
%47 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %83
48: ; preds = %30
%49 = load i8, i8* %8
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 127
br i1 %51, label %56, label %52
52: ; preds = %48
%53 = load i8, i8* %8
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 8
br i1 %55, label %56, label %70
56: ; preds = %52, %48
%57 = load i32, i32* %4
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %56
%60 = call i32 @uart_putc(i8 signext 8)
%61 = call i32 @uart_putc(i8 signext 32)
%62 = call i32 @uart_putc(i8 signext 8)
%63 = load i32, i32* %4
%64 = sub nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %15, i64 %65
store i8 0, i8* %66
%67 = load i32, i32* %4
%68 = add nsw i32 %67, -1
store i32 %68, i32* %4
br label %69
69: ; preds = %59, %56
br label %82
70: ; preds = %52
%71 = load i8, i8* %8
%72 = load i32, i32* %4
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %15, i64 %73
store i8 %71, i8* %74
%75 = load i32, i32* %4
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %15, i64 %76
%78 = load i8, i8* %77
%79 = call i32 @uart_putc(i8 signext %78)
%80 = load i32, i32* %4
%81 = add nsw i32 %80, 1
store i32 %81, i32* %4
br label %82
82: ; preds = %70, %69
br label %83
83: ; preds = %82, %42
br label %84
84: ; preds = %83, %25
br label %16
85: ; preds = %16
store i32 1, i32* %1
store i32 1, i32* %9
br label %86
86: ; preds = %85, %39
%87 = load i8*, i8** %6
%88 = load i32, i32* %1
ret i32 %88
}

LLVM IR (-O2):
@.str = [43 x i8] c">>Welcome to the calculator application.\0D\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [25 x i8] c"Max string len reached\0D\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
@.str.4 = [33 x i8] c">>Exiting calculator. Goodbye!\0D\0A\00"
define i32 @calc_init() {
%1 = tail call i32 @uart_puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %15, %0
%6 = phi i32 [ 0, %0 ], [ %16, %15 ]
%7 = icmp sgt i32 %6, 79
%8 = icmp sgt i32 %6, 0
br label %9
9: ; preds = %5, %28
%10 = call signext i8 (...) @uart_getc()
br i1 %7, label %11, label %17
11: ; preds = %9
%12 = call i32 @uart_puts(i8* %4)
%13 = call i32 @reset_string(i8* %4, i32 %6)
%14 = call i32 @uart_puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %11, %24, %29, %36
%16 = phi i32 [ %40, %36 ], [ %33, %29 ], [ 0, %24 ], [ 0, %11 ]
br label %5
17: ; preds = %9
switch i8 %10, label %36 [
i8 13, label %18
i8 127, label %28
i8 8, label %28
]
18: ; preds = %17
%19 = load i8, i8* %4
%20 = icmp eq i8 %19, 113
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @uart_puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
ret i32 1
24: ; preds = %18
%25 = call i32 @do_math(i8* %4, i32 %6)
%26 = call i32 @reset_string(i8* %4, i32 %6)
%27 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %15
28: ; preds = %17, %17
br i1 %8, label %29, label %9
29: ; preds = %28
%30 = call i32 @uart_putc(i8 signext 8)
%31 = call i32 @uart_putc(i8 signext 32)
%32 = call i32 @uart_putc(i8 signext 8)
%33 = add nsw i32 %6, -1
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %34
store i8 0, i8* %35
br label %15
36: ; preds = %17
%37 = sext i32 %6 to i64
%38 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 %37
store i8 %10, i8* %38
%39 = call i32 @uart_putc(i8 signext %10)
%40 = add nsw i32 %6, 1
br label %15
}