#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 *, long, char const *);
const char *bytes_to_str(char *buf, int buflen, long bits) {
  const char *sizstr;
  int div;
  if (bits < 1024) {
    div = 1;
    sizstr = "";
  } else if (bits < 1024 * 1024) {
    div = 1024;
    sizstr = "K";
  } else if (bits < 1024 * 1024 * 1024) {
    div = 1024 * 1024;
    sizstr = "M";
  } else {
    div = 1024 * 1024 * 1024;
    sizstr = "G";
  }
  snprintf(buf, buflen, "%ld %sB", bits / div, sizstr);
  return buf;
}


x86 assembly (clang, -O0):
<bytes_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x400,-0x18(%rbp)
jge    1148 <bytes_to_str+0x38>
movl   $0x1,-0x24(%rbp)
lea    0xece(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11ae <bytes_to_str+0x9e>
cmpq   $0x100000,-0x18(%rbp)
jge    116d <bytes_to_str+0x5d>
movl   $0x400,-0x24(%rbp)
lea    0xe9c(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11a9 <bytes_to_str+0x99>
cmpq   $0x40000000,-0x18(%rbp)
40
jge    1192 <bytes_to_str+0x82>
movl   $0x100000,-0x24(%rbp)
lea    0xe79(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11a4 <bytes_to_str+0x94>
movl   $0x40000000,-0x24(%rbp)
lea    0xe64(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11a9 <bytes_to_str+0x99>
jmp    11ae <bytes_to_str+0x9e>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x20(%rbp),%r8
lea    0xe36(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes_to_str>:
push   %rbx
mov    %rdx,%rax
mov    %rdi,%rbx
cmp    $0x400,%rdx
jge    112e <bytes_to_str+0x1e>
mov    $0x1,%ecx
lea    0xee1(%rip),%r8
jmp    116a <bytes_to_str+0x5a>
cmp    $0x100000,%rax
jae    1144 <bytes_to_str+0x34>
mov    $0x400,%ecx
lea    0xebe(%rip),%r8
jmp    116a <bytes_to_str+0x5a>
cmp    $0x40000000,%rax
lea    0xeb1(%rip),%rcx
lea    0xeac(%rip),%r8
cmovb  %rcx,%r8
mov    $0x100000,%edx
mov    $0x40000000,%ecx
cmovb  %rdx,%rcx
mov    %rax,%rdx
shr    $0x20,%rdx
je     117a <bytes_to_str+0x6a>
cqto
idiv   %rcx
jmp    117e <bytes_to_str+0x6e>
xor    %edx,%edx
div    %ecx
lea    0xe81(%rip),%rdx
mov    %rbx,%rdi
mov    %rax,%rcx
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"K\00"
@.str.2 = [2 x i8] c"M\00"
@.str.3 = [2 x i8] c"G\00"
@.str.4 = [8 x i8] c"%ld %sB\00"
define i8* @bytes_to_str(i8* returned %0, i32 %1, i64 %2) {
%4 = icmp slt i64 %2, 1024
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = icmp ult i64 %2, 1048576
br i1 %6, label %11, label %7
7: ; preds = %5
%8 = icmp ult i64 %2, 1073741824
%9 = select i1 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0)
%10 = select i1 %8, i64 1048576, i64 1073741824
br label %11
11: ; preds = %7, %5, %3
%12 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %5 ], [ %9, %7 ]
%13 = phi i64 [ 1, %3 ], [ 1024, %5 ], [ %10, %7 ]
%14 = sdiv i64 %2, %13
%15 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i64 %14, i8* %12)
ret i8* %0
}