#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 scalar_t__ off_t;

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, double, char *);
char *size_str(off_t bytes) {
  double number = (double)bytes;
  char *unit = "B";
  if (number >= 1099511627776) {
    unit = "TiB";
    number = (double)bytes / 1099511627776;
  } else if (number >= 1073741824) {
    unit = "GiB";
    number = (double)bytes / 1073741824;
  } else if (number >= 1048576) {
    unit = "MiB";
    number = (double)bytes / 1048576;
  } else if (number >= 1024) {
    unit = "KiB";
    number = (double)bytes / 1024;
  }
  char *buffer = malloc(15);
  if (buffer == NULL)
    return NULL;
  snprintf(buffer, 15, "%.2f %s", number, unit);
  return buffer;
}


x86 assembly (clang, -O0):
<size_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    0xee8(%rip),%rax
mov    %rax,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xeb1(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1180 <size_str+0x60>
lea    0xec0(%rip),%rax
mov    %rax,-0x20(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  0xe8e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1244 <size_str+0x124>
movsd  -0x18(%rbp),%xmm0
movsd  0xe7b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11be <size_str+0x9e>
lea    0xe86(%rip),%rax
mov    %rax,-0x20(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  0xe58(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    123f <size_str+0x11f>
movsd  -0x18(%rbp),%xmm0
movsd  0xe45(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11fc <size_str+0xdc>
lea    0xe4c(%rip),%rax
mov    %rax,-0x20(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  0xe22(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    123a <size_str+0x11a>
movsd  -0x18(%rbp),%xmm0
movsd  0xe0f(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1235 <size_str+0x115>
lea    0xe12(%rip),%rax
mov    %rax,-0x20(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  0xdec(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    123a <size_str+0x11a>
jmp    123f <size_str+0x11f>
jmp    1244 <size_str+0x124>
mov    $0xf,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126a <size_str+0x14a>
movq   $0x0,-0x8(%rbp)
jmp    1290 <size_str+0x170>
mov    -0x28(%rbp),%rdi
movsd  -0x18(%rbp),%xmm0
mov    -0x20(%rbp),%rcx
mov    $0xf,%esi
lea    0xdad(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<size_str>:
push   %r14
push   %rbx
push   %rax
cvtsi2sd %rdi,%xmm0
movabs $0x10000000000,%rax
cmp    %rax,%rdi
jl     114e <size_str+0x2e>
mulsd  0xed8(%rip),%xmm0
movsd  %xmm0,(%rsp)
lea    0xed4(%rip),%r14
jmp    11b7 <size_str+0x97>
cmp    $0x40000000,%rdi
jl     116d <size_str+0x4d>
mulsd  0xeb1(%rip),%xmm0
movsd  %xmm0,(%rsp)
lea    0xeb9(%rip),%r14
jmp    11b7 <size_str+0x97>
cmp    $0x100000,%rdi
jl     118c <size_str+0x6c>
mulsd  0xe8a(%rip),%xmm0
movsd  %xmm0,(%rsp)
lea    0xe9e(%rip),%r14
jmp    11b7 <size_str+0x97>
cmp    $0x400,%rdi
jl     11ab <size_str+0x8b>
mulsd  0xe63(%rip),%xmm0
movsd  %xmm0,(%rsp)
lea    0xe83(%rip),%r14
jmp    11b7 <size_str+0x97>
movsd  %xmm0,(%rsp)
lea    0xe6f(%rip),%r14
mov    $0xf,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e5 <size_str+0xc5>
lea    0xe60(%rip),%rdx
mov    %rbx,%rdi
mov    $0xf,%esi
movsd  (%rsp),%xmm0
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"B\00"
@.str.1 = [4 x i8] c"TiB\00"
@.str.2 = [4 x i8] c"GiB\00"
@.str.3 = [4 x i8] c"MiB\00"
@.str.4 = [4 x i8] c"KiB\00"
@.str.5 = [8 x i8] c"%.2f %s\00"
define i8* @size_str(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca double
%5 = alloca i8*
%6 = alloca i8*
store i64 %0, i64* %3
%7 = load i64, i64* %3
%8 = sitofp i64 %7 to double
store double %8, double* %4
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %5
%9 = load double, double* %4
%10 = fcmp oge double %9, 0x4270000000000000
br i1 %10, label %11, label %15
11: ; preds = %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %5
%12 = load i64, i64* %3
%13 = sitofp i64 %12 to double
%14 = fdiv double %13, 0x4270000000000000
store double %14, double* %4
br label %39
15: ; preds = %1
%16 = load double, double* %4
%17 = fcmp oge double %16, 0x41D0000000000000
br i1 %17, label %18, label %22
18: ; preds = %15
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %5
%19 = load i64, i64* %3
%20 = sitofp i64 %19 to double
%21 = fdiv double %20, 0x41D0000000000000
store double %21, double* %4
br label %38
22: ; preds = %15
%23 = load double, double* %4
%24 = fcmp oge double %23, 0x4130000000000000
br i1 %24, label %25, label %29
25: ; preds = %22
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %5
%26 = load i64, i64* %3
%27 = sitofp i64 %26 to double
%28 = fdiv double %27, 0x4130000000000000
store double %28, double* %4
br label %37
29: ; preds = %22
%30 = load double, double* %4
%31 = fcmp oge double %30, 1.024000e+03
br i1 %31, label %32, label %36
32: ; preds = %29
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8** %5
%33 = load i64, i64* %3
%34 = sitofp i64 %33 to double
%35 = fdiv double %34, 1.024000e+03
store double %35, double* %4
br label %36
36: ; preds = %32, %29
br label %37
37: ; preds = %36, %25
br label %38
38: ; preds = %37, %18
br label %39
39: ; preds = %38, %11
%40 = call i8* @malloc(i32 15)
store i8* %40, i8** %6
%41 = load i8*, i8** %6
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %44
43: ; preds = %39
store i8* null, i8** %2
br label %50
44: ; preds = %39
%45 = load i8*, i8** %6
%46 = load double, double* %4
%47 = load i8*, i8** %5
%48 = call i32 @snprintf(i8* %45, i32 15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), double %46, i8* %47)
%49 = load i8*, i8** %6
store i8* %49, i8** %2
br label %50
50: ; preds = %44, %43
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"B\00"
@.str.1 = [4 x i8] c"TiB\00"
@.str.2 = [4 x i8] c"GiB\00"
@.str.3 = [4 x i8] c"MiB\00"
@.str.4 = [4 x i8] c"KiB\00"
@.str.5 = [8 x i8] c"%.2f %s\00"
define i8* @size_str(i64 %0) {
%2 = sitofp i64 %0 to double
%3 = icmp sgt i64 %0, 1099511627775
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = fmul double %2, 0x3D70000000000000
br label %18
6: ; preds = %1
%7 = icmp sgt i64 %0, 1073741823
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = fmul double %2, 0x3E10000000000000
br label %18
10: ; preds = %6
%11 = icmp sgt i64 %0, 1048575
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = fmul double %2, 0x3EB0000000000000
br label %18
14: ; preds = %10
%15 = icmp sgt i64 %0, 1023
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = fmul double %2, 0x3F50000000000000
br label %18
18: ; preds = %8, %14, %16, %12, %4
%19 = phi double [ %5, %4 ], [ %9, %8 ], [ %13, %12 ], [ %17, %16 ], [ %2, %14 ]
%20 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %12 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), %16 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %14 ]
%21 = tail call dereferenceable_or_null(15) i8* @malloc(i32 15)
%22 = icmp eq i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = tail call i32 @snprintf(i8* %21, i32 15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), double %19, i8* %20)
br label %25
25: ; preds = %18, %23
ret i8* %21
}