char determineMemoryUnit(unsigned int n) {
  char units[5] = {' ', 'K', 'M', 'G', 'T'};
  float n_float = (float)n;
  for (int i = 0; i < 5; i++) {
    if (n_float < 1)
      return units[i - 1];
    n_float = n_float / 1024;
  }
  return units[0];
}


x86 assembly (clang, -O0):
<determineMemoryUnit>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
movb   $0x54,-0xc(%rbp)
movl   $0x474d4b20,-0x10(%rbp)
mov    -0x8(%rbp),%eax
cvtsi2ss %rax,%xmm0
movss  %xmm0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x5,-0x18(%rbp)
jge    117a <determineMemoryUnit+0x7a>
movss  0xec8(%rip),%xmm0
ucomiss -0x14(%rbp),%xmm0
jbe    1156 <determineMemoryUnit+0x56>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x10(%rbp,%rax,1),%al
mov    %al,-0x1(%rbp)
jmp    1180 <determineMemoryUnit+0x80>
movss  -0x14(%rbp),%xmm0
movss  0xea1(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <determineMemoryUnit+0x26>
mov    -0x10(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<determineMemoryUnit>:
test   %edi,%edi
je     1124 <determineMemoryUnit+0x24>
mov    %edi,%eax
cvtsi2ss %rax,%xmm0
mulss  0xeed(%rip),%xmm0
movss  0xee9(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    112d <determineMemoryUnit+0x2d>
xor    %ecx,%ecx
jmp    1165 <determineMemoryUnit+0x65>
mov    $0xffffffffffffffff,%rcx
jmp    1165 <determineMemoryUnit+0x65>
mulss  0xecb(%rip),%xmm0
mov    $0x1,%ecx
ucomiss %xmm0,%xmm1
ja     1165 <determineMemoryUnit+0x65>
mulss  0xeb9(%rip),%xmm0
mov    $0x2,%ecx
ucomiss %xmm0,%xmm1
ja     1165 <determineMemoryUnit+0x65>
mulss  0xea7(%rip),%xmm0
mov    $0x20,%al
mov    $0x3,%ecx
ucomiss %xmm0,%xmm1
jbe    116f <determineMemoryUnit+0x6f>
lea    0xe9c(%rip),%rax
mov    (%rcx,%rax,1),%al
ret

LLVM IR (-O0):
@__const.determineMemoryUnit.units = [5 x i8] c" KMGT"
define signext i8 @determineMemoryUnit(i32 %0) {
%2 = alloca i8
%3 = alloca i32
%4 = alloca [5 x i8]
%5 = alloca float
%6 = alloca i32
store i32 %0, i32* %3
%7 = bitcast [5 x i8]* %4 to i8*
%8 = load i32, i32* %3
%9 = uitofp i32 %8 to float
store float %9, float* %5
store i32 0, i32* %6
br label %10
10: ; preds = %25, %1
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 5
br i1 %12, label %13, label %28
13: ; preds = %10
%14 = load float, float* %5
%15 = fcmp olt float %14, 1.000000e+00
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %6
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 %19
%21 = load i8, i8* %20
store i8 %21, i8* %2
br label %31
22: ; preds = %13
%23 = load float, float* %5
%24 = fdiv float %23, 1.024000e+03
store float %24, float* %5
br label %25
25: ; preds = %22
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %10
28: ; preds = %10
%29 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%30 = load i8, i8* %29
store i8 %30, i8* %2
br label %31
31: ; preds = %28, %16
%32 = load i8, i8* %2
ret i8 %32
}

LLVM IR (-O2):
@__const.determineMemoryUnit.units = [5 x i8] c" KMGT"
define signext i8 @determineMemoryUnit(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %7
3: ; preds = %17, %14, %11, %7, %1
%4 = phi i64 [ -1, %1 ], [ 0, %7 ], [ 1, %11 ], [ 2, %14 ], [ 3, %17 ]
%5 = getelementptr inbounds [5 x i8], [5 x i8]* @__const.determineMemoryUnit.units, i64 0, i64 %4
%6 = load i8, i8* %5
br label %20
7: ; preds = %1
%8 = uitofp i32 %0 to float
%9 = fmul float %8, 0x3F50000000000000
%10 = fcmp olt float %9, 1.000000e+00
br i1 %10, label %3, label %11
11: ; preds = %7
%12 = fmul float %9, 0x3F50000000000000
%13 = fcmp olt float %12, 1.000000e+00
br i1 %13, label %3, label %14
14: ; preds = %11
%15 = fmul float %12, 0x3F50000000000000
%16 = fcmp olt float %15, 1.000000e+00
br i1 %16, label %3, label %17
17: ; preds = %14
%18 = fmul float %15, 0x3F50000000000000
%19 = fcmp olt float %18, 1.000000e+00
br i1 %19, label %3, label %20
20: ; preds = %17, %3
%21 = phi i8 [ %6, %3 ], [ 32, %17 ]
ret i8 %21
}