#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 const snprintf(char *, int, char *, unsigned long, ...);
const char *scale_num(unsigned long num, const int width, const unsigned type) {
  static double scale[] = {1024.0, 1024.0 * 1024, 1024.0 * 1024 * 1024,
                           1024.0 * 1024 * 1024 * 1024, 0};
  static char nextup[] = {'k', 'm', 'g', 't', 0};
  static char buf[32];
  double *dp;
  char *up;
  if (width >= snprintf(buf, sizeof(buf), "%lu", num))
    return buf;
  for (up = nextup + type, dp = scale; *dp; ++dp, ++up) {
    if (width >= snprintf(buf, sizeof(buf), "%.1f%c", num / *dp, *up))
      return buf;
    if (width >=
        snprintf(buf, sizeof(buf), "%ld%c", (unsigned long)(num / *dp), *up))
      return buf;
  }
  return "?";
}


x86 assembly (clang, -O0):
<scale_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rcx
lea    0x2f3d(%rip),%rdi
mov    $0x20,%esi
lea    0xee9(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jl     1163 <scale_num+0x53>
lea    0x2f16(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    130a <scale_num+0x1fa>
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
lea    0x2ee9(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
lea    0x2eb3(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11a1 <scale_num+0x91>
jp     11a1 <scale_num+0x91>
jmp    12ff <scale_num+0x1ef>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
movaps 0xe52(%rip),%xmm1
movq   -0x10(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe51(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rcx
mov    %rcx,%rdx
sar    $0x3f,%rdx
movsd  0xe2f(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
and    %rdx,%rax
or     %rax,%rcx
mov    -0x28(%rbp),%rax
movsbl (%rax),%r8d
lea    0x2e61(%rip),%rdi
mov    $0x20,%esi
lea    0xe11(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jl     123f <scale_num+0x12f>
lea    0x2e3a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    130a <scale_num+0x1fa>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
movaps 0xdb4(%rip),%xmm1
movq   -0x10(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xdb3(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rcx
mov    %rcx,%rdx
sar    $0x3f,%rdx
movsd  0xd91(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
and    %rdx,%rax
or     %rax,%rcx
mov    -0x28(%rbp),%rax
movsbl (%rax),%r8d
lea    0x2dc3(%rip),%rdi
mov    $0x20,%esi
lea    0xd7a(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jl     12dd <scale_num+0x1cd>
lea    0x2d9c(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    130a <scale_num+0x1fa>
jmp    12e2 <scale_num+0x1d2>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1181 <scale_num+0x71>
lea    0xd33(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scale_num>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x2f13(%rip),%r12
lea    0xf19(%rip),%rdx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jle    13a5 <scale_num+0x295>
mov    %ebx,%ebx
lea    0xef0(%rip),%r13
movq   %rbp,%xmm0
punpckldq 0xe9b(%rip),%xmm0
subpd  0xea3(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe9f(%rip),%xmm0
movapd %xmm1,(%rsp)
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xe8e(%rip),%xmm0
cvttsd2si %xmm0,%rbp
sar    $0x3f,%rcx
and    %rcx,%rbp
or     %rax,%rbp
movsbl (%rbx,%r13,1),%r15d
lea    0x2e8b(%rip),%r12
lea    0xe95(%rip),%rdx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jle    13a5 <scale_num+0x295>
lea    0x2e5f(%rip),%r12
lea    0xe70(%rip),%rdx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jle    13a5 <scale_num+0x295>
movsd  0xe22(%rip),%xmm0
mulsd  (%rsp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xe05(%rip),%xmm0
cvttsd2si %xmm0,%rbp
sar    $0x3f,%rcx
and    %rcx,%rbp
or     %rax,%rbp
movsbl 0x1(%rbx,%r13,1),%r15d
lea    0x2e01(%rip),%r12
lea    0xe0b(%rip),%rdx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jle    13a5 <scale_num+0x295>
lea    0x2dd5(%rip),%r12
lea    0xde6(%rip),%rdx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jle    13a5 <scale_num+0x295>
movsd  0xda0(%rip),%xmm0
mulsd  (%rsp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xd7b(%rip),%xmm0
cvttsd2si %xmm0,%rbp
sar    $0x3f,%rcx
and    %rcx,%rbp
or     %rax,%rbp
movsbl 0x2(%rbx,%r13,1),%r15d
lea    0x2d77(%rip),%r12
lea    0xd81(%rip),%rdx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jle    13a5 <scale_num+0x295>
lea    0x2d4b(%rip),%r12
lea    0xd5c(%rip),%rdx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jle    13a5 <scale_num+0x295>
movapd (%rsp),%xmm0
mulsd  0xd19(%rip),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xcf1(%rip),%xmm0
cvttsd2si %xmm0,%rbp
sar    $0x3f,%rcx
and    %rcx,%rbp
or     %rax,%rbp
movsbl 0x3(%rbx,%r13,1),%r15d
lea    0x2ced(%rip),%r12
lea    0xcf7(%rip),%rdx
mov    %r12,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jle    13a5 <scale_num+0x295>
lea    0x2cc5(%rip),%rbx
lea    0xcd6(%rip),%rdx
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %rbp,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
cmp    %r14d,%eax
lea    0xcbd(%rip),%r12
cmovle %rbx,%r12
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@scale_num.scale = internal global [5 x double] [double 1.024000e+03, double 0x4130000000000000, double 0x41D0000000000000, double 0x4270000000000000, double 0.000000e+00]
@scale_num.nextup = internal global [5 x i8] c"kmgt\00"
@scale_num.buf = internal global [32 x i8] zeroinitializer
@.str = [4 x i8] c"%lu\00"
@.str.1 = [7 x i8] c"%.1f%c\00"
@.str.2 = [6 x i8] c"%ld%c\00"
@.str.3 = [2 x i8] c"?\00"
define i8* @scale_num(i64 %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca double*
%9 = alloca i8*
store i64 %0, i64* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = load i64, i64* %5
%12 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %11)
%13 = icmp sge i32 %10, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i8** %4
br label %58
15: ; preds = %3
%16 = load i32, i32* %7
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @scale_num.nextup, i64 0, i64 0), i64 %17
store i8* %18, i8** %9
store double* getelementptr inbounds ([5 x double], [5 x double]* @scale_num.scale, i64 0, i64 0), double** %8
br label %19
19: ; preds = %52, %15
%20 = load double*, double** %8
%21 = load double, double* %20
%22 = fcmp une double %21, 0.000000e+00
br i1 %22, label %23, label %57
23: ; preds = %19
%24 = load i32, i32* %6
%25 = load i64, i64* %5
%26 = uitofp i64 %25 to double
%27 = load double*, double** %8
%28 = load double, double* %27
%29 = fdiv double %26, %28
%30 = fptoui double %29 to i64
%31 = load i8*, i8** %9
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %30, i32 %33)
%35 = icmp sge i32 %24, %34
br i1 %35, label %36, label %37
36: ; preds = %23
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i8** %4
br label %58
37: ; preds = %23
%38 = load i32, i32* %6
%39 = load i64, i64* %5
%40 = uitofp i64 %39 to double
%41 = load double*, double** %8
%42 = load double, double* %41
%43 = fdiv double %40, %42
%44 = fptoui double %43 to i64
%45 = load i8*, i8** %9
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %44, i32 %47)
%49 = icmp sge i32 %38, %48
br i1 %49, label %50, label %51
50: ; preds = %37
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i8** %4
br label %58
51: ; preds = %37
br label %52
52: ; preds = %51
%53 = load double*, double** %8
%54 = getelementptr inbounds double, double* %53, i32 1
store double* %54, double** %8
%55 = load i8*, i8** %9
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %9
br label %19
57: ; preds = %19
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %4
br label %58
58: ; preds = %57, %50, %36, %14
%59 = load i8*, i8** %4
ret i8* %59
}

LLVM IR (-O2):
@scale_num.nextup = internal unnamed_addr constant [5 x i8] c"kmgt\00"
@scale_num.buf = internal global [32 x i8] zeroinitializer
@.str = [4 x i8] c"%lu\00"
@.str.1 = [7 x i8] c"%.1f%c\00"
@.str.2 = [6 x i8] c"%ld%c\00"
@.str.3 = [2 x i8] c"?\00"
define i8* @scale_num(i64 %0, i32 %1, i32 %2) {
%4 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %0)
%5 = icmp sgt i32 %4, %1
br i1 %5, label %6, label %53
6: ; preds = %3
%7 = zext i32 %2 to i64
%8 = getelementptr inbounds [5 x i8], [5 x i8]* @scale_num.nextup, i64 0, i64 %7
%9 = uitofp i64 %0 to double
%10 = fmul double %9, 0x3F50000000000000
%11 = fptoui double %10 to i64
%12 = load i8, i8* %8
%13 = sext i8 %12 to i32
%14 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %11, i32 %13)
%15 = icmp sgt i32 %14, %1
br i1 %15, label %16, label %53
16: ; preds = %6
%17 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %11, i32 %13)
%18 = icmp sgt i32 %17, %1
br i1 %18, label %19, label %53
19: ; preds = %16
%20 = getelementptr inbounds i8, i8* %8, i64 1
%21 = fmul double %9, 0x3EB0000000000000
%22 = fptoui double %21 to i64
%23 = load i8, i8* %20
%24 = sext i8 %23 to i32
%25 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %22, i32 %24)
%26 = icmp sgt i32 %25, %1
br i1 %26, label %27, label %53
27: ; preds = %19
%28 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %22, i32 %24)
%29 = icmp sgt i32 %28, %1
br i1 %29, label %30, label %53
30: ; preds = %27
%31 = getelementptr inbounds i8, i8* %8, i64 2
%32 = fmul double %9, 0x3E10000000000000
%33 = fptoui double %32 to i64
%34 = load i8, i8* %31
%35 = sext i8 %34 to i32
%36 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %33, i32 %35)
%37 = icmp sgt i32 %36, %1
br i1 %37, label %38, label %53
38: ; preds = %30
%39 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %33, i32 %35)
%40 = icmp sgt i32 %39, %1
br i1 %40, label %41, label %53
41: ; preds = %38
%42 = getelementptr inbounds i8, i8* %8, i64 3
%43 = fmul double %9, 0x3D70000000000000
%44 = fptoui double %43 to i64
%45 = load i8, i8* %42
%46 = sext i8 %45 to i32
%47 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %44, i32 %46)
%48 = icmp sgt i32 %47, %1
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %44, i32 %46)
%51 = icmp sgt i32 %50, %1
%52 = select i1 %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0)
br label %53
53: ; preds = %49, %16, %6, %19, %27, %30, %38, %41, %3
%54 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), %3 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), %6 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), %16 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), %19 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), %27 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), %30 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), %38 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @scale_num.buf, i64 0, i64 0), %41 ], [ %52, %49 ]
ret i8* %54
}