#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*/ asprintf(char **, char *, long long);
char *bytes_to_string(long long bytes) {
  long div = 1;
  char *str, *fmt;
  if (bytes < 1024) {
    fmt = "%lldb";
  } else if (bytes < 1024 * 1024) {
    fmt = "%lldkb";
    div = 1024;
  } else if (bytes < 1024 * 1024 * 1024) {
    fmt = "%lldmb";
    div = 1024 * 1024;
  } else {
    fmt = "%lldgb";
    div = 1024 * 1024 * 1024;
  }
  asprintf(&str, fmt, bytes / div);
  return str;
}


x86 assembly (clang, -O0):
<bytes_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x1,-0x10(%rbp)
cmpq   $0x400,-0x8(%rbp)
jge    1142 <bytes_to_string+0x32>
lea    0xec7(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11ab <bytes_to_string+0x9b>
cmpq   $0x100000,-0x8(%rbp)
jge    1168 <bytes_to_string+0x58>
lea    0xeaf(%rip),%rax
mov    %rax,-0x20(%rbp)
movq   $0x400,-0x10(%rbp)
jmp    11a6 <bytes_to_string+0x96>
cmpq   $0x40000000,-0x8(%rbp)
40
jge    118e <bytes_to_string+0x7e>
lea    0xe90(%rip),%rax
mov    %rax,-0x20(%rbp)
movq   $0x100000,-0x10(%rbp)
jmp    11a1 <bytes_to_string+0x91>
lea    0xe7f(%rip),%rax
mov    %rax,-0x20(%rbp)
movq   $0x40000000,-0x10(%rbp)
40
jmp    11a6 <bytes_to_string+0x96>
jmp    11ab <bytes_to_string+0x9b>
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rax
cqto
idivq  -0x10(%rbp)
mov    %rax,%rdx
lea    -0x18(%rbp),%rdi
call   1030 <asprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes_to_string>:
mov    %rdi,%rax
cmp    $0x400,%rdi
jge    112a <bytes_to_string+0x1a>
lea    0xedd(%rip),%rsi
mov    $0x1,%ecx
jmp    1166 <bytes_to_string+0x56>
cmp    $0x100000,%rax
jae    1140 <bytes_to_string+0x30>
lea    0xecd(%rip),%rsi
mov    $0x400,%ecx
jmp    1166 <bytes_to_string+0x56>
cmp    $0x40000000,%rax
mov    $0x100000,%edx
mov    $0x40000000,%ecx
cmovb  %rdx,%rcx
lea    0xeb2(%rip),%rdx
lea    0xeb2(%rip),%rsi
cmovb  %rdx,%rsi
mov    %rax,%rdx
shr    $0x20,%rdx
je     1176 <bytes_to_string+0x66>
cqto
idiv   %rcx
jmp    117a <bytes_to_string+0x6a>
xor    %edx,%edx
div    %ecx
push   %rax
mov    %rsp,%rdi
mov    %rax,%rdx
call   1030 <asprintf@plt>
mov    (%rsp),%rax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%lldb\00"
@.str.1 = [7 x i8] c"%lldkb\00"
@.str.2 = [7 x i8] c"%lldmb\00"
@.str.3 = [7 x i8] c"%lldgb\00"
define i8* @bytes_to_string(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
store i64 %0, i64* %2
store i64 1, i64* %3
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 1024
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %5
br label %20
9: ; preds = %1
%10 = load i64, i64* %2
%11 = icmp slt i64 %10, 1048576
br i1 %11, label %12, label %13
12: ; preds = %9
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %5
store i64 1024, i64* %3
br label %19
13: ; preds = %9
%14 = load i64, i64* %2
%15 = icmp slt i64 %14, 1073741824
br i1 %15, label %16, label %17
16: ; preds = %13
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %5
store i64 1048576, i64* %3
br label %18
17: ; preds = %13
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %5
store i64 1073741824, i64* %3
br label %18
18: ; preds = %17, %16
br label %19
19: ; preds = %18, %12
br label %20
20: ; preds = %19, %8
%21 = load i8*, i8** %5
%22 = load i64, i64* %2
%23 = load i64, i64* %3
%24 = sdiv i64 %22, %23
%25 = call i32 @asprintf(i8** %4, i8* %21, i64 %24)
%26 = load i8*, i8** %4
ret i8* %26
}

LLVM IR (-O2):
@.str = [6 x i8] c"%lldb\00"
@.str.1 = [7 x i8] c"%lldkb\00"
@.str.2 = [7 x i8] c"%lldmb\00"
@.str.3 = [7 x i8] c"%lldgb\00"
define i8* @bytes_to_string(i64 %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = icmp slt i64 %0, 1024
br i1 %4, label %11, label %5
5: ; preds = %1
%6 = icmp ult i64 %0, 1048576
br i1 %6, label %11, label %7
7: ; preds = %5
%8 = icmp ult i64 %0, 1073741824
%9 = select i1 %8, i64 1048576, i64 1073741824
%10 = select i1 %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0)
br label %11
11: ; preds = %7, %5, %1
%12 = phi i64 [ 1, %1 ], [ 1024, %5 ], [ %9, %7 ]
%13 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %5 ], [ %10, %7 ]
%14 = sdiv i64 %0, %12
%15 = call i32 @asprintf(i8** %2, i8* %13, i64 %14)
%16 = load i8*, i8** %2
ret i8* %16
}