#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 double uint64_t;
typedef int /*<<< orphan*/ sfx;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, size_t, char *, double, char const *);
int rat_lib_bytes_to_str(char *buf, size_t buflen, uint64_t bytes) {
  unsigned int i = 0;
  double dbytes;
  const char const *sfx[] = {"Bytes", "KiB", "MiB", "GiB", "TiB",
                             "PiB",   "EiB", "ZiB", "YiB"};
  if (!buf || !buflen)
    goto exit_err;
  dbytes = bytes;
  while ((dbytes / 1024.0) >= 1024.0) {
    dbytes /= 1024.0;
    i++;
    if (i > 2 && (i - 2) >= sizeof(sfx) / sizeof(sfx[0]))
      break;
  }
  if (i)
    snprintf(buf, buflen, "%0.2f %s", dbytes, sfx[i]);
  else
    snprintf(buf, buflen, "%.0f %s", dbytes, sfx[i]);
  return 0;
exit_err:
  return -1;
}


x86 assembly (clang, -O0):
<rat_lib_bytes_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x80(%rbp),%rdi
lea    0x2c86(%rip),%rsi
mov    $0x48,%edx
call   1040 <memcpy@plt>
cmpq   $0x0,-0x10(%rbp)
je     116a <rat_lib_bytes_to_str+0x4a>
cmpq   $0x0,-0x18(%rbp)
jne    116f <rat_lib_bytes_to_str+0x4f>
jmp    123e <rat_lib_bytes_to_str+0x11e>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  0xe7a(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe6e(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11e1 <rat_lib_bytes_to_str+0xc1>
movsd  -0x30(%rbp),%xmm0
movsd  0xe57(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x2,-0x24(%rbp)
jbe    11dc <rat_lib_bytes_to_str+0xbc>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
mov    %eax,%eax
cmp    $0x9,%rax
jb     11dc <rat_lib_bytes_to_str+0xbc>
jmp    11e1 <rat_lib_bytes_to_str+0xc1>
jmp    1179 <rat_lib_bytes_to_str+0x59>
cmpl   $0x0,-0x24(%rbp)
je     1211 <rat_lib_bytes_to_str+0xf1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movsd  -0x30(%rbp),%xmm0
mov    -0x24(%rbp),%eax
mov    -0x80(%rbp,%rax,8),%rcx
lea    0xe27(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1232 <rat_lib_bytes_to_str+0x112>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movsd  -0x30(%rbp),%xmm0
mov    -0x24(%rbp),%eax
mov    -0x80(%rbp,%rax,8),%rcx
lea    0xe0a(%rip),%rdx
call   1030 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <rat_lib_bytes_to_str+0x125>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rat_lib_bytes_to_str>:
push   %rax
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     12c3 <rat_lib_bytes_to_str+0x1b3>
test   %rsi,%rsi
je     12c3 <rat_lib_bytes_to_str+0x1b3>
movsd  0xed0(%rip),%xmm1
mulsd  %xmm0,%xmm1
ucomisd 0xecc(%rip),%xmm1
jae    1151 <rat_lib_bytes_to_str+0x41>
lea    0xefa(%rip),%rdx
lea    0xec4(%rip),%rcx
jmp    12bc <rat_lib_bytes_to_str+0x1ac>
movsd  0xea7(%rip),%xmm0
mulsd  %xmm1,%xmm0
mov    $0x1,%eax
ucomisd 0xe9e(%rip),%xmm0
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xe88(%rip),%xmm2
mulsd  %xmm0,%xmm2
mov    $0x2,%eax
ucomisd 0xe7f(%rip),%xmm2
movapd %xmm0,%xmm1
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xe65(%rip),%xmm0
mulsd  %xmm2,%xmm0
mov    $0x3,%eax
ucomisd 0xe5c(%rip),%xmm0
movapd %xmm2,%xmm1
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xe42(%rip),%xmm2
mulsd  %xmm0,%xmm2
mov    $0x4,%eax
ucomisd 0xe39(%rip),%xmm2
movapd %xmm0,%xmm1
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xe1f(%rip),%xmm0
mulsd  %xmm2,%xmm0
mov    $0x5,%eax
ucomisd 0xe16(%rip),%xmm0
movapd %xmm2,%xmm1
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xdfc(%rip),%xmm2
mulsd  %xmm0,%xmm2
mov    $0x6,%eax
ucomisd 0xdf3(%rip),%xmm2
movapd %xmm0,%xmm1
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xdd9(%rip),%xmm0
mulsd  %xmm2,%xmm0
mov    $0x7,%eax
ucomisd 0xdd0(%rip),%xmm0
movapd %xmm2,%xmm1
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xdba(%rip),%xmm3
mulsd  %xmm0,%xmm3
mov    $0x8,%eax
ucomisd 0xdb1(%rip),%xmm3
movapd %xmm0,%xmm1
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xd9b(%rip),%xmm2
mulsd  %xmm3,%xmm2
mov    $0x9,%eax
ucomisd 0xd92(%rip),%xmm2
movapd %xmm3,%xmm1
jb     12a0 <rat_lib_bytes_to_str+0x190>
movsd  0xd7c(%rip),%xmm0
mulsd  %xmm2,%xmm0
mov    $0xb,%ecx
mov    $0xa,%eax
ucomisd 0xd6e(%rip),%xmm0
movapd %xmm2,%xmm1
jae    12a7 <rat_lib_bytes_to_str+0x197>
movapd %xmm1,%xmm0
mov    %rax,%rcx
lea    0xd9a(%rip),%rax
movslq (%rax,%rcx,4),%rcx
add    %rax,%rcx
lea    0xd7a(%rip),%rdx
call   1030 <snprintf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"Bytes\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
@.str.3 = [4 x i8] c"GiB\00"
@.str.4 = [4 x i8] c"TiB\00"
@.str.5 = [4 x i8] c"PiB\00"
@.str.6 = [4 x i8] c"EiB\00"
@.str.7 = [4 x i8] c"ZiB\00"
@.str.8 = [4 x i8] c"YiB\00"
@__const.rat_lib_bytes_to_str.sfx = [9 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i32 0, i32 0)]
@.str.9 = [9 x i8] c"%0.2f %s\00"
@.str.10 = [8 x i8] c"%.0f %s\00"
define i32 @rat_lib_bytes_to_str(i8* %0, i64 %1, double %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca double
%8 = alloca i32
%9 = alloca double
%10 = alloca [9 x i8*]
store i8* %0, i8** %5
store i64 %1, i64* %6
store double %2, double* %7
store i32 0, i32* %8
%11 = bitcast [9 x i8*]* %10 to i8*
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i64, i64* %6
%16 = icmp ne i64 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %14, %3
br label %60
18: ; preds = %14
%19 = load double, double* %7
store double %19, double* %9
br label %20
20: ; preds = %37, %18
%21 = load double, double* %9
%22 = fdiv double %21, 1.024000e+03
%23 = fcmp oge double %22, 1.024000e+03
br i1 %23, label %24, label %38
24: ; preds = %20
%25 = load double, double* %9
%26 = fdiv double %25, 1.024000e+03
store double %26, double* %9
%27 = load i32, i32* %8
%28 = add i32 %27, 1
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp ugt i32 %29, 2
br i1 %30, label %31, label %37
31: ; preds = %24
%32 = load i32, i32* %8
%33 = sub i32 %32, 2
%34 = zext i32 %33 to i64
%35 = icmp uge i64 %34, 9
br i1 %35, label %36, label %37
36: ; preds = %31
br label %38
37: ; preds = %31, %24
br label %20
38: ; preds = %36, %20
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %38
%42 = load i8*, i8** %5
%43 = load i64, i64* %6
%44 = load double, double* %9
%45 = load i32, i32* %8
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds [9 x i8*], [9 x i8*]* %10, i64 0, i64 %46
%48 = load i8*, i8** %47
%49 = call i32 @snprintf(i8* %42, i64 %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), double %44, i8* %48)
br label %59
50: ; preds = %38
%51 = load i8*, i8** %5
%52 = load i64, i64* %6
%53 = load double, double* %9
%54 = load i32, i32* %8
%55 = zext i32 %54 to i64
%56 = getelementptr inbounds [9 x i8*], [9 x i8*]* %10, i64 0, i64 %55
%57 = load i8*, i8** %56
%58 = call i32 @snprintf(i8* %51, i64 %52, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), double %53, i8* %57)
br label %59
59: ; preds = %50, %41
store i32 0, i32* %4
br label %61
60: ; preds = %17
store i32 -1, i32* %4
br label %61
61: ; preds = %60, %59
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
@.str = [6 x i8] c"Bytes\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
@.str.3 = [4 x i8] c"GiB\00"
@.str.4 = [4 x i8] c"TiB\00"
@.str.5 = [4 x i8] c"PiB\00"
@.str.6 = [4 x i8] c"EiB\00"
@.str.7 = [4 x i8] c"ZiB\00"
@.str.8 = [4 x i8] c"YiB\00"
@reltable.rat_lib_bytes_to_str = [9 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([6 x i8]* @.str to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.2 to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.3 to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.4 to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.5 to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.6 to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.7 to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.8 to i64), i64 ptrtoint ([9 x i32]* @reltable.rat_lib_bytes_to_str to i64)) to i32)]
@.str.9 = [9 x i8] c"%0.2f %s\00"
@.str.10 = [8 x i8] c"%.0f %s\00"
define i32 @rat_lib_bytes_to_str(i8* %0, i64 %1, double %2) {
%4 = icmp ne i8* %0, null
%5 = icmp ne i64 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %51
7: ; preds = %3
%8 = fmul double %2, 0x3F50000000000000
%9 = fcmp ult double %8, 1.024000e+03
br i1 %9, label %49, label %10
10: ; preds = %7
%11 = fmul double %8, 0x3F50000000000000
%12 = fcmp ult double %11, 1.024000e+03
br i1 %12, label %40, label %13
13: ; preds = %10
%14 = fmul double %11, 0x3F50000000000000
%15 = fcmp ult double %14, 1.024000e+03
br i1 %15, label %40, label %16
16: ; preds = %13
%17 = fmul double %14, 0x3F50000000000000
%18 = fcmp ult double %17, 1.024000e+03
br i1 %18, label %40, label %19
19: ; preds = %16
%20 = fmul double %17, 0x3F50000000000000
%21 = fcmp ult double %20, 1.024000e+03
br i1 %21, label %40, label %22
22: ; preds = %19
%23 = fmul double %20, 0x3F50000000000000
%24 = fcmp ult double %23, 1.024000e+03
br i1 %24, label %40, label %25
25: ; preds = %22
%26 = fmul double %23, 0x3F50000000000000
%27 = fcmp ult double %26, 1.024000e+03
br i1 %27, label %40, label %28
28: ; preds = %25
%29 = fmul double %26, 0x3F50000000000000
%30 = fcmp ult double %29, 1.024000e+03
br i1 %30, label %40, label %31
31: ; preds = %28
%32 = fmul double %29, 0x3F50000000000000
%33 = fcmp ult double %32, 1.024000e+03
br i1 %33, label %40, label %34
34: ; preds = %31
%35 = fmul double %32, 0x3F50000000000000
%36 = fcmp ult double %35, 1.024000e+03
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = fmul double %35, 0x3F50000000000000
%39 = fcmp ult double %38, 1.024000e+03
br i1 %39, label %40, label %43
40: ; preds = %37, %34, %31, %28, %25, %22, %19, %16, %13, %10
%41 = phi i64 [ 1, %10 ], [ 2, %13 ], [ 3, %16 ], [ 4, %19 ], [ 5, %22 ], [ 6, %25 ], [ 7, %28 ], [ 8, %31 ], [ 9, %34 ], [ 10, %37 ]
%42 = phi double [ %8, %10 ], [ %11, %13 ], [ %14, %16 ], [ %17, %19 ], [ %20, %22 ], [ %23, %25 ], [ %26, %28 ], [ %29, %31 ], [ %32, %34 ], [ %35, %37 ]
br label %43
43: ; preds = %40, %37
%44 = phi double [ %38, %37 ], [ %42, %40 ]
%45 = phi i64 [ 11, %37 ], [ %41, %40 ]
%46 = shl i64 %45, 2
%47 = call i8* @llvm.load.relative.i64(i8* bitcast ([9 x i32]* @reltable.rat_lib_bytes_to_str to i8*), i64 %46)
%48 = tail call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), double %44, i8* %47)
br label %51
49: ; preds = %7
%50 = tail call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), double %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %51
51: ; preds = %3, %43, %49
%52 = phi i32 [ 0, %49 ], [ 0, %43 ], [ -1, %3 ]
ret i32 %52
}