#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*/ digit_to_string(double);
int *left_argument(char *, int);
char *replace_sub_string(char *, int, int, int /*<<< orphan*/);
int string_length(char *);
double string_to_digit(int /*<<< orphan*/);
int /*<<< orphan*/ sub_string(char *, int, int);
char *replace_KMG(char buffer[]) {
  int length = string_length(buffer);
  int count;
  int *left;
  double left_arg, answer;
  for (count = 0; count < length; count++) {
    if (buffer[count] == 'K') {
      left = left_argument(buffer, count);
      left_arg = string_to_digit(sub_string(buffer, left[0], left[1]));
      answer = left_arg * 1024;
      buffer =
          replace_sub_string(buffer, left[0], count, digit_to_string(answer));
      break;
    }
    if (buffer[count] == 'M') {
      left = left_argument(buffer, count);
      left_arg = string_to_digit(sub_string(buffer, left[0], left[1]));
      answer = left_arg * 1048576.0;
      buffer =
          replace_sub_string(buffer, left[0], count, digit_to_string(answer));
      break;
    }
    if (buffer[count] == 'G') {
      left = left_argument(buffer, count);
      left_arg = string_to_digit(sub_string(buffer, left[0], left[1]));
      answer = left_arg * 1073741824;
      buffer =
          replace_sub_string(buffer, left[0], count, digit_to_string(answer));
      break;
    }
  }
  return buffer;
}


