#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, size_t, ...);
int print_human_readable_size(FILE *stream, size_t size) {
  static const char *prefixes = " KMGTPEZY";
  size_t order = 0;
  size_t hr_size = size;
  size_t hr_fraction = 0;
  double double_fraction = 0;
  char prefix = '\0';
  while (hr_size > 1023) {
    hr_size = hr_size >> 10;
    ++order;
  }
  prefix = prefixes[order];
  order = (size_t)(1) << (10 * order);
  hr_fraction = size % order;
  double_fraction = (double)(hr_fraction);
  while (order > 1024) {
    double_fraction = double_fraction / 1024;
    order = order >> 10;
  }
  hr_fraction = (size_t)(0.5 + double_fraction * 0.9765625);
  if (hr_fraction == 1000) {
    ++hr_size;
    hr_fraction = 0;
  }
  if (prefix == ' ') {
    fprintf(stream, "%zu B", hr_size);
  } else {
    fprintf(stream, "%zu.%03zu %ciB", hr_size, hr_fraction, prefix);
  }
  return (0);
}


x86 assembly (clang, -O0):
<print_human_readable_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movb   $0x0,-0x31(%rbp)
cmpq   $0x3ff,-0x20(%rbp)
jbe    116f <print_human_readable_size+0x5f>
mov    -0x20(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1144 <print_human_readable_size+0x34>
mov    0x2eb2(%rip),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x31(%rbp)
mov    -0x18(%rbp),%ecx
add    %ecx,%ecx
mov    %ecx,%eax
lea    (%rax,%rax,4),%eax
mov    %al,%cl
mov    $0x1,%eax
shl    %cl,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x28(%rbp)
movaps 0xe50(%rip),%xmm1
movq   -0x28(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe4f(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
cmpq   $0x400,-0x18(%rbp)
jbe    120a <print_human_readable_size+0xfa>
movsd  -0x30(%rbp),%xmm0
movsd  0xe48(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x18(%rbp)
jmp    11d5 <print_human_readable_size+0xc5>
movsd  -0x30(%rbp),%xmm0
movsd  0xe09(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe05(%rip),%xmm1
addsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xdf2(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x3e8,-0x28(%rbp)
jne    1273 <print_human_readable_size+0x163>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movsbl -0x31(%rbp),%eax
cmp    $0x20,%eax
jne    129b <print_human_readable_size+0x18b>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    12ba <print_human_readable_size+0x1aa>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
movsbl -0x31(%rbp),%r8d
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_human_readable_size>:
xor    %r8d,%r8d
cmp    $0x400,%rsi
jb     1145 <print_human_readable_size+0x35>
mov    %rsi,%rax
mov    %rsi,%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
shr    $0xa,%rdx
add    $0x1,%r8
cmp    $0xfffff,%rax
mov    %rdx,%rax
ja     1130 <print_human_readable_size+0x20>
jmp    1148 <print_human_readable_size+0x38>
mov    %rsi,%rdx
push   %rax
lea    (%r8,%r8,1),%rax
lea    (%rax,%rax,4),%rcx
mov    $0x1,%r9d
shl    %cl,%r9
lea    -0x1(%r9),%rax
and    %rsi,%rax
movq   %rax,%xmm1
punpckldq 0xe92(%rip),%xmm1
subpd  0xe9a(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
cmp    $0xb,%rcx
jb     11b4 <print_human_readable_size+0xa4>
movsd  0xe90(%rip),%xmm1
mov    %r9,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mulsd  %xmm1,%xmm0
shr    $0xa,%rcx
cmp    $0x1003ff,%r9
mov    %rcx,%r9
ja     11a0 <print_human_readable_size+0x90>
mulsd  0xe6c(%rip),%xmm0
addsd  0xe6c(%rip),%xmm0
cvttsd2si %xmm0,%rcx
mov    %rcx,%rsi
subsd  0xe64(%rip),%xmm0
cvttsd2si %xmm0,%rax
sar    $0x3f,%rsi
and    %rsi,%rax
or     %rcx,%rax
xor    %ecx,%ecx
cmp    $0x3e8,%rax
sete   %cl
add    %rcx,%rdx
test   %r8,%r8
je     1221 <print_human_readable_size+0x111>
lea    0xe43(%rip),%rcx
movsbl (%r8,%rcx,1),%r8d
cmp    $0x3e8,%rax
mov    $0x0,%ecx
cmovne %rax,%rcx
lea    0xe38(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    122f <print_human_readable_size+0x11f>
lea    0xe22(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@print_human_readable_size.prefixes = internal global i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0)
@.str = [10 x i8] c" KMGTPEZY\00"
@.str.1 = [6 x i8] c"%zu B\00"
@.str.2 = [15 x i8] c"%zu.%03zu %ciB\00"
define i32 @print_human_readable_size(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca i8
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 0, i64* %5
%10 = load i64, i64* %4
store i64 %10, i64* %6
store i64 0, i64* %7
store double 0.000000e+00, double* %8
store i8 0, i8* %9
br label %11
11: ; preds = %14, %2
%12 = load i64, i64* %6
%13 = icmp ugt i64 %12, 1023
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i64, i64* %6
%16 = lshr i64 %15, 10
store i64 %16, i64* %6
%17 = load i64, i64* %5
%18 = add i64 %17, 1
store i64 %18, i64* %5
br label %11
19: ; preds = %11
%20 = load i8*, i8** @print_human_readable_size.prefixes
%21 = load i64, i64* %5
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i8, i8* %22
store i8 %23, i8* %9
%24 = load i64, i64* %5
%25 = mul i64 10, %24
%26 = shl i64 1, %25
store i64 %26, i64* %5
%27 = load i64, i64* %4
%28 = load i64, i64* %5
%29 = urem i64 %27, %28
store i64 %29, i64* %7
%30 = load i64, i64* %7
%31 = uitofp i64 %30 to double
store double %31, double* %8
br label %32
32: ; preds = %35, %19
%33 = load i64, i64* %5
%34 = icmp ugt i64 %33, 1024
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load double, double* %8
%37 = fdiv double %36, 1.024000e+03
store double %37, double* %8
%38 = load i64, i64* %5
%39 = lshr i64 %38, 10
store i64 %39, i64* %5
br label %32
40: ; preds = %32
%41 = load double, double* %8
%42 = call double @llvm.fmuladd.f64(double %41, double 0x3FEF400000000000, double 5.000000e-01)
%43 = fptoui double %42 to i64
store i64 %43, i64* %7
%44 = load i64, i64* %7
%45 = icmp eq i64 %44, 1000
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i64, i64* %6
%48 = add i64 %47, 1
store i64 %48, i64* %6
store i64 0, i64* %7
br label %49
49: ; preds = %46, %40
%50 = load i8, i8* %9
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 32
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i32*, i32** %3
%55 = load i64, i64* %6
%56 = call i32 (i32*, i8*, i64, ...) @fprintf(i32* %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %55)
br label %64
57: ; preds = %49
%58 = load i32*, i32** %3
%59 = load i64, i64* %6
%60 = load i64, i64* %7
%61 = load i8, i8* %9
%62 = sext i8 %61 to i32
%63 = call i32 (i32*, i8*, i64, ...) @fprintf(i32* %58, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %59, i64 %60, i32 %62)
br label %64
64: ; preds = %57, %53
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c" KMGTPEZY\00"
@.str.1 = [6 x i8] c"%zu B\00"
@.str.2 = [15 x i8] c"%zu.%03zu %ciB\00"
define i32 @print_human_readable_size(i32* nocapture %0, i64 %1) {
%3 = icmp ugt i64 %1, 1023
br i1 %3, label %4, label %10
4: ; preds = %2, %4
%5 = phi i64 [ %7, %4 ], [ %1, %2 ]
%6 = phi i64 [ %8, %4 ], [ 0, %2 ]
%7 = lshr i64 %5, 10
%8 = add nuw nsw i64 %6, 1
%9 = icmp ugt i64 %5, 1048575
br i1 %9, label %4, label %10
10: ; preds = %4, %2
%11 = phi i64 [ 0, %2 ], [ %8, %4 ]
%12 = phi i64 [ %1, %2 ], [ %7, %4 ]
%13 = getelementptr inbounds [10 x i8], [10 x i8]* @.str, i64 0, i64 %11
%14 = load i8, i8* %13
%15 = mul i64 %11, 10
%16 = shl nuw i64 1, %15
%17 = add i64 %16, -1
%18 = and i64 %17, %1
%19 = uitofp i64 %18 to double
%20 = icmp ugt i64 %15, 10
br i1 %20, label %21, label %27
21: ; preds = %10, %21
%22 = phi double [ %24, %21 ], [ %19, %10 ]
%23 = phi i64 [ %25, %21 ], [ %16, %10 ]
%24 = fmul double %22, 0x3F50000000000000
%25 = lshr i64 %23, 10
%26 = icmp ugt i64 %23, 1049599
br i1 %26, label %21, label %27
27: ; preds = %21, %10
%28 = phi double [ %19, %10 ], [ %24, %21 ]
%29 = tail call double @llvm.fmuladd.f64(double %28, double 0x3FEF400000000000, double 5.000000e-01)
%30 = fptoui double %29 to i64
%31 = icmp eq i64 %30, 1000
%32 = zext i1 %31 to i64
%33 = add i64 %12, %32
%34 = icmp eq i64 %11, 0
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = tail call i32 (i32*, i8*, i64, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %33)
br label %41
37: ; preds = %27
%38 = select i1 %31, i64 0, i64 %30
%39 = sext i8 %14 to i32
%40 = tail call i32 (i32*, i8*, i64, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %33, i64 %38, i32 %39)
br label %41
41: ; preds = %37, %35
ret i32 0
}