#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 uint64_t;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int);
void size2str(uint64_t size, char *buf, int bufsize) {
  uint64_t i, j;
  if ((i = (size / 1024)) < 1024) {
    (void)snprintf(buf, bufsize, "%luK", i);
  } else if ((j = i / 1024) < 1024) {
    (void)snprintf(buf, bufsize, "%luM", j);
  } else {
    (void)snprintf(buf, bufsize, "%luG", j / 1024);
  }
}


x86 assembly (clang, -O0):
<size2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
cmp    $0x400,%eax
jge    1156 <size2str+0x46>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%ecx
lea    0xeb4(%rip),%rdx
call   1030 <snprintf@plt>
jmp    11af <size2str+0x9f>
mov    -0x18(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
cmp    $0x400,%eax
jge    118a <size2str+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%ecx
lea    0xe85(%rip),%rdx
call   1030 <snprintf@plt>
jmp    11aa <size2str+0x9a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
lea    0xe65(%rip),%rdx
call   1030 <snprintf@plt>
jmp    11af <size2str+0x9f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<size2str>:
mov    %edi,%eax
cmp    $0xfffff,%edi
jg     113c <size2str+0x2c>
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0xa,%ecx
lea    0xed1(%rip),%rax
mov    %rsi,%rdi
mov    %edx,%esi
mov    %rax,%rdx
jmp    1030 <snprintf@plt>
cmp    $0x3fffffff,%eax
ja     115c <size2str+0x4c>
shr    $0x14,%eax
lea    0xeb8(%rip),%rcx
mov    %rsi,%rdi
mov    %edx,%esi
mov    %rcx,%rdx
mov    %eax,%ecx
jmp    1030 <snprintf@plt>
lea    0xea7(%rip),%rax
mov    %rsi,%rdi
mov    %edx,%esi
mov    %rax,%rdx
mov    $0x1,%ecx
jmp    1030 <snprintf@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%luK\00"
@.str.1 = [5 x i8] c"%luM\00"
@.str.2 = [5 x i8] c"%luG\00"
define void @size2str(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = sdiv i32 %9, 1024
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 1024
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %33
17: ; preds = %3
%18 = load i32, i32* %7
%19 = sdiv i32 %18, 1024
store i32 %19, i32* %8
%20 = icmp slt i32 %19, 1024
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = load i32, i32* %8
%25 = call i32 @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %32
26: ; preds = %17
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = load i32, i32* %8
%30 = sdiv i32 %29, 1024
%31 = call i32 @snprintf(i8* %27, i32 %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %26, %21
br label %33
33: ; preds = %32, %12
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%luK\00"
@.str.1 = [5 x i8] c"%luM\00"
@.str.2 = [5 x i8] c"%luG\00"
define void @size2str(i32 %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %0, 1048576
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = sdiv i32 %0, 1024
%7 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %15
8: ; preds = %3
%9 = icmp ult i32 %0, 1073741824
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = lshr i32 %0, 20
%12 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %15
13: ; preds = %8
%14 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %15
15: ; preds = %10, %13, %5
ret void
}