#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 */
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, float, char);
char *byteconvert(unsigned long b_) {
  float b = (float)b_;
  int n = 0;
  while (b / 1000 >= 1) {
    b /= 1000;
    n++;
  }
  char *buffer = malloc(32);
  sprintf(buffer, "%.2f%c", b, "BKMGT"[n]);
  return buffer;
}


x86 assembly (clang, -O0):
<byteconvert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x20(%rbp)
test   %rax,%rax
movss  %xmm0,-0x1c(%rbp)
js     1169 <byteconvert+0x49>
movss  -0x20(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movss  -0xc(%rbp),%xmm0
movss  0xe7d(%rip),%xmm1
divss  %xmm1,%xmm0
movss  0xe6d(%rip),%xmm1
ucomiss %xmm1,%xmm0
jb     11c0 <byteconvert+0xa0>
movss  -0xc(%rbp),%xmm0
movss  0xe5b(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    117a <byteconvert+0x5a>
mov    $0x20,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
movss  -0xc(%rbp),%xmm0
movslq -0x10(%rbp),%rcx
lea    0xe26(%rip),%rsi
lea    0xe26(%rip),%rax
movsbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<byteconvert>:
test   %rdi,%rdi
js     112c <byteconvert+0xc>
cvtsi2ss %rdi,%xmm0
jmp    1141 <byteconvert+0x21>
mov    %rdi,%rax
shr    %rax
and    $0x1,%edi
or     %rax,%rdi
cvtsi2ss %rdi,%xmm0
addss  %xmm0,%xmm0
push   %r14
push   %rbx
push   %rax
lea    0xec2(%rip),%rbx
movss  0xeac(%rip),%xmm1
movss  0xea8(%rip),%xmm2
nopl   0x0(%rax)
movaps %xmm0,%xmm3
divss  %xmm1,%xmm0
add    $0x1,%rbx
ucomiss %xmm2,%xmm0
jae    1160 <byteconvert+0x40>
mov    $0x20,%edi
movss  %xmm3,0x4(%rsp)
call   1030 <malloc@plt>
mov    %rax,%r14
movsbl (%rbx),%edx
lea    0xe7b(%rip),%rsi
mov    %rax,%rdi
movss  0x4(%rsp),%xmm0
call   1040 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%.2f%c\00"
@.str.1 = [6 x i8] c"BKMGT\00"
define i8* @byteconvert(i64 %0) {
%2 = alloca i64
%3 = alloca float
%4 = alloca i32
%5 = alloca i8*
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = uitofp i64 %6 to float
store float %7, float* %3
store i32 0, i32* %4
br label %8
8: ; preds = %12, %1
%9 = load float, float* %3
%10 = fdiv float %9, 1.000000e+03
%11 = fcmp oge float %10, 1.000000e+00
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load float, float* %3
%14 = fdiv float %13, 1.000000e+03
store float %14, float* %3
%15 = load i32, i32* %4
%16 = add nsw i32 %15, 1
store i32 %16, i32* %4
br label %8
17: ; preds = %8
%18 = call i8* @malloc(i32 32)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = load float, float* %3
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [6 x i8], [6 x i8]* @.str.1, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), float %20, i8 signext %24)
%26 = load i8*, i8** %5
ret i8* %26
}

LLVM IR (-O2):
@.str = [7 x i8] c"%.2f%c\00"
@.str.1 = [6 x i8] c"BKMGT\00"
define noalias i8* @byteconvert(i64 %0) {
%2 = uitofp i64 %0 to float
br label %3
3: ; preds = %3, %1
%4 = phi i32 [ 0, %1 ], [ %8, %3 ]
%5 = phi float [ %2, %1 ], [ %6, %3 ]
%6 = fdiv float %5, 1.000000e+03
%7 = fcmp ult float %6, 1.000000e+00
%8 = add nuw nsw i32 %4, 1
br i1 %7, label %9, label %3
9: ; preds = %3
%10 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%11 = zext i32 %4 to i64
%12 = getelementptr inbounds [6 x i8], [6 x i8]* @.str.1, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), float %5, i8 signext %13)
ret i8* %10
}