#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*/ snprintf(unsigned char *, size_t, char *, ...);
unsigned char *num_to_size_str(unsigned char *buf, size_t buf_size, int num) {
  if (!num)
    snprintf(buf, buf_size, "0");
  else if (!(num % (1024 * 1024 * 1024)))
    snprintf(buf, buf_size, "%uG", num / (1024 * 1024 * 1024));
  else if (!(num % (1024 * 1024)))
    snprintf(buf, buf_size, "%uM", num / (1024 * 1024));
  else if (!(num % (1024)))
    snprintf(buf, buf_size, "%uK", num / (1024));
  else
    snprintf(buf, buf_size, "%u", num);
  return buf;
}


x86 assembly (clang, -O0):
<num_to_size_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1148 <num_to_size_str+0x38>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    0xec4(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    1224 <num_to_size_str+0x114>
mov    -0x14(%rbp),%eax
mov    $0x40000000,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1184 <num_to_size_str+0x74>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    $0x40000000,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xe8a(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    121f <num_to_size_str+0x10f>
mov    -0x14(%rbp),%eax
mov    $0x100000,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11c0 <num_to_size_str+0xb0>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    $0x100000,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xe52(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    121a <num_to_size_str+0x10a>
mov    -0x14(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11fc <num_to_size_str+0xec>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xe1a(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    1215 <num_to_size_str+0x105>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%ecx
lea    0xe00(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    121a <num_to_size_str+0x10a>
jmp    121f <num_to_size_str+0x10f>
jmp    1224 <num_to_size_str+0x114>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_to_size_str>:
push   %rbx
mov    %rdi,%rbx
test   %edx,%edx
je     113e <num_to_size_str+0x2e>
mov    %edx,%eax
test   $0x3fffffff,%edx
je     1151 <num_to_size_str+0x41>
test   $0xfffff,%eax
je     1168 <num_to_size_str+0x58>
test   $0x3ff,%eax
je     117f <num_to_size_str+0x6f>
lea    0xed7(%rip),%rdx
mov    %rbx,%rdi
mov    %eax,%ecx
jmp    1197 <num_to_size_str+0x87>
lea    0xebb(%rip),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    119e <num_to_size_str+0x8e>
lea    0x3fffffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x1e,%ecx
lea    0xe9c(%rip),%rdx
jmp    1194 <num_to_size_str+0x84>
lea    0xfffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x14,%ecx
lea    0xe89(%rip),%rdx
jmp    1194 <num_to_size_str+0x84>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0xa,%ecx
lea    0xe76(%rip),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@.str.1 = [4 x i8] c"%uG\00"
@.str.2 = [4 x i8] c"%uM\00"
@.str.3 = [4 x i8] c"%uK\00"
@.str.4 = [3 x i8] c"%u\00"
define i8* @num_to_size_str(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
%8 = icmp ne i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = load i8*, i8** %4
%11 = load i64, i64* %5
%12 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %10, i64 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %51
13: ; preds = %3
%14 = load i32, i32* %6
%15 = srem i32 %14, 1073741824
%16 = icmp ne i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i64, i64* %5
%20 = load i32, i32* %6
%21 = sdiv i32 %20, 1073741824
%22 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %18, i64 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %50
23: ; preds = %13
%24 = load i32, i32* %6
%25 = srem i32 %24, 1048576
%26 = icmp ne i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %23
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = load i32, i32* %6
%31 = sdiv i32 %30, 1048576
%32 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %28, i64 %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %49
33: ; preds = %23
%34 = load i32, i32* %6
%35 = srem i32 %34, 1024
%36 = icmp ne i32 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %33
%38 = load i8*, i8** %4
%39 = load i64, i64* %5
%40 = load i32, i32* %6
%41 = sdiv i32 %40, 1024
%42 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %38, i64 %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %41)
br label %48
43: ; preds = %33
%44 = load i8*, i8** %4
%45 = load i64, i64* %5
%46 = load i32, i32* %6
%47 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %44, i64 %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %43, %37
br label %49
49: ; preds = %48, %27
br label %50
50: ; preds = %49, %17
br label %51
51: ; preds = %50, %9
%52 = load i8*, i8** %4
ret i8* %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
@.str.1 = [4 x i8] c"%uG\00"
@.str.2 = [4 x i8] c"%uM\00"
@.str.3 = [4 x i8] c"%uK\00"
@.str.4 = [3 x i8] c"%u\00"
define i8* @num_to_size_str(i8* returned writeonly %0, i64 %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %27
7: ; preds = %3
%8 = and i32 %2, 1073741823
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = sdiv i32 %2, 1073741824
%12 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %27
13: ; preds = %7
%14 = and i32 %2, 1048575
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = sdiv i32 %2, 1048576
%18 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %27
19: ; preds = %13
%20 = and i32 %2, 1023
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = sdiv i32 %2, 1024
%24 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %27
25: ; preds = %19
%26 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %2)
br label %27
27: ; preds = %10, %22, %25, %16, %5
ret i8* %0
}