#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*/ units;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, unsigned int,
                            char const *const);
char *bytes_to_human_readable(size_t bytes) {
  static const char *const units[] = {"B", "KB", "MB", "GB", "TB", "PB"};
  static char buf[15 * 4];
  unsigned int i, shift, quoti, remain;
  shift = 0;
  for (i = 0; i < sizeof(units) / sizeof(units[0]); i++) {
    if (bytes / (1ULL << (shift + 10)) == 0)
      break;
    shift += 10;
  }
  quoti = (unsigned int)(bytes / (1ULL << shift));
  remain = 0;
  if (shift > 0) {
    remain = (unsigned int)((bytes & ((1ULL << shift) - 1)) >> (shift - 10));
    remain = (remain * 100) / 1024;
  }
  snprintf(buf, sizeof(buf), "%u.%02u %s", quoti, remain, units[i]);
  return buf;
}


x86 assembly (clang, -O0):
<bytes_to_human_readable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    $0x6,%rax
jae    1179 <bytes_to_human_readable+0x69>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0xa,%ecx
mov    %ecx,%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
xor    %edx,%edx
div    %rcx
cmp    $0x0,%rax
jne    1162 <bytes_to_human_readable+0x52>
jmp    1179 <bytes_to_human_readable+0x69>
mov    -0x10(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112a <bytes_to_human_readable+0x1a>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x10(%rbp)
jbe    11d5 <bytes_to_human_readable+0xc5>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
sub    $0x1,%rcx
and    %rcx,%rax
mov    -0x10(%rbp),%ecx
sub    $0xa,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
mov    %eax,-0x18(%rbp)
imul   $0x64,-0x18(%rbp),%eax
shr    $0xa,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    -0xc(%rbp),%eax
mov    %eax,%edx
lea    0x2c08(%rip),%rax
mov    (%rax,%rdx,8),%r9
lea    0x2e4d(%rip),%rdi
mov    $0x3c,%esi
lea    0xe10(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2e35(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes_to_human_readable>:
mov    %rdi,%rax
cmp    $0x400,%rdi
jae    1128 <bytes_to_human_readable+0x18>
xor    %edx,%edx
xor    %esi,%esi
xor    %r8d,%r8d
jmp    11bb <bytes_to_human_readable+0xab>
cmp    $0x100000,%rax
jae    113c <bytes_to_human_readable+0x2c>
mov    $0x1,%esi
mov    $0xa,%ecx
jmp    119b <bytes_to_human_readable+0x8b>
cmp    $0x40000000,%rax
jae    1150 <bytes_to_human_readable+0x40>
mov    $0x2,%esi
mov    $0x14,%ecx
jmp    119b <bytes_to_human_readable+0x8b>
mov    %rax,%rcx
shr    $0x28,%rcx
jne    1165 <bytes_to_human_readable+0x55>
mov    $0x3,%esi
mov    $0x1e,%ecx
jmp    119b <bytes_to_human_readable+0x8b>
mov    %rax,%rcx
shr    $0x32,%rcx
jne    117a <bytes_to_human_readable+0x6a>
mov    $0x4,%esi
mov    $0x28,%ecx
jmp    119b <bytes_to_human_readable+0x8b>
xor    %edx,%edx
mov    %rax,%rcx
shr    $0x3c,%rcx
sete   %dl
mov    $0x32,%esi
mov    $0x3c,%ecx
cmove  %esi,%ecx
mov    $0x6,%esi
sub    %rdx,%rsi
mov    $0xffffffffffffffff,%rdi
shl    %cl,%rdi
mov    %ecx,%edx
not    %rdi
and    %rax,%rdi
add    $0xf6,%cl
shr    %cl,%rdi
imul   $0x64,%edi,%r8d
shr    $0xa,%r8d
push   %rbx
mov    %edx,%ecx
shr    %cl,%rax
lea    0xe38(%rip),%rcx
movslq (%rcx,%rsi,4),%r9
add    %rcx,%r9
lea    0x2e6a(%rip),%rbx
lea    0xe4a(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3c,%esi
mov    %eax,%ecx
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@bytes_to_human_readable.units = internal constant [6 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i32 0, i32 0)]
@.str = [2 x i8] c"B\00"
@.str.1 = [3 x i8] c"KB\00"
@.str.2 = [3 x i8] c"MB\00"
@.str.3 = [3 x i8] c"GB\00"
@.str.4 = [3 x i8] c"TB\00"
@.str.5 = [3 x i8] c"PB\00"
@bytes_to_human_readable.buf = internal global [60 x i8] zeroinitializer
@.str.6 = [11 x i8] c"%u.%02u %s\00"
define i8* @bytes_to_human_readable(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i64 %0, i64* %2
store i32 0, i32* %4
store i32 0, i32* %3
br label %7
7: ; preds = %23, %1
%8 = load i32, i32* %3
%9 = zext i32 %8 to i64
%10 = icmp ult i64 %9, 6
br i1 %10, label %11, label %26
11: ; preds = %7
%12 = load i64, i64* %2
%13 = load i32, i32* %4
%14 = add i32 %13, 10
%15 = zext i32 %14 to i64
%16 = shl i64 1, %15
%17 = udiv i64 %12, %16
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11
br label %26
20: ; preds = %11
%21 = load i32, i32* %4
%22 = add i32 %21, 10
store i32 %22, i32* %4
br label %23
23: ; preds = %20
%24 = load i32, i32* %3
%25 = add i32 %24, 1
store i32 %25, i32* %3
br label %7
26: ; preds = %19, %7
%27 = load i64, i64* %2
%28 = load i32, i32* %4
%29 = zext i32 %28 to i64
%30 = shl i64 1, %29
%31 = udiv i64 %27, %30
%32 = trunc i64 %31 to i32
store i32 %32, i32* %5
store i32 0, i32* %6
%33 = load i32, i32* %4
%34 = icmp ugt i32 %33, 0
br i1 %34, label %35, label %50
35: ; preds = %26
%36 = load i64, i64* %2
%37 = load i32, i32* %4
%38 = zext i32 %37 to i64
%39 = shl i64 1, %38
%40 = sub i64 %39, 1
%41 = and i64 %36, %40
%42 = load i32, i32* %4
%43 = sub i32 %42, 10
%44 = zext i32 %43 to i64
%45 = lshr i64 %41, %44
%46 = trunc i64 %45 to i32
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = mul i32 %47, 100
%49 = udiv i32 %48, 1024
store i32 %49, i32* %6
br label %50
50: ; preds = %35, %26
%51 = load i32, i32* %5
%52 = load i32, i32* %6
%53 = load i32, i32* %3
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds [6 x i8*], [6 x i8*]* @bytes_to_human_readable.units, i64 0, i64 %54
%56 = load i8*, i8** %55
%57 = call i32 @snprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @bytes_to_human_readable.buf, i64 0, i64 0), i32 60, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %51, i32 %52, i8* %56)
ret i8* getelementptr inbounds ([60 x i8], [60 x i8]* @bytes_to_human_readable.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@reltable.bytes_to_human_readable = internal unnamed_addr constant [6 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str to i64), i64 ptrtoint ([6 x i32]* @reltable.bytes_to_human_readable to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([6 x i32]* @reltable.bytes_to_human_readable to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([6 x i32]* @reltable.bytes_to_human_readable to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([6 x i32]* @reltable.bytes_to_human_readable to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.4 to i64), i64 ptrtoint ([6 x i32]* @reltable.bytes_to_human_readable to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.5 to i64), i64 ptrtoint ([6 x i32]* @reltable.bytes_to_human_readable to i64)) to i32)]
@.str = [2 x i8] c"B\00"
@.str.1 = [3 x i8] c"KB\00"
@.str.2 = [3 x i8] c"MB\00"
@.str.3 = [3 x i8] c"GB\00"
@.str.4 = [3 x i8] c"TB\00"
@.str.5 = [3 x i8] c"PB\00"
@bytes_to_human_readable.buf = internal global [60 x i8] zeroinitializer
@.str.6 = [11 x i8] c"%u.%02u %s\00"
define i8* @bytes_to_human_readable(i64 %0) {
%2 = icmp ult i64 %0, 1024
br i1 %2, label %28, label %3
3: ; preds = %1
%4 = icmp ult i64 %0, 1048576
br i1 %4, label %15, label %5
5: ; preds = %3
%6 = icmp ult i64 %0, 1073741824
br i1 %6, label %15, label %7
7: ; preds = %5
%8 = icmp ult i64 %0, 1099511627776
br i1 %8, label %15, label %9
9: ; preds = %7
%10 = icmp ult i64 %0, 1125899906842624
br i1 %10, label %15, label %11
11: ; preds = %9
%12 = icmp ult i64 %0, 1152921504606846976
%13 = select i1 %12, i32 50, i32 60
%14 = select i1 %12, i64 5, i64 6
br label %15
15: ; preds = %11, %9, %7, %5, %3
%16 = phi i32 [ 10, %3 ], [ 20, %5 ], [ 30, %7 ], [ 40, %9 ], [ %13, %11 ]
%17 = phi i64 [ 1, %3 ], [ 2, %5 ], [ 3, %7 ], [ 4, %9 ], [ %14, %11 ]
%18 = zext i32 %16 to i64
%19 = shl nsw i64 -1, %18
%20 = xor i64 %19, -1
%21 = and i64 %20, %0
%22 = add nsw i32 %16, -10
%23 = zext i32 %22 to i64
%24 = lshr i64 %21, %23
%25 = trunc i64 %24 to i32
%26 = mul i32 %25, 100
%27 = lshr i32 %26, 10
br label %28
28: ; preds = %1, %15
%29 = phi i64 [ %18, %15 ], [ 0, %1 ]
%30 = phi i64 [ %17, %15 ], [ 0, %1 ]
%31 = phi i32 [ %27, %15 ], [ 0, %1 ]
%32 = lshr i64 %0, %29
%33 = trunc i64 %32 to i32
%34 = shl i64 %30, 2
%35 = call i8* @llvm.load.relative.i64(i8* bitcast ([6 x i32]* @reltable.bytes_to_human_readable to i8*), i64 %34)
%36 = tail call i32 @snprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @bytes_to_human_readable.buf, i64 0, i64 0), i32 60, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %33, i32 %31, i8* %35)
ret i8* getelementptr inbounds ([60 x i8], [60 x i8]* @bytes_to_human_readable.buf, i64 0, i64 0)
}