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


x86 assembly (clang, -O0):
<adjust_unit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
lea    0xed5(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xecf(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xec7(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xebf(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xeb7(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x40(%rbp)
xor    %eax,%eax
cmpl   $0x5,-0x34(%rbp)
mov    %al,-0x41(%rbp)
jge    119d <adjust_unit+0x8d>
movsd  -0x40(%rbp),%xmm0
movsd  0xe6d(%rip),%xmm1
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11ad <adjust_unit+0x9d>
jmp    11d1 <adjust_unit+0xc1>
movsd  -0x40(%rbp),%xmm0
movsd  0xe46(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1177 <adjust_unit+0x67>
movsd  -0x40(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adjust_unit>:
lea    0xf0e(%rip),%rax
mov    %rax,-0x20(%rsp)
lea    0xf05(%rip),%rax
mov    %rax,-0x18(%rsp)
lea    0xefc(%rip),%rax
mov    %rax,-0x10(%rsp)
lea    0xef3(%rip),%rax
mov    %rax,-0x8(%rsp)
movsd  (%rdi),%xmm0
ucomisd 0xec4(%rip),%xmm0
jae    114a <adjust_unit+0x4a>
lea    0xecb(%rip),%rax
movsd  %xmm0,(%rdi)
ret
mulsd  0xeb6(%rip),%xmm0
mov    $0x1,%eax
ucomisd 0xea1(%rip),%xmm0
jb     11b3 <adjust_unit+0xb3>
mulsd  0xe9f(%rip),%xmm0
mov    $0x2,%eax
ucomisd 0xe8a(%rip),%xmm0
jb     11b3 <adjust_unit+0xb3>
mulsd  0xe88(%rip),%xmm0
mov    $0x3,%eax
ucomisd 0xe73(%rip),%xmm0
jb     11b3 <adjust_unit+0xb3>
mulsd  0xe71(%rip),%xmm0
mov    $0x4,%eax
ucomisd 0xe5c(%rip),%xmm0
jb     11b3 <adjust_unit+0xb3>
mulsd  0xe5a(%rip),%xmm0
mov    $0x5,%eax
mov    -0x28(%rsp,%rax,8),%rax
movsd  %xmm0,(%rdi)
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"
define i8* @adjust_unit(double* %0) {
%2 = alloca double*
%3 = alloca [5 x i8*]
%4 = alloca i32
%5 = alloca double
store double* %0, double** %2
%6 = bitcast [5 x i8*]* %3 to i8*
%7 = bitcast i8* %6 to [5 x i8*]*
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8** %12
store i32 0, i32* %4
%13 = load double*, double** %2
%14 = load double, double* %13
store double %14, double* %5
br label %15
15: ; preds = %23, %1
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 5
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load double, double* %5
%20 = fcmp oge double %19, 1.024000e+03
br label %21
21: ; preds = %18, %15
%22 = phi i1 [ false, %15 ], [ %20, %18 ]
br i1 %22, label %23, label %28
23: ; preds = %21
%24 = load double, double* %5
%25 = fdiv double %24, 1.024000e+03
store double %25, double* %5
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %15
28: ; preds = %21
%29 = load double, double* %5
%30 = load double*, double** %2
store double %29, double* %30
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %32
%34 = load i8*, i8** %33
ret i8* %34
}

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"
define i8* @adjust_unit(double* nocapture %0) {
%2 = alloca [5 x i8*]
%3 = bitcast [5 x i8*]* %2 to i8*
%4 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %4
%5 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %7
%8 = load double, double* %0
%9 = fcmp ult double %8, 1.024000e+03
br i1 %9, label %29, label %10
10: ; preds = %1
%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 = %22, %19, %16, %13, %10
%25 = phi double [ %11, %10 ], [ %14, %13 ], [ %17, %16 ], [ %20, %19 ], [ %23, %22 ]
%26 = phi i64 [ 1, %10 ], [ 2, %13 ], [ 3, %16 ], [ 4, %19 ], [ 5, %22 ]
%27 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %26
%28 = load i8*, i8** %27
br label %29
29: ; preds = %1, %24
%30 = phi i8* [ %28, %24 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %1 ]
%31 = phi double [ %25, %24 ], [ %8, %1 ]
store double %31, double* %0
ret i8* %30
}