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

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, double, char const *);
void volume_size_to_str(uint64_t size, char *str, uint32_t len) {
  static const char *suffix[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
  double ap;
  uint32_t i;
  for (ap = size, i = 0;
       ap > 1024 && i < (sizeof(suffix) / sizeof(*suffix)) - 1; ap /= 1024, i++)
    continue;
  snprintf(str, len, "%.3g %s", ap, suffix[i]);
}


x86 assembly (clang, -O0):
<volume_size_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movsd  -0x20(%rbp),%xmm0
xor    %eax,%eax
movsd  0xebc(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x25(%rbp)
jbe    115f <volume_size_to_str+0x4f>
movslq -0x24(%rbp),%rax
cmp    $0x5,%rax
setb   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    116f <volume_size_to_str+0x5f>
jmp    1198 <volume_size_to_str+0x88>
jmp    1174 <volume_size_to_str+0x64>
movsd  -0x20(%rbp),%xmm0
movsd  0xe7f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1135 <volume_size_to_str+0x25>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
movsd  -0x20(%rbp),%xmm0
movslq -0x24(%rbp),%rcx
lea    0x2e81(%rip),%rax
mov    (%rax,%rcx,8),%rcx
lea    0xe62(%rip),%rdx
call   1030 <snprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<volume_size_to_str>:
ucomisd 0xee8(%rip),%xmm0
jbe    1185 <volume_size_to_str+0x75>
mulsd  0xee6(%rip),%xmm0
mov    $0x1,%eax
ucomisd 0xed1(%rip),%xmm0
jbe    1187 <volume_size_to_str+0x77>
mulsd  0xecf(%rip),%xmm0
mov    $0x2,%eax
ucomisd 0xeba(%rip),%xmm0
jbe    1187 <volume_size_to_str+0x77>
mulsd  0xeb8(%rip),%xmm0
mov    $0x3,%eax
ucomisd 0xea3(%rip),%xmm0
jbe    1187 <volume_size_to_str+0x77>
mulsd  0xea1(%rip),%xmm0
mov    $0x4,%eax
ucomisd 0xe8c(%rip),%xmm0
jbe    1187 <volume_size_to_str+0x77>
mulsd  0xe8a(%rip),%xmm0
mov    $0x5,%eax
jmp    1187 <volume_size_to_str+0x77>
xor    %eax,%eax
lea    0xe82(%rip),%rdx
movslq (%rdx,%rax,4),%rcx
add    %rdx,%rcx
lea    0xea0(%rip),%rdx
jmp    1030 <snprintf@plt>

LLVM IR (-O0):
@volume_size_to_str.suffix = internal global [6 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0)]
@.str = [2 x i8] c"B\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
@.str.3 = [4 x i8] c"GiB\00"
@.str.4 = [4 x i8] c"TiB\00"
@.str.5 = [4 x i8] c"PiB\00"
@.str.6 = [8 x i8] c"%.3g %s\00"
define void @volume_size_to_str(double %0, i8* %1, i32 %2) {
%4 = alloca double
%5 = alloca i8*
%6 = alloca i32
%7 = alloca double
%8 = alloca i32
store double %0, double* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load double, double* %4
store double %9, double* %7
store i32 0, i32* %8
br label %10
10: ; preds = %20, %3
%11 = load double, double* %7
%12 = fcmp ogt double %11, 1.024000e+03
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* %8
%15 = sext i32 %14 to i64
%16 = icmp ult i64 %15, 5
br label %17
17: ; preds = %13, %10
%18 = phi i1 [ false, %10 ], [ %16, %13 ]
br i1 %18, label %19, label %25
19: ; preds = %17
br label %20
20: ; preds = %19
%21 = load double, double* %7
%22 = fdiv double %21, 1.024000e+03
store double %22, double* %7
%23 = load i32, i32* %8
%24 = add nsw i32 %23, 1
store i32 %24, i32* %8
br label %10
25: ; preds = %17
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = load double, double* %7
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [6 x i8*], [6 x i8*]* @volume_size_to_str.suffix, i64 0, i64 %30
%32 = load i8*, i8** %31
%33 = call i32 @snprintf(i8* %26, i32 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), double %28, i8* %32)
ret void
}

LLVM IR (-O2):
@reltable.volume_size_to_str = internal unnamed_addr constant [6 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str to i64), i64 ptrtoint ([6 x i32]* @reltable.volume_size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([6 x i32]* @reltable.volume_size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.2 to i64), i64 ptrtoint ([6 x i32]* @reltable.volume_size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.3 to i64), i64 ptrtoint ([6 x i32]* @reltable.volume_size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.4 to i64), i64 ptrtoint ([6 x i32]* @reltable.volume_size_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.5 to i64), i64 ptrtoint ([6 x i32]* @reltable.volume_size_to_str to i64)) to i32)]
@.str = [2 x i8] c"B\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
@.str.3 = [4 x i8] c"GiB\00"
@.str.4 = [4 x i8] c"TiB\00"
@.str.5 = [4 x i8] c"PiB\00"
@.str.6 = [8 x i8] c"%.3g %s\00"
define void @volume_size_to_str(double %0, i8* %1, i32 %2) {
%4 = fcmp ogt double %0, 1.024000e+03
br i1 %4, label %5, label %19
5: ; preds = %3
%6 = fmul double %0, 0x3F50000000000000
%7 = fcmp ogt double %6, 1.024000e+03
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = fmul double %6, 0x3F50000000000000
%10 = fcmp ogt double %9, 1.024000e+03
br i1 %10, label %11, label %19
11: ; preds = %8
%12 = fmul double %9, 0x3F50000000000000
%13 = fcmp ogt double %12, 1.024000e+03
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = fmul double %12, 0x3F50000000000000
%16 = fcmp ogt double %15, 1.024000e+03
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = fmul double %15, 0x3F50000000000000
br label %19
19: ; preds = %5, %8, %11, %14, %17, %3
%20 = phi double [ %0, %3 ], [ %6, %5 ], [ %9, %8 ], [ %12, %11 ], [ %15, %14 ], [ %18, %17 ]
%21 = phi i64 [ 0, %3 ], [ 1, %5 ], [ 2, %8 ], [ 3, %11 ], [ 4, %14 ], [ 5, %17 ]
%22 = shl i64 %21, 2
%23 = call i8* @llvm.load.relative.i64(i8* bitcast ([6 x i32]* @reltable.volume_size_to_str to i8*), i64 %22)
%24 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), double %20, i8* %23)
ret void
}