#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*/ lnprintf(char *, int, char *, double, ...);
char *easy_string_format_size(double byte, char *buffer, int size) {
  static const char units[] = " KMGTPEZY";
  int idx = 0;
  while (byte >= 1024) {
    byte /= 1024;
    idx++;
  }
  buffer[0] = '\0';
  if (idx == 0)
    lnprintf(buffer, size, "%.2f", byte);
  else if (idx < 9)
    lnprintf(buffer, size, "%.2f %cB", byte, units[idx]);
  return buffer;
}


x86 assembly (clang, -O0):
<easy_string_format_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xec8(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1166 <easy_string_format_size+0x56>
movsd  -0x8(%rbp),%xmm0
movsd  0xeb1(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112b <easy_string_format_size+0x1b>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
cmpl   $0x0,-0x18(%rbp)
jne    1196 <easy_string_format_size+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
movsd  -0x8(%rbp),%xmm0
lea    0xe88(%rip),%rdx
mov    $0x1,%al
call   1030 <lnprintf@plt>
jmp    11ce <easy_string_format_size+0xbe>
cmpl   $0x9,-0x18(%rbp)
jge    11c9 <easy_string_format_size+0xb9>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
movsd  -0x8(%rbp),%xmm0
movslq -0x18(%rbp),%rcx
lea    0xe51(%rip),%rax
movsbl (%rax,%rcx,1),%ecx
lea    0xe55(%rip),%rdx
mov    $0x1,%al
call   1030 <lnprintf@plt>
jmp    11ce <easy_string_format_size+0xbe>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<easy_string_format_size>:
push   %rbx
mov    %rdi,%rbx
ucomisd 0xee4(%rip),%xmm0
jae    1134 <easy_string_format_size+0x24>
movb   $0x0,(%rbx)
lea    0xef2(%rip),%rdx
mov    %rbx,%rdi
mov    $0x1,%al
call   1030 <lnprintf@plt>
jmp    1185 <easy_string_format_size+0x75>
xor    %eax,%eax
movsd  0xeca(%rip),%xmm1
movsd  0xeba(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
mulsd  %xmm1,%xmm0
add    $0x1,%rax
ucomisd %xmm2,%xmm0
jae    1150 <easy_string_format_size+0x40>
movb   $0x0,(%rbx)
lea    -0x1(%rax),%ecx
cmp    $0x7,%ecx
ja     1185 <easy_string_format_size+0x75>
lea    0xea0(%rip),%rcx
movsbl (%rax,%rcx,1),%ecx
lea    0xea4(%rip),%rdx
mov    %rbx,%rdi
mov    $0x1,%al
call   1030 <lnprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@easy_string_format_size.units = internal constant [10 x i8] c" KMGTPEZY\00"
@.str = [5 x i8] c"%.2f\00"
@.str.1 = [9 x i8] c"%.2f %cB\00"
define i8* @easy_string_format_size(double %0, i8* %1, i32 %2) {
%4 = alloca double
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store double %0, double* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %11, %3
%9 = load double, double* %4
%10 = fcmp oge double %9, 1.024000e+03
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load double, double* %4
%13 = fdiv double %12, 1.024000e+03
store double %13, double* %4
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
store i32 %15, i32* %7
br label %8
16: ; preds = %8
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 0, i8* %18
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = load double, double* %4
%25 = call i32 (i8*, i32, i8*, double, ...) @lnprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), double %24)
br label %40
26: ; preds = %16
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 9
br i1 %28, label %29, label %39
29: ; preds = %26
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = load double, double* %4
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [10 x i8], [10 x i8]* @easy_string_format_size.units, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = call i32 (i8*, i32, i8*, double, ...) @lnprintf(i8* %30, i32 %31, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %32, i32 %37)
br label %39
39: ; preds = %29, %26
br label %40
40: ; preds = %39, %21
%41 = load i8*, i8** %5
ret i8* %41
}

LLVM IR (-O2):
@easy_string_format_size.units = internal unnamed_addr constant [10 x i8] c" KMGTPEZY\00"
@.str = [5 x i8] c"%.2f\00"
@.str.1 = [9 x i8] c"%.2f %cB\00"
define i8* @easy_string_format_size(double %0, i8* returned %1, i32 %2) {
%4 = fcmp ult double %0, 1.024000e+03
br i1 %4, label %11, label %5
5: ; preds = %3, %5
%6 = phi i32 [ %9, %5 ], [ 0, %3 ]
%7 = phi double [ %8, %5 ], [ %0, %3 ]
%8 = fmul double %7, 0x3F50000000000000
%9 = add nuw nsw i32 %6, 1
%10 = fcmp ult double %8, 1.024000e+03
br i1 %10, label %13, label %5
11: ; preds = %3
store i8 0, i8* %1
%12 = tail call i32 (i8*, i32, i8*, double, ...) @lnprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), double %0)
br label %21
13: ; preds = %5
store i8 0, i8* %1
%14 = icmp ult i32 %6, 8
br i1 %14, label %15, label %21
15: ; preds = %13
%16 = zext i32 %9 to i64
%17 = getelementptr inbounds [10 x i8], [10 x i8]* @easy_string_format_size.units, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = tail call i32 (i8*, i32, i8*, double, ...) @lnprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %8, i32 %19)
br label %21
21: ; preds = %13, %15, %11
ret i8* %1
}