#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, double, char *);
char *scale(unsigned long long n) {
  static char buf[1024];
  double val = 0.0;
  int i = 0;
  unsigned long p = 1;
  char *suffixes[] = {" bytes", "K", "M", "G", "T", "P"};
  for (i = 0; i < 5; i++) {
    if ((int)(n / (p *= 1024)) == 0)
      break;
  }
  val = ((double)n) / (p / 1024.0);
  if (i > 1)
    snprintf(buf, sizeof(buf), "%.2f%s", val, suffixes[i]);
  else
    snprintf(buf, sizeof(buf), "%.0f%s", val, suffixes[i]);
  return (buf);
}


x86 assembly (clang, -O0):
<scale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x1,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
lea    0xed6(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xed2(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xec9(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xec0(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xeb7(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xeae(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    11dc <scale+0xbc>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0xa,%rcx
mov    %rcx,-0x20(%rbp)
xor    %edx,%edx
div    %rcx
cmp    $0x0,%eax
jne    11c9 <scale+0xa9>
jmp    11dc <scale+0xbc>
jmp    11ce <scale+0xae>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119c <scale+0x7c>
movq   -0x8(%rbp),%xmm0
movaps 0xe28(%rip),%xmm3
punpckldq %xmm3,%xmm0
movapd 0xe2c(%rip),%xmm2
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movq   -0x20(%rbp),%xmm1
punpckldq %xmm3,%xmm1
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
movsd  0xddd(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    1265 <scale+0x145>
movsd  -0x10(%rbp),%xmm0
movslq -0x14(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rcx
lea    0x2df1(%rip),%rdi
mov    $0x400,%esi
lea    0xde6(%rip),%rdx
call   1030 <snprintf@plt>
jmp    128b <scale+0x16b>
movsd  -0x10(%rbp),%xmm0
movslq -0x14(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rcx
lea    0x2dc6(%rip),%rdi
mov    $0x400,%esi
lea    0xdc2(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2dae(%rip),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scale>:
sub    $0x38,%rsp
lea    0xf35(%rip),%rax
mov    %rax,(%rsp)
lea    0xf31(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xf27(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xf1d(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xf13(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xf09(%rip),%rcx
mov    %rcx,0x28(%rsp)
movabs $0x3fffffffc00,%rax
test   %rax,%rdi
je     11a0 <scale+0x90>
movabs $0xffffffff00000,%rax
test   %rax,%rdi
je     11ac <scale+0x9c>
movabs $0x3fffffffc0000000,%rax
ff ff 3f
test   %rax,%rdi
je     11f2 <scale+0xe2>
mov    %rdi,%rax
shr    $0x28,%rax
jne    1201 <scale+0xf1>
movsd  0xe7f(%rip),%xmm1
mov    $0x3,%eax
jmp    1217 <scale+0x107>
movsd  0xe58(%rip),%xmm1
xor    %eax,%eax
jmp    11b9 <scale+0xa9>
movsd  0xe54(%rip),%xmm1
mov    $0x1,%eax
movq   %rdi,%xmm2
punpckldq 0xe6a(%rip),%xmm2
subpd  0xe72(%rip),%xmm2
movapd %xmm2,%xmm0
unpckhpd %xmm2,%xmm0
addsd  %xmm2,%xmm0
divsd  %xmm1,%xmm0
mov    (%rsp,%rax,8),%rcx
lea    0x2e57(%rip),%rdi
lea    0xe78(%rip),%rdx
jmp    124e <scale+0x13e>
movsd  0xe16(%rip),%xmm1
mov    $0x2,%eax
jmp    1217 <scale+0x107>
mov    %rdi,%rax
shr    $0x32,%rax
jne    1264 <scale+0x154>
movsd  0xe0e(%rip),%xmm1
mov    $0x4,%eax
movq   %rdi,%xmm2
punpckldq 0xe0c(%rip),%xmm2
subpd  0xe14(%rip),%xmm2
movapd %xmm2,%xmm0
unpckhpd %xmm2,%xmm0
addsd  %xmm2,%xmm0
divsd  %xmm1,%xmm0
mov    (%rsp,%rax,8),%rcx
lea    0x2df9(%rip),%rdi
lea    0xe13(%rip),%rdx
mov    $0x400,%esi
call   1030 <snprintf@plt>
lea    0x2de1(%rip),%rax
add    $0x38,%rsp
ret
movq   %rdi,%xmm1
punpckldq 0xdbf(%rip),%xmm1
subpd  0xdc7(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
mulsd  0xd9b(%rip),%xmm0
jmp    1240 <scale+0x130>

LLVM IR (-O0):
@scale.buf = internal global [1024 x i8] zeroinitializer
@.str = [7 x i8] c" bytes\00"
@.str.1 = [2 x i8] c"K\00"
@.str.2 = [2 x i8] c"M\00"
@.str.3 = [2 x i8] c"G\00"
@.str.4 = [2 x i8] c"T\00"
@.str.5 = [2 x i8] c"P\00"
@.str.6 = [7 x i8] c"%.2f%s\00"
@.str.7 = [7 x i8] c"%.0f%s\00"
define i8* @scale(i64 %0) {
%2 = alloca i64
%3 = alloca double
%4 = alloca i32
%5 = alloca i64
%6 = alloca [6 x i8*]
store i64 %0, i64* %2
store double 0.000000e+00, double* %3
store i32 0, i32* %4
store i64 1, i64* %5
%7 = bitcast [6 x i8*]* %6 to i8*
%8 = bitcast i8* %7 to [6 x i8*]*
%9 = getelementptr inbounds [6 x i8*], [6 x i8*]* %8, i32 0, i32 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i32 0, i32 0), i8** %9
%10 = getelementptr inbounds [6 x i8*], [6 x i8*]* %8, i32 0, i32 1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8** %10
%11 = getelementptr inbounds [6 x i8*], [6 x i8*]* %8, i32 0, i32 2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [6 x i8*], [6 x i8*]* %8, i32 0, i32 3
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [6 x i8*], [6 x i8*]* %8, i32 0, i32 4
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %8, i32 0, i32 5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i32 0, i32 0), i8** %14
store i32 0, i32* %4
br label %15
15: ; preds = %27, %1
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 5
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i64, i64* %2
%20 = load i64, i64* %5
%21 = mul i64 %20, 1024
store i64 %21, i64* %5
%22 = udiv i64 %19, %21
%23 = trunc i64 %22 to i32
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
br label %30
26: ; preds = %18
br label %27
27: ; preds = %26
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %15
30: ; preds = %25, %15
%31 = load i64, i64* %2
%32 = uitofp i64 %31 to double
%33 = load i64, i64* %5
%34 = uitofp i64 %33 to double
%35 = fdiv double %34, 1.024000e+03
%36 = fdiv double %32, %35
store double %36, double* %3
%37 = load i32, i32* %4
%38 = icmp sgt i32 %37, 1
br i1 %38, label %39, label %46
39: ; preds = %30
%40 = load double, double* %3
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [6 x i8*], [6 x i8*]* %6, i64 0, i64 %42
%44 = load i8*, i8** %43
%45 = call i32 @snprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @scale.buf, i64 0, i64 0), i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), double %40, i8* %44)
br label %53
46: ; preds = %30
%47 = load double, double* %3
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [6 x i8*], [6 x i8*]* %6, i64 0, i64 %49
%51 = load i8*, i8** %50
%52 = call i32 @snprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @scale.buf, i64 0, i64 0), i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), double %47, i8* %51)
br label %53
53: ; preds = %46, %39
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @scale.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@scale.buf = internal global [1024 x i8] zeroinitializer
@.str = [7 x i8] c" bytes\00"
@.str.1 = [2 x i8] c"K\00"
@.str.2 = [2 x i8] c"M\00"
@.str.3 = [2 x i8] c"G\00"
@.str.4 = [2 x i8] c"T\00"
@.str.5 = [2 x i8] c"P\00"
@.str.6 = [7 x i8] c"%.2f%s\00"
@.str.7 = [7 x i8] c"%.0f%s\00"
define i8* @scale(i64 %0) {
%2 = alloca [6 x i8*]
%3 = bitcast [6 x i8*]* %2 to i8*
%4 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 2
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 3
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 4
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8** %9
%10 = and i64 %0, 4398046510080
%11 = icmp eq i64 %10, 0
br i1 %11, label %36, label %12
12: ; preds = %1
%13 = and i64 %0, 4503599626321920
%14 = icmp eq i64 %13, 0
br i1 %14, label %36, label %15
15: ; preds = %12
%16 = and i64 %0, 4611686017353646080
%17 = icmp eq i64 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = icmp ult i64 %0, 1099511627776
br i1 %19, label %25, label %20
20: ; preds = %18
%21 = icmp ult i64 %0, 1125899906842624
br i1 %21, label %25, label %22
22: ; preds = %20
%23 = uitofp i64 %0 to double
%24 = fmul double %23, 0x3D70000000000000
br label %32
25: ; preds = %20, %18, %15
%26 = phi i64 [ 2, %15 ], [ 3, %18 ], [ 4, %20 ]
%27 = phi double [ 0x4130000000000000, %15 ], [ 0x41D0000000000000, %18 ], [ 0x4270000000000000, %20 ]
%28 = uitofp i64 %0 to double
%29 = fdiv double %28, %27
%30 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 %26
%31 = load i8*, i8** %30
br label %32
32: ; preds = %25, %22
%33 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), %22 ], [ %31, %25 ]
%34 = phi double [ %24, %22 ], [ %29, %25 ]
%35 = tail call i32 @snprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @scale.buf, i64 0, i64 0), i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), double %34, i8* %33)
br label %44
36: ; preds = %1, %12
%37 = phi i64 [ 1, %12 ], [ 0, %1 ]
%38 = phi double [ 1.024000e+03, %12 ], [ 1.000000e+00, %1 ]
%39 = uitofp i64 %0 to double
%40 = fdiv double %39, %38
%41 = getelementptr inbounds [6 x i8*], [6 x i8*]* %2, i64 0, i64 %37
%42 = load i8*, i8** %41
%43 = tail call i32 @snprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @scale.buf, i64 0, i64 0), i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), double %40, i8* %42)
br label %44
44: ; preds = %36, %32
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @scale.buf, i64 0, i64 0)
}