#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 */
typedef int /*<<< orphan*/ unit;

/* Variables and functions */
int /*<<< orphan*/ safef(char *, int, char *, int, double, char *);
void sprintWithMetricBaseUnit(char *s, int slength, long long size) {
  char *unit[] = {"bp", "kB", "Mb", "Gb", "Tb", "Pb"};
  int maxUnit = (sizeof(unit) / sizeof(char *)) - 1;
  int i = 0;
  long long d = 1;
  while (((size / d) >= 1000) && (i != maxUnit)) {
    ++i;
    d *= 1000;
  }
  double result = ((double)size) / d;
  safef(s, slength, "%3.*f %s", result < 10 ? 1 : 0, ((double)size) / d,
        unit[i]);
}


x86 assembly (clang, -O0):
<sprintWithMetricBaseUnit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
lea    0xebe(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xeb6(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xeae(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xea6(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe9e(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe96(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x5,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
movq   $0x1,-0x60(%rbp)
mov    -0x18(%rbp),%rax
cqto
idivq  -0x60(%rbp)
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x3e8,%rcx
mov    %al,-0x69(%rbp)
jl     11c6 <sprintWithMetricBaseUnit+0xa6>
mov    -0x58(%rbp),%eax
cmp    -0x54(%rbp),%eax
setne  %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    11d6 <sprintWithMetricBaseUnit+0xb6>
jmp    11f0 <sprintWithMetricBaseUnit+0xd0>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
imul   $0x3e8,-0x60(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    119b <sprintWithMetricBaseUnit+0x7b>
cvtsi2sdq -0x18(%rbp),%xmm0
cvtsi2sdq -0x60(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
movsd  -0x68(%rbp),%xmm1
movsd  0xde7(%rip),%xmm0
xor    %ecx,%ecx
mov    $0x1,%eax
ucomisd %xmm1,%xmm0
cmova  %eax,%ecx
cvtsi2sdq -0x18(%rbp),%xmm0
cvtsi2sdq -0x60(%rbp),%xmm1
divsd  %xmm1,%xmm0
movslq -0x58(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%r8
lea    0xdd3(%rip),%rdx
call   1040 <safef@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sprintWithMetricBaseUnit>:
lea    0xf24(%rip),%rax
mov    %rax,-0x30(%rsp)
lea    0xf1b(%rip),%rax
mov    %rax,-0x28(%rsp)
lea    0xf12(%rip),%rax
mov    %rax,-0x20(%rsp)
lea    0xf09(%rip),%rax
mov    %rax,-0x18(%rsp)
lea    0xf00(%rip),%rax
mov    %rax,-0x10(%rsp)
cmp    $0x3e8,%rdx
jl     119a <sprintWithMetricBaseUnit+0x8a>
cmp    $0xf4240,%rdx
jl     11ab <sprintWithMetricBaseUnit+0x9b>
cmp    $0x3b9aca00,%rdx
jl     11ba <sprintWithMetricBaseUnit+0xaa>
movabs $0xe8d4a51000,%rax
cmp    %rax,%rdx
jl     11c9 <sprintWithMetricBaseUnit+0xb9>
movabs $0x38d7ea4c67fff,%rcx
xor    %eax,%eax
cmp    %rcx,%rdx
setg   %al
lea    0xe99(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm1
or     $0x4,%rax
jmp    11d6 <sprintWithMetricBaseUnit+0xc6>
movsd  0xe5e(%rip),%xmm1
lea    0xe8f(%rip),%r8
jmp    11db <sprintWithMetricBaseUnit+0xcb>
movsd  0xe55(%rip),%xmm1
mov    $0x1,%eax
jmp    11d6 <sprintWithMetricBaseUnit+0xc6>
movsd  0xe4e(%rip),%xmm1
mov    $0x2,%eax
jmp    11d6 <sprintWithMetricBaseUnit+0xc6>
movsd  0xe47(%rip),%xmm1
mov    $0x3,%eax
mov    -0x38(%rsp,%rax,8),%r8
cvtsi2sd %rdx,%xmm0
divsd  %xmm1,%xmm0
movsd  0xe34(%rip),%xmm1
xor    %ecx,%ecx
ucomisd %xmm0,%xmm1
seta   %cl
lea    0xe4e(%rip),%rdx
jmp    1030 <safef@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"bp\00"
@.str.1 = [3 x i8] c"kB\00"
@.str.2 = [3 x i8] c"Mb\00"
@.str.3 = [3 x i8] c"Gb\00"
@.str.4 = [3 x i8] c"Tb\00"
@.str.5 = [3 x i8] c"Pb\00"
@.str.6 = [9 x i8] c"%3.*f %s\00"
define void @sprintWithMetricBaseUnit(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca [6 x i8*]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca double
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%12 = bitcast [6 x i8*]* %7 to i8*
%13 = bitcast i8* %12 to [6 x i8*]*
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8** %15
%16 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8** %16
%17 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8** %17
%18 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8** %18
%19 = getelementptr inbounds [6 x i8*], [6 x i8*]* %13, i32 0, i32 5
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i32 0, i32 0), i8** %19
store i32 5, i32* %8
store i32 0, i32* %9
store i64 1, i64* %10
br label %20
20: ; preds = %31, %3
%21 = load i64, i64* %6
%22 = load i64, i64* %10
%23 = sdiv i64 %21, %22
%24 = icmp sge i64 %23, 1000
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* %9
%27 = load i32, i32* %8
%28 = icmp ne i32 %26, %27
br label %29
29: ; preds = %25, %20
%30 = phi i1 [ false, %20 ], [ %28, %25 ]
br i1 %30, label %31, label %36
31: ; preds = %29
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
%34 = load i64, i64* %10
%35 = mul nsw i64 %34, 1000
store i64 %35, i64* %10
br label %20
36: ; preds = %29
%37 = load i64, i64* %6
%38 = sitofp i64 %37 to double
%39 = load i64, i64* %10
%40 = sitofp i64 %39 to double
%41 = fdiv double %38, %40
store double %41, double* %11
%42 = load i8*, i8** %4
%43 = load i32, i32* %5
%44 = load double, double* %11
%45 = fcmp olt double %44, 1.000000e+01
%46 = zext i1 %45 to i64
%47 = select i1 %45, i32 1, i32 0
%48 = load i64, i64* %6
%49 = sitofp i64 %48 to double
%50 = load i64, i64* %10
%51 = sitofp i64 %50 to double
%52 = fdiv double %49, %51
%53 = load i32, i32* %9
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [6 x i8*], [6 x i8*]* %7, i64 0, i64 %54
%56 = load i8*, i8** %55
%57 = call i32 @safef(i8* %42, i32 %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %47, double %52, i8* %56)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"bp\00"
@.str.1 = [3 x i8] c"kB\00"
@.str.2 = [3 x i8] c"Mb\00"
@.str.3 = [3 x i8] c"Gb\00"
@.str.4 = [3 x i8] c"Tb\00"
@.str.5 = [3 x i8] c"Pb\00"
@.str.6 = [9 x i8] c"%3.*f %s\00"
define void @sprintWithMetricBaseUnit(i8* %0, i32 %1, i64 %2) {
%4 = alloca [6 x i8*]
%5 = bitcast [6 x i8*]* %4 to i8*
%6 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 5
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8** %10
%11 = icmp sgt i64 %2, 999
br i1 %11, label %12, label %27
12: ; preds = %3
%13 = icmp sgt i64 %2, 999999
br i1 %13, label %14, label %22
14: ; preds = %12
%15 = icmp sgt i64 %2, 999999999
br i1 %15, label %16, label %22
16: ; preds = %14
%17 = icmp sgt i64 %2, 999999999999
br i1 %17, label %18, label %22
18: ; preds = %16
%19 = icmp sgt i64 %2, 999999999999999
%20 = select i1 %19, i64 5, i64 4
%21 = select i1 %19, double 1.000000e+15, double 1.000000e+12
br label %22
22: ; preds = %18, %16, %14, %12
%23 = phi i64 [ 1, %12 ], [ 2, %14 ], [ 3, %16 ], [ %20, %18 ]
%24 = phi double [ 1.000000e+03, %12 ], [ 1.000000e+06, %14 ], [ 1.000000e+09, %16 ], [ %21, %18 ]
%25 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 %23
%26 = load i8*, i8** %25
br label %27
27: ; preds = %3, %22
%28 = phi i8* [ %26, %22 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %3 ]
%29 = phi double [ %24, %22 ], [ 1.000000e+00, %3 ]
%30 = sitofp i64 %2 to double
%31 = fdiv double %30, %29
%32 = fcmp olt double %31, 1.000000e+01
%33 = zext i1 %32 to i32
%34 = tail call i32 @safef(i8* %0, i32 %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %33, double %31, i8* %28)
ret void
}