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

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, double, ...);
const char *sizefmt(size_t si) {
  static char buf[32];
  double s = si;
  char *e = " kMGT", fmt[32];
  int k = 0;
  while (s >= 1000 && e[0]) {
    s /= 1024;
    e++;
  }
  if (s < 100)
    k++;
  if (s < 10)
    k++;
  snprintf(fmt, sizeof(fmt), "%%.%if%%c", k);
  snprintf(buf, sizeof(buf), fmt, s, e[0]);
  return buf;
}


x86 assembly (clang, -O0):
<sizefmt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movaps 0xedd(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xedc(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    0xef1(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x44(%rbp)
movsd  -0x10(%rbp),%xmm0
xor    %eax,%eax
movsd  0xeb7(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x45(%rbp)
jb     1186 <sizefmt+0x76>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    1196 <sizefmt+0x86>
jmp    11bd <sizefmt+0xad>
movsd  -0x10(%rbp),%xmm0
movsd  0xe95(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    115a <sizefmt+0x4a>
movsd  0xe63(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11d9 <sizefmt+0xc9>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
movsd  0xe4f(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11f5 <sizefmt+0xe5>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
lea    -0x40(%rbp),%rdi
cvtsi2sdl -0x44(%rbp),%xmm0
mov    $0x20,%esi
lea    0xe3c(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdx
movsd  -0x10(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
lea    0x2e18(%rip),%rdi
mov    $0x20,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
lea    0x2e05(%rip),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sizefmt>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movq   %rdi,%xmm0
punpckldq 0xedc(%rip),%xmm0
subpd  0xee4(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
cmp    $0x3e8,%rdi
jae    114a <sizefmt+0x3a>
lea    0xef8(%rip),%rbx
jmp    11c5 <sizefmt+0xb5>
mulsd  0xece(%rip),%xmm1
ucomisd 0xece(%rip),%xmm1
jae    1165 <sizefmt+0x55>
lea    0xede(%rip),%rbx
jmp    11c5 <sizefmt+0xb5>
mulsd  0xeb3(%rip),%xmm1
ucomisd 0xeb3(%rip),%xmm1
jae    1180 <sizefmt+0x70>
lea    0xec4(%rip),%rbx
jmp    11c5 <sizefmt+0xb5>
mulsd  0xe98(%rip),%xmm1
ucomisd 0xe98(%rip),%xmm1
jae    119b <sizefmt+0x8b>
lea    0xeaa(%rip),%rbx
jmp    11c5 <sizefmt+0xb5>
mulsd  0xe7d(%rip),%xmm1
ucomisd 0xe7d(%rip),%xmm1
jae    11b6 <sizefmt+0xa6>
lea    0xe90(%rip),%rbx
jmp    11c5 <sizefmt+0xb5>
mulsd  0xe62(%rip),%xmm1
lea    0xe80(%rip),%rbx
movapd %xmm1,(%rsp)
movsd  0xe5e(%rip),%xmm0
xor    %eax,%eax
ucomisd %xmm1,%xmm0
seta   %al
movsd  0xe55(%rip),%xmm0
ucomisd %xmm1,%xmm0
lea    0x1(%rax),%ecx
cmovbe %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
lea    0xe4b(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x20,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
movsbl (%rbx),%ecx
lea    0x2e27(%rip),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %r14,%rdx
movaps (%rsp),%xmm0
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@sizefmt.buf = internal global [32 x i8] zeroinitializer
@.str = [6 x i8] c" kMGT\00"
@.str.1 = [10 x i8] c"%%.%if%%c\00"
define i8* @sizefmt(i64 %0) {
%2 = alloca i64
%3 = alloca double
%4 = alloca i8*
%5 = alloca [32 x i8]
%6 = alloca i32
store i64 %0, i64* %2
%7 = load i64, i64* %2
%8 = uitofp i64 %7 to double
store double %8, double* %3
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 0, i32* %6
br label %9
9: ; preds = %20, %1
%10 = load double, double* %3
%11 = fcmp oge double %10, 1.000000e+03
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br label %18
18: ; preds = %12, %9
%19 = phi i1 [ false, %9 ], [ %17, %12 ]
br i1 %19, label %20, label %25
20: ; preds = %18
%21 = load double, double* %3
%22 = fdiv double %21, 1.024000e+03
store double %22, double* %3
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %4
br label %9
25: ; preds = %18
%26 = load double, double* %3
%27 = fcmp olt double %26, 1.000000e+02
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %31
31: ; preds = %28, %25
%32 = load double, double* %3
%33 = fcmp olt double %32, 1.000000e+01
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %37
37: ; preds = %34, %31
%38 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%39 = load i32, i32* %6
%40 = sitofp i32 %39 to double
%41 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %38, i32 32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), double %40)
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%43 = load double, double* %3
%44 = load i8*, i8** %4
%45 = getelementptr inbounds i8, i8* %44, i64 0
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @sizefmt.buf, i64 0, i64 0), i32 32, i8* %42, double %43, i32 %47)
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @sizefmt.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@sizefmt.buf = internal global [32 x i8] zeroinitializer
@.str = [6 x i8] c" kMGT\00"
@.str.1 = [10 x i8] c"%%.%if%%c\00"
define i8* @sizefmt(i64 %0) {
%2 = alloca [32 x i8]
%3 = uitofp i64 %0 to double
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%5 = icmp ult i64 %0, 1000
br i1 %5, label %20, label %6
6: ; preds = %1
%7 = fmul double %3, 0x3F50000000000000
%8 = fcmp ult double %7, 1.000000e+03
br i1 %8, label %20, label %9
9: ; preds = %6
%10 = fmul double %7, 0x3F50000000000000
%11 = fcmp ult double %10, 1.000000e+03
br i1 %11, label %20, label %12
12: ; preds = %9
%13 = fmul double %10, 0x3F50000000000000
%14 = fcmp ult double %13, 1.000000e+03
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = fmul double %13, 0x3F50000000000000
%17 = fcmp ult double %16, 1.000000e+03
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = fmul double %16, 0x3F50000000000000
br label %20
20: ; preds = %18, %6, %9, %12, %15, %1
%21 = phi double [ %3, %1 ], [ %7, %6 ], [ %10, %9 ], [ %13, %12 ], [ %16, %15 ], [ %19, %18 ]
%22 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 1), %6 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 2), %9 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 3), %12 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 4), %15 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 5), %18 ]
%23 = fcmp olt double %21, 1.000000e+02
%24 = zext i1 %23 to i32
%25 = fcmp olt double %21, 1.000000e+01
%26 = select i1 %23, i32 2, i32 1
%27 = select i1 %25, i32 %26, i32 %24
%28 = sitofp i32 %27 to double
%29 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %4, i32 32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), double %28)
%30 = load i8, i8* %22
%31 = sext i8 %30 to i32
%32 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @sizefmt.buf, i64 0, i64 0), i32 32, i8* %4, double %21, i32 %31)
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @sizefmt.buf, i64 0, i64 0)
}