void display_factored_freq(int freq, int align) {
  char buffer[128];
  if (freq < 1000) {
    sprintf(buffer, "%dHz", freq);
    printf("%*s", align, buffer);
  } else if (freq < 1000000) {
    sprintf(buffer, "%.2fMHz", (float)freq / 1000.0);
    printf("%*s", align, buffer);
  } else {
    sprintf(buffer, "%.2fGHz", (float)freq / 1000000.0);
    printf("%*s", align, buffer);
  }
}


x86 assembly (clang, -O0):
<display_factored_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x3e8,-0x4(%rbp)
jge    1173 <display_factored_freq+0x53>
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xec1(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    -0x8(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120e <display_factored_freq+0xee>
cmpl   $0xf4240,-0x4(%rbp)
jge    11c7 <display_factored_freq+0xa7>
lea    -0x90(%rbp),%rdi
cvtsi2ssl -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe70(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xe76(%rip),%rsi
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    -0x8(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1209 <display_factored_freq+0xe9>
lea    -0x90(%rbp),%rdi
cvtsi2ssl -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe21(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xe37(%rip),%rsi
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    -0x8(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120e <display_factored_freq+0xee>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<display_factored_freq>:
push   %rbx
sub    $0x80,%rsp
mov    %esi,%ebx
mov    %edi,%edx
cmp    $0x3e7,%edi
jg     1147 <display_factored_freq+0x27>
lea    0xed5(%rip),%rsi
mov    %rsp,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    1181 <display_factored_freq+0x61>
cvtsi2ss %edx,%xmm0
cvtss2sd %xmm0,%xmm0
cmp    $0xf423f,%edx
ja     1168 <display_factored_freq+0x48>
divsd  0xea9(%rip),%xmm0
lea    0xeb3(%rip),%rsi
jmp    1177 <display_factored_freq+0x57>
divsd  0xe90(%rip),%xmm0
lea    0xeaa(%rip),%rsi
mov    %rsp,%rdi
mov    $0x1,%al
call   1040 <sprintf@plt>
lea    0xe8d(%rip),%rdi
mov    %rsp,%rdx
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x80,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%dHz\00"
@.str.1 = [4 x i8] c"%*s\00"
@.str.2 = [8 x i8] c"%.2fMHz\00"
@.str.3 = [8 x i8] c"%.2fGHz\00"
define void @display_factored_freq(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 1000
br i1 %7, label %8, label %15
8: ; preds = %2
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %4
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %12, i8* %13)
br label %39
15: ; preds = %2
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 1000000
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%20 = load i32, i32* %3
%21 = sitofp i32 %20 to float
%22 = fpext float %21 to double
%23 = fdiv double %22, 1.000000e+03
%24 = call i32 (i8*, i8*, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), double %23)
%25 = load i32, i32* %4
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %25, i8* %26)
br label %38
28: ; preds = %15
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%30 = load i32, i32* %3
%31 = sitofp i32 %30 to float
%32 = fpext float %31 to double
%33 = fdiv double %32, 1.000000e+06
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double %33)
%35 = load i32, i32* %4
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %35, i8* %36)
br label %38
38: ; preds = %28, %18
br label %39
39: ; preds = %38, %8
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%dHz\00"
@.str.1 = [4 x i8] c"%*s\00"
@.str.2 = [8 x i8] c"%.2fMHz\00"
@.str.3 = [8 x i8] c"%.2fGHz\00"
define void @display_factored_freq(i32 %0, i32 %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 1000
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %18
8: ; preds = %2
%9 = icmp ult i32 %0, 1000000
%10 = sitofp i32 %0 to float
%11 = fpext float %10 to double
br i1 %9, label %12, label %15
12: ; preds = %8
%13 = fdiv double %11, 1.000000e+03
%14 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), double %13)
br label %18
15: ; preds = %8
%16 = fdiv double %11, 1.000000e+06
%17 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double %16)
br label %18
18: ; preds = %12, %15, %6
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8* %4)
ret void
}