#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*/ str_size;
typedef int /*<<< orphan*/ format_size;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
char *strdup(char *);
char *string_format_size(unsigned long long size) {
  char *unit_name[] = {"", ("KB"), ("MB"), ("GB"), ("TB")};
  char *unit_format[] = {"%.0f", "%.1f", "%.02f", "%.02f", "%.02f"};
  float unit_divide[] = {1.0, 1000.0, 1000.0 * 1000.0, 1000.0 * 1000.0 * 1000.0,
                         1000.0 * 1000.0 * 1000.0 * 1000.0};
  char format_size[128], str_size[128];
  int num_unit;
  float size_float;
  str_size[0] = '\0';
  if (size < 10ULL * 1000ULL)
    num_unit = 0;
  else if (size < 1000ULL * 1000ULL)
    num_unit = 1;
  else if (size < 1000ULL * 1000ULL * 1000ULL)
    num_unit = 2;
  else if (size < 1000ULL * 1000ULL * 1000ULL * 1000ULL)
    num_unit = 3;
  else
    num_unit = 4;
  snprintf(format_size, sizeof(format_size), "%s %%s", unit_format[num_unit]);
  size_float = ((float)size) / ((float)(unit_divide[num_unit]));
  snprintf(str_size, sizeof(str_size), format_size, size_float,
           (num_unit == 0) ? ("bytes") : (unit_name[num_unit]));
  return strdup(str_size);
}


