#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(char *, int, char *, unsigned long, char);
const char *format_kbytes(long unsigned val) {
  static char buf[64];
  char prefix;
  if (val / (1024 * 1024) > 10) {
    prefix = 'G';
    val = val / (1024 * 1024);
  } else if (val / 1024 > 10) {
    prefix = 'M';
    val /= 1024;
  } else {
    prefix = 'K';
  }
  snprintf(buf, sizeof buf, "%lu%c", val, prefix);
  return buf;
}


x86 assembly (clang, -O0):
<format_kbytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x14,%rax
cmp    $0xa,%rax
jbe    1143 <format_kbytes+0x33>
movb   $0x47,-0x9(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x14,%rax
mov    %rax,-0x8(%rbp)
jmp    1173 <format_kbytes+0x63>
mov    -0x8(%rbp),%rax
shr    $0xa,%rax
cmp    $0xa,%rax
jbe    116a <format_kbytes+0x5a>
movb   $0x4d,-0x9(%rbp)
mov    -0x8(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x8(%rbp)
jmp    116e <format_kbytes+0x5e>
movb   $0x4b,-0x9(%rbp)
jmp    1173 <format_kbytes+0x63>
mov    -0x8(%rbp),%rcx
lea    0x2ec2(%rip),%rdi
mov    $0x40,%esi
lea    0xe76(%rip),%rdx
movsbl -0x9(%rbp),%r8d
call   1030 <snprintf@plt>
lea    0x2ea5(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_kbytes>:
mov    %rdi,%rcx
cmp    $0xb00000,%rdi
jb     1124 <format_kbytes+0x14>
shr    $0x14,%rcx
mov    $0x47,%al
jmp    113d <format_kbytes+0x2d>
mov    %rcx,%rdx
shr    $0xa,%rdx
cmp    $0x2c00,%rcx
setae  %al
cmovae %rdx,%rcx
add    %al,%al
add    $0x4b,%al
push   %rbx
movzbl %al,%r8d
lea    0x2ef7(%rip),%rbx
lea    0xeb0(%rip),%rdx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@format_kbytes.buf = internal global [64 x i8] zeroinitializer
@.str = [6 x i8] c"%lu%c\00"
define i8* @format_kbytes(i64 %0) {
%2 = alloca i64
%3 = alloca i8
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = udiv i64 %4, 1048576
%6 = icmp ugt i64 %5, 10
br i1 %6, label %7, label %10
7: ; preds = %1
store i8 71, i8* %3
%8 = load i64, i64* %2
%9 = udiv i64 %8, 1048576
store i64 %9, i64* %2
br label %19
10: ; preds = %1
%11 = load i64, i64* %2
%12 = udiv i64 %11, 1024
%13 = icmp ugt i64 %12, 10
br i1 %13, label %14, label %17
14: ; preds = %10
store i8 77, i8* %3
%15 = load i64, i64* %2
%16 = udiv i64 %15, 1024
store i64 %16, i64* %2
br label %18
17: ; preds = %10
store i8 75, i8* %3
br label %18
18: ; preds = %17, %14
br label %19
19: ; preds = %18, %7
%20 = load i64, i64* %2
%21 = load i8, i8* %3
%22 = call i32 @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_kbytes.buf, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %20, i8 signext %21)
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_kbytes.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@format_kbytes.buf = internal global [64 x i8] zeroinitializer
@.str = [6 x i8] c"%lu%c\00"
define i8* @format_kbytes(i64 %0) {
%2 = icmp ugt i64 %0, 11534335
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = lshr i64 %0, 20
br label %10
5: ; preds = %1
%6 = icmp ugt i64 %0, 11263
%7 = lshr i64 %0, 10
%8 = select i1 %6, i64 %7, i64 %0
%9 = select i1 %6, i8 77, i8 75
br label %10
10: ; preds = %5, %3
%11 = phi i64 [ %4, %3 ], [ %8, %5 ]
%12 = phi i8 [ 71, %3 ], [ %9, %5 ]
%13 = tail call i32 @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_kbytes.buf, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %11, i8 signext %12)
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @format_kbytes.buf, i64 0, i64 0)
}