char *string_metric(double value, int power_needed, char *buffer) {
  static const char suffix[][3] = {"", " K", " M", " G", " T", " P"};
  static char localbuffer[100];
  double magnitude;
  if (power_needed == -1) {
    magnitude = floor(log(value) / log(1024.0));
  } else {
    magnitude = power_needed;
  }
  magnitude = fmin(fmax(magnitude, 0.0),
                   (double)(sizeof(suffix) / sizeof(suffix[0]) - 1));
  if (!buffer)
    buffer = localbuffer;
  snprintf(buffer, sizeof(localbuffer), "%.1f%s",
           value / pow(1024.0, magnitude), suffix[(int)magnitude]);
  return buffer;
}


x86 assembly (clang, -O0):
<string_metric>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1195 <string_metric+0x55>
movsd  -0x8(%rbp),%xmm0
call   1040 <log@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  0xe8b(%rip),%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
divsd  %xmm1,%xmm0
call   1060 <floor@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    119f <string_metric+0x5f>
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
maxsd  %xmm1,%xmm0
movsd  0xe55(%rip),%xmm1
minsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <string_metric+0x92>
lea    0x2e82(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  0xe0f(%rip),%xmm0
call   1050 <pow@plt>
mov    -0x38(%rbp),%rdi
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
divsd  %xmm1,%xmm0
cvttsd2si -0x20(%rbp),%eax
cltq
lea    0xdfc(%rip),%rcx
imul   $0x3,%rax,%rax
add    %rax,%rcx
mov    $0x64,%esi
lea    0xdfb(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_metric>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
cmp    $0xffffffff,%edi
movsd  %xmm0,0x10(%rsp)
je     115b <string_metric+0x1b>
cvtsi2sd %edi,%xmm1
jmp    1171 <string_metric+0x31>
call   1050 <log@plt>
divsd  0xe98(%rip),%xmm0
call   1060 <floor@plt>
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
maxsd  %xmm0,%xmm1
minsd  0xe87(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
test   %r14,%r14
lea    0x2ebf(%rip),%rbx
cmovne %r14,%rbx
movsd  0xe73(%rip),%xmm0
mulsd  %xmm1,%xmm0
call   1030 <exp2@plt>
movsd  0x10(%rsp),%xmm1
divsd  %xmm0,%xmm1
cvttsd2si 0x8(%rsp),%eax
cltq
lea    (%rax,%rax,2),%rax
lea    0xe5d(%rip),%rcx
add    %rax,%rcx
lea    0xe65(%rip),%rdx
mov    $0x64,%esi
mov    %rbx,%rdi
movapd %xmm1,%xmm0
mov    $0x1,%al
call   1040 <snprintf@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@string_metric.suffix = internal constant [6 x [3 x i8]] [[3 x i8] zeroinitializer, [3 x i8] c" K\00", [3 x i8] c" M\00", [3 x i8] c" G\00", [3 x i8] c" T\00", [3 x i8] c" P\00"]
@string_metric.localbuffer = internal global [100 x i8] zeroinitializer
@.str = [7 x i8] c"%.1f%s\00"
define i8* @string_metric(double %0, i32 %1, i8* %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca i8*
%7 = alloca double
store double %0, double* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = load double, double* %4
%12 = call double @log(double %11)
%13 = call double @log(double 1.024000e+03)
%14 = fdiv double %12, %13
%15 = call double @llvm.floor.f64(double %14)
store double %15, double* %7
br label %19
16: ; preds = %3
%17 = load i32, i32* %5
%18 = sitofp i32 %17 to double
store double %18, double* %7
br label %19
19: ; preds = %16, %10
%20 = load double, double* %7
%21 = call double @llvm.maxnum.f64(double %20, double 0.000000e+00)
%22 = call double @llvm.minnum.f64(double %21, double 5.000000e+00)
store double %22, double* %7
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %19
store i8* getelementptr inbounds ([100 x i8], [100 x i8]* @string_metric.localbuffer, i64 0, i64 0), i8** %6
br label %26
26: ; preds = %25, %19
%27 = load i8*, i8** %6
%28 = load double, double* %4
%29 = load double, double* %7
%30 = call double @pow(double 1.024000e+03, double %29)
%31 = fdiv double %28, %30
%32 = load double, double* %7
%33 = fptosi double %32 to i32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [6 x [3 x i8]], [6 x [3 x i8]]* @string_metric.suffix, i64 0, i64 %34
%36 = getelementptr inbounds [3 x i8], [3 x i8]* %35, i64 0, i64 0
%37 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %27, i64 100, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), double %31, i8* %36)
%38 = load i8*, i8** %6
ret i8* %38
}

LLVM IR (-O2):
@string_metric.suffix = internal constant [6 x [3 x i8]] [[3 x i8] zeroinitializer, [3 x i8] c" K\00", [3 x i8] c" M\00", [3 x i8] c" G\00", [3 x i8] c" T\00", [3 x i8] c" P\00"]
@string_metric.localbuffer = internal global [100 x i8] zeroinitializer
@.str = [7 x i8] c"%.1f%s\00"
define i8* @string_metric(double %0, i32 %1, i8* writeonly %2) {
%4 = icmp eq i32 %1, -1
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = tail call double @log(double %0)
%7 = fdiv double %6, 0x401BB9D3BEB8C86B
%8 = tail call double @llvm.floor.f64(double %7)
br label %11
9: ; preds = %3
%10 = sitofp i32 %1 to double
br label %11
11: ; preds = %9, %5
%12 = phi double [ %8, %5 ], [ %10, %9 ]
%13 = tail call double @llvm.maxnum.f64(double %12, double 0.000000e+00)
%14 = tail call double @llvm.minnum.f64(double %13, double 5.000000e+00)
%15 = icmp eq i8* %2, null
%16 = select i1 %15, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @string_metric.localbuffer, i64 0, i64 0), i8* %2
%17 = fmul double %14, 1.000000e+01
%18 = tail call double @exp2(double %17)
%19 = fdiv double %0, %18
%20 = fptosi double %14 to i32
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [6 x [3 x i8]], [6 x [3 x i8]]* @string_metric.suffix, i64 0, i64 %21, i64 0
%23 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) %16, i64 100, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), double %19, i8* %22)
ret i8* %16
}