void compute_display_size(unsigned long long size, const char **fmt,
                          double *display_size) {
  static const char *const fmts[] = {"%g b",     "%g Kb",    "%#.1f Mb",
                                     "%#.1f Gb", "%#.1f Tb", "%#.1f Pb",
                                     "%#.1f Eb", "%#.1f Zb", "%#.1f Yb"};
  int shift = 0;
  unsigned long long tmpsize;
  double denominator;
  tmpsize = size;
  denominator = 1.0;
  while (tmpsize >= 1024 && shift < (sizeof((fmts)) / sizeof(*(fmts))) - 1) {
    tmpsize >>= 10;
    denominator *= 1024.0;
    shift++;
  }
  *display_size = size / denominator;
  *fmt = fmts[shift];
}


x86 assembly (clang, -O0):
<compute_display_size>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movsd  0xed9(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
xor    %eax,%eax
cmpq   $0x400,-0x28(%rbp)
mov    %al,-0x31(%rbp)
jb     114d <compute_display_size+0x4d>
movslq -0x1c(%rbp),%rax
cmp    $0x8,%rax
setb   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    115d <compute_display_size+0x5d>
jmp    1189 <compute_display_size+0x89>
mov    -0x28(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x28(%rbp)
movsd  0xe97(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112c <compute_display_size+0x2c>
movaps 0xe80(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe7f(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
divsd  -0x30(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
movslq -0x1c(%rbp),%rcx
lea    0x2c38(%rip),%rax
mov    (%rax,%rcx,8),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_display_size>:
cmp    $0x400,%rdi
jb     114b <compute_display_size+0x4b>
cmp    $0x100000,%rdi
jb     1157 <compute_display_size+0x57>
cmp    $0x40000000,%rdi
jb     1166 <compute_display_size+0x66>
mov    %rdi,%rax
shr    $0x28,%rax
je     1175 <compute_display_size+0x75>
mov    %rdi,%rax
shr    $0x32,%rax
je     1184 <compute_display_size+0x84>
xor    %eax,%eax
mov    %rdi,%rcx
shr    $0x3c,%rcx
setne  %al
lea    0xef0(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm0
add    $0x5,%rax
jmp    1191 <compute_display_size+0x91>
movsd  0xead(%rip),%xmm0
xor    %eax,%eax
jmp    1191 <compute_display_size+0x91>
movsd  0xea9(%rip),%xmm0
mov    $0x1,%eax
jmp    1191 <compute_display_size+0x91>
movsd  0xea2(%rip),%xmm0
mov    $0x2,%eax
jmp    1191 <compute_display_size+0x91>
movsd  0xe9b(%rip),%xmm0
mov    $0x3,%eax
jmp    1191 <compute_display_size+0x91>
movsd  0xe94(%rip),%xmm0
mov    $0x4,%eax
movq   %rdi,%xmm1
punpckldq 0xea2(%rip),%xmm1
subpd  0xeaa(%rip),%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
divsd  %xmm0,%xmm2
movsd  %xmm2,(%rdx)
lea    0xe9f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
mov    %rax,(%rsi)
ret

LLVM IR (-O0):
@compute_display_size.fmts = internal constant [9 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i32 0, i32 0)]
@.str = [5 x i8] c"%g b\00"
@.str.1 = [6 x i8] c"%g Kb\00"
@.str.2 = [9 x i8] c"%#.1f Mb\00"
@.str.3 = [9 x i8] c"%#.1f Gb\00"
@.str.4 = [9 x i8] c"%#.1f Tb\00"
@.str.5 = [9 x i8] c"%#.1f Pb\00"
@.str.6 = [9 x i8] c"%#.1f Eb\00"
@.str.7 = [9 x i8] c"%#.1f Zb\00"
@.str.8 = [9 x i8] c"%#.1f Yb\00"
define void @compute_display_size(i64 %0, i8** %1, double* %2) {
%4 = alloca i64
%5 = alloca i8**
%6 = alloca double*
%7 = alloca i32
%8 = alloca i64
%9 = alloca double
store i64 %0, i64* %4
store i8** %1, i8*** %5
store double* %2, double** %6
store i32 0, i32* %7
%10 = load i64, i64* %4
store i64 %10, i64* %8
store double 1.000000e+00, double* %9
br label %11
11: ; preds = %20, %3
%12 = load i64, i64* %8
%13 = icmp uge i64 %12, 1024
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = icmp ult i64 %16, 8
br label %18
18: ; preds = %14, %11
%19 = phi i1 [ false, %11 ], [ %17, %14 ]
br i1 %19, label %20, label %27
20: ; preds = %18
%21 = load i64, i64* %8
%22 = lshr i64 %21, 10
store i64 %22, i64* %8
%23 = load double, double* %9
%24 = fmul double %23, 1.024000e+03
store double %24, double* %9
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %11
27: ; preds = %18
%28 = load i64, i64* %4
%29 = uitofp i64 %28 to double
%30 = load double, double* %9
%31 = fdiv double %29, %30
%32 = load double*, double** %6
store double %31, double* %32
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [9 x i8*], [9 x i8*]* @compute_display_size.fmts, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = load i8**, i8*** %5
store i8* %36, i8** %37
ret void
}

LLVM IR (-O2):
@reltable.compute_display_size = internal unnamed_addr constant [9 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str.1 to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.2 to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.3 to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.4 to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.5 to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.6 to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.7 to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.8 to i64), i64 ptrtoint ([9 x i32]* @reltable.compute_display_size to i64)) to i32)]
@.str = [5 x i8] c"%g b\00"
@.str.1 = [6 x i8] c"%g Kb\00"
@.str.2 = [9 x i8] c"%#.1f Mb\00"
@.str.3 = [9 x i8] c"%#.1f Gb\00"
@.str.4 = [9 x i8] c"%#.1f Tb\00"
@.str.5 = [9 x i8] c"%#.1f Pb\00"
@.str.6 = [9 x i8] c"%#.1f Eb\00"
@.str.7 = [9 x i8] c"%#.1f Zb\00"
@.str.8 = [9 x i8] c"%#.1f Yb\00"
define void @compute_display_size(i64 %0, i8** nocapture writeonly %1, double* nocapture writeonly %2) {
%4 = icmp ugt i64 %0, 1023
br i1 %4, label %5, label %17
5: ; preds = %3
%6 = icmp ugt i64 %0, 1048575
br i1 %6, label %7, label %17
7: ; preds = %5
%8 = icmp ugt i64 %0, 1073741823
br i1 %8, label %9, label %17
9: ; preds = %7
%10 = icmp ugt i64 %0, 1099511627775
br i1 %10, label %11, label %17
11: ; preds = %9
%12 = icmp ugt i64 %0, 1125899906842623
br i1 %12, label %13, label %17
13: ; preds = %11
%14 = icmp ugt i64 %0, 1152921504606846975
%15 = select i1 %14, double 0x43B0000000000000, double 0x4310000000000000
%16 = select i1 %14, i64 6, i64 5
br label %17
17: ; preds = %13, %5, %7, %9, %11, %3
%18 = phi i64 [ 0, %3 ], [ 1, %5 ], [ 2, %7 ], [ 3, %9 ], [ 4, %11 ], [ %16, %13 ]
%19 = phi double [ 1.000000e+00, %3 ], [ 1.024000e+03, %5 ], [ 0x4130000000000000, %7 ], [ 0x41D0000000000000, %9 ], [ 0x4270000000000000, %11 ], [ %15, %13 ]
%20 = uitofp i64 %0 to double
%21 = fdiv double %20, %19
store double %21, double* %2
%22 = shl i64 %18, 2
%23 = call i8* @llvm.load.relative.i64(i8* bitcast ([9 x i32]* @reltable.compute_display_size to i8*), i64 %22)
store i8* %23, i8** %1
ret void
}