#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 double uint64_t;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, double);
void format_number(uint64_t num, char *s, int scale, int fixed_width) {
  double f = num;
  if (!scale) {
    snprintf(s, 31, "%llu", (long long unsigned)num);
  } else {
    if (f >= (double)(1000.0 * 1000.0 * 1000.0 * 1000.0)) {
      if (fixed_width)
        snprintf(s, 32 - 1, "%5.1f T",
                 f / (double)(1000.0 * 1000.0 * 1000.0 * 1000.0));
      else
        snprintf(s, 32 - 1, "%.1f T",
                 f / (double)(1000.0 * 1000.0 * 1000.0 * 1000.0));
    } else if (f >= (double)(1000.0 * 1000.0 * 1000.0)) {
      if (fixed_width)
        snprintf(s, 32 - 1, "%5.1f G", f / (double)(1000.0 * 1000.0 * 1000.0));
      else
        snprintf(s, 32 - 1, "%.1f G", f / (double)(1000.0 * 1000.0 * 1000.0));
    } else if (f >= (double)(1000.0 * 1000.0)) {
      if (fixed_width)
        snprintf(s, 32 - 1, "%5.1f M", f / (double)(1000.0 * 1000.0));
      else
        snprintf(s, 32 - 1, "%.1f M", f / (double)(1000.0 * 1000.0));
    } else {
      if (fixed_width)
        snprintf(s, 32 - 1, "%4.0f", f);
      else
        snprintf(s, 32 - 1, "%.0f", f);
    }
    s[32 - 1] = '\0';
  }
}