x86 assembly (clang, -O0):
<replace_KMG>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <string_length@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    135a <replace_KMG+0x1fa>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x4b,%eax
jne    121f <replace_KMG+0xbf>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <left_argument@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
call   1050 <sub_string@plt>
mov    %eax,%edi
call   1030 <string_to_digit@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  0xe36(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1060 <digit_to_string@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
call   1080 <replace_sub_string@plt>
mov    %rax,-0x8(%rbp)
jmp    135a <replace_KMG+0x1fa>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x4d,%eax
jne    12b3 <replace_KMG+0x153>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <left_argument@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
call   1050 <sub_string@plt>
mov    %eax,%edi
call   1030 <string_to_digit@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  0xd9a(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1060 <digit_to_string@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
call   1080 <replace_sub_string@plt>
mov    %rax,-0x8(%rbp)
jmp    135a <replace_KMG+0x1fa>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x47,%eax
jne    1347 <replace_KMG+0x1e7>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <left_argument@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
call   1050 <sub_string@plt>
mov    %eax,%edi
call   1030 <string_to_digit@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  0xcfe(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x4c(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1060 <digit_to_string@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%esi
mov    -0x4c(%rbp),%edx
mov    %eax,%ecx
call   1080 <replace_sub_string@plt>
mov    %rax,-0x8(%rbp)
jmp    135a <replace_KMG+0x1fa>
jmp    134c <replace_KMG+0x1ec>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    117f <replace_KMG+0x1f>
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_KMG>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1070 <string_length@plt>
test   %eax,%eax
jle    119d <replace_KMG+0x3d>
mov    %eax,%eax
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%ecx
cmp    $0x4d,%cl
je     11ab <replace_KMG+0x4b>
cmp    $0x4b,%cl
je     11b5 <replace_KMG+0x55>
cmp    $0x47,%cl
je     11bf <replace_KMG+0x5f>
add    $0x1,%rbx
cmp    %rbx,%rax
jne    1180 <replace_KMG+0x20>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movsd  0xe55(%rip),%xmm0
jmp    11c7 <replace_KMG+0x67>
movsd  0xe53(%rip),%xmm0
jmp    11c7 <replace_KMG+0x67>
movsd  0xe39(%rip),%xmm0
movsd  %xmm0,(%rsp)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <left_argument@plt>
mov    %rax,%r15
mov    (%rax),%esi
mov    0x4(%rax),%edx
mov    %r14,%rdi
call   1050 <sub_string@plt>
mov    %eax,%edi
call   1030 <string_to_digit@plt>
mulsd  (%rsp),%xmm0
mov    (%r15),%ebp
call   1060 <digit_to_string@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %eax,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <replace_sub_string@plt>

LLVM IR (-O0):
define i8* @replace_KMG(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca double
%7 = alloca double
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 @string_length(i8* %8)
store i32 %9, i32* %3
store i32 0, i32* %4
br label %10
10: ; preds = %108, %1
%11 = load i32, i32* %4
%12 = load i32, i32* %3
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %111
14: ; preds = %10
%15 = load i8*, i8** %2
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 75
br i1 %21, label %22, label %45
22: ; preds = %14
%23 = load i8*, i8** %2
%24 = load i32, i32* %4
%25 = call i32* @left_argument(i8* %23, i32 %24)
store i32* %25, i32** %5
%26 = load i8*, i8** %2
%27 = load i32*, i32** %5
%28 = getelementptr inbounds i32, i32* %27, i64 0
%29 = load i32, i32* %28
%30 = load i32*, i32** %5
%31 = getelementptr inbounds i32, i32* %30, i64 1
%32 = load i32, i32* %31
%33 = call i32 @sub_string(i8* %26, i32 %29, i32 %32)
%34 = call double @string_to_digit(i32 %33)
store double %34, double* %6
%35 = load double, double* %6
%36 = fmul double %35, 1.024000e+03
store double %36, double* %7
%37 = load i8*, i8** %2
%38 = load i32*, i32** %5
%39 = getelementptr inbounds i32, i32* %38, i64 0
%40 = load i32, i32* %39
%41 = load i32, i32* %4
%42 = load double, double* %7
%43 = call i32 @digit_to_string(double %42)
%44 = call i8* @replace_sub_string(i8* %37, i32 %40, i32 %41, i32 %43)
store i8* %44, i8** %2
br label %111
45: ; preds = %14
%46 = load i8*, i8** %2
%47 = load i32, i32* %4
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 77
br i1 %52, label %53, label %76
53: ; preds = %45
%54 = load i8*, i8** %2
%55 = load i32, i32* %4
%56 = call i32* @left_argument(i8* %54, i32 %55)
store i32* %56, i32** %5
%57 = load i8*, i8** %2
%58 = load i32*, i32** %5
%59 = getelementptr inbounds i32, i32* %58, i64 0
%60 = load i32, i32* %59
%61 = load i32*, i32** %5
%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* %6
%66 = load double, double* %6
%67 = fmul double %66, 0x4130000000000000
store double %67, double* %7
%68 = load i8*, i8** %2
%69 = load i32*, i32** %5
%70 = getelementptr inbounds i32, i32* %69, i64 0
%71 = load i32, i32* %70
%72 = load i32, i32* %4
%73 = load double, double* %7
%74 = call i32 @digit_to_string(double %73)
%75 = call i8* @replace_sub_string(i8* %68, i32 %71, i32 %72, i32 %74)
store i8* %75, i8** %2
br label %111
76: ; preds = %45
%77 = load i8*, i8** %2
%78 = load i32, i32* %4
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %77, i64 %79
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = icmp eq i32 %82, 71
br i1 %83, label %84, label %107
84: ; preds = %76
%85 = load i8*, i8** %2
%86 = load i32, i32* %4
%87 = call i32* @left_argument(i8* %85, i32 %86)
store i32* %87, i32** %5
%88 = load i8*, i8** %2
%89 = load i32*, i32** %5
%90 = getelementptr inbounds i32, i32* %89, i64 0
%91 = load i32, i32* %90
%92 = load i32*, i32** %5
%93 = getelementptr inbounds i32, i32* %92, i64 1
%94 = load i32, i32* %93
%95 = call i32 @sub_string(i8* %88, i32 %91, i32 %94)
%96 = call double @string_to_digit(i32 %95)
store double %96, double* %6
%97 = load double, double* %6
%98 = fmul double %97, 0x41D0000000000000
store double %98, double* %7
%99 = load i8*, i8** %2
%100 = load i32*, i32** %5
%101 = getelementptr inbounds i32, i32* %100, i64 0
%102 = load i32, i32* %101
%103 = load i32, i32* %4
%104 = load double, double* %7
%105 = call i32 @digit_to_string(double %104)
%106 = call i8* @replace_sub_string(i8* %99, i32 %102, i32 %103, i32 %105)
store i8* %106, i8** %2
br label %111
107: ; preds = %76
br label %108
108: ; preds = %107
%109 = load i32, i32* %4
%110 = add nsw i32 %109, 1
store i32 %110, i32* %4
br label %10
111: ; preds = %84, %53, %22, %10
%112 = load i8*, i8** %2
ret i8* %112
}

LLVM IR (-O2):
define i8* @replace_KMG(i8* %0) {
%2 = tail call i32 @string_length(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %28
4: ; preds = %1
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %4, %11
%7 = phi i64 [ 0, %4 ], [ %12, %11 ]
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
switch i8 %9, label %11 [
i8 75, label %14
i8 77, label %15
i8 71, label %10
]
10: ; preds = %6
br label %15
11: ; preds = %6
%12 = add nuw nsw i64 %7, 1
%13 = icmp eq i64 %12, %5
br i1 %13, label %28, label %6
14: ; preds = %6
br label %15
15: ; preds = %6, %14, %10
%16 = phi double [ 0x41D0000000000000, %10 ], [ 1.024000e+03, %14 ], [ 0x4130000000000000, %6 ]
%17 = trunc i64 %7 to i32
%18 = tail call i32* @left_argument(i8* %0, i32 %17)
%19 = load i32, i32* %18
%20 = getelementptr inbounds i32, i32* %18, i64 1
%21 = load i32, i32* %20
%22 = tail call i32 @sub_string(i8* %0, i32 %19, i32 %21)
%23 = tail call double @string_to_digit(i32 %22)
%24 = fmul double %23, %16
%25 = load i32, i32* %18
%26 = tail call i32 @digit_to_string(double %24)
%27 = tail call i8* @replace_sub_string(i8* %0, i32 %25, i32 %17, i32 %26)
br label %28
28: ; preds = %11, %15, %1
%29 = phi i8* [ %0, %1 ], [ %27, %15 ], [ %0, %11 ]
ret i8* %29
}