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

/* Variables and functions */
int /*<<< orphan*/ Com_sprintf(char *, int, char *, int);
int strlen(char *);
void UI_ReadableSize(char *buf, int bufsize, int value) {
  if (value > 1024 * 1024 * 1024) {
    Com_sprintf(buf, bufsize, "%d", value / (1024 * 1024 * 1024));
    Com_sprintf(buf + strlen(buf), bufsize - strlen(buf), ".%02d GB",
                (value % (1024 * 1024 * 1024)) * 100 / (1024 * 1024 * 1024));
  } else if (value > 1024 * 1024) {
    Com_sprintf(buf, bufsize, "%d", value / (1024 * 1024));
    Com_sprintf(buf + strlen(buf), bufsize - strlen(buf), ".%02d MB",
                (value % (1024 * 1024)) * 100 / (1024 * 1024));
  } else if (value > 1024) {
    Com_sprintf(buf, bufsize, "%d KB", value / 1024);
  } else {
    Com_sprintf(buf, bufsize, "%d bytes", value);
  }
}


x86 assembly (clang, -O0):
<UI_ReadableSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x40000000,-0x10(%rbp)
jle    11c1 <UI_ReadableSize+0xa1>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%eax
mov    $0x40000000,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xea6(%rip),%rdx
call   1040 <Com_sprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdi
sub    %eax,%esi
mov    -0x10(%rbp),%eax
mov    $0x40000000,%ecx
cltd
idiv   %ecx
imul   $0x64,%edx,%eax
mov    $0x40000000,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xe4c(%rip),%rdx
call   1040 <Com_sprintf@plt>
jmp    12a2 <UI_ReadableSize+0x182>
cmpl   $0x100000,-0x10(%rbp)
jle    1250 <UI_ReadableSize+0x130>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%eax
mov    $0x100000,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xe17(%rip),%rdx
call   1040 <Com_sprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rdi
sub    %eax,%esi
mov    -0x10(%rbp),%eax
mov    $0x100000,%ecx
cltd
idiv   %ecx
imul   $0x64,%edx,%eax
mov    $0x100000,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xdc6(%rip),%rdx
call   1040 <Com_sprintf@plt>
jmp    129d <UI_ReadableSize+0x17d>
cmpl   $0x400,-0x10(%rbp)
jle    1282 <UI_ReadableSize+0x162>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xd9d(%rip),%rdx
call   1040 <Com_sprintf@plt>
jmp    1298 <UI_ReadableSize+0x178>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%ecx
lea    0xd88(%rip),%rdx
call   1040 <Com_sprintf@plt>
jmp    129d <UI_ReadableSize+0x17d>
jmp    12a2 <UI_ReadableSize+0x182>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UI_ReadableSize>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
cmp    $0x40000001,%edx
jl     117c <UI_ReadableSize+0x5c>
lea    0xec2(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x1,%ecx
call   1040 <Com_sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%r15
add    %rbx,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
sub    %eax,%r14d
and    $0x3fffffff,%ebp
imul   $0x64,%ebp,%ecx
shr    $0x1e,%ecx
lea    0xe89(%rip),%rdx
jmp    11c7 <UI_ReadableSize+0xa7>
cmp    $0x100001,%ebp
jl     11cf <UI_ReadableSize+0xaf>
mov    %ebp,%ecx
shr    $0x14,%ecx
lea    0xe70(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <Com_sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%r15
add    %rbx,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
sub    %eax,%r14d
and    $0xfffff,%ebp
imul   $0x64,%ebp,%ecx
shr    $0x14,%ecx
lea    0xe45(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
jmp    11f2 <UI_ReadableSize+0xd2>
cmp    $0x401,%ebp
jl     11e3 <UI_ReadableSize+0xc3>
shr    $0xa,%ebp
lea    0xe34(%rip),%rdx
jmp    11ea <UI_ReadableSize+0xca>
lea    0xe31(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %ebp,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <Com_sprintf@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c".%02d GB\00"
@.str.2 = [9 x i8] c".%02d MB\00"
@.str.3 = [6 x i8] c"%d KB\00"
@.str.4 = [9 x i8] c"%d bytes\00"
define void @UI_ReadableSize(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
%8 = icmp sgt i32 %7, 1073741824
br i1 %8, label %9, label %29
9: ; preds = %3
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = load i32, i32* %6
%13 = sdiv i32 %12, 1073741824
%14 = call i32 @Com_sprintf(i8* %10, i32 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i8*, i8** %4
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = load i32, i32* %5
%21 = load i8*, i8** %4
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %20, %22
%24 = load i32, i32* %6
%25 = srem i32 %24, 1073741824
%26 = mul nsw i32 %25, 100
%27 = sdiv i32 %26, 1073741824
%28 = call i32 @Com_sprintf(i8* %19, i32 %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %68
29: ; preds = %3
%30 = load i32, i32* %6
%31 = icmp sgt i32 %30, 1048576
br i1 %31, label %32, label %52
32: ; preds = %29
%33 = load i8*, i8** %4
%34 = load i32, i32* %5
%35 = load i32, i32* %6
%36 = sdiv i32 %35, 1048576
%37 = call i32 @Com_sprintf(i8* %33, i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = load i8*, i8** %4
%39 = load i8*, i8** %4
%40 = call i32 @strlen(i8* %39)
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = load i32, i32* %5
%44 = load i8*, i8** %4
%45 = call i32 @strlen(i8* %44)
%46 = sub nsw i32 %43, %45
%47 = load i32, i32* %6
%48 = srem i32 %47, 1048576
%49 = mul nsw i32 %48, 100
%50 = sdiv i32 %49, 1048576
%51 = call i32 @Com_sprintf(i8* %42, i32 %46, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %50)
br label %67
52: ; preds = %29
%53 = load i32, i32* %6
%54 = icmp sgt i32 %53, 1024
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load i8*, i8** %4
%57 = load i32, i32* %5
%58 = load i32, i32* %6
%59 = sdiv i32 %58, 1024
%60 = call i32 @Com_sprintf(i8* %56, i32 %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %59)
br label %66
61: ; preds = %52
%62 = load i8*, i8** %4
%63 = load i32, i32* %5
%64 = load i32, i32* %6
%65 = call i32 @Com_sprintf(i8* %62, i32 %63, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %61, %55
br label %67
67: ; preds = %66, %32
br label %68
68: ; preds = %67, %9
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c".%02d GB\00"
@.str.2 = [9 x i8] c".%02d MB\00"
@.str.3 = [6 x i8] c"%d KB\00"
@.str.4 = [9 x i8] c"%d bytes\00"
define void @UI_ReadableSize(i8* %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %2, 1073741824
br i1 %4, label %5, label %16
5: ; preds = %3
%6 = tail call i32 @Com_sprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 1)
%7 = tail call i32 @strlen(i8* %0)
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = tail call i32 @strlen(i8* %0)
%11 = sub nsw i32 %1, %10
%12 = and i32 %2, 1073741823
%13 = mul nsw i32 %12, 100
%14 = lshr i32 %13, 30
%15 = tail call i32 @Com_sprintf(i8* %9, i32 %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %37
16: ; preds = %3
%17 = icmp sgt i32 %2, 1048576
br i1 %17, label %18, label %30
18: ; preds = %16
%19 = lshr i32 %2, 20
%20 = tail call i32 @Com_sprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = tail call i32 @strlen(i8* %0)
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = tail call i32 @strlen(i8* %0)
%25 = sub nsw i32 %1, %24
%26 = and i32 %2, 1048575
%27 = mul nuw nsw i32 %26, 100
%28 = lshr i32 %27, 20
%29 = tail call i32 @Com_sprintf(i8* %23, i32 %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %37
30: ; preds = %16
%31 = icmp sgt i32 %2, 1024
br i1 %31, label %32, label %35
32: ; preds = %30
%33 = lshr i32 %2, 10
%34 = tail call i32 @Com_sprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %37
35: ; preds = %30
%36 = tail call i32 @Com_sprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %2)
br label %37
37: ; preds = %18, %35, %32, %5
ret void
}