x86 assembly (clang, -O0):
<format_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11a7 <format_number+0x97>
mov    -0x10(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
movsd  0xeb4(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rcx
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rdx,%rcx
or     %rcx,%rax
movq   %rax,%xmm0
movaps 0xeaa(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xeae(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
mov    $0x1f,%esi
lea    0xea3(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1366 <format_number+0x256>
movsd  -0x20(%rbp),%xmm0
movsd  0xe54(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     121e <format_number+0x10e>
cmpl   $0x0,-0x18(%rbp)
je     11f3 <format_number+0xe3>
mov    -0x10(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
movsd  0xe2f(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0x1f,%esi
lea    0xe5c(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1219 <format_number+0x109>
mov    -0x10(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
movsd  0xe04(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0x1f,%esi
lea    0xe39(%rip),%rdx
call   1030 <snprintf@plt>
jmp    135e <format_number+0x24e>
movsd  -0x20(%rbp),%xmm0
movsd  0xde5(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1295 <format_number+0x185>
cmpl   $0x0,-0x18(%rbp)
je     126a <format_number+0x15a>
mov    -0x10(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
movsd  0xdc0(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0x1f,%esi
lea    0xdf4(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1290 <format_number+0x180>
mov    -0x10(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
movsd  0xd95(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0x1f,%esi
lea    0xdd1(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1359 <format_number+0x249>
movsd  -0x20(%rbp),%xmm0
movsd  0xd76(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     130c <format_number+0x1fc>
cmpl   $0x0,-0x18(%rbp)
je     12e1 <format_number+0x1d1>
mov    -0x10(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
movsd  0xd51(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0x1f,%esi
lea    0xd8c(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1307 <format_number+0x1f7>
mov    -0x10(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
movsd  0xd26(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0x1f,%esi
lea    0xd69(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1354 <format_number+0x244>
cmpl   $0x0,-0x18(%rbp)
je     1335 <format_number+0x225>
mov    -0x10(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
mov    $0x1f,%esi
lea    0xd47(%rip),%rdx
call   1030 <snprintf@plt>
jmp    134f <format_number+0x23f>
mov    -0x10(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
mov    $0x1f,%esi
lea    0xd2e(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1354 <format_number+0x244>
jmp    1359 <format_number+0x249>
jmp    135e <format_number+0x24e>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1f(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_number>:
push   %rbx
mov    %rdi,%rbx
test   %esi,%esi
je     1156 <format_number+0x46>
ucomisd 0xee0(%rip),%xmm0
jae    11ab <format_number+0x9b>
ucomisd 0xeda(%rip),%xmm0
jae    11c0 <format_number+0xb0>
ucomisd 0xed4(%rip),%xmm0
jae    11de <format_number+0xce>
test   %edx,%edx
je     11fc <format_number+0xec>
lea    0xf21(%rip),%rdx
jmp    120c <format_number+0xfc>
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xeb2(%rip),%xmm0
cvttsd2si %xmm0,%rdx
sar    $0x3f,%rcx
and    %rcx,%rdx
or     %rax,%rdx
movq   %rdx,%xmm1
punpckldq 0xe9e(%rip),%xmm1
subpd  0xea6(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
lea    0xea3(%rip),%rdx
mov    %rbx,%rdi
mov    $0x1f,%esi
pop    %rbx
jmp    1030 <snprintf@plt>
divsd  0xe4d(%rip),%xmm0
test   %edx,%edx
je     11d5 <format_number+0xc5>
lea    0xe87(%rip),%rdx
jmp    120c <format_number+0xfc>
divsd  0xe40(%rip),%xmm0
test   %edx,%edx
je     11f3 <format_number+0xe3>
lea    0xe81(%rip),%rdx
jmp    120c <format_number+0xfc>
lea    0xe71(%rip),%rdx
jmp    120c <format_number+0xfc>
divsd  0xe2a(%rip),%xmm0
test   %edx,%edx
je     1205 <format_number+0xf5>
lea    0xe72(%rip),%rdx
jmp    120c <format_number+0xfc>
lea    0xe62(%rip),%rdx
jmp    120c <format_number+0xfc>
lea    0xe75(%rip),%rdx
jmp    120c <format_number+0xfc>
lea    0xe5f(%rip),%rdx
mov    %rbx,%rdi
mov    $0x1f,%esi
call   1030 <snprintf@plt>
movb   $0x0,0x1f(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%llu\00"
@.str.1 = [8 x i8] c"%5.1f T\00"
@.str.2 = [7 x i8] c"%.1f T\00"
@.str.3 = [8 x i8] c"%5.1f G\00"
@.str.4 = [7 x i8] c"%.1f G\00"
@.str.5 = [8 x i8] c"%5.1f M\00"
@.str.6 = [7 x i8] c"%.1f M\00"
@.str.7 = [6 x i8] c"%4.0f\00"
@.str.8 = [5 x i8] c"%.0f\00"
define void @format_number(double %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca double
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
store double %0, double* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load double, double* %5
store double %10, double* %9
%11 = load i32, i32* %7
%12 = icmp ne i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %4
%14 = load i8*, i8** %6
%15 = load double, double* %5
%16 = fptoui double %15 to i64
%17 = uitofp i64 %16 to double
%18 = call i32 @snprintf(i8* %14, i32 31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), double %17)
br label %87
19: ; preds = %4
%20 = load double, double* %9
%21 = fcmp oge double %20, 1.000000e+12
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = load double, double* %9
%28 = fdiv double %27, 1.000000e+12
%29 = call i32 @snprintf(i8* %26, i32 31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), double %28)
br label %35
30: ; preds = %22
%31 = load i8*, i8** %6
%32 = load double, double* %9
%33 = fdiv double %32, 1.000000e+12
%34 = call i32 @snprintf(i8* %31, i32 31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), double %33)
br label %35
35: ; preds = %30, %25
br label %84
36: ; preds = %19
%37 = load double, double* %9
%38 = fcmp oge double %37, 1.000000e+09
br i1 %38, label %39, label %53
39: ; preds = %36
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i8*, i8** %6
%44 = load double, double* %9
%45 = fdiv double %44, 1.000000e+09
%46 = call i32 @snprintf(i8* %43, i32 31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double %45)
br label %52
47: ; preds = %39
%48 = load i8*, i8** %6
%49 = load double, double* %9
%50 = fdiv double %49, 1.000000e+09
%51 = call i32 @snprintf(i8* %48, i32 31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), double %50)
br label %52
52: ; preds = %47, %42
br label %83
53: ; preds = %36
%54 = load double, double* %9
%55 = fcmp oge double %54, 1.000000e+06
br i1 %55, label %56, label %70
56: ; preds = %53
%57 = load i32, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i8*, i8** %6
%61 = load double, double* %9
%62 = fdiv double %61, 1.000000e+06
%63 = call i32 @snprintf(i8* %60, i32 31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), double %62)
br label %69
64: ; preds = %56
%65 = load i8*, i8** %6
%66 = load double, double* %9
%67 = fdiv double %66, 1.000000e+06
%68 = call i32 @snprintf(i8* %65, i32 31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), double %67)
br label %69
69: ; preds = %64, %59
br label %82
70: ; preds = %53
%71 = load i32, i32* %8
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %70
%74 = load i8*, i8** %6
%75 = load double, double* %9
%76 = call i32 @snprintf(i8* %74, i32 31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), double %75)
br label %81
77: ; preds = %70
%78 = load i8*, i8** %6
%79 = load double, double* %9
%80 = call i32 @snprintf(i8* %78, i32 31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), double %79)
br label %81
81: ; preds = %77, %73
br label %82
82: ; preds = %81, %69
br label %83
83: ; preds = %82, %52
br label %84
84: ; preds = %83, %35
%85 = load i8*, i8** %6
%86 = getelementptr inbounds i8, i8* %85, i64 31
store i8 0, i8* %86
br label %87
87: ; preds = %84, %13
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%llu\00"
@.str.1 = [8 x i8] c"%5.1f T\00"
@.str.2 = [7 x i8] c"%.1f T\00"
@.str.3 = [8 x i8] c"%5.1f G\00"
@.str.4 = [7 x i8] c"%.1f G\00"
@.str.5 = [8 x i8] c"%5.1f M\00"
@.str.6 = [7 x i8] c"%.1f M\00"
@.str.7 = [6 x i8] c"%4.0f\00"
@.str.8 = [5 x i8] c"%.0f\00"
define void @format_number(double %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %2, 0
br i1 %5, label %6, label %10
6: ; preds = %4
%7 = fptoui double %0 to i64
%8 = uitofp i64 %7 to double
%9 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), double %8)
br label %44
10: ; preds = %4
%11 = fcmp ult double %0, 1.000000e+12
br i1 %11, label %19, label %12
12: ; preds = %10
%13 = icmp eq i32 %3, 0
%14 = fdiv double %0, 1.000000e+12
br i1 %13, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), double %14)
br label %42
17: ; preds = %12
%18 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), double %14)
br label %42
19: ; preds = %10
%20 = fcmp ult double %0, 1.000000e+09
br i1 %20, label %28, label %21
21: ; preds = %19
%22 = icmp eq i32 %3, 0
%23 = fdiv double %0, 1.000000e+09
br i1 %22, label %26, label %24
24: ; preds = %21
%25 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double %23)
br label %42
26: ; preds = %21
%27 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), double %23)
br label %42
28: ; preds = %19
%29 = fcmp ult double %0, 1.000000e+06
%30 = icmp eq i32 %3, 0
br i1 %29, label %37, label %31
31: ; preds = %28
%32 = fdiv double %0, 1.000000e+06
br i1 %30, label %35, label %33
33: ; preds = %31
%34 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), double %32)
br label %42
35: ; preds = %31
%36 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), double %32)
br label %42
37: ; preds = %28
br i1 %30, label %40, label %38
38: ; preds = %37
%39 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), double %0)
br label %42
40: ; preds = %37
%41 = tail call i32 @snprintf(i8* %1, i32 31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), double %0)
br label %42
42: ; preds = %26, %24, %38, %40, %33, %35, %15, %17
%43 = getelementptr inbounds i8, i8* %1, i64 31
store i8 0, i8* %43
br label %44
44: ; preds = %42, %6
ret void
}