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

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, ...);
void format_size(char *str, size_t size) {
  static const char *const unit_vec[] = {"byte", "KiB", "MiB", "GiB",
                                         "TiB",  "PiB", "EiB"};
  unsigned int i;
  for (i = 0; i < (sizeof(unit_vec) / sizeof((unit_vec)[0])); i++) {
    if (size / 1024 == 0) {
      sprintf(str, "%zu %s", size, unit_vec[i]);
      return;
    }
    size /= 1024;
  }
  sprintf(str, "huge");
}


x86 assembly (clang, -O0):
<format_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    $0x7,%rax
jae    118b <format_size+0x7b>
mov    -0x10(%rbp),%rax
shr    $0xa,%rax
cmp    $0x0,%rax
jne    1171 <format_size+0x61>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
lea    0x2c86(%rip),%rax
mov    (%rax,%rcx,8),%rcx
lea    0xeb8(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    119d <format_size+0x8d>
mov    -0x10(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1127 <format_size+0x17>
mov    -0x8(%rbp),%rdi
lea    0xe8e(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_size>:
cmp    $0x400,%rsi
jae    111d <format_size+0xd>
xor    %eax,%eax
jmp    118b <format_size+0x7b>
cmp    $0x100000,%rsi
jae    1131 <format_size+0x21>
shr    $0xa,%rsi
mov    $0x1,%eax
jmp    118b <format_size+0x7b>
cmp    $0x40000000,%rsi
jae    1145 <format_size+0x35>
shr    $0x14,%rsi
mov    $0x2,%eax
jmp    118b <format_size+0x7b>
mov    %rsi,%rdx
shr    $0x28,%rdx
jne    1159 <format_size+0x49>
shr    $0x1e,%rsi
mov    $0x3,%eax
jmp    118b <format_size+0x7b>
mov    %rsi,%rax
shr    $0x32,%rax
mov    $0x4,%eax
je     118e <format_size+0x7e>
xor    %edx,%edx
mov    %rsi,%rax
shr    $0x3c,%rax
sete   %dl
mov    $0x32,%eax
mov    $0x3c,%ecx
cmove  %eax,%ecx
mov    $0x6,%eax
sub    %rdx,%rax
shr    %cl,%rsi
mov    %rsi,%rdx
lea    0xe6b(%rip),%rsi
movslq (%rsi,%rax,4),%rcx
add    %rsi,%rcx
lea    0xe96(%rip),%rsi
xor    %eax,%eax
jmp    1030 <sprintf@plt>

LLVM IR (-O0):
@format_size.unit_vec = internal constant [7 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 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* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i32 0, i32 0)]
@.str = [5 x i8] c"byte\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
@.str.3 = [4 x i8] c"GiB\00"
@.str.4 = [4 x i8] c"TiB\00"
@.str.5 = [4 x i8] c"PiB\00"
@.str.6 = [4 x i8] c"EiB\00"
@.str.7 = [7 x i8] c"%zu %s\00"
@.str.8 = [5 x i8] c"huge\00"
define void @format_size(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 0, i32* %5
br label %6
6: ; preds = %25, %2
%7 = load i32, i32* %5
%8 = zext i32 %7 to i64
%9 = icmp ult i64 %8, 7
br i1 %9, label %10, label %28
10: ; preds = %6
%11 = load i64, i64* %4
%12 = udiv i64 %11, 1024
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i64, i64* %4
%17 = load i32, i32* %5
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [7 x i8*], [7 x i8*]* @format_size.unit_vec, i64 0, i64 %18
%20 = load i8*, i8** %19
%21 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i64 %16, i8* %20)
br label %31
22: ; preds = %10
%23 = load i64, i64* %4
%24 = udiv i64 %23, 1024
store i64 %24, i64* %4
br label %25
25: ; preds = %22
%26 = load i32, i32* %5
%27 = add i32 %26, 1
store i32 %27, i32* %5
br label %6
28: ; preds = %6
%29 = load i8*, i8** %3
%30 = call i32 (i8*, i8*, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %31
31: ; preds = %28, %14
ret void
}

LLVM IR (-O2):
@reltable.format_size = internal unnamed_addr constant [7 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str to i64), i64 ptrtoint ([7 x i32]* @reltable.format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([7 x i32]* @reltable.format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.2 to i64), i64 ptrtoint ([7 x i32]* @reltable.format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.3 to i64), i64 ptrtoint ([7 x i32]* @reltable.format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.4 to i64), i64 ptrtoint ([7 x i32]* @reltable.format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.5 to i64), i64 ptrtoint ([7 x i32]* @reltable.format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.6 to i64), i64 ptrtoint ([7 x i32]* @reltable.format_size to i64)) to i32)]
@.str = [5 x i8] c"byte\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
@.str.3 = [4 x i8] c"GiB\00"
@.str.4 = [4 x i8] c"TiB\00"
@.str.5 = [4 x i8] c"PiB\00"
@.str.6 = [4 x i8] c"EiB\00"
@.str.7 = [7 x i8] c"%zu %s\00"
define void @format_size(i8* nocapture writeonly %0, i64 %1) {
%3 = icmp ult i64 %1, 1024
br i1 %3, label %21, label %4
4: ; preds = %2
%5 = lshr i64 %1, 10
%6 = icmp ult i64 %1, 1048576
br i1 %6, label %21, label %7
7: ; preds = %4
%8 = lshr i64 %1, 20
%9 = icmp ult i64 %1, 1073741824
br i1 %9, label %21, label %10
10: ; preds = %7
%11 = lshr i64 %1, 30
%12 = icmp ult i64 %1, 1099511627776
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = lshr i64 %1, 40
%15 = icmp ult i64 %1, 1125899906842624
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = icmp ult i64 %1, 1152921504606846976
%18 = select i1 %17, i64 5, i64 6
%19 = select i1 %17, i64 50, i64 60
%20 = lshr i64 %1, %19
br label %21
21: ; preds = %16, %2, %4, %7, %10, %13
%22 = phi i64 [ 0, %2 ], [ 1, %4 ], [ 2, %7 ], [ 3, %10 ], [ 4, %13 ], [ %18, %16 ]
%23 = phi i64 [ %1, %2 ], [ %5, %4 ], [ %8, %7 ], [ %11, %10 ], [ %14, %13 ], [ %20, %16 ]
%24 = shl i64 %22, 2
%25 = call i8* @llvm.load.relative.i64(i8* bitcast ([7 x i32]* @reltable.format_size to i8*), i64 %24)
%26 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i64 %23, i8* %25)
ret void
}