double f_to_kHz_MHz(double f, const char **unit) {
  if (f > 1e6) {
    f /= 1e6;
    *unit = "MHz";
  } else if (f > 1e3) {
    f /= 1000;
    *unit = "kHz";
  } else
    *unit = "Hz";
  return f;
}


x86 assembly (clang, -O0):
<f_to_kHz_MHz>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xee6(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    114d <f_to_kHz_MHz+0x4d>
movsd  -0x8(%rbp),%xmm0
movsd  0xecf(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x10(%rbp),%rax
lea    0xecb(%rip),%rcx
mov    %rcx,(%rax)
jmp    11a0 <f_to_kHz_MHz+0xa0>
movsd  -0x8(%rbp),%xmm0
movsd  0xeae(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    118d <f_to_kHz_MHz+0x8d>
movsd  -0x8(%rbp),%xmm0
movsd  0xe97(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x10(%rbp),%rax
lea    0xe8f(%rip),%rcx
mov    %rcx,(%rax)
jmp    119b <f_to_kHz_MHz+0x9b>
mov    -0x10(%rbp),%rax
lea    0xe79(%rip),%rcx
mov    %rcx,(%rax)
jmp    11a0 <f_to_kHz_MHz+0xa0>
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<f_to_kHz_MHz>:
ucomisd 0xef8(%rip),%xmm0
jbe    111d <f_to_kHz_MHz+0x1d>
divsd  0xeee(%rip),%xmm0
lea    0xef7(%rip),%rax
mov    %rax,(%rdi)
ret
ucomisd 0xee3(%rip),%xmm0
jbe    113a <f_to_kHz_MHz+0x3a>
divsd  0xed9(%rip),%xmm0
lea    0xede(%rip),%rax
mov    %rax,(%rdi)
ret
lea    0xed0(%rip),%rax
mov    %rax,(%rdi)
ret

LLVM IR (-O0):
@.str = [4 x i8] c"MHz\00"
@.str.1 = [4 x i8] c"kHz\00"
@.str.2 = [3 x i8] c"Hz\00"
define double @f_to_kHz_MHz(double %0, i8** %1) {
%3 = alloca double
%4 = alloca i8**
store double %0, double* %3
store i8** %1, i8*** %4
%5 = load double, double* %3
%6 = fcmp ogt double %5, 1.000000e+06
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load double, double* %3
%9 = fdiv double %8, 1.000000e+06
store double %9, double* %3
%10 = load i8**, i8*** %4
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %10
br label %21
11: ; preds = %2
%12 = load double, double* %3
%13 = fcmp ogt double %12, 1.000000e+03
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load double, double* %3
%16 = fdiv double %15, 1.000000e+03
store double %16, double* %3
%17 = load i8**, i8*** %4
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %17
br label %20
18: ; preds = %11
%19 = load i8**, i8*** %4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %19
br label %20
20: ; preds = %18, %14
br label %21
21: ; preds = %20, %7
%22 = load double, double* %3
ret double %22
}

LLVM IR (-O2):
@.str = [4 x i8] c"MHz\00"
@.str.1 = [4 x i8] c"kHz\00"
@.str.2 = [3 x i8] c"Hz\00"
define double @f_to_kHz_MHz(double %0, i8** nocapture writeonly %1) {
%3 = fcmp ogt double %0, 1.000000e+06
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = fdiv double %0, 1.000000e+06
br label %10
6: ; preds = %2
%7 = fcmp ogt double %0, 1.000000e+03
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = fdiv double %0, 1.000000e+03
br label %10
10: ; preds = %6, %8, %4
%11 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %8 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %6 ]
%12 = phi double [ %9, %8 ], [ %5, %4 ], [ %0, %6 ]
store i8* %11, i8** %1
ret double %12
}