const char *adjust_unit(double *ptr_bytes) {
  const char *units[] = {"Byte", "KB", "MB", "GB", "TB", "PB", "EB"};
  int i = 0;
  double final = *ptr_bytes;
  while (i < 7 && final >= 1024) {
    final /= 1024;
    i++;
  }
  *ptr_bytes = final;
  return units[i];
}


x86 assembly (clang, -O0):
<adjust_unit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x40(%rbp),%rdi
lea    0x2cb9(%rip),%rsi
mov    $0x38,%edx
call   1030 <memcpy@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x50(%rbp)
xor    %eax,%eax
cmpl   $0x7,-0x44(%rbp)
mov    %al,-0x51(%rbp)
jge    116b <adjust_unit+0x5b>
movsd  -0x50(%rbp),%xmm0
movsd  0xe9f(%rip),%xmm1
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    117b <adjust_unit+0x6b>
jmp    119f <adjust_unit+0x8f>
movsd  -0x50(%rbp),%xmm0
movsd  0xe78(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1145 <adjust_unit+0x35>
movsd  -0x50(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adjust_unit>:
movsd  (%rdi),%xmm0
ucomisd 0xef4(%rip),%xmm0
jae    1115 <adjust_unit+0x15>
xor    %eax,%eax
jmp    11b0 <adjust_unit+0xb0>
mulsd  0xeeb(%rip),%xmm0
mov    $0x1,%eax
ucomisd 0xed6(%rip),%xmm0
jb     11b0 <adjust_unit+0xb0>
mulsd  0xed0(%rip),%xmm0
mov    $0x2,%eax
ucomisd 0xebb(%rip),%xmm0
jb     11b0 <adjust_unit+0xb0>
mulsd  0xeb9(%rip),%xmm0
mov    $0x3,%eax
ucomisd 0xea4(%rip),%xmm0
jb     11b0 <adjust_unit+0xb0>
mulsd  0xea2(%rip),%xmm0
mov    $0x4,%eax
ucomisd 0xe8d(%rip),%xmm0
jb     11b0 <adjust_unit+0xb0>
mulsd  0xe8b(%rip),%xmm0
mov    $0x5,%eax
ucomisd 0xe76(%rip),%xmm0
jb     11b0 <adjust_unit+0xb0>
mulsd  0xe74(%rip),%xmm0
mov    $0x6,%eax
ucomisd 0xe5f(%rip),%xmm0
jb     11b0 <adjust_unit+0xb0>
mulsd  0xe5d(%rip),%xmm0
mov    $0x7,%eax
movsd  %xmm0,(%rdi)
lea    0xe6d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"Byte\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"
@reltable.adjust_unit = [7 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.adjust_unit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([7 x i32]* @reltable.adjust_unit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([7 x i32]* @reltable.adjust_unit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([7 x i32]* @reltable.adjust_unit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.4 to i64), i64 ptrtoint ([7 x i32]* @reltable.adjust_unit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.5 to i64), i64 ptrtoint ([7 x i32]* @reltable.adjust_unit to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.6 to i64), i64 ptrtoint ([7 x i32]* @reltable.adjust_unit to i64)) to i32)]
define i8* @adjust_unit(double* nocapture %0) {
%2 = load double, double* %0
%3 = fcmp ult double %2, 1.024000e+03
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = fmul double %2, 0x3F50000000000000
%6 = fcmp ult double %5, 1.024000e+03
br i1 %6, label %24, label %7
7: ; preds = %4
%8 = fmul double %5, 0x3F50000000000000
%9 = fcmp ult double %8, 1.024000e+03
br i1 %9, label %24, label %10
10: ; preds = %7
%11 = fmul double %8, 0x3F50000000000000
%12 = fcmp ult double %11, 1.024000e+03
br i1 %12, label %24, label %13
13: ; preds = %10
%14 = fmul double %11, 0x3F50000000000000
%15 = fcmp ult double %14, 1.024000e+03
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = fmul double %14, 0x3F50000000000000
%18 = fcmp ult double %17, 1.024000e+03
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = fmul double %17, 0x3F50000000000000
%21 = fcmp ult double %20, 1.024000e+03
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = fmul double %20, 0x3F50000000000000
br label %24
24: ; preds = %4, %7, %10, %13, %16, %19, %22, %1
%25 = phi i64 [ 0, %1 ], [ 1, %4 ], [ 2, %7 ], [ 3, %10 ], [ 4, %13 ], [ 5, %16 ], [ 6, %19 ], [ 7, %22 ]
%26 = phi double [ %2, %1 ], [ %5, %4 ], [ %8, %7 ], [ %11, %10 ], [ %14, %13 ], [ %17, %16 ], [ %20, %19 ], [ %23, %22 ]
store double %26, double* %0
%27 = shl i64 %25, 2
%28 = call i8* @llvm.load.relative.i64(i8* bitcast ([7 x i32]* @reltable.adjust_unit to i8*), i64 %27)
ret i8* %28
}