#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 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;
  uart_puts(">>Welcome to the calculator application.\r\n");
  uart_puts("> ");
  while (calc_on) {
    int str_len = 80;
    char stringin[str_len];
    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;
      }
      res = 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)
lea    0xe93(%rip),%rdi
call   1070 <uart_puts@plt>
lea    0xeb2(%rip),%rdi
call   1070 <uart_puts@plt>
cmpl   $0x0,-0xc(%rbp)
je     1336 <calc_init+0x1e6>
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)
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    11fd <calc_init+0xad>
mov    -0x38(%rbp),%rdi
call   1070 <uart_puts@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <reset_string@plt>
lea    0xe42(%rip),%rdi
call   1070 <uart_puts@plt>
movl   $0x0,-0x10(%rbp)
jmp    130e <calc_init+0x1be>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
jne    127f <calc_init+0x12f>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x71,%eax
jne    124c <calc_init+0xfc>
movl   $0x0,-0xc(%rbp)
lea    0xe3d(%rip),%rdi
call   1070 <uart_puts@plt>
lea    0xe13(%rip),%rdi
call   1070 <uart_puts@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1315 <calc_init+0x1c5>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <do_math@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%esi
call   1060 <reset_string@plt>
lea    0xdbd(%rip),%rdi
call   1070 <uart_puts@plt>
movl   $0x0,-0x10(%rbp)
jmp    1309 <calc_init+0x1b9>
movsbl -0x29(%rbp),%eax
cmp    $0x7f,%eax
je     1299 <calc_init+0x149>
movsbl -0x29(%rbp),%eax
cmp    $0x8,%eax
jne    12e0 <calc_init+0x190>
cmpl   $0x0,-0x10(%rbp)
jle    12db <calc_init+0x18b>
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    1304 <calc_init+0x1b4>
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    1309 <calc_init+0x1b9>
jmp    130e <calc_init+0x1be>
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     1331 <calc_init+0x1e1>
jmp    132c <calc_init+0x1dc>
jmp    133d <calc_init+0x1ed>
jmp    117e <calc_init+0x2e>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_init>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9b(%rip),%rdi
call   1070 <uart_puts@plt>
lea    0xeba(%rip),%r14
mov    %r14,%rdi
call   1070 <uart_puts@plt>
lea    0xeae(%rip),%r15
jmp    119b <calc_init+0x4b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1070 <uart_puts@plt>
xor    %r12d,%r12d
mov    %r13,%rsp
mov    %rsp,%r13
mov    %rsp,%rbx
add    $0xffffffffffffffb0,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1050 <uart_getc@plt>
cmp    $0x50,%r12d
jge    1230 <calc_init+0xe0>
cmp    $0x8,%al
je     11f0 <calc_init+0xa0>
cmp    $0x7f,%al
je     11f0 <calc_init+0xa0>
cmp    $0xd,%al
jne    124b <calc_init+0xfb>
cmpb   $0x71,(%rbx)
je     1265 <calc_init+0x115>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <do_math@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1060 <reset_string@plt>
mov    %r14,%rdi
jmp    1190 <calc_init+0x40>
nopl   0x0(%rax)
test   %r12d,%r12d
jle    125d <calc_init+0x10d>
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,%r12d
movb   $0x0,(%rbx,%r12,1)
mov    %r13,%rsp
jmp    119b <calc_init+0x4b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1070 <uart_puts@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1060 <reset_string@plt>
mov    %r15,%rdi
jmp    1190 <calc_init+0x40>
movslq %r12d,%rcx
mov    %al,(%rbx,%rcx,1)
movsbl %al,%edi
call   1040 <uart_putc@plt>
add    $0x1,%r12d
mov    %r13,%rsp
jmp    119b <calc_init+0x4b>
lea    0xdf9(%rip),%rdi
call   1070 <uart_puts@plt>
lea    0xdcf(%rip),%rdi
call   1070 <uart_puts@plt>
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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
%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))
br label %12
12: ; preds = %88, %0
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %89
15: ; preds = %12
store i32 80, i32* %5
%16 = load i32, i32* %5
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %6
%19 = alloca i8, i64 %17
store i64 %17, i64* %7
%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 = %15
%26 = call i32 @uart_puts(i8* %19)
%27 = load i32, i32* %4
%28 = call i32 @reset_string(i8* %19, 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 = %15
%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* %19, 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 %85
42: ; preds = %34
%43 = load i32, i32* %4
%44 = call i32 @do_math(i8* %19, i32 %43)
store i32 %44, i32* %2
%45 = load i32, i32* %4
%46 = call i32 @reset_string(i8* %19, 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* %19, 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* %19, i64 %73
store i8 %71, i8* %74
%75 = load i32, i32* %4
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %19, 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
store i32 0, i32* %9
br label %85
85: ; preds = %84, %39
%86 = load i8*, i8** %6
%87 = load i32, i32* %9
switch i32 %87, label %92 [
i32 0, label %88
i32 1, label %90
]
88: ; preds = %85
br label %12
89: ; preds = %12
store i32 1, i32* %1
br label %90
90: ; preds = %89, %85
%91 = load i32, i32* %1
ret i32 %91
92: ; preds = %85
unreachable
}

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))
br label %3
3: ; preds = %39, %0
%4 = phi i32 [ undef, %0 ], [ %40, %39 ]
%5 = call i8* @llvm.stacksave()
%6 = alloca [80 x i8]
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%8 = call signext i8 (...) @uart_getc()
%9 = icmp sgt i32 %4, 79
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = call i32 @uart_puts(i8* %7)
%12 = call i32 @reset_string(i8* %7, i32 %4)
%13 = call i32 @uart_puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %39
14: ; preds = %3
switch i8 %8, label %34 [
i8 13, label %15
i8 127, label %25
i8 8, label %25
]
15: ; preds = %14
%16 = load i8, i8* %7
%17 = icmp eq i8 %16, 113
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @uart_puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
ret i32 1
21: ; preds = %15
%22 = call i32 @do_math(i8* %7, i32 %4)
%23 = call i32 @reset_string(i8* %7, i32 %4)
%24 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %39
25: ; preds = %14, %14
%26 = icmp sgt i32 %4, 0
br i1 %26, label %27, label %39
27: ; preds = %25
%28 = call i32 @uart_putc(i8 signext 8)
%29 = call i32 @uart_putc(i8 signext 32)
%30 = call i32 @uart_putc(i8 signext 8)
%31 = add nsw i32 %4, -1
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %32
store i8 0, i8* %33
br label %39
34: ; preds = %14
%35 = sext i32 %4 to i64
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %35
store i8 %8, i8* %36
%37 = call i32 @uart_putc(i8 signext %8)
%38 = add nsw i32 %4, 1
br label %39
39: ; preds = %10, %34, %27, %25, %21
%40 = phi i32 [ 0, %10 ], [ 0, %21 ], [ %31, %27 ], [ %4, %25 ], [ %38, %34 ]
br label %3
}