#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 */
char *digit_to_string(double);
int /*<<< orphan*/ exit(int);
int find_character(char *, char);
int *left_argument(char *, int);
double pow(double, double);
int /*<<< orphan*/ printf(char *, ...);
char *replace_sub_string(char *, int, int, char *);
int *right_argument(char *, int);
double string_to_digit(int /*<<< orphan*/);
int /*<<< orphan*/ sub_string(char *, int, int);
char *perform_operation(char buffer[], char operator) {
  int index = find_character(buffer, operator);
  int *left, *right;
  double x, y;
  char *input;
  if (index != -1) {
    left = left_argument(buffer, index);
    right = right_argument(buffer, index);
    if (right[0] > right[1]) {
      printf("ERROR, no right argument:%s\n", buffer);
      printf("Index of operator:%d\n", index);
      printf("Left argument:%d->%d\n", left[0], left[1]);
      printf("Right argument:%d->%d\n", right[0], right[1]);
      exit(1);
    }
    if (left[0] == -1)
      x = 0.0;
    else
      x = string_to_digit(sub_string(buffer, left[0], left[1]));
    y = string_to_digit(sub_string(buffer, right[0], right[1]));
    if (operator== '+')
      input = digit_to_string(x + y);
    else if (operator== '-')
      input = digit_to_string(x - y);
    else if (operator== '*')
      input = digit_to_string(x * y);
    else if (operator== '/')
      input = digit_to_string(x / y);
    else if (operator== '^')
      input = digit_to_string(pow(x, y));
    buffer = replace_sub_string(buffer, left[0], right[1], input);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<perform_operation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <find_character@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     13ab <perform_operation+0x20b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1050 <left_argument@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1090 <right_argument@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x20(%rbp),%rcx
cmp    0x4(%rcx),%eax
jle    1262 <perform_operation+0xc2>
mov    -0x8(%rbp),%rsi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    127c <perform_operation+0xdc>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    129e <perform_operation+0xfe>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <sub_string@plt>
mov    %eax,%edi
call   1040 <string_to_digit@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <sub_string@plt>
mov    %eax,%edi
call   1040 <string_to_digit@plt>
movsd  %xmm0,-0x30(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x2b,%eax
jne    12e5 <perform_operation+0x145>
movsd  -0x28(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
call   1080 <digit_to_string@plt>
mov    %rax,-0x38(%rbp)
jmp    138d <perform_operation+0x1ed>
movsbl -0x9(%rbp),%eax
cmp    $0x2d,%eax
jne    130a <perform_operation+0x16a>
movsd  -0x28(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
call   1080 <digit_to_string@plt>
mov    %rax,-0x38(%rbp)
jmp    1388 <perform_operation+0x1e8>
movsbl -0x9(%rbp),%eax
cmp    $0x2a,%eax
jne    132f <perform_operation+0x18f>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
call   1080 <digit_to_string@plt>
mov    %rax,-0x38(%rbp)
jmp    1383 <perform_operation+0x1e3>
movsbl -0x9(%rbp),%eax
cmp    $0x2f,%eax
jne    1354 <perform_operation+0x1b4>
movsd  -0x28(%rbp),%xmm0
divsd  -0x30(%rbp),%xmm0
call   1080 <digit_to_string@plt>
mov    %rax,-0x38(%rbp)
jmp    137e <perform_operation+0x1de>
movsbl -0x9(%rbp),%eax
cmp    $0x5e,%eax
jne    1379 <perform_operation+0x1d9>
movsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
call   10a0 <pow@plt>
call   1080 <digit_to_string@plt>
mov    %rax,-0x38(%rbp)
jmp    137e <perform_operation+0x1de>
jmp    1383 <perform_operation+0x1e3>
jmp    1388 <perform_operation+0x1e8>
jmp    138d <perform_operation+0x1ed>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x38(%rbp),%rcx
call   10c0 <replace_sub_string@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perform_operation>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r12
call   1030 <find_character@plt>
cmp    $0xffffffff,%eax
je     1210 <perform_operation+0x70>
mov    %eax,%ebx
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <left_argument@plt>
mov    %rax,%r15
mov    %r12,%rdi
mov    %ebx,%esi
call   1090 <right_argument@plt>
mov    %rax,%r14
mov    (%rax),%esi
mov    0x4(%rax),%edx
cmp    %edx,%esi
jg     12b9 <perform_operation+0x119>
mov    (%r15),%eax
cmp    $0xffffffff,%eax
je     1220 <perform_operation+0x80>
mov    0x4(%r15),%edx
mov    %r12,%rdi
mov    %eax,%esi
call   1070 <sub_string@plt>
mov    %eax,%edi
call   1040 <string_to_digit@plt>
movsd  %xmm0,0x8(%rsp)
mov    (%r14),%esi
mov    0x4(%r14),%edx
jmp    122a <perform_operation+0x8a>
mov    %r12,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r12,%rdi
call   1070 <sub_string@plt>
mov    %eax,%edi
call   1040 <string_to_digit@plt>
movapd %xmm0,%xmm1
add    $0xd6,%bpl
cmp    $0x5,%bpl
ja     128b <perform_operation+0xeb>
movzbl %bpl,%eax
lea    0xdae(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsd  0x8(%rsp),%xmm0
mulsd  %xmm1,%xmm0
jmp    1296 <perform_operation+0xf6>
movsd  0x8(%rsp),%xmm0
divsd  %xmm1,%xmm0
jmp    1296 <perform_operation+0xf6>
movsd  0x8(%rsp),%xmm0
addsd  %xmm1,%xmm0
jmp    1296 <perform_operation+0xf6>
movsd  0x8(%rsp),%xmm0
subsd  %xmm1,%xmm0
jmp    1296 <perform_operation+0xf6>
movsd  0x8(%rsp),%xmm0
call   10a0 <pow@plt>
call   1080 <digit_to_string@plt>
mov    (%r15),%esi
mov    0x4(%r14),%edx
mov    %r12,%rdi
mov    %rax,%rcx
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <replace_sub_string@plt>
lea    0xd58(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd64(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r15),%esi
mov    0x4(%r15),%edx
lea    0xd63(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r14),%esi
mov    0x4(%r14),%edx
lea    0xd64(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"ERROR, no right argument:%s\0A\00"
@.str.1 = [22 x i8] c"Index of operator:%d\0A\00"
@.str.2 = [22 x i8] c"Left argument:%d->%d\0A\00"
@.str.3 = [23 x i8] c"Right argument:%d->%d\0A\00"
define i8* @perform_operation(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca double
%9 = alloca double
%10 = alloca i8*
store i8* %0, i8** %3
store i8 %1, i8* %4
%11 = load i8*, i8** %3
%12 = load i8, i8* %4
%13 = call i32 @find_character(i8* %11, i8 signext %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %134
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = load i32, i32* %5
%19 = call i32* @left_argument(i8* %17, i32 %18)
store i32* %19, i32** %6
%20 = load i8*, i8** %3
%21 = load i32, i32* %5
%22 = call i32* @right_argument(i8* %20, i32 %21)
store i32* %22, i32** %7
%23 = load i32*, i32** %7
%24 = getelementptr inbounds i32, i32* %23, i64 0
%25 = load i32, i32* %24
%26 = load i32*, i32** %7
%27 = getelementptr inbounds i32, i32* %26, i64 1
%28 = load i32, i32* %27
%29 = icmp sgt i32 %25, %28
br i1 %29, label %30, label %50
30: ; preds = %16
%31 = load i8*, i8** %3
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = load i32, i32* %5
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32*, i32** %6
%36 = getelementptr inbounds i32, i32* %35, i64 0
%37 = load i32, i32* %36
%38 = load i32*, i32** %6
%39 = getelementptr inbounds i32, i32* %38, i64 1
%40 = load i32, i32* %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %40)
%42 = load i32*, i32** %7
%43 = getelementptr inbounds i32, i32* %42, i64 0
%44 = load i32, i32* %43
%45 = load i32*, i32** %7
%46 = getelementptr inbounds i32, i32* %45, i64 1
%47 = load i32, i32* %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %16
%51 = load i32*, i32** %6
%52 = getelementptr inbounds i32, i32* %51, i64 0
%53 = load i32, i32* %52
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %56
55: ; preds = %50
store double 0.000000e+00, double* %8
br label %66
56: ; preds = %50
%57 = load i8*, i8** %3
%58 = load i32*, i32** %6
%59 = getelementptr inbounds i32, i32* %58, i64 0
%60 = load i32, i32* %59
%61 = load i32*, i32** %6
%62 = getelementptr inbounds i32, i32* %61, i64 1
%63 = load i32, i32* %62
%64 = call i32 @sub_string(i8* %57, i32 %60, i32 %63)
%65 = call double @string_to_digit(i32 %64)
store double %65, double* %8
br label %66
66: ; preds = %56, %55
%67 = load i8*, i8** %3
%68 = load i32*, i32** %7
%69 = getelementptr inbounds i32, i32* %68, i64 0
%70 = load i32, i32* %69
%71 = load i32*, i32** %7
%72 = getelementptr inbounds i32, i32* %71, i64 1
%73 = load i32, i32* %72
%74 = call i32 @sub_string(i8* %67, i32 %70, i32 %73)
%75 = call double @string_to_digit(i32 %74)
store double %75, double* %9
%76 = load i8, i8* %4
%77 = sext i8 %76 to i32
%78 = icmp eq i32 %77, 43
br i1 %78, label %79, label %84
79: ; preds = %66
%80 = load double, double* %8
%81 = load double, double* %9
%82 = fadd double %80, %81
%83 = call i8* @digit_to_string(double %82)
store i8* %83, i8** %10
br label %124
84: ; preds = %66
%85 = load i8, i8* %4
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 45
br i1 %87, label %88, label %93
88: ; preds = %84
%89 = load double, double* %8
%90 = load double, double* %9
%91 = fsub double %89, %90
%92 = call i8* @digit_to_string(double %91)
store i8* %92, i8** %10
br label %123
93: ; preds = %84
%94 = load i8, i8* %4
%95 = sext i8 %94 to i32
%96 = icmp eq i32 %95, 42
br i1 %96, label %97, label %102
97: ; preds = %93
%98 = load double, double* %8
%99 = load double, double* %9
%100 = fmul double %98, %99
%101 = call i8* @digit_to_string(double %100)
store i8* %101, i8** %10
br label %122
102: ; preds = %93
%103 = load i8, i8* %4
%104 = sext i8 %103 to i32
%105 = icmp eq i32 %104, 47
br i1 %105, label %106, label %111
106: ; preds = %102
%107 = load double, double* %8
%108 = load double, double* %9
%109 = fdiv double %107, %108
%110 = call i8* @digit_to_string(double %109)
store i8* %110, i8** %10
br label %121
111: ; preds = %102
%112 = load i8, i8* %4
%113 = sext i8 %112 to i32
%114 = icmp eq i32 %113, 94
br i1 %114, label %115, label %120
115: ; preds = %111
%116 = load double, double* %8
%117 = load double, double* %9
%118 = call double @pow(double %116, double %117)
%119 = call i8* @digit_to_string(double %118)
store i8* %119, i8** %10
br label %120
120: ; preds = %115, %111
br label %121
121: ; preds = %120, %106
br label %122
122: ; preds = %121, %97
br label %123
123: ; preds = %122, %88
br label %124
124: ; preds = %123, %79
%125 = load i8*, i8** %3
%126 = load i32*, i32** %6
%127 = getelementptr inbounds i32, i32* %126, i64 0
%128 = load i32, i32* %127
%129 = load i32*, i32** %7
%130 = getelementptr inbounds i32, i32* %129, i64 1
%131 = load i32, i32* %130
%132 = load i8*, i8** %10
%133 = call i8* @replace_sub_string(i8* %125, i32 %128, i32 %131, i8* %132)
store i8* %133, i8** %3
br label %134
134: ; preds = %124, %2
%135 = load i8*, i8** %3
ret i8* %135
}

LLVM IR (-O2):
@.str = [29 x i8] c"ERROR, no right argument:%s\0A\00"
@.str.1 = [22 x i8] c"Index of operator:%d\0A\00"
@.str.2 = [22 x i8] c"Left argument:%d->%d\0A\00"
@.str.3 = [23 x i8] c"Right argument:%d->%d\0A\00"
define i8* @perform_operation(i8* %0, i8 signext %1) {
%3 = tail call i32 @find_character(i8* %0, i8 signext %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %56, label %5
5: ; preds = %2
%6 = tail call i32* @left_argument(i8* %0, i32 %3)
%7 = tail call i32* @right_argument(i8* %0, i32 %3)
%8 = load i32, i32* %7
%9 = getelementptr inbounds i32, i32* %7, i64 1
%10 = load i32, i32* %9
%11 = icmp sgt i32 %8, %10
br i1 %11, label %12, label %23
12: ; preds = %5
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%15 = load i32, i32* %6
%16 = getelementptr inbounds i32, i32* %6, i64 1
%17 = load i32, i32* %16
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %17)
%19 = load i32, i32* %7
%20 = load i32, i32* %9
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %5
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = getelementptr inbounds i32, i32* %6, i64 1
%28 = load i32, i32* %27
%29 = tail call i32 @sub_string(i8* %0, i32 %24, i32 %28)
%30 = tail call double @string_to_digit(i32 %29)
%31 = load i32, i32* %7
%32 = load i32, i32* %9
br label %33
33: ; preds = %23, %26
%34 = phi i32 [ %32, %26 ], [ %10, %23 ]
%35 = phi i32 [ %31, %26 ], [ %8, %23 ]
%36 = phi double [ %30, %26 ], [ 0.000000e+00, %23 ]
%37 = tail call i32 @sub_string(i8* %0, i32 %35, i32 %34)
%38 = tail call double @string_to_digit(i32 %37)
switch i8 %1, label %49 [
i8 43, label %39
i8 45, label %41
i8 42, label %43
i8 47, label %45
i8 94, label %47
]
39: ; preds = %33
%40 = fadd double %36, %38
br label %50
41: ; preds = %33
%42 = fsub double %36, %38
br label %50
43: ; preds = %33
%44 = fmul double %36, %38
br label %50
45: ; preds = %33
%46 = fdiv double %36, %38
br label %50
47: ; preds = %33
%48 = tail call double @pow(double %36, double %38)
br label %50
49: ; preds = %33
unreachable
50: ; preds = %41, %45, %47, %43, %39
%51 = phi double [ %42, %41 ], [ %46, %45 ], [ %48, %47 ], [ %44, %43 ], [ %40, %39 ]
%52 = tail call i8* @digit_to_string(double %51)
%53 = load i32, i32* %6
%54 = load i32, i32* %9
%55 = tail call i8* @replace_sub_string(i8* %0, i32 %53, i32 %54, i8* %52)
br label %56
56: ; preds = %50, %2
%57 = phi i8* [ %55, %50 ], [ %0, %2 ]
ret i8* %57
}