#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 unsigned long long uint64_t;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, double);
char *str_size_human(uint64_t size) {
  static int str_table_index = 0;
  static char str_table[5][10];
  ++str_table_index;
  str_table_index %= 5;
  char *s = str_table[str_table_index];
  if (size < 1024ULL) {
    snprintf(s, 10, "%u B", (unsigned)size);
  } else if (size < 999.5 * 1024) {
    snprintf(s, 10, "%.1f KiB", (double)size / 1024);
  } else if (size < 999.5 * 1024 * 1024) {
    snprintf(s, 10, "%.1f MiB", (double)size / (1024 * 1024));
  } else if (size < 999.5 * 1024 * 1024 * 1024) {
    snprintf(s, 10, "%.1f GiB", (double)size / (1024 * 1024 * 1024));
  } else {
    snprintf(s, 10, "%.2f TiB",
             (double)size / ((uint64_t)1024 * 1024 * 1024 * 1024));
  }
  return s;
}


x86 assembly (clang, -O0):
<str_size_human>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2f1e(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2f15(%rip)
mov    0x2f0f(%rip),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,0x2f01(%rip)
movslq 0x2efa(%rip),%rcx
lea    0x2f03(%rip),%rax
imul   $0xa,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x400,-0x8(%rbp)
jae    1188 <str_size_human+0x78>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%eax
cvtsi2sd %rax,%xmm0
mov    $0xa,%esi
lea    0xee2(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1371 <str_size_human+0x261>
movaps 0xeb1(%rip),%xmm0
movq   -0x8(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xeb0(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe49(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    120e <str_size_human+0xfe>
mov    -0x10(%rbp),%rdi
movaps 0xe74(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe73(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe3c(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0xa,%esi
lea    0xe61(%rip),%rdx
call   1030 <snprintf@plt>
jmp    136c <str_size_human+0x25c>
movaps 0xe2b(%rip),%xmm0
movq   -0x8(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xe2a(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xdcb(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1294 <str_size_human+0x184>
mov    -0x10(%rbp),%rdi
movaps 0xdee(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xded(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xdae(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0xa,%esi
lea    0xde4(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1367 <str_size_human+0x257>
movaps 0xda5(%rip),%xmm0
movq   -0x8(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xda4(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xd4d(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    131a <str_size_human+0x20a>
mov    -0x10(%rbp),%rdi
movaps 0xd68(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xd67(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xd20(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0xa,%esi
lea    0xd67(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1362 <str_size_human+0x252>
mov    -0x10(%rbp),%rdi
movaps 0xd1b(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xd1a(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xccb(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0xa,%esi
lea    0xd23(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1367 <str_size_human+0x257>
jmp    136c <str_size_human+0x25c>
jmp    1371 <str_size_human+0x261>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_size_human>:
push   %rbx
mov    0x2f29(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   $0x66666667,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,0x2efd(%rip)
lea    (%rax,%rax,4),%rax
lea    0x2f02(%rip),%rcx
lea    (%rcx,%rax,2),%rbx
cmp    $0x3ff,%rdi
ja     116e <str_size_human+0x5e>
mov    %edi,%eax
cvtsi2sd %rax,%xmm0
lea    0xed7(%rip),%rdx
jmp    11f0 <str_size_human+0xe0>
movq   %rdi,%xmm1
punpckldq 0xe85(%rip),%xmm1
subpd  0xe8d(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
cmp    $0xf9dff,%rdi
ja     11a9 <str_size_human+0x99>
mulsd  0xe98(%rip),%xmm0
lea    0xe9e(%rip),%rdx
jmp    11f0 <str_size_human+0xe0>
cmp    $0x3e77ffff,%rdi
ja     11c3 <str_size_human+0xb3>
mulsd  0xe76(%rip),%xmm0
lea    0xe8d(%rip),%rdx
jmp    11f0 <str_size_human+0xe0>
shr    $0x1d,%rdi
cmp    $0x7ce,%rdi
ja     11e1 <str_size_human+0xd1>
mulsd  0xe50(%rip),%xmm0
lea    0xe78(%rip),%rdx
jmp    11f0 <str_size_human+0xe0>
mulsd  0xe37(%rip),%xmm0
lea    0xe70(%rip),%rdx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@str_size_human.str_table_index = internal global i32 0
@str_size_human.str_table = internal global [5 x [10 x i8]] zeroinitializer
@.str = [5 x i8] c"%u B\00"
@.str.1 = [9 x i8] c"%.1f KiB\00"
@.str.2 = [9 x i8] c"%.1f MiB\00"
@.str.3 = [9 x i8] c"%.1f GiB\00"
@.str.4 = [9 x i8] c"%.2f TiB\00"
define i8* @str_size_human(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
store i64 %0, i64* %2
%4 = load i32, i32* @str_size_human.str_table_index
%5 = add nsw i32 %4, 1
store i32 %5, i32* @str_size_human.str_table_index
%6 = load i32, i32* @str_size_human.str_table_index
%7 = srem i32 %6, 5
store i32 %7, i32* @str_size_human.str_table_index
%8 = load i32, i32* @str_size_human.str_table_index
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [5 x [10 x i8]], [5 x [10 x i8]]* @str_size_human.str_table, i64 0, i64 %9
%11 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
store i8* %11, i8** %3
%12 = load i64, i64* %2
%13 = icmp ult i64 %12, 1024
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i64, i64* %2
%17 = trunc i64 %16 to i32
%18 = uitofp i32 %17 to double
%19 = call i32 @snprintf(i8* %15, i32 10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), double %18)
br label %59
20: ; preds = %1
%21 = load i64, i64* %2
%22 = uitofp i64 %21 to double
%23 = fcmp olt double %22, 0x412F3C0000000000
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = load i64, i64* %2
%27 = uitofp i64 %26 to double
%28 = fdiv double %27, 1.024000e+03
%29 = call i32 @snprintf(i8* %25, i32 10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %28)
br label %58
30: ; preds = %20
%31 = load i64, i64* %2
%32 = uitofp i64 %31 to double
%33 = fcmp olt double %32, 0x41CF3C0000000000
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i8*, i8** %3
%36 = load i64, i64* %2
%37 = uitofp i64 %36 to double
%38 = fdiv double %37, 0x4130000000000000
%39 = call i32 @snprintf(i8* %35, i32 10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), double %38)
br label %57
40: ; preds = %30
%41 = load i64, i64* %2
%42 = uitofp i64 %41 to double
%43 = fcmp olt double %42, 0x426F3C0000000000
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i8*, i8** %3
%46 = load i64, i64* %2
%47 = uitofp i64 %46 to double
%48 = fdiv double %47, 0x41D0000000000000
%49 = call i32 @snprintf(i8* %45, i32 10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), double %48)
br label %56
50: ; preds = %40
%51 = load i8*, i8** %3
%52 = load i64, i64* %2
%53 = uitofp i64 %52 to double
%54 = fdiv double %53, 0x4270000000000000
%55 = call i32 @snprintf(i8* %51, i32 10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), double %54)
br label %56
56: ; preds = %50, %44
br label %57
57: ; preds = %56, %34
br label %58
58: ; preds = %57, %24
br label %59
59: ; preds = %58, %14
%60 = load i8*, i8** %3
ret i8* %60
}

LLVM IR (-O2):
@str_size_human.str_table_index = internal unnamed_addr global i32 0
@str_size_human.str_table = internal global [5 x [10 x i8]] zeroinitializer
@.str = [5 x i8] c"%u B\00"
@.str.1 = [9 x i8] c"%.1f KiB\00"
@.str.2 = [9 x i8] c"%.1f MiB\00"
@.str.3 = [9 x i8] c"%.1f GiB\00"
@.str.4 = [9 x i8] c"%.2f TiB\00"
define i8* @str_size_human(i64 %0) {
%2 = load i32, i32* @str_size_human.str_table_index
%3 = add nsw i32 %2, 1
%4 = srem i32 %3, 5
store i32 %4, i32* @str_size_human.str_table_index
%5 = zext i32 %4 to i64
%6 = getelementptr inbounds [5 x [10 x i8]], [5 x [10 x i8]]* @str_size_human.str_table, i64 0, i64 %5, i64 0
%7 = icmp ult i64 %0, 1024
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = trunc i64 %0 to i32
%10 = uitofp i32 %9 to double
%11 = tail call i32 @snprintf(i8* %6, i32 10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), double %10)
br label %31
12: ; preds = %1
%13 = uitofp i64 %0 to double
%14 = icmp ult i64 %0, 1023488
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = fmul double %13, 0x3F50000000000000
%17 = tail call i32 @snprintf(i8* %6, i32 10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %16)
br label %31
18: ; preds = %12
%19 = icmp ult i64 %0, 1048051712
br i1 %19, label %20, label %23
20: ; preds = %18
%21 = fmul double %13, 0x3EB0000000000000
%22 = tail call i32 @snprintf(i8* %6, i32 10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), double %21)
br label %31
23: ; preds = %18
%24 = icmp ult i64 %0, 1073204953088
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = fmul double %13, 0x3E10000000000000
%27 = tail call i32 @snprintf(i8* %6, i32 10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), double %26)
br label %31
28: ; preds = %23
%29 = fmul double %13, 0x3D70000000000000
%30 = tail call i32 @snprintf(i8* %6, i32 10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), double %29)
br label %31
31: ; preds = %15, %25, %28, %20, %8
ret i8* %6
}