#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 next_num(char *, int *, int *);
char next_tok(char *, int *);
int strlen(char *);
int /*<<< orphan*/ total_trim(char *);
int calc(char *expr, int *status) {
  int pos = 0;
  int num = 0, result = 0;
  char op = 0;
  total_trim(expr);
  result = next_num(expr, &pos, status);
  while (pos < strlen(expr)) {
    op = next_tok(expr, &pos);
    num = next_num(expr, &pos, status);
    switch (op) {
    case 0:
      *status = 1;
      return 0;
      break;
    case '+':
      result += num;
      break;
    case '-':
      result -= num;
      break;
    case '/':
      result /= num;
      break;
    case '*':
      result *= num;
      break;
    case '%':
      result %= num;
      break;
    case '&':
      result &= num;
      break;
    case '|':
      result |= num;
      break;
    case '^':
      result ^= num;
      break;
    default:
      *status = 2;
      return 0;
      break;
    }
  }
  *status = 0;
  return result;
}


x86 assembly (clang, -O0):
<calc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movb   $0x0,-0x25(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <total_trim@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rsi
call   1040 <next_num@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jge    1310 <calc+0x1d0>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    %rsi,-0x38(%rbp)
call   1060 <next_tok@plt>
mov    -0x38(%rbp),%rsi
mov    %al,-0x25(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1040 <next_num@plt>
mov    %eax,-0x20(%rbp)
movsbl -0x25(%rbp),%eax
mov    %eax,-0x30(%rbp)
test   %eax,%eax
je     1266 <calc+0x126>
jmp    11de <calc+0x9e>
mov    -0x30(%rbp),%eax
sub    $0x25,%eax
je     12ba <calc+0x17a>
jmp    11ef <calc+0xaf>
mov    -0x30(%rbp),%eax
sub    $0x26,%eax
je     12cb <calc+0x18b>
jmp    1200 <calc+0xc0>
mov    -0x30(%rbp),%eax
sub    $0x2a,%eax
je     12ab <calc+0x16b>
jmp    1211 <calc+0xd1>
mov    -0x30(%rbp),%eax
sub    $0x2b,%eax
je     127c <calc+0x13c>
jmp    1222 <calc+0xe2>
mov    -0x30(%rbp),%eax
sub    $0x2d,%eax
je     128a <calc+0x14a>
jmp    1233 <calc+0xf3>
mov    -0x30(%rbp),%eax
sub    $0x2f,%eax
je     129a <calc+0x15a>
jmp    1244 <calc+0x104>
mov    -0x30(%rbp),%eax
sub    $0x5e,%eax
je     12e7 <calc+0x1a7>
jmp    1255 <calc+0x115>
mov    -0x30(%rbp),%eax
sub    $0x7c,%eax
je     12d9 <calc+0x199>
jmp    12f5 <calc+0x1b5>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1320 <calc+0x1e0>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    130b <calc+0x1cb>
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    130b <calc+0x1cb>
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
jmp    130b <calc+0x1cb>
mov    -0x20(%rbp),%eax
imul   -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    130b <calc+0x1cb>
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x24(%rbp)
jmp    130b <calc+0x1cb>
mov    -0x20(%rbp),%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    130b <calc+0x1cb>
mov    -0x20(%rbp),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    130b <calc+0x1cb>
mov    -0x20(%rbp),%eax
xor    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    130b <calc+0x1cb>
mov    -0x18(%rbp),%rax
movl   $0x2,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1320 <calc+0x1e0>
jmp    1186 <calc+0x46>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
movl   $0x0,(%rsp)
call   1050 <total_trim@plt>
mov    %rsp,%rsi
mov    %rbp,%rdi
mov    %r14,%rdx
call   1040 <next_num@plt>
mov    %eax,%r12d
mov    (%rsp),%ebx
mov    %rbp,%rdi
call   1030 <strlen@plt>
movl   $0x0,0x4(%rsp)
cmp    %eax,%ebx
jge    1216 <calc+0xd6>
mov    %rsp,%r15
lea    0xe6d(%rip),%r13
jmp    11af <calc+0x6f>
xor    %ecx,%r12d
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%ebx
mov    %rbp,%rdi
call   1030 <strlen@plt>
cmp    %eax,%ebx
jge    1216 <calc+0xd6>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <next_tok@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <next_num@plt>
mov    %eax,%ecx
movsbl %bl,%eax
lea    -0x25(%rax),%edx
cmp    $0x39,%edx
ja     11ec <calc+0xac>
movslq 0x0(%r13,%rdx,4),%rax
add    %r13,%rax
jmp    *%rax
mov    %r12d,%eax
cltd
idiv   %ecx
mov    %edx,%r12d
jmp    11a0 <calc+0x60>
cmp    $0x7c,%eax
jne    122f <calc+0xef>
or     %ecx,%r12d
jmp    11a0 <calc+0x60>
and    %ecx,%r12d
jmp    11a0 <calc+0x60>
imul   %ecx,%r12d
jmp    11a0 <calc+0x60>
add    %ecx,%r12d
jmp    11a0 <calc+0x60>
sub    %ecx,%r12d
jmp    11a0 <calc+0x60>
mov    %r12d,%eax
cltd
idiv   %ecx
mov    %eax,%r12d
jmp    11a0 <calc+0x60>
mov    %r12d,%eax
mov    0x4(%rsp),%ecx
mov    %ecx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movl   $0x1,0x4(%rsp)
test   %eax,%eax
je     1219 <calc+0xd9>
xor    %eax,%eax
movl   $0x2,0x4(%rsp)
jmp    1219 <calc+0xd9>

LLVM IR (-O0):
define i32 @calc(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i8 0, i8* %9
%10 = load i8*, i8** %4
%11 = call i32 @total_trim(i8* %10)
%12 = load i8*, i8** %4
%13 = load i32*, i32** %5
%14 = call i32 @next_num(i8* %12, i32* %6, i32* %13)
store i32 %14, i32* %8
br label %15
15: ; preds = %64, %2
%16 = load i32, i32* %6
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %65
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = call signext i8 @next_tok(i8* %21, i32* %6)
store i8 %22, i8* %9
%23 = load i8*, i8** %4
%24 = load i32*, i32** %5
%25 = call i32 @next_num(i8* %23, i32* %6, i32* %24)
store i32 %25, i32* %7
%26 = load i8, i8* %9
%27 = sext i8 %26 to i32
switch i32 %27, label %62 [
i32 0, label %28
i32 43, label %30
i32 45, label %34
i32 47, label %38
i32 42, label %42
i32 37, label %46
i32 38, label %50
i32 124, label %54
i32 94, label %58
]
28: ; preds = %20
%29 = load i32*, i32** %5
store i32 1, i32* %29
store i32 0, i32* %3
br label %68
30: ; preds = %20
%31 = load i32, i32* %7
%32 = load i32, i32* %8
%33 = add nsw i32 %32, %31
store i32 %33, i32* %8
br label %64
34: ; preds = %20
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %8
br label %64
38: ; preds = %20
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = sdiv i32 %40, %39
store i32 %41, i32* %8
br label %64
42: ; preds = %20
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = mul nsw i32 %44, %43
store i32 %45, i32* %8
br label %64
46: ; preds = %20
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = srem i32 %48, %47
store i32 %49, i32* %8
br label %64
50: ; preds = %20
%51 = load i32, i32* %7
%52 = load i32, i32* %8
%53 = and i32 %52, %51
store i32 %53, i32* %8
br label %64
54: ; preds = %20
%55 = load i32, i32* %7
%56 = load i32, i32* %8
%57 = or i32 %56, %55
store i32 %57, i32* %8
br label %64
58: ; preds = %20
%59 = load i32, i32* %7
%60 = load i32, i32* %8
%61 = xor i32 %60, %59
store i32 %61, i32* %8
br label %64
62: ; preds = %20
%63 = load i32*, i32** %5
store i32 2, i32* %63
store i32 0, i32* %3
br label %68
64: ; preds = %58, %54, %50, %46, %42, %38, %34, %30
br label %15
65: ; preds = %15
%66 = load i32*, i32** %5
store i32 0, i32* %66
%67 = load i32, i32* %8
store i32 %67, i32* %3
br label %68
68: ; preds = %65, %62, %28
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
define i32 @calc(i8* %0, i32* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i32 @total_trim(i8* %0)
%6 = call i32 @next_num(i8* %0, i32* %3, i32* %1)
%7 = load i32, i32* %3
%8 = call i32 @strlen(i8* %0)
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %37
10: ; preds = %2, %31
%11 = phi i32 [ %32, %31 ], [ %6, %2 ]
%12 = call signext i8 @next_tok(i8* %0, i32* %3)
%13 = call i32 @next_num(i8* %0, i32* %3, i32* %1)
%14 = sext i8 %12 to i32
switch i32 %14, label %36 [
i32 0, label %37
i32 43, label %15
i32 45, label %17
i32 47, label %19
i32 42, label %21
i32 37, label %23
i32 38, label %25
i32 124, label %27
i32 94, label %29
]
15: ; preds = %10
%16 = add nsw i32 %13, %11
br label %31
17: ; preds = %10
%18 = sub nsw i32 %11, %13
br label %31
19: ; preds = %10
%20 = sdiv i32 %11, %13
br label %31
21: ; preds = %10
%22 = mul nsw i32 %13, %11
br label %31
23: ; preds = %10
%24 = srem i32 %11, %13
br label %31
25: ; preds = %10
%26 = and i32 %13, %11
br label %31
27: ; preds = %10
%28 = or i32 %13, %11
br label %31
29: ; preds = %10
%30 = xor i32 %13, %11
br label %31
31: ; preds = %29, %27, %25, %23, %21, %19, %17, %15
%32 = phi i32 [ %30, %29 ], [ %28, %27 ], [ %26, %25 ], [ %24, %23 ], [ %22, %21 ], [ %20, %19 ], [ %18, %17 ], [ %16, %15 ]
%33 = load i32, i32* %3
%34 = call i32 @strlen(i8* %0)
%35 = icmp slt i32 %33, %34
br i1 %35, label %10, label %37
36: ; preds = %10
br label %37
37: ; preds = %31, %10, %36, %2
%38 = phi i32 [ 0, %2 ], [ 2, %36 ], [ 0, %31 ], [ 1, %10 ]
%39 = phi i32 [ %6, %2 ], [ 0, %36 ], [ %32, %31 ], [ %14, %10 ]
store i32 %38, i32* %1
ret i32 %39
}