#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*/ wchar_t;
typedef int /*<<< orphan*/ suffix;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ swprintf(int /*<<< orphan*/ *, int, char *, int, char, ...);
int /*<<< orphan*/ wcscpy(int /*<<< orphan*/ *, char *);
void filesize_to_string(wchar_t *buf, off_t filesize) {
  char suffix[] = {' ', 'K', 'M', 'G', 'T', 'P', 'E'};
  size_t cs = 0;
  off_t cv = filesize;
  while (cv > 1024 && cs < sizeof(suffix) - 1) {
    filesize = cv;
    cv /= 1024;
    cs++;
  }
  if (cv >= 10000)
    wcscpy(buf, L">9000");
  else if (cv < 100 && cs > 0)
    swprintf(buf, 6, L"%lu.%1lu%c", cv, (filesize - cv * 1024) * 10 / 1024,
             suffix[cs]);
  else
    swprintf(buf, 6, L"%lu%c", cv, suffix[cs]);
}


x86 assembly (clang, -O0):
<filesize_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xecb(%rip),%eax
mov    %eax,-0x13(%rbp)
mov    0xec5(%rip),%ax
mov    %ax,-0xf(%rbp)
mov    0xebd(%rip),%al
mov    %al,-0xd(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x400,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jle    1177 <filesize_to_string+0x57>
cmpq   $0x6,-0x20(%rbp)
setb   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1187 <filesize_to_string+0x67>
jmp    11ac <filesize_to_string+0x8c>
mov    -0x24(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    115a <filesize_to_string+0x3a>
cmpl   $0x2710,-0x24(%rbp)
jl     11ce <filesize_to_string+0xae>
mov    -0x8(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1030 <wcscpy@plt>
jmp    124f <filesize_to_string+0x12f>
cmpl   $0x64,-0x24(%rbp)
jge    1226 <filesize_to_string+0x106>
cmpq   $0x0,-0x20(%rbp)
jbe    1226 <filesize_to_string+0x106>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    -0xc(%rbp),%eax
mov    -0x24(%rbp),%edx
shl    $0xa,%edx
sub    %edx,%eax
imul   $0xa,%eax,%eax
mov    $0x400,%esi
cltd
idiv   %esi
mov    -0x20(%rbp),%rdx
movsbl -0x13(%rbp,%rdx,1),%r9d
mov    $0x6,%esi
lea    0xe0a(%rip),%rdx
movsbl %al,%r8d
mov    $0x0,%al
call   1040 <swprintf@plt>
jmp    124a <filesize_to_string+0x12a>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    $0x6,%esi
lea    0xe0f(%rip),%rdx
movsbl -0x13(%rbp,%rax,1),%r8d
mov    $0x0,%al
call   1040 <swprintf@plt>
jmp    124f <filesize_to_string+0x12f>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filesize_to_string>:
mov    %esi,%eax
cmp    $0x401,%esi
jl     116b <filesize_to_string+0x4b>
mov    %eax,%ecx
shr    $0xa,%ecx
mov    $0x1,%edx
cmp    $0x100400,%eax
jb     1158 <filesize_to_string+0x38>
mov    %eax,%esi
shr    $0x14,%esi
mov    $0x2,%edx
cmp    $0x40100000,%eax
jb     116f <filesize_to_string+0x4f>
mov    %eax,%ecx
shr    $0x1e,%ecx
mov    $0x3,%edx
mov    %esi,%eax
cmp    $0x9c4000,%eax
jb     117a <filesize_to_string+0x5a>
lea    0xea2(%rip),%rsi
jmp    1030 <wcscpy@plt>
xor    %edx,%edx
jmp    11c2 <filesize_to_string+0xa2>
mov    %ecx,%eax
mov    %esi,%ecx
cmp    $0x9c4000,%eax
jae    115f <filesize_to_string+0x3f>
cmp    $0x18fff,%eax
ja     11c0 <filesize_to_string+0xa0>
mov    %ecx,%esi
shl    $0xa,%esi
sub    %esi,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%esi
lea    (%rax,%rax,4),%eax
add    $0x3ff,%eax
test   %esi,%esi
cmovns %esi,%eax
shr    $0xa,%eax
lea    0xe5c(%rip),%rsi
movsbl (%rdx,%rsi,1),%r9d
movsbl %al,%r8d
lea    0xe6c(%rip),%rdx
mov    $0x6,%esi
xor    %eax,%eax
jmp    1040 <swprintf@plt>
mov    %ecx,%eax
lea    0xe37(%rip),%rcx
movsbl (%rdx,%rcx,1),%r8d
lea    0xe77(%rip),%rdx
mov    $0x6,%esi
mov    %eax,%ecx
xor    %eax,%eax
jmp    1040 <swprintf@plt>

LLVM IR (-O0):
@__const.filesize_to_string.suffix = [7 x i8] c" KMGTPE"
@.str = [6 x i32] [i32 62, i32 57, i32 48, i32 48, i32 48, i32 0]
@.str.1 = [11 x i32] [i32 37, i32 108, i32 117, i32 46, i32 37, i32 49, i32 108, i32 117, i32 37, i32 99, i32 0]
@.str.2 = [6 x i32] [i32 37, i32 108, i32 117, i32 37, i32 99, i32 0]
define void @filesize_to_string(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [7 x i8]
%6 = alloca i64
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = bitcast [7 x i8]* %5 to i8*
store i64 0, i64* %6
%9 = load i32, i32* %4
store i32 %9, i32* %7
br label %10
10: ; preds = %18, %2
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 1024
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i64, i64* %6
%15 = icmp ult i64 %14, 6
br label %16
16: ; preds = %13, %10
%17 = phi i1 [ false, %10 ], [ %15, %13 ]
br i1 %17, label %18, label %24
18: ; preds = %16
%19 = load i32, i32* %7
store i32 %19, i32* %4
%20 = load i32, i32* %7
%21 = sdiv i32 %20, 1024
store i32 %21, i32* %7
%22 = load i64, i64* %6
%23 = add i64 %22, 1
store i64 %23, i64* %6
br label %10
24: ; preds = %16
%25 = load i32, i32* %7
%26 = icmp sge i32 %25, 10000
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32*, i32** %3
%29 = call i32 @wcscpy(i32* %28, i8* bitcast ([6 x i32]* @.str to i8*))
br label %59
30: ; preds = %24
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 100
br i1 %32, label %33, label %51
33: ; preds = %30
%34 = load i64, i64* %6
%35 = icmp ugt i64 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %33
%37 = load i32*, i32** %3
%38 = load i32, i32* %7
%39 = load i32, i32* %4
%40 = load i32, i32* %7
%41 = mul nsw i32 %40, 1024
%42 = sub nsw i32 %39, %41
%43 = mul nsw i32 %42, 10
%44 = sdiv i32 %43, 1024
%45 = trunc i32 %44 to i8
%46 = load i64, i64* %6
%47 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = call i32 (i32*, i32, i8*, i32, i8, ...) @swprintf(i32* %37, i32 6, i8* bitcast ([11 x i32]* @.str.1 to i8*), i32 %38, i8 signext %45, i32 %49)
br label %58
51: ; preds = %33, %30
%52 = load i32*, i32** %3
%53 = load i32, i32* %7
%54 = load i64, i64* %6
%55 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = call i32 (i32*, i32, i8*, i32, i8, ...) @swprintf(i32* %52, i32 6, i8* bitcast ([6 x i32]* @.str.2 to i8*), i32 %53, i8 signext %56)
br label %58
58: ; preds = %51, %36
br label %59
59: ; preds = %58, %27
ret void
}

LLVM IR (-O2):
@__const.filesize_to_string.suffix = [7 x i8] c" KMGTPE"
@.str = [6 x i32] [i32 62, i32 57, i32 48, i32 48, i32 48, i32 0]
@.str.1 = [11 x i32] [i32 37, i32 108, i32 117, i32 46, i32 37, i32 49, i32 108, i32 117, i32 37, i32 99, i32 0]
@.str.2 = [6 x i32] [i32 37, i32 108, i32 117, i32 37, i32 99, i32 0]
define void @filesize_to_string(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1024
br i1 %3, label %4, label %31
4: ; preds = %2
%5 = lshr i32 %1, 10
%6 = icmp ugt i32 %1, 1049599
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = lshr i32 %1, 20
%9 = icmp ugt i32 %1, 1074790399
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = lshr i32 %1, 30
br label %12
12: ; preds = %10, %7, %4
%13 = phi i32 [ %1, %4 ], [ %5, %7 ], [ %8, %10 ]
%14 = phi i32 [ %5, %4 ], [ %8, %7 ], [ %11, %10 ]
%15 = phi i64 [ 1, %4 ], [ 2, %7 ], [ 3, %10 ]
%16 = icmp ugt i32 %13, 10239999
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @wcscpy(i32* %0, i8* bitcast ([6 x i32]* @.str to i8*))
br label %37
19: ; preds = %12
%20 = icmp ult i32 %13, 102400
br i1 %20, label %21, label %31
21: ; preds = %19
%22 = mul nsw i32 %14, -1024
%23 = add i32 %22, %13
%24 = mul nsw i32 %23, 10
%25 = sdiv i32 %24, 1024
%26 = trunc i32 %25 to i8
%27 = getelementptr inbounds [7 x i8], [7 x i8]* @__const.filesize_to_string.suffix, i64 0, i64 %15
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = tail call i32 (i32*, i32, i8*, i32, i8, ...) @swprintf(i32* %0, i32 6, i8* bitcast ([11 x i32]* @.str.1 to i8*), i32 %14, i8 signext %26, i32 %29)
br label %37
31: ; preds = %2, %19
%32 = phi i64 [ %15, %19 ], [ 0, %2 ]
%33 = phi i32 [ %14, %19 ], [ %1, %2 ]
%34 = getelementptr inbounds [7 x i8], [7 x i8]* @__const.filesize_to_string.suffix, i64 0, i64 %32
%35 = load i8, i8* %34
%36 = tail call i32 (i32*, i32, i8*, i32, i8, ...) @swprintf(i32* %0, i32 6, i8* bitcast ([6 x i32]* @.str.2 to i8*), i32 %33, i8 signext %35)
br label %37
37: ; preds = %21, %31, %17
ret void
}