#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *const, size_t const, char *, double,
                            char const *, char *);
char *units_to_str(const double val, char *units, char *const buf,
                   const size_t buflen) {
  double v = (double)val;
  size_t i;
  static const char *scales[] = {"", "m", "µ", "n", "p", "f", "a", NULL};
  for (i = 0; i < 6; i++, v *= 1000) {
    if (v > 0.5)
      break;
  }
  (void)snprintf(buf, buflen, "%.2f %s%s", v, scales[i], units);
  return buf;
}


x86 assembly (clang, -O0):
<units_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x6,-0x30(%rbp)
jae    118a <units_to_str+0x7a>
movsd  -0x28(%rbp),%xmm0
movsd  0xead(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1162 <units_to_str+0x52>
jmp    118a <units_to_str+0x7a>
jmp    1167 <units_to_str+0x57>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
movsd  0xe8d(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    113b <units_to_str+0x2b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movsd  -0x28(%rbp),%xmm0
mov    -0x30(%rbp),%rcx
lea    0x2e8e(%rip),%rax
mov    (%rax,%rcx,8),%rcx
mov    -0x10(%rbp),%r8
lea    0xe6c(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<units_to_str>:
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r8
ucomisd 0xee1(%rip),%xmm0
jbe    1128 <units_to_str+0x18>
xor    %eax,%eax
jmp    11a8 <units_to_str+0x98>
mulsd  0xed8(%rip),%xmm0
mov    $0x1,%eax
ucomisd 0xec3(%rip),%xmm0
ja     11a8 <units_to_str+0x98>
mulsd  0xec1(%rip),%xmm0
mov    $0x2,%eax
ucomisd 0xeac(%rip),%xmm0
ja     11a8 <units_to_str+0x98>
mulsd  0xeaa(%rip),%xmm0
mov    $0x3,%eax
ucomisd 0xe95(%rip),%xmm0
ja     11a8 <units_to_str+0x98>
mulsd  0xe93(%rip),%xmm0
mov    $0x4,%eax
ucomisd 0xe7e(%rip),%xmm0
ja     11a8 <units_to_str+0x98>
mulsd  0xe7c(%rip),%xmm0
mov    $0x5,%eax
ucomisd 0xe67(%rip),%xmm0
ja     11a8 <units_to_str+0x98>
mulsd  0xe65(%rip),%xmm0
mov    $0x6,%eax
lea    0x2c31(%rip),%rcx
mov    (%rcx,%rax,8),%rcx
lea    0xe63(%rip),%rax
mov    %rbx,%rdi
mov    %rdx,%rsi
mov    %rax,%rdx
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@units_to_str.scales = internal global [8 x i8*] [i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i32 0, i32 0), i8* null]
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"m\00"
@.str.2 = [3 x i8] c"\C2\B5\00"
@.str.3 = [2 x i8] c"n\00"
@.str.4 = [2 x i8] c"p\00"
@.str.5 = [2 x i8] c"f\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [10 x i8] c"%.2f %s%s\00"
define i8* @units_to_str(double %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca double
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca double
%10 = alloca i64
store double %0, double* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%11 = load double, double* %5
store double %11, double* %9
store i64 0, i64* %10
br label %12
12: ; preds = %20, %4
%13 = load i64, i64* %10
%14 = icmp ult i64 %13, 6
br i1 %14, label %15, label %25
15: ; preds = %12
%16 = load double, double* %9
%17 = fcmp ogt double %16, 5.000000e-01
br i1 %17, label %18, label %19
18: ; preds = %15
br label %25
19: ; preds = %15
br label %20
20: ; preds = %19
%21 = load i64, i64* %10
%22 = add i64 %21, 1
store i64 %22, i64* %10
%23 = load double, double* %9
%24 = fmul double %23, 1.000000e+03
store double %24, double* %9
br label %12
25: ; preds = %18, %12
%26 = load i8*, i8** %7
%27 = load i64, i64* %8
%28 = load double, double* %9
%29 = load i64, i64* %10
%30 = getelementptr inbounds [8 x i8*], [8 x i8*]* @units_to_str.scales, i64 0, i64 %29
%31 = load i8*, i8** %30
%32 = load i8*, i8** %6
%33 = call i32 @snprintf(i8* %26, i64 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), double %28, i8* %31, i8* %32)
%34 = load i8*, i8** %7
ret i8* %34
}

LLVM IR (-O2):
@units_to_str.scales = internal unnamed_addr constant [8 x i8*] [i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i32 0, i32 0), i8* null]
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"m\00"
@.str.2 = [3 x i8] c"\C2\B5\00"
@.str.3 = [2 x i8] c"n\00"
@.str.4 = [2 x i8] c"p\00"
@.str.5 = [2 x i8] c"f\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [10 x i8] c"%.2f %s%s\00"
define i8* @units_to_str(double %0, i8* %1, i8* returned %2, i64 %3) {
%5 = fcmp ogt double %0, 5.000000e-01
br i1 %5, label %23, label %6
6: ; preds = %4
%7 = fmul double %0, 1.000000e+03
%8 = fcmp ogt double %7, 5.000000e-01
br i1 %8, label %23, label %9
9: ; preds = %6
%10 = fmul double %7, 1.000000e+03
%11 = fcmp ogt double %10, 5.000000e-01
br i1 %11, label %23, label %12
12: ; preds = %9
%13 = fmul double %10, 1.000000e+03
%14 = fcmp ogt double %13, 5.000000e-01
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = fmul double %13, 1.000000e+03
%17 = fcmp ogt double %16, 5.000000e-01
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = fmul double %16, 1.000000e+03
%20 = fcmp ogt double %19, 5.000000e-01
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = fmul double %19, 1.000000e+03
br label %23
23: ; preds = %6, %9, %12, %15, %18, %21, %4
%24 = phi double [ %0, %4 ], [ %7, %6 ], [ %10, %9 ], [ %13, %12 ], [ %16, %15 ], [ %19, %18 ], [ %22, %21 ]
%25 = phi i64 [ 0, %4 ], [ 1, %6 ], [ 2, %9 ], [ 3, %12 ], [ 4, %15 ], [ 5, %18 ], [ 6, %21 ]
%26 = getelementptr inbounds [8 x i8*], [8 x i8*]* @units_to_str.scales, i64 0, i64 %25
%27 = load i8*, i8** %26
%28 = tail call i32 @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), double %24, i8* %27, i8* %1)
ret i8* %2
}