char *readable_fs(double size, char *output) {
  int i = 0;
  const char *units[] = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
  while (size > 1024) {
    size /= 1024;
    i++;
  }
  snprintf(output, 50, "%.*f %s", i, size, units[i]);
  return output;
}


x86 assembly (clang, -O0):
<readable_fs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
lea    0x2c8d(%rip),%rsi
mov    $0x48,%edx
call   1040 <memcpy@plt>
movsd  -0x8(%rbp),%xmm0
movsd  0xea6(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1188 <readable_fs+0x68>
movsd  -0x8(%rbp),%xmm0
movsd  0xe8f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114d <readable_fs+0x2d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%ecx
movsd  -0x8(%rbp),%xmm0
movslq -0x14(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%r8
mov    $0x32,%esi
lea    0xe77(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readable_fs>:
push   %rbx
mov    %rdi,%rbx
xor    %ecx,%ecx
ucomisd 0xee2(%rip),%xmm0
jbe    114d <readable_fs+0x3d>
xor    %ecx,%ecx
movsd  0xede(%rip),%xmm1
movsd  0xece(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mulsd  %xmm1,%xmm0
add    $0x1,%ecx
ucomisd %xmm2,%xmm0
ja     1140 <readable_fs+0x30>
mov    %ecx,%eax
lea    0xeda(%rip),%rdx
movslq (%rdx,%rax,4),%r8
add    %rdx,%r8
lea    0xec4(%rip),%rdx
mov    $0x32,%esi
mov    %rbx,%rdi
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 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"
@__const.readable_fs.units = [9 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 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.9 = [8 x i8] c"%.*f %s\00"
define i8* @readable_fs(double %0, i8* %1) {
%3 = alloca double
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [9 x i8*]
store double %0, double* %3
store i8* %1, i8** %4
store i32 0, i32* %5
%7 = bitcast [9 x i8*]* %6 to i8*
br label %8
8: ; preds = %11, %2
%9 = load double, double* %3
%10 = fcmp ogt double %9, 1.024000e+03
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load double, double* %3
%13 = fdiv double %12, 1.024000e+03
store double %13, double* %3
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %5
br label %8
16: ; preds = %8
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = load double, double* %3
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [9 x i8*], [9 x i8*]* %6, i64 0, i64 %21
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %17, i64 50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 %18, double %19, i8* %23)
%25 = load i8*, i8** %4
ret i8* %25
}

LLVM IR (-O2):
@.str = [2 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"
@reltable.readable_fs = [9 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.4 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.5 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.6 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.7 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.8 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_fs to i64)) to i32)]
@.str.9 = [8 x i8] c"%.*f %s\00"
define i8* @readable_fs(double %0, i8* returned writeonly %1) {
%3 = fcmp ogt double %0, 1.024000e+03
br i1 %3, label %4, label %10
4: ; preds = %2, %4
%5 = phi i32 [ %8, %4 ], [ 0, %2 ]
%6 = phi double [ %7, %4 ], [ %0, %2 ]
%7 = fmul double %6, 0x3F50000000000000
%8 = add nuw nsw i32 %5, 1
%9 = fcmp ogt double %7, 1.024000e+03
br i1 %9, label %4, label %10
10: ; preds = %4, %2
%11 = phi double [ %0, %2 ], [ %7, %4 ]
%12 = phi i32 [ 0, %2 ], [ %8, %4 ]
%13 = zext i32 %12 to i64
%14 = shl i64 %13, 2
%15 = call i8* @llvm.load.relative.i64(i8* bitcast ([9 x i32]* @reltable.readable_fs to i8*), i64 %14)
%16 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) %1, i64 50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 %12, double %11, i8* %15)
ret i8* %1
}