#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*/ snprintf(char *, int, char *, float, ...);
int format_size(char *buf, float size) {
  int ret = 0;
  float f = 0;
  if (NULL == buf || size <= 0) {
    ret = -1;
    goto FINISH;
  }
  if ((f = size / 1048576) >= 1.0) {
    snprintf(buf, 1024, "%.2f%s", f, "MB");
  } else if ((f = size / 1024) >= 1.0) {
    snprintf(buf, 1024, "%.2f%s", f, "KB");
  } else {
    snprintf(buf, 1024, "%.2fB", size);
  }
FINISH:
  return ret;
}


x86 assembly (clang, -O0):
<format_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movss  %xmm0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     1149 <format_size+0x39>
xorps  %xmm0,%xmm0
ucomiss -0xc(%rbp),%xmm0
jb     1155 <format_size+0x45>
movl   $0xffffffff,-0x10(%rbp)
jmp    1223 <format_size+0x113>
movss  -0xc(%rbp),%xmm0
movss  0xea6(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x14(%rbp)
cvtss2sd %xmm0,%xmm0
movsd  0xe89(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11a9 <format_size+0x99>
mov    -0x8(%rbp),%rdi
movss  -0x14(%rbp),%xmm0
mov    $0x400,%esi
lea    0xe7a(%rip),%rdx
lea    0xe7a(%rip),%rcx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    121e <format_size+0x10e>
movss  -0xc(%rbp),%xmm0
movss  0xe56(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x14(%rbp)
cvtss2sd %xmm0,%xmm0
movsd  0xe35(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11fd <format_size+0xed>
mov    -0x8(%rbp),%rdi
movss  -0x14(%rbp),%xmm0
mov    $0x400,%esi
lea    0xe26(%rip),%rdx
lea    0xe29(%rip),%rcx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    1219 <format_size+0x109>
mov    -0x8(%rbp),%rdi
movss  -0xc(%rbp),%xmm0
mov    $0x400,%esi
lea    0xe0b(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    121e <format_size+0x10e>
jmp    1223 <format_size+0x113>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_size>:
push   %rax
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1191 <format_size+0x81>
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm1
jae    1191 <format_size+0x81>
movss  0xed5(%rip),%xmm1
mulss  %xmm0,%xmm1
ucomiss 0xece(%rip),%xmm1
jae    1162 <format_size+0x52>
movss  0xec8(%rip),%xmm1
mulss  %xmm0,%xmm1
ucomiss 0xeb9(%rip),%xmm1
jae    1172 <format_size+0x62>
lea    0xec5(%rip),%rdx
mov    $0x400,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    118f <format_size+0x7f>
lea    0xea3(%rip),%rdx
lea    0xea3(%rip),%rcx
jmp    1180 <format_size+0x70>
lea    0xe93(%rip),%rdx
lea    0xe96(%rip),%rcx
mov    $0x400,%esi
movaps %xmm1,%xmm0
mov    $0x1,%al
call   1030 <snprintf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%.2f%s\00"
@.str.1 = [3 x i8] c"MB\00"
@.str.2 = [3 x i8] c"KB\00"
@.str.3 = [6 x i8] c"%.2fB\00"
define i32 @format_size(i8* %0, float %1) {
%3 = alloca i8*
%4 = alloca float
%5 = alloca i32
%6 = alloca float
store i8* %0, i8** %3
store float %1, float* %4
store i32 0, i32* %5
store float 0.000000e+00, float* %6
%7 = load i8*, i8** %3
%8 = icmp eq i8* null, %7
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load float, float* %4
%11 = fcmp ole float %10, 0.000000e+00
br i1 %11, label %12, label %13
12: ; preds = %9, %2
store i32 -1, i32* %5
br label %37
13: ; preds = %9
%14 = load float, float* %4
%15 = fdiv float %14, 0x4130000000000000
store float %15, float* %6
%16 = fpext float %15 to double
%17 = fcmp oge double %16, 1.000000e+00
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = load float, float* %6
%21 = call i32 (i8*, i32, i8*, float, ...) @snprintf(i8* %19, i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), float %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %36
22: ; preds = %13
%23 = load float, float* %4
%24 = fdiv float %23, 1.024000e+03
store float %24, float* %6
%25 = fpext float %24 to double
%26 = fcmp oge double %25, 1.000000e+00
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i8*, i8** %3
%29 = load float, float* %6
%30 = call i32 (i8*, i32, i8*, float, ...) @snprintf(i8* %28, i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), float %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %35
31: ; preds = %22
%32 = load i8*, i8** %3
%33 = load float, float* %4
%34 = call i32 (i8*, i32, i8*, float, ...) @snprintf(i8* %32, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), float %33)
br label %35
35: ; preds = %31, %27
br label %36
36: ; preds = %35, %18
br label %37
37: ; preds = %36, %12
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
@.str = [7 x i8] c"%.2f%s\00"
@.str.1 = [3 x i8] c"MB\00"
@.str.2 = [3 x i8] c"KB\00"
@.str.3 = [6 x i8] c"%.2fB\00"
define i32 @format_size(i8* %0, float %1) {
%3 = icmp eq i8* %0, null
%4 = fcmp ole float %1, 0.000000e+00
%5 = or i1 %3, %4
br i1 %5, label %18, label %6
6: ; preds = %2
%7 = fmul float %1, 0x3EB0000000000000
%8 = fcmp ult float %7, 1.000000e+00
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 (i8*, i32, i8*, float, ...) @snprintf(i8* %0, i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), float %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %18
11: ; preds = %6
%12 = fmul float %1, 0x3F50000000000000
%13 = fcmp ult float %12, 1.000000e+00
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 (i8*, i32, i8*, float, ...) @snprintf(i8* %0, i32 1024, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), float %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %18
16: ; preds = %11
%17 = tail call i32 (i8*, i32, i8*, float, ...) @snprintf(i8* %0, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), float %1)
br label %18
18: ; preds = %2, %9, %16, %14
%19 = phi i32 [ 0, %9 ], [ 0, %14 ], [ 0, %16 ], [ -1, %2 ]
ret i32 %19
}