x86 assembly (clang, -O0):
<string_format_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    0xeb2(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe9f(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe97(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe8f(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    0xe6f(%rip),%rax
mov    %rax,-0x60(%rbp)
lea    0xe69(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xe58(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe4d(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe5c(%rip),%rax
mov    %rax,-0x80(%rbp)
mov    0xe59(%rip),%rax
mov    %rax,-0x78(%rbp)
mov    0xe57(%rip),%eax
mov    %eax,-0x70(%rbp)
movb   $0x0,-0x180(%rbp)
cmpq   $0x2710,-0x8(%rbp)
jae    1210 <string_format_size+0xe0>
movl   $0x0,-0x184(%rbp)
jmp    1286 <string_format_size+0x156>
cmpq   $0xf4240,-0x8(%rbp)
jae    122d <string_format_size+0xfd>
movl   $0x1,-0x184(%rbp)
jmp    1281 <string_format_size+0x151>
cmpq   $0x3b9aca00,-0x8(%rbp)
3b
jae    124a <string_format_size+0x11a>
movl   $0x2,-0x184(%rbp)
jmp    127c <string_format_size+0x14c>
movabs $0xe8d4a51000,%rax
cmp    %rax,-0x8(%rbp)
jae    126d <string_format_size+0x13d>
movl   $0x3,-0x184(%rbp)
jmp    1277 <string_format_size+0x147>
movl   $0x4,-0x184(%rbp)
jmp    127c <string_format_size+0x14c>
jmp    1281 <string_format_size+0x151>
jmp    1286 <string_format_size+0x156>
movslq -0x184(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rcx
lea    0xd83(%rip),%rdx
xor    %eax,%eax
lea    -0x100(%rbp),%rdi
mov    $0x80,%esi
call   1030 <snprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x190(%rbp)
ff
test   %rax,%rax
movss  %xmm0,-0x18c(%rbp)
ff
js     12f5 <string_format_size+0x1c5>
movss  -0x190(%rbp),%xmm0
ff
movss  %xmm0,-0x18c(%rbp)
ff
movss  -0x18c(%rbp),%xmm0
ff
movslq -0x184(%rbp),%rax
divss  -0x80(%rbp,%rax,4),%xmm0
movss  %xmm0,-0x188(%rbp)
ff
lea    -0x180(%rbp),%rax
mov    %rax,-0x1a8(%rbp)
lea    -0x100(%rbp),%rax
mov    %rax,-0x1a0(%rbp)
movss  -0x188(%rbp),%xmm0
ff
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x198(%rbp)
ff
cmpl   $0x0,-0x184(%rbp)
jne    1362 <string_format_size+0x232>
lea    0xccd(%rip),%rax
mov    %rax,-0x1b0(%rbp)
jmp    1375 <string_format_size+0x245>
movslq -0x184(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rax
mov    %rax,-0x1b0(%rbp)
movsd  -0x198(%rbp),%xmm0
ff
mov    -0x1a0(%rbp),%rdx
mov    -0x1a8(%rbp),%rdi
mov    -0x1b0(%rbp),%rcx
mov    $0x80,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
lea    -0x180(%rbp),%rdi
call   1050 <strdup@plt>
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_format_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x160,%rsp
mov    %rdi,%rbx
lea    0xed3(%rip),%rax
mov    %rax,0x30(%rsp)
lea    0xebf(%rip),%rax
mov    %rax,0x38(%rsp)
lea    0xeb6(%rip),%rax
mov    %rax,0x40(%rsp)
lea    0xead(%rip),%rax
mov    %rax,0x48(%rsp)
lea    0xea4(%rip),%rax
mov    %rax,0x50(%rsp)
lea    0xe9b(%rip),%rax
mov    %rax,(%rsp)
lea    0xe95(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xe8e(%rip),%rax
mov    %rax,0x10(%rsp)
mov    %rax,0x18(%rsp)
mov    %rax,0x20(%rsp)
movb   $0x0,0x60(%rsp)
cmp    $0x2710,%rdi
jae    11ad <string_format_size+0x8d>
mov    $0x1,%bpl
xor    %r14d,%r14d
jmp    11e8 <string_format_size+0xc8>
cmp    $0xf4240,%rbx
jae    11be <string_format_size+0x9e>
mov    $0x1,%r14d
jmp    11e6 <string_format_size+0xc6>
cmp    $0x3b9aca00,%rbx
jae    11cf <string_format_size+0xaf>
mov    $0x2,%r14d
jmp    11e6 <string_format_size+0xc6>
mov    %rbx,%rax
shr    $0xc,%rax
cmp    $0xe8d4a51,%rax
mov    $0x4,%r14d
sbb    $0x0,%r14
xor    %ebp,%ebp
mov    (%rsp,%r14,8),%rcx
lea    0xe29(%rip),%rdx
lea    0xe0(%rsp),%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    0xe22(%rip),%rax
test   %bpl,%bpl
je     121c <string_format_size+0xfc>
lea    0xe09(%rip),%rcx
jmp    1221 <string_format_size+0x101>
mov    0x30(%rsp,%r14,8),%rcx
movss  (%rax,%r14,4),%xmm0
test   %rbx,%rbx
js     1233 <string_format_size+0x113>
cvtsi2ss %rbx,%xmm1
jmp    1248 <string_format_size+0x128>
mov    %rbx,%rax
shr    %rax
and    $0x1,%ebx
or     %rax,%rbx
cvtsi2ss %rbx,%xmm1
addss  %xmm1,%xmm1
divss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
lea    0x60(%rsp),%rbx
lea    0xe0(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strdup@plt>
add    $0x160,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.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 = [5 x i8] c"%.0f\00"
@.str.6 = [5 x i8] c"%.1f\00"
@.str.7 = [6 x i8] c"%.02f\00"
@__const.string_format_size.unit_divide = [5 x float] [float 1.000000e+00, float 1.000000e+03, float 1.000000e+06, float 1.000000e+09, float 0x426D1A94A0000000]
@.str.8 = [7 x i8] c"%s %%s\00"
@.str.9 = [6 x i8] c"bytes\00"
define i8* @string_format_size(i64 %0) {
%2 = alloca i64
%3 = alloca [5 x i8*]
%4 = alloca [5 x i8*]
%5 = alloca [5 x float]
%6 = alloca [128 x i8]
%7 = alloca [128 x i8]
%8 = alloca i32
%9 = alloca float
store i64 %0, i64* %2
%10 = bitcast [5 x i8*]* %3 to i8*
%11 = bitcast i8* %10 to [5 x i8*]*
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 0
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8** %15
%16 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8** %16
%17 = bitcast [5 x i8*]* %4 to i8*
%18 = bitcast i8* %17 to [5 x i8*]*
%19 = getelementptr inbounds [5 x i8*], [5 x i8*]* %18, i32 0, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i32 0, i32 0), i8** %19
%20 = getelementptr inbounds [5 x i8*], [5 x i8*]* %18, i32 0, i32 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i32 0, i32 0), i8** %20
%21 = getelementptr inbounds [5 x i8*], [5 x i8*]* %18, i32 0, i32 2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i32 0, i32 0), i8** %21
%22 = getelementptr inbounds [5 x i8*], [5 x i8*]* %18, i32 0, i32 3
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i32 0, i32 0), i8** %22
%23 = getelementptr inbounds [5 x i8*], [5 x i8*]* %18, i32 0, i32 4
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i32 0, i32 0), i8** %23
%24 = bitcast [5 x float]* %5 to i8*
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
store i8 0, i8* %25
%26 = load i64, i64* %2
%27 = icmp ult i64 %26, 10000
br i1 %27, label %28, label %29
28: ; preds = %1
store i32 0, i32* %8
br label %45
29: ; preds = %1
%30 = load i64, i64* %2
%31 = icmp ult i64 %30, 1000000
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 1, i32* %8
br label %44
33: ; preds = %29
%34 = load i64, i64* %2
%35 = icmp ult i64 %34, 1000000000
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 2, i32* %8
br label %43
37: ; preds = %33
%38 = load i64, i64* %2
%39 = icmp ult i64 %38, 1000000000000
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 3, i32* %8
br label %42
41: ; preds = %37
store i32 4, i32* %8
br label %42
42: ; preds = %41, %40
br label %43
43: ; preds = %42, %36
br label %44
44: ; preds = %43, %32
br label %45
45: ; preds = %44, %28
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [5 x i8*], [5 x i8*]* %4, i64 0, i64 %48
%50 = load i8*, i8** %49
%51 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %46, i32 128, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* %50)
%52 = load i64, i64* %2
%53 = uitofp i64 %52 to float
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [5 x float], [5 x float]* %5, i64 0, i64 %55
%57 = load float, float* %56
%58 = fdiv float %53, %57
store float %58, float* %9
%59 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%60 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%61 = load float, float* %9
%62 = fpext float %61 to double
%63 = load i32, i32* %8
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %45
br label %71
66: ; preds = %45
%67 = load i32, i32* %8
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %68
%70 = load i8*, i8** %69
br label %71
71: ; preds = %66, %65
%72 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), %65 ], [ %70, %66 ]
%73 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %59, i32 128, i8* %60, double %62, i8* %72)
%74 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%75 = call i8* @strdup(i8* %74)
ret i8* %75
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.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 = [5 x i8] c"%.0f\00"
@.str.6 = [5 x i8] c"%.1f\00"
@.str.7 = [6 x i8] c"%.02f\00"
@__const.string_format_size.unit_divide = [5 x float] [float 1.000000e+00, float 1.000000e+03, float 1.000000e+06, float 1.000000e+09, float 0x426D1A94A0000000]
@.str.8 = [7 x i8] c"%s %%s\00"
@.str.9 = [6 x i8] c"bytes\00"
define noalias i8* @string_format_size(i64 %0) {
%2 = alloca [5 x i8*]
%3 = alloca [5 x i8*]
%4 = alloca [128 x i8]
%5 = alloca [128 x i8]
%6 = bitcast [5 x i8*]* %2 to i8*
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %11
%12 = bitcast [5 x i8*]* %3 to i8*
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8** %14
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8** %16
%17 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 4
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8** %17
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
store i8 0, i8* %19
%20 = icmp ult i64 %0, 10000
br i1 %20, label %28, label %21
21: ; preds = %1
%22 = icmp ult i64 %0, 1000000
br i1 %22, label %28, label %23
23: ; preds = %21
%24 = icmp ult i64 %0, 1000000000
br i1 %24, label %28, label %25
25: ; preds = %23
%26 = icmp ult i64 %0, 1000000000000
%27 = select i1 %26, i64 3, i64 4
br label %28
28: ; preds = %25, %23, %21, %1
%29 = phi i1 [ true, %1 ], [ false, %21 ], [ false, %23 ], [ false, %25 ]
%30 = phi i64 [ 0, %1 ], [ 1, %21 ], [ 2, %23 ], [ %27, %25 ]
%31 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %30
%32 = load i8*, i8** %31
%33 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %18, i32 128, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds [5 x float], [5 x float]* @__const.string_format_size.unit_divide, i64 0, i64 %30
%35 = load float, float* %34
br i1 %29, label %39, label %36
36: ; preds = %28
%37 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %30
%38 = load i8*, i8** %37
br label %39
39: ; preds = %28, %36
%40 = phi i8* [ %38, %36 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), %28 ]
%41 = uitofp i64 %0 to float
%42 = fdiv float %41, %35
%43 = fpext float %42 to double
%44 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %19, i32 128, i8* %18, double %43, i8* %40)
%45 = call i8* @strdup(i8* %19)
ret i8* %45
}