#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*/ snprintf(char *const, size_t const, char const *const,
                            double, char const *const);
void size_to_str(const double val, const char *const fmt, char *const buf,
                 const size_t buflen) {
  double v = val;
  int i;
  static const char *const sizes[] = {
      "B ", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB",
  };
  for (i = 0; i < 8; i++, v /= 1024.0) {
    if (v <= 512.0)
      break;
  }
  (void)snprintf(buf, buflen, fmt, v, sizes[i]);
}


x86 assembly (clang, -O0):
<size_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x8,-0x2c(%rbp)
jge    1185 <size_to_str+0x75>
movsd  0xeb4(%rip),%xmm0
ucomisd -0x28(%rbp),%xmm0
jb     115c <size_to_str+0x4c>
jmp    1185 <size_to_str+0x75>
jmp    1161 <size_to_str+0x51>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe91(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    113a <size_to_str+0x2a>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
movsd  -0x28(%rbp),%xmm0
movslq -0x2c(%rbp),%rcx
lea    0x2c2f(%rip),%rax
mov    (%rax,%rcx,8),%rcx
call   1030 <snprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<size_to_str>:
mov    %rdi,%rax
movsd  0xee5(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    11bd <size_to_str+0xad>
mulsd  0xedb(%rip),%xmm0
mov    $0x1,%ecx
ucomisd %xmm0,%xmm1
jae    11bf <size_to_str+0xaf>
mulsd  0xec4(%rip),%xmm0
mov    $0x2,%ecx
ucomisd %xmm0,%xmm1
jae    11bf <size_to_str+0xaf>
mulsd  0xeb1(%rip),%xmm0
mov    $0x3,%ecx
ucomisd %xmm0,%xmm1
jae    11bf <size_to_str+0xaf>
mulsd  0xe9e(%rip),%xmm0
mov    $0x4,%ecx
ucomisd %xmm0,%xmm1
jae    11bf <size_to_str+0xaf>
mulsd  0xe8b(%rip),%xmm0
mov    $0x5,%ecx
ucomisd %xmm0,%xmm1
jae    11bf <size_to_str+0xaf>
mulsd  0xe78(%rip),%xmm0
mov    $0x6,%ecx
ucomisd %xmm0,%xmm1
jae    11bf <size_to_str+0xaf>
mulsd  0xe65(%rip),%xmm0
mov    $0x7,%ecx
ucomisd %xmm0,%xmm1
jae    11bf <size_to_str+0xaf>
mulsd  0xe52(%rip),%xmm0
mov    $0x8,%ecx
jmp    11bf <size_to_str+0xaf>
xor    %ecx,%ecx
lea    0xe4a(%rip),%rdi
movslq (%rdi,%rcx,4),%rcx
add    %rdi,%rcx
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %rax,%rdx
jmp    1030 <snprintf@plt>

LLVM IR (-O0):
@size_to_str.sizes = internal constant [9 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i32 0, i32 0)]
@.str = [3 x i8] c"B \00"
@.str.1 = [3 x i8] c"KB\00"
@.str.2 = [3 x i8] c"MB\00"
@.str.3 = [3 x i8] c"GB\00"
@.str.4 = [3 x i8] c"TB\00"
@.str.5 = [3 x i8] c"PB\00"
@.str.6 = [3 x i8] c"EB\00"
@.str.7 = [3 x i8] c"ZB\00"
@.str.8 = [3 x i8] c"YB\00"
define void @size_to_str(double %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca double
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca double
%10 = alloca i32
store double %0, double* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%11 = load double, double* %5
store double %11, double* %9
store i32 0, i32* %10
br label %12
12: ; preds = %20, %4
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 8
br i1 %14, label %15, label %25
15: ; preds = %12
%16 = load double, double* %9
%17 = fcmp ole double %16, 5.120000e+02
br i1 %17, label %18, label %19
18: ; preds = %15
br label %25
19: ; preds = %15
br label %20
20: ; preds = %19
%21 = load i32, i32* %10
%22 = add nsw i32 %21, 1
store i32 %22, i32* %10
%23 = load double, double* %9
%24 = fdiv double %23, 1.024000e+03
store double %24, double* %9
br label %12
25: ; preds = %18, %12
%26 = load i8*, i8** %7
%27 = load i64, i64* %8
%28 = load i8*, i8** %6
%29 = load double, double* %9
%30 = load i32, i32* %10
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [9 x i8*], [9 x i8*]* @size_to_str.sizes, i64 0, i64 %31
%33 = load i8*, i8** %32
%34 = call i32 @snprintf(i8* %26, i64 %27, i8* %28, double %29, i8* %33)
ret void
}

LLVM IR (-O2):
@reltable.size_to_str = internal unnamed_addr constant [9 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.4 to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.5 to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.6 to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.7 to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.8 to i64), i64 ptrtoint ([9 x i32]* @reltable.size_to_str to i64)) to i32)]
@.str = [3 x i8] c"B \00"
@.str.1 = [3 x i8] c"KB\00"
@.str.2 = [3 x i8] c"MB\00"
@.str.3 = [3 x i8] c"GB\00"
@.str.4 = [3 x i8] c"TB\00"
@.str.5 = [3 x i8] c"PB\00"
@.str.6 = [3 x i8] c"EB\00"
@.str.7 = [3 x i8] c"ZB\00"
@.str.8 = [3 x i8] c"YB\00"
define void @size_to_str(double %0, i8* %1, i8* %2, i64 %3) {
%5 = fcmp ugt double %0, 5.120000e+02
br i1 %5, label %6, label %29
6: ; preds = %4
%7 = fmul double %0, 0x3F50000000000000
%8 = fcmp ugt double %7, 5.120000e+02
br i1 %8, label %9, label %29
9: ; preds = %6
%10 = fmul double %7, 0x3F50000000000000
%11 = fcmp ugt double %10, 5.120000e+02
br i1 %11, label %12, label %29
12: ; preds = %9
%13 = fmul double %10, 0x3F50000000000000
%14 = fcmp ugt double %13, 5.120000e+02
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = fmul double %13, 0x3F50000000000000
%17 = fcmp ugt double %16, 5.120000e+02
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = fmul double %16, 0x3F50000000000000
%20 = fcmp ugt double %19, 5.120000e+02
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = fmul double %19, 0x3F50000000000000
%23 = fcmp ugt double %22, 5.120000e+02
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = fmul double %22, 0x3F50000000000000
%26 = fcmp ugt double %25, 5.120000e+02
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = fmul double %25, 0x3F50000000000000
br label %29
29: ; preds = %6, %9, %12, %15, %18, %21, %24, %27, %4
%30 = phi double [ %0, %4 ], [ %7, %6 ], [ %10, %9 ], [ %13, %12 ], [ %16, %15 ], [ %19, %18 ], [ %22, %21 ], [ %25, %24 ], [ %28, %27 ]
%31 = phi i64 [ 0, %4 ], [ 1, %6 ], [ 2, %9 ], [ 3, %12 ], [ 4, %15 ], [ 5, %18 ], [ 6, %21 ], [ 7, %24 ], [ 8, %27 ]
%32 = shl i64 %31, 2
%33 = call i8* @llvm.load.relative.i64(i8* bitcast ([9 x i32]* @reltable.size_to_str to i8*), i64 %32)
%34 = tail call i32 @snprintf(i8* %2, i64 %3, i8* %1, double %30, i8* %33)
ret void
}