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

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, size_t);
const char *format_size(size_t size) {
  static char str[64];
  if (size < 1024)
    sprintf(str, "%zu B", size);
  else if (size < 1024 * 1024)
    sprintf(str, "%.2f KB", size / 1024.0);
  else
    sprintf(str, "%.2f MB", size / 1024.0 / 1024.0);
  return str;
}


x86 assembly (clang, -O0):
<format_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x400,-0x8(%rbp)
jae    1146 <format_size+0x36>
mov    -0x8(%rbp),%rdx
lea    0x2f0b(%rip),%rdi
lea    0xef4(%rip),%rsi
call   1030 <sprintf@plt>
jmp    123a <format_size+0x12a>
cmpq   $0x100000,-0x8(%rbp)
jae    11c5 <format_size+0xb5>
movaps 0xea5(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xea4(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe9d(%rip),%xmm1
divsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rdx
mov    %rdx,%rcx
sar    $0x3f,%rcx
movsd  0xe8a(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
and    %rcx,%rax
or     %rax,%rdx
lea    0x2e8c(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1030 <sprintf@plt>
jmp    1235 <format_size+0x125>
movaps 0xe34(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe33(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe2c(%rip),%xmm1
divsd  %xmm1,%xmm0
divsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rdx
mov    %rdx,%rcx
sar    $0x3f,%rcx
movsd  0xe15(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
and    %rcx,%rax
or     %rax,%rdx
lea    0x2e17(%rip),%rdi
lea    0xe0e(%rip),%rsi
call   1030 <sprintf@plt>
jmp    123a <format_size+0x12a>
lea    0x2dff(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_size>:
push   %rax
mov    %rdi,%rdx
cmp    $0x3ff,%rdi
ja     1130 <format_size+0x20>
lea    0x2f1c(%rip),%rdi
lea    0xf05(%rip),%rsi
jmp    11c6 <format_size+0xb6>
movq   %rdx,%xmm1
punpckldq 0xec3(%rip),%xmm1
subpd  0xecb(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
mulsd  0xec7(%rip),%xmm0
cmp    $0xfffff,%rdx
ja     1191 <format_size+0x81>
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
sar    $0x3f,%rcx
subsd  0xeb2(%rip),%xmm0
cvttsd2si %xmm0,%rdx
and    %rcx,%rdx
or     %rax,%rdx
lea    0x2eb8(%rip),%rdi
lea    0xea7(%rip),%rsi
jmp    11c6 <format_size+0xb6>
mulsd  0xe87(%rip),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
sar    $0x3f,%rcx
subsd  0xe7b(%rip),%xmm0
cvttsd2si %xmm0,%rdx
and    %rcx,%rdx
or     %rax,%rdx
lea    0x2e81(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1030 <sprintf@plt>
lea    0x2e6e(%rip),%rax
pop    %rcx
ret

LLVM IR (-O0):
@format_size.str = internal global [64 x i8] zeroinitializer
@.str = [6 x i8] c"%zu B\00"
@.str.1 = [8 x i8] c"%.2f KB\00"
@.str.2 = [8 x i8] c"%.2f MB\00"
define i8* @format_size(i64 %0) {
%2 = alloca i64
store i64 %0, i64* %2
%3 = load i64, i64* %2
%4 = icmp ult i64 %3, 1024
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i64, i64* %2
%7 = call i32 @sprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_size.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %6)
br label %25
8: ; preds = %1
%9 = load i64, i64* %2
%10 = icmp ult i64 %9, 1048576
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i64, i64* %2
%13 = uitofp i64 %12 to double
%14 = fdiv double %13, 1.024000e+03
%15 = fptoui double %14 to i64
%16 = call i32 @sprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_size.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64 %15)
br label %24
17: ; preds = %8
%18 = load i64, i64* %2
%19 = uitofp i64 %18 to double
%20 = fdiv double %19, 1.024000e+03
%21 = fdiv double %20, 1.024000e+03
%22 = fptoui double %21 to i64
%23 = call i32 @sprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_size.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 %22)
br label %24
24: ; preds = %17, %11
br label %25
25: ; preds = %24, %5
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_size.str, i64 0, i64 0)
}

LLVM IR (-O2):
@format_size.str = internal global [64 x i8] zeroinitializer
@.str = [6 x i8] c"%zu B\00"
@.str.1 = [8 x i8] c"%.2f KB\00"
@.str.2 = [8 x i8] c"%.2f MB\00"
define i8* @format_size(i64 %0) {
%2 = icmp ult i64 %0, 1024
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @format_size.str, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %0)
br label %16
5: ; preds = %1
%6 = icmp ult i64 %0, 1048576
%7 = uitofp i64 %0 to double
%8 = fmul double %7, 0x3F50000000000000
br i1 %6, label %9, label %12
9: ; preds = %5
%10 = fptoui double %8 to i64
%11 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @format_size.str, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64 %10)
br label %16
12: ; preds = %5
%13 = fmul double %8, 0x3F50000000000000
%14 = fptoui double %13 to i64
%15 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @format_size.str, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 %14)
br label %16
16: ; preds = %9, %12, %3
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_size.str, i64 0, i64 0)
}