#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 *, ...);
char *readable_size(unsigned long size, char *buf, int buffer_size) {
  int i = 0;
  const char *units[] = {"b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"};
  while (size > 1024) {
    size /= 1024;
    i++;
  }
  if (!i) {
    snprintf(buf, buffer_size, "0%s", units[i]);
  } else {
    snprintf(buf, buffer_size, "%.*lu%s", i, size, units[i]);
  }
  return buf;
}


x86 assembly (clang, -O0):
<readable_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
lea    0x2c8b(%rip),%rsi
mov    $0x48,%edx
call   1040 <memcpy@plt>
cmpq   $0x400,-0x8(%rbp)
jbe    1177 <readable_size+0x57>
mov    -0x8(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    114f <readable_size+0x2f>
cmpl   $0x0,-0x18(%rbp)
jne    11a4 <readable_size+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
movslq -0x18(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rcx
lea    0xe80(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11c9 <readable_size+0xa9>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%r8
movslq -0x18(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%r9
lea    0xe5a(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readable_size>:
push   %rbx
mov    %rsi,%rbx
cmp    $0x401,%rdi
jb     116c <readable_size+0x5c>
mov    %rdi,%r8
xor    %ecx,%ecx
lea    0xefb(%rip),%rax
nopl   0x0(%rax)
mov    %r8,%rsi
shr    $0xa,%r8
add    $0x1,%ecx
add    $0x4,%rax
cmp    $0x1003ff,%rsi
ja     1130 <readable_size+0x20>
movslq (%rax),%rax
lea    0xed3(%rip),%r9
add    %rax,%r9
lea    0xec1(%rip),%rax
mov    %rbx,%rdi
mov    %edx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    1189 <readable_size+0x79>
lea    0xea5(%rip),%rax
lea    0xe96(%rip),%rcx
mov    %rbx,%rdi
mov    %edx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.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"
@.str.6 = [3 x i8] c"Eb\00"
@.str.7 = [3 x i8] c"Zb\00"
@.str.8 = [3 x i8] c"Yb\00"
@__const.readable_size.units = [9 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), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i32 0, i32 0)]
@.str.9 = [4 x i8] c"0%s\00"
@.str.10 = [8 x i8] c"%.*lu%s\00"
define i8* @readable_size(i64 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [9 x i8*]
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%9 = bitcast [9 x i8*]* %8 to i8*
br label %10
10: ; preds = %13, %3
%11 = load i64, i64* %4
%12 = icmp ugt i64 %11, 1024
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i64, i64* %4
%15 = udiv i64 %14, 1024
store i64 %15, i64* %4
%16 = load i32, i32* %7
%17 = add nsw i32 %16, 1
store i32 %17, i32* %7
br label %10
18: ; preds = %10
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [9 x i8*], [9 x i8*]* %8, i64 0, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* %27)
br label %39
29: ; preds = %18
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = load i32, i32* %7
%33 = load i64, i64* %4
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [9 x i8*], [9 x i8*]* %8, i64 0, i64 %35
%37 = load i8*, i8** %36
%38 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %30, i32 %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), i32 %32, i64 %33, i8* %37)
br label %39
39: ; preds = %29, %21
%40 = load i8*, i8** %5
ret i8* %40
}

LLVM IR (-O2):
@.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"
@.str.6 = [3 x i8] c"Eb\00"
@.str.7 = [3 x i8] c"Zb\00"
@.str.8 = [3 x i8] c"Yb\00"
@reltable.readable_size = [9 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.4 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.5 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.6 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.7 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.8 to i64), i64 ptrtoint ([9 x i32]* @reltable.readable_size to i64)) to i32)]
@.str.9 = [4 x i8] c"0%s\00"
@.str.10 = [8 x i8] c"%.*lu%s\00"
define i8* @readable_size(i64 %0, i8* returned %1, i32 %2) {
%4 = icmp ugt i64 %0, 1024
br i1 %4, label %5, label %11
5: ; preds = %3, %5
%6 = phi i32 [ %9, %5 ], [ 0, %3 ]
%7 = phi i64 [ %8, %5 ], [ %0, %3 ]
%8 = lshr i64 %7, 10
%9 = add nuw nsw i32 %6, 1
%10 = icmp ugt i64 %7, 1049599
br i1 %10, label %5, label %13
11: ; preds = %3
%12 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %18
13: ; preds = %5
%14 = zext i32 %9 to i64
%15 = shl i64 %14, 2
%16 = call i8* @llvm.load.relative.i64(i8* bitcast ([9 x i32]* @reltable.readable_size to i8*), i64 %15)
%17 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), i32 %9, i64 %8, i8* %16)
br label %18
18: ; preds = %13, %11
ret i8* %1
}