#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 size) {
  char *unit_name[] = {("bytes"), ("KB"), ("MB"), ("GB")};
  char *unit_format[] = {"%.0f", "%.1f", "%.02f", "%.02f"};
  float unit_divide[] = {1, 1024, 1024 * 1024, 1024 * 1024 * 1024};
  char format_size[128], str_size[128];
  int num_unit;
  str_size[0] = '\0';
  if (size < 1024 * 10)
    num_unit = 0;
  else if (size < 1024 * 1024)
    num_unit = 1;
  else if (size < 1024 * 1024 * 1024)
    num_unit = 2;
  else
    num_unit = 3;
  snprintf(format_size, sizeof(format_size), "%s %%s", unit_format[num_unit]);
  snprintf(str_size, sizeof(str_size), format_size,
           ((float)size) / ((float)(unit_divide[num_unit])),
           (size <= 1) ? ("byte") : (unit_name[num_unit]));
  return strdup(str_size);
}


x86 assembly (clang, -O0):
<string_format_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2caa(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2ca7(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2ca4(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2ca1(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2c9e(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c9b(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2c98(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c95(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xea2(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0xe9f(%rip),%rax
mov    %rax,-0x58(%rbp)
movb   $0x0,-0x160(%rbp)
cmpq   $0x2800,-0x8(%rbp)
jae    11c1 <string_format_size+0xa1>
movl   $0x0,-0x164(%rbp)
jmp    120f <string_format_size+0xef>
cmpq   $0x100000,-0x8(%rbp)
jae    11de <string_format_size+0xbe>
movl   $0x1,-0x164(%rbp)
jmp    120a <string_format_size+0xea>
cmpq   $0x40000000,-0x8(%rbp)
40
jae    11fb <string_format_size+0xdb>
movl   $0x2,-0x164(%rbp)
jmp    1205 <string_format_size+0xe5>
movl   $0x3,-0x164(%rbp)
jmp    120a <string_format_size+0xea>
jmp    120f <string_format_size+0xef>
movslq -0x164(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rcx
lea    0xdfd(%rip),%rdx
xor    %eax,%eax
lea    -0xe0(%rbp),%rdi
mov    %rdi,-0x180(%rbp)
mov    $0x80,%esi
call   1030 <snprintf@plt>
lea    -0x160(%rbp),%rax
mov    %rax,-0x178(%rbp)
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,-0x16c(%rbp)
ff
test   %rax,%rax
movss  %xmm0,-0x168(%rbp)
ff
js     1293 <string_format_size+0x173>
movss  -0x16c(%rbp),%xmm0
ff
movss  %xmm0,-0x168(%rbp)
ff
movss  -0x168(%rbp),%xmm0
ff
movslq -0x164(%rbp),%rax
divss  -0x60(%rbp,%rax,4),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x188(%rbp)
ff
cmpq   $0x1,-0x8(%rbp)
ja     12d2 <string_format_size+0x1b2>
lea    0xd60(%rip),%rax
mov    %rax,-0x190(%rbp)
jmp    12e5 <string_format_size+0x1c5>
movslq -0x164(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rax
mov    %rax,-0x190(%rbp)
movsd  -0x188(%rbp),%xmm0
ff
mov    -0x180(%rbp),%rdx
mov    -0x178(%rbp),%rdi
mov    -0x190(%rbp),%rcx
mov    $0x80,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
lea    -0x160(%rbp),%rdi
call   1040 <strdup@plt>
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_format_size>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%rbx
movb   $0x0,(%rsp)
cmp    $0x2800,%rdi
jae    113f <string_format_size+0x1f>
xor    %r14d,%r14d
jmp    115f <string_format_size+0x3f>
mov    $0x1,%r14d
cmp    $0x100000,%rbx
jb     115f <string_format_size+0x3f>
cmp    $0x40000000,%rbx
mov    $0x3,%r14d
sbb    $0x0,%r14
lea    0xed6(%rip),%rax
movslq (%rax,%r14,4),%rcx
add    %rax,%rcx
lea    0xeab(%rip),%rdx
lea    0x80(%rsp),%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    0xec1(%rip),%rax
cmp    $0x2,%rbx
jae    119e <string_format_size+0x7e>
lea    0xe8a(%rip),%rcx
jmp    11ac <string_format_size+0x8c>
lea    0xe87(%rip),%rdx
movslq (%rdx,%r14,4),%rcx
add    %rdx,%rcx
movss  (%rax,%r14,4),%xmm0
test   %rbx,%rbx
js     11be <string_format_size+0x9e>
cvtsi2ss %rbx,%xmm1
jmp    11d3 <string_format_size+0xb3>
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
mov    %rsp,%rbx
lea    0x80(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strdup@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"bytes\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"
@__const.string_format_size.unit_name = [4 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 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)]
@.str.4 = [5 x i8] c"%.0f\00"
@.str.5 = [5 x i8] c"%.1f\00"
@.str.6 = [6 x i8] c"%.02f\00"
@__const.string_format_size.unit_format = [4 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i32 0, i32 0)]
@__const.string_format_size.unit_divide = [4 x float] [float 1.000000e+00, float 1.024000e+03, float 0x4130000000000000, float 0x41D0000000000000]
@.str.7 = [7 x i8] c"%s %%s\00"
@.str.8 = [5 x i8] c"byte\00"
define i8* @string_format_size(i64 %0) {
%2 = alloca i64
%3 = alloca [4 x i8*]
%4 = alloca [4 x i8*]
%5 = alloca [4 x float]
%6 = alloca [128 x i8]
%7 = alloca [128 x i8]
%8 = alloca i32
store i64 %0, i64* %2
%9 = bitcast [4 x i8*]* %3 to i8*
%10 = bitcast [4 x i8*]* %4 to i8*
%11 = bitcast [4 x float]* %5 to i8*
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
store i8 0, i8* %12
%13 = load i64, i64* %2
%14 = icmp ult i64 %13, 10240
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 0, i32* %8
br label %27
16: ; preds = %1
%17 = load i64, i64* %2
%18 = icmp ult i64 %17, 1048576
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 1, i32* %8
br label %26
20: ; preds = %16
%21 = load i64, i64* %2
%22 = icmp ult i64 %21, 1073741824
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 2, i32* %8
br label %25
24: ; preds = %20
store i32 3, i32* %8
br label %25
25: ; preds = %24, %23
br label %26
26: ; preds = %25, %19
br label %27
27: ; preds = %26, %15
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 %30
%32 = load i8*, i8** %31
%33 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %28, i32 128, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%36 = load i64, i64* %2
%37 = uitofp i64 %36 to float
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4 x float], [4 x float]* %5, i64 0, i64 %39
%41 = load float, float* %40
%42 = fdiv float %37, %41
%43 = fpext float %42 to double
%44 = load i64, i64* %2
%45 = icmp ule i64 %44, 1
br i1 %45, label %46, label %47
46: ; preds = %27
br label %52
47: ; preds = %27
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 %49
%51 = load i8*, i8** %50
br label %52
52: ; preds = %47, %46
%53 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), %46 ], [ %51, %47 ]
%54 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %34, i32 128, i8* %35, double %43, i8* %53)
%55 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%56 = call i8* @strdup(i8* %55)
ret i8* %56
}

LLVM IR (-O2):
@.str = [6 x i8] c"bytes\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"
@reltable.string_format_size = [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str to i64), i64 ptrtoint ([4 x i32]* @reltable.string_format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([4 x i32]* @reltable.string_format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.2 to i64), i64 ptrtoint ([4 x i32]* @reltable.string_format_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([4 x i32]* @reltable.string_format_size to i64)) to i32)]
@.str.4 = [5 x i8] c"%.0f\00"
@.str.5 = [5 x i8] c"%.1f\00"
@.str.6 = [6 x i8] c"%.02f\00"
@reltable.string_format_size.9 = [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.4 to i64), i64 ptrtoint ([4 x i32]* @reltable.string_format_size.9 to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.5 to i64), i64 ptrtoint ([4 x i32]* @reltable.string_format_size.9 to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str.6 to i64), i64 ptrtoint ([4 x i32]* @reltable.string_format_size.9 to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str.6 to i64), i64 ptrtoint ([4 x i32]* @reltable.string_format_size.9 to i64)) to i32)]
@__const.string_format_size.unit_divide = [4 x float] [float 1.000000e+00, float 1.024000e+03, float 0x4130000000000000, float 0x41D0000000000000]
@.str.7 = [7 x i8] c"%s %%s\00"
@.str.8 = [5 x i8] c"byte\00"
define noalias i8* @string_format_size(i64 %0) {
%2 = alloca [128 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i8 0, i8* %5
%6 = icmp ult i64 %0, 10240
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = icmp ult i64 %0, 1048576
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = icmp ult i64 %0, 1073741824
%11 = select i1 %10, i64 2, i64 3
br label %12
12: ; preds = %9, %7, %1
%13 = phi i64 [ 0, %1 ], [ 1, %7 ], [ %11, %9 ]
%14 = shl i64 %13, 2
%15 = call i8* @llvm.load.relative.i64(i8* bitcast ([4 x i32]* @reltable.string_format_size.9 to i8*), i64 %14)
%16 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [4 x float], [4 x float]* @__const.string_format_size.unit_divide, i64 0, i64 %13
%18 = load float, float* %17
%19 = icmp ult i64 %0, 2
br i1 %19, label %23, label %20
20: ; preds = %12
%21 = shl i64 %13, 2
%22 = call i8* @llvm.load.relative.i64(i8* bitcast ([4 x i32]* @reltable.string_format_size to i8*), i64 %21)
br label %23
23: ; preds = %12, %20
%24 = phi i8* [ %22, %20 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), %12 ]
%25 = uitofp i64 %0 to float
%26 = fdiv float %25, %18
%27 = fpext float %26 to double
%28 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 128, i8* %4, double %27, i8* %24)
%29 = call i8* @strdup(i8* %5)
ret i8* %29
}