#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 uint64_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, double, char const *);
char *hsize(uint64_t s) {
  static char buffer[32];
  static const char *units[] = {"KiB", "MiB", "GiB", "TiB", "PiB", "EiB", NULL};
  int i;
  uint64_t k = s / 1024;
  double d = (double)k;
  for (i = 0; (d > 1024.0) && units[i]; i++)
    d /= 1024.0;
  snprintf(buffer, sizeof(buffer), "%.02f %s", d, units[i]);
  return buffer;
}


x86 assembly (clang, -O0):
<hsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
movsd  -0x18(%rbp),%xmm0
xor    %eax,%eax
movsd  0xeb7(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x19(%rbp)
jbe    116c <hsize+0x5c>
movslq -0x8(%rbp),%rcx
lea    0x2ecf(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    117c <hsize+0x6c>
jmp    11a0 <hsize+0x90>
movsd  -0x18(%rbp),%xmm0
movsd  0xe77(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    113a <hsize+0x2a>
movsd  -0x18(%rbp),%xmm0
movslq -0x8(%rbp),%rcx
lea    0x2e80(%rip),%rax
mov    (%rax,%rcx,8),%rcx
lea    0x2ec5(%rip),%rdi
mov    $0x20,%esi
lea    0xe59(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2ead(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hsize>:
push   %rbx
lea    0x3ff(%rdi),%eax
test   %edi,%edi
cmovns %edi,%eax
sar    $0xa,%eax
cvtsi2sd %eax,%xmm0
cmp    $0x100400,%edi
jge    1132 <hsize+0x22>
xor    %eax,%eax
jmp    11b2 <hsize+0xa2>
mulsd  0xec6(%rip),%xmm0
mov    $0x1,%eax
ucomisd 0xec1(%rip),%xmm0
jbe    11b2 <hsize+0xa2>
mulsd  0xeaf(%rip),%xmm0
mov    $0x2,%eax
ucomisd 0xeaa(%rip),%xmm0
jbe    11b2 <hsize+0xa2>
mulsd  0xe98(%rip),%xmm0
mov    $0x3,%eax
ucomisd 0xe93(%rip),%xmm0
jbe    11b2 <hsize+0xa2>
mulsd  0xe81(%rip),%xmm0
mov    $0x4,%eax
ucomisd 0xe7c(%rip),%xmm0
jbe    11b2 <hsize+0xa2>
mulsd  0xe6a(%rip),%xmm0
mov    $0x5,%eax
ucomisd 0xe65(%rip),%xmm0
jbe    11b2 <hsize+0xa2>
mulsd  0xe53(%rip),%xmm0
mov    $0x6,%eax
lea    0x2c27(%rip),%rcx
mov    (%rcx,%rax,8),%rcx
lea    0x2e7c(%rip),%rbx
lea    0xe5d(%rip),%rdx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@hsize.buffer = internal global [32 x i8] zeroinitializer
@hsize.units = internal global [7 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* null]
@.str = [4 x i8] c"KiB\00"
@.str.1 = [4 x i8] c"MiB\00"
@.str.2 = [4 x i8] c"GiB\00"
@.str.3 = [4 x i8] c"TiB\00"
@.str.4 = [4 x i8] c"PiB\00"
@.str.5 = [4 x i8] c"EiB\00"
@.str.6 = [9 x i8] c"%.02f %s\00"
define i8* @hsize(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = sdiv i32 %6, 1024
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = sitofp i32 %8 to double
store double %9, double* %5
store i32 0, i32* %3
br label %10
10: ; preds = %24, %1
%11 = load double, double* %5
%12 = fcmp ogt double %11, 1.024000e+03
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [7 x i8*], [7 x i8*]* @hsize.units, i64 0, i64 %15
%17 = load i8*, i8** %16
%18 = icmp ne i8* %17, null
br label %19
19: ; preds = %13, %10
%20 = phi i1 [ false, %10 ], [ %18, %13 ]
br i1 %20, label %21, label %27
21: ; preds = %19
%22 = load double, double* %5
%23 = fdiv double %22, 1.024000e+03
store double %23, double* %5
br label %24
24: ; preds = %21
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %10
27: ; preds = %19
%28 = load double, double* %5
%29 = load i32, i32* %3
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [7 x i8*], [7 x i8*]* @hsize.units, i64 0, i64 %30
%32 = load i8*, i8** %31
%33 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @hsize.buffer, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), double %28, i8* %32)
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @hsize.buffer, i64 0, i64 0)
}

LLVM IR (-O2):
@hsize.buffer = internal global [32 x i8] zeroinitializer
@hsize.units = internal unnamed_addr constant [7 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* null]
@.str = [4 x i8] c"KiB\00"
@.str.1 = [4 x i8] c"MiB\00"
@.str.2 = [4 x i8] c"GiB\00"
@.str.3 = [4 x i8] c"TiB\00"
@.str.4 = [4 x i8] c"PiB\00"
@.str.5 = [4 x i8] c"EiB\00"
@.str.6 = [9 x i8] c"%.02f %s\00"
define i8* @hsize(i32 %0) {
%2 = sdiv i32 %0, 1024
%3 = sitofp i32 %2 to double
%4 = icmp slt i32 %0, 1049600
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = fmul double %3, 0x3F50000000000000
%7 = fcmp ule double %6, 1.024000e+03
br i1 %7, label %22, label %8
8: ; preds = %5
%9 = fmul double %6, 0x3F50000000000000
%10 = fcmp ule double %9, 1.024000e+03
br i1 %10, label %22, label %11
11: ; preds = %8
%12 = fmul double %9, 0x3F50000000000000
%13 = fcmp ule double %12, 1.024000e+03
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = fmul double %12, 0x3F50000000000000
%16 = fcmp ule double %15, 1.024000e+03
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = fmul double %15, 0x3F50000000000000
%19 = fcmp ule double %18, 1.024000e+03
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = fmul double %18, 0x3F50000000000000
br label %22
22: ; preds = %5, %8, %11, %14, %17, %20, %1
%23 = phi i64 [ 0, %1 ], [ 1, %5 ], [ 2, %8 ], [ 3, %11 ], [ 4, %14 ], [ 5, %17 ], [ 6, %20 ]
%24 = phi double [ %3, %1 ], [ %6, %5 ], [ %9, %8 ], [ %12, %11 ], [ %15, %14 ], [ %18, %17 ], [ %21, %20 ]
%25 = getelementptr inbounds [7 x i8*], [7 x i8*]* @hsize.units, i64 0, i64 %23
%26 = load i8*, i8** %25
%27 = tail call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @hsize.buffer, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), double %24, i8* %26)
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @hsize.buffer, i64 0, i64 0)
}