#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 */
typedef int /*<<< orphan*/ suffix;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, double, ...);
char *decimal_to_string(double value, char *result, int size, int is_bit) {
  char suffix[] = " KMGTPEZ";
  int pos = 0;
  double div;
  (void)memset(result, 0, size);
  div = is_bit ? 1000.0 : 1024.0;
  if (value == 0)
    return ("0");
  while (value > div && pos < sizeof(suffix)) {
    value /= div;
    pos++;
  }
  if (is_bit == 1) {
    if (pos == 0)
      (void)snprintf(result, size, "%.2fb/s", value);
    else
      (void)snprintf(result, size, "%.2f%cb/s", value, suffix[pos]);
  } else {
    if (pos == 0)
      (void)snprintf(result, size, "%.2fB", value);
    else
      (void)snprintf(result, size, "%.2f%cB", value, suffix[pos]);
  }
  return (result);
}


x86 assembly (clang, -O0):
<decimal_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    %edx,-0x20(%rbp)
mov    0xed2(%rip),%rax
mov    %rax,-0x29(%rbp)
mov    0xed0(%rip),%al
mov    %al,-0x21(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%eax
movsd  0xe95(%rip),%xmm0
movsd  0xe95(%rip),%xmm1
movsd  %xmm1,-0x48(%rbp)
cmp    $0x0,%eax
movsd  %xmm0,-0x40(%rbp)
jne    1190 <decimal_to_string+0x70>
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11c2 <decimal_to_string+0xa2>
jp     11c2 <decimal_to_string+0xa2>
lea    0xe60(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12cf <decimal_to_string+0x1af>
jmp    11c7 <decimal_to_string+0xa7>
movsd  -0x10(%rbp),%xmm0
xor    %eax,%eax
ucomisd -0x38(%rbp),%xmm0
mov    %al,-0x49(%rbp)
jbe    11ea <decimal_to_string+0xca>
movslq -0x30(%rbp),%rax
cmp    $0x9,%rax
setb   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11fa <decimal_to_string+0xda>
jmp    121b <decimal_to_string+0xfb>
movsd  -0x38(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11c7 <decimal_to_string+0xa7>
cmpl   $0x1,-0x20(%rbp)
jne    1276 <decimal_to_string+0x156>
cmpl   $0x0,-0x30(%rbp)
jne    124e <decimal_to_string+0x12e>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
movsd  -0x10(%rbp),%xmm0
lea    0xdd9(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    1271 <decimal_to_string+0x151>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
movsd  -0x10(%rbp),%xmm0
movslq -0x30(%rbp),%rax
movsbl -0x29(%rbp,%rax,1),%ecx
lea    0xdb9(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    12c7 <decimal_to_string+0x1a7>
cmpl   $0x0,-0x30(%rbp)
jne    129f <decimal_to_string+0x17f>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
movsd  -0x10(%rbp),%xmm0
lea    0xd9a(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    12c2 <decimal_to_string+0x1a2>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
movsd  -0x10(%rbp),%xmm0
movslq -0x30(%rbp),%rax
movsbl -0x29(%rbp,%rax,1),%ecx
lea    0xd78(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    12c7 <decimal_to_string+0x1a7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decimal_to_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
movsd  %xmm0,0x8(%rsp)
xor    %ebx,%ebx
test   %edx,%edx
sete   %r12b
xor    %esi,%esi
mov    %r14d,%edx
call   1040 <memset@plt>
movsd  0x8(%rsp),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    116a <decimal_to_string+0x4a>
jp     116a <decimal_to_string+0x4a>
lea    0xeb4(%rip),%r15
jmp    11f7 <decimal_to_string+0xd7>
mov    %r12b,%bl
lea    0xe8c(%rip),%rax
movsd  (%rax,%rbx,8),%xmm1
ucomisd %xmm1,%xmm0
jbe    11bf <decimal_to_string+0x9f>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
divsd  %xmm1,%xmm0
lea    0x1(%rax),%rcx
ucomisd %xmm1,%xmm0
jbe    11a6 <decimal_to_string+0x86>
cmp    $0x8,%eax
mov    %rcx,%rax
jb     1190 <decimal_to_string+0x70>
lea    0xe63(%rip),%rax
movsbl (%rcx,%rax,1),%ecx
cmp    $0x1,%ebp
jne    11cd <decimal_to_string+0xad>
lea    0xe66(%rip),%rdx
jmp    11d4 <decimal_to_string+0xb4>
cmp    $0x1,%ebp
jne    11e3 <decimal_to_string+0xc3>
lea    0xe50(%rip),%rdx
jmp    11ea <decimal_to_string+0xca>
lea    0xe5f(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    11f7 <decimal_to_string+0xd7>
lea    0xe43(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.decimal_to_string.suffix = [9 x i8] c" KMGTPEZ\00"
@.str = [2 x i8] c"0\00"
@.str.1 = [8 x i8] c"%.2fb/s\00"
@.str.2 = [10 x i8] c"%.2f%cb/s\00"
@.str.3 = [6 x i8] c"%.2fB\00"
@.str.4 = [8 x i8] c"%.2f%cB\00"
define i8* @decimal_to_string(double %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca double
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [9 x i8]
%11 = alloca i32
%12 = alloca double
store double %0, double* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = bitcast [9 x i8]* %10 to i8*
store i32 0, i32* %11
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = call i32 @memset(i8* %14, i32 0, i32 %15)
%17 = load i32, i32* %9
%18 = icmp ne i32 %17, 0
%19 = zext i1 %18 to i64
%20 = select i1 %18, double 1.000000e+03, double 1.024000e+03
store double %20, double* %12
%21 = load double, double* %6
%22 = fcmp oeq double %21, 0.000000e+00
br i1 %22, label %23, label %24
23: ; preds = %4
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %5
br label %84
24: ; preds = %4
br label %25
25: ; preds = %35, %24
%26 = load double, double* %6
%27 = load double, double* %12
%28 = fcmp ogt double %26, %27
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = icmp ult i64 %31, 9
br label %33
33: ; preds = %29, %25
%34 = phi i1 [ false, %25 ], [ %32, %29 ]
br i1 %34, label %35, label %41
35: ; preds = %33
%36 = load double, double* %12
%37 = load double, double* %6
%38 = fdiv double %37, %36
store double %38, double* %6
%39 = load i32, i32* %11
%40 = add nsw i32 %39, 1
store i32 %40, i32* %11
br label %25
41: ; preds = %33
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %63
44: ; preds = %41
%45 = load i32, i32* %11
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load i8*, i8** %7
%49 = load i32, i32* %8
%50 = load double, double* %6
%51 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %48, i32 %49, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), double %50)
br label %62
52: ; preds = %44
%53 = load i8*, i8** %7
%54 = load i32, i32* %8
%55 = load double, double* %6
%56 = load i32, i32* %11
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %53, i32 %54, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), double %55, i32 %60)
br label %62
62: ; preds = %52, %47
br label %82
63: ; preds = %41
%64 = load i32, i32* %11
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %63
%67 = load i8*, i8** %7
%68 = load i32, i32* %8
%69 = load double, double* %6
%70 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %67, i32 %68, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), double %69)
br label %81
71: ; preds = %63
%72 = load i8*, i8** %7
%73 = load i32, i32* %8
%74 = load double, double* %6
%75 = load i32, i32* %11
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 %76
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %72, i32 %73, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), double %74, i32 %79)
br label %81
81: ; preds = %71, %66
br label %82
82: ; preds = %81, %62
%83 = load i8*, i8** %7
store i8* %83, i8** %5
br label %84
84: ; preds = %82, %23
%85 = load i8*, i8** %5
ret i8* %85
}

LLVM IR (-O2):
@__const.decimal_to_string.suffix = [9 x i8] c" KMGTPEZ\00"
@.str = [2 x i8] c"0\00"
@.str.1 = [8 x i8] c"%.2fb/s\00"
@.str.2 = [10 x i8] c"%.2f%cb/s\00"
@.str.3 = [6 x i8] c"%.2fB\00"
@.str.4 = [8 x i8] c"%.2f%cB\00"
define i8* @decimal_to_string(double %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%6 = icmp eq i32 %3, 0
%7 = select i1 %6, double 1.024000e+03, double 1.000000e+03
%8 = fcmp oeq double %0, 0.000000e+00
br i1 %8, label %35, label %9
9: ; preds = %4
%10 = fcmp olt double %7, %0
br i1 %10, label %11, label %25
11: ; preds = %9, %11
%12 = phi i32 [ %15, %11 ], [ 0, %9 ]
%13 = phi double [ %14, %11 ], [ %0, %9 ]
%14 = fdiv double %13, %7
%15 = add nuw nsw i32 %12, 1
%16 = fcmp ogt double %14, %7
%17 = icmp ult i32 %12, 8
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %11, label %19
19: ; preds = %11
%20 = zext i32 %15 to i64
%21 = icmp eq i32 %3, 1
%22 = getelementptr inbounds [9 x i8], [9 x i8]* @__const.decimal_to_string.suffix, i64 0, i64 %20
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
br i1 %21, label %29, label %33
25: ; preds = %9
%26 = icmp eq i32 %3, 1
br i1 %26, label %27, label %31
27: ; preds = %25
%28 = tail call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), double %0)
br label %35
29: ; preds = %19
%30 = tail call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), double %14, i32 %24)
br label %35
31: ; preds = %25
%32 = tail call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), double %0)
br label %35
33: ; preds = %19
%34 = tail call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), double %14, i32 %24)
br label %35
35: ; preds = %29, %27, %33, %31, %4
%36 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %4 ], [ %1, %31 ], [ %1, %33 ], [ %1, %27 ], [ %1, %29 ]
ret i8* %36
}