print2digits(f) double f;
{
  double a;
  a = fabs(f);
  if (a > 1000.0)
    printf("%#.2g", f);
  else if (a > 10.0)
    printf("%.0f", f);
  else if (a > 1.0)
    printf("%.1f", f);
  else if (a > 0.1)
    printf("%.2f", f);
  else if (a > 0.01)
    printf("%.3f", f);
  else
    printf("%#.2g", f);
}


x86 assembly (clang, -O0):
<print2digits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movaps 0xf07(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xec1(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1161 <print2digits+0x51>
movsd  -0x10(%rbp),%xmm0
lea    0xeeb(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    1244 <print2digits+0x134>
movsd  -0x18(%rbp),%xmm0
movsd  0xe9a(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1190 <print2digits+0x80>
movsd  -0x10(%rbp),%xmm0
lea    0xec2(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    123f <print2digits+0x12f>
movsd  -0x18(%rbp),%xmm0
movsd  0xe73(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11bf <print2digits+0xaf>
movsd  -0x10(%rbp),%xmm0
lea    0xe98(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    123a <print2digits+0x12a>
movsd  -0x18(%rbp),%xmm0
movsd  0xe4c(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11ee <print2digits+0xde>
movsd  -0x10(%rbp),%xmm0
lea    0xe6e(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    1235 <print2digits+0x125>
movsd  -0x18(%rbp),%xmm0
movsd  0xe25(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    121d <print2digits+0x10d>
movsd  -0x10(%rbp),%xmm0
lea    0xe44(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    1230 <print2digits+0x120>
movsd  -0x10(%rbp),%xmm0
lea    0xe17(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
jmp    1235 <print2digits+0x125>
jmp    123a <print2digits+0x12a>
jmp    123f <print2digits+0x12f>
jmp    1244 <print2digits+0x134>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print2digits>:
movapd 0xee8(%rip),%xmm1
andpd  %xmm0,%xmm1
ucomisd 0xeec(%rip),%xmm1
jbe    1134 <print2digits+0x24>
lea    0xf0b(%rip),%rdi
mov    $0x1,%al
jmp    1030 <printf@plt>
ucomisd 0xedc(%rip),%xmm1
jbe    114c <print2digits+0x3c>
lea    0xef9(%rip),%rdi
mov    $0x1,%al
jmp    1030 <printf@plt>
ucomisd 0xecc(%rip),%xmm1
jbe    1164 <print2digits+0x54>
lea    0xee6(%rip),%rdi
mov    $0x1,%al
jmp    1030 <printf@plt>
ucomisd 0xebc(%rip),%xmm1
jbe    117c <print2digits+0x6c>
lea    0xed3(%rip),%rdi
mov    $0x1,%al
jmp    1030 <printf@plt>
ucomisd 0xeac(%rip),%xmm1
lea    0xec2(%rip),%rax
lea    0xea6(%rip),%rdi
cmova  %rax,%rdi
mov    $0x1,%al
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%#.2g\00"
@.str.1 = [5 x i8] c"%.0f\00"
@.str.2 = [5 x i8] c"%.1f\00"
@.str.3 = [5 x i8] c"%.2f\00"
@.str.4 = [5 x i8] c"%.3f\00"
define i32 @print2digits(double %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca double
store double %0, double* %3
%5 = load double, double* %3
%6 = call double @llvm.fabs.f64(double %5)
store double %6, double* %4
%7 = load double, double* %4
%8 = fcmp ogt double %7, 1.000000e+03
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load double, double* %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %10)
br label %43
12: ; preds = %1
%13 = load double, double* %4
%14 = fcmp ogt double %13, 1.000000e+01
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load double, double* %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), double %16)
br label %42
18: ; preds = %12
%19 = load double, double* %4
%20 = fcmp ogt double %19, 1.000000e+00
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load double, double* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), double %22)
br label %41
24: ; preds = %18
%25 = load double, double* %4
%26 = fcmp ogt double %25, 1.000000e-01
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load double, double* %3
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), double %28)
br label %40
30: ; preds = %24
%31 = load double, double* %4
%32 = fcmp ogt double %31, 1.000000e-02
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load double, double* %3
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), double %34)
br label %39
36: ; preds = %30
%37 = load double, double* %3
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %37)
br label %39
39: ; preds = %36, %33
br label %40
40: ; preds = %39, %27
br label %41
41: ; preds = %40, %21
br label %42
42: ; preds = %41, %15
br label %43
43: ; preds = %42, %9
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [6 x i8] c"%#.2g\00"
@.str.1 = [5 x i8] c"%.0f\00"
@.str.2 = [5 x i8] c"%.1f\00"
@.str.3 = [5 x i8] c"%.2f\00"
@.str.4 = [5 x i8] c"%.3f\00"
define i32 @print2digits(double %0) {
%2 = tail call double @llvm.fabs.f64(double %0)
%3 = fcmp ogt double %2, 1.000000e+03
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = fcmp ogt double %2, 1.000000e+01
br i1 %5, label %13, label %6
6: ; preds = %4
%7 = fcmp ogt double %2, 1.000000e+00
br i1 %7, label %13, label %8
8: ; preds = %6
%9 = fcmp ogt double %2, 1.000000e-01
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = fcmp ogt double %2, 1.000000e-02
%12 = select i1 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0)
br label %13
13: ; preds = %10, %8, %6, %4, %1
%14 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %8 ], [ %12, %10 ]
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %14, double %0)
ret i32 undef
}