#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 int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, size_t, char *, float, char const *);
void format_rate(char *dst, size_t size, uint64_t bytes, uint64_t seconds) {
  static const char *prefix[] = {"bps",  "Kbps", "Mbps", "Gbps", "Tbps",
                                 "Pbps", "Ebps", "Zbps", "Ybps"};
  const uint64_t rate = (bytes * 8) / seconds;
  unsigned int n = 0;
  uint64_t multiplier = 1;
  while (rate / multiplier >= 1024 && n < 8) {
    multiplier *= 1024;
    n++;
  }
  float tmp = (float)rate / multiplier;
  snprintf(dst, size, "%.1f %s", tmp, prefix[n]);
}


x86 assembly (clang, -O0):
<format_rate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x3,%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x24(%rbp)
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x400,%ecx
mov    %al,-0x29(%rbp)
jl     1165 <format_rate+0x55>
cmpl   $0x8,-0x20(%rbp)
setb   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1175 <format_rate+0x65>
jmp    118c <format_rate+0x7c>
mov    -0x24(%rbp),%eax
shl    $0xa,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1141 <format_rate+0x31>
cvtsi2ssl -0x1c(%rbp),%xmm0
cvtsi2ssl -0x24(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movss  -0x28(%rbp),%xmm0
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
lea    0x2e78(%rip),%rax
mov    (%rax,%rcx,8),%rcx
lea    0xe65(%rip),%rdx
call   1030 <snprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_rate>:
lea    0x0(,%rdx,8),%eax
cltd
idiv   %ecx
cmp    $0x400,%eax
jl     1144 <format_rate+0x34>
cmp    $0x100000,%eax
jl     1150 <format_rate+0x40>
xor    %ecx,%ecx
cmp    $0x40000000,%eax
setge  %cl
lea    0xecf(%rip),%rdx
movss  (%rdx,%rcx,4),%xmm1
or     $0x2,%rcx
jmp    115d <format_rate+0x4d>
movss  0xeb4(%rip),%xmm1
xor    %ecx,%ecx
jmp    115d <format_rate+0x4d>
mov    $0x1,%ecx
movss  0xea7(%rip),%xmm1
cvtsi2ss %eax,%xmm0
divss  %xmm1,%xmm0
lea    0xea4(%rip),%rax
movslq (%rax,%rcx,4),%rcx
add    %rax,%rcx
lea    0xee2(%rip),%rdx
jmp    1030 <snprintf@plt>

LLVM IR (-O0):
@format_rate.prefix = internal global [9 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i32 0, i32 0)]
@.str = [4 x i8] c"bps\00"
@.str.1 = [5 x i8] c"Kbps\00"
@.str.2 = [5 x i8] c"Mbps\00"
@.str.3 = [5 x i8] c"Gbps\00"
@.str.4 = [5 x i8] c"Tbps\00"
@.str.5 = [5 x i8] c"Pbps\00"
@.str.6 = [5 x i8] c"Ebps\00"
@.str.7 = [5 x i8] c"Zbps\00"
@.str.8 = [5 x i8] c"Ybps\00"
@.str.9 = [8 x i8] c"%.1f %s\00"
define void @format_rate(i8* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca float
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = load i32, i32* %7
%14 = mul nsw i32 %13, 8
%15 = load i32, i32* %8
%16 = sdiv i32 %14, %15
store i32 %16, i32* %9
store i32 0, i32* %10
store i32 1, i32* %11
br label %17
17: ; preds = %27, %4
%18 = load i32, i32* %9
%19 = load i32, i32* %11
%20 = sdiv i32 %18, %19
%21 = icmp sge i32 %20, 1024
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* %10
%24 = icmp ult i32 %23, 8
br label %25
25: ; preds = %22, %17
%26 = phi i1 [ false, %17 ], [ %24, %22 ]
br i1 %26, label %27, label %32
27: ; preds = %25
%28 = load i32, i32* %11
%29 = mul nsw i32 %28, 1024
store i32 %29, i32* %11
%30 = load i32, i32* %10
%31 = add i32 %30, 1
store i32 %31, i32* %10
br label %17
32: ; preds = %25
%33 = load i32, i32* %9
%34 = sitofp i32 %33 to float
%35 = load i32, i32* %11
%36 = sitofp i32 %35 to float
%37 = fdiv float %34, %36
store float %37, float* %12
%38 = load i8*, i8** %5
%39 = load i64, i64* %6
%40 = load float, float* %12
%41 = load i32, i32* %10
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds [9 x i8*], [9 x i8*]* @format_rate.prefix, i64 0, i64 %42
%44 = load i8*, i8** %43
%45 = call i32 @snprintf(i8* %38, i64 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), float %40, i8* %44)
ret void
}

LLVM IR (-O2):
@reltable.format_rate = internal unnamed_addr constant [9 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.1 to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.2 to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.3 to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.4 to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.5 to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.6 to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.7 to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.8 to i64), i64 ptrtoint ([9 x i32]* @reltable.format_rate to i64)) to i32)]
@.str = [4 x i8] c"bps\00"
@.str.1 = [5 x i8] c"Kbps\00"
@.str.2 = [5 x i8] c"Mbps\00"
@.str.3 = [5 x i8] c"Gbps\00"
@.str.4 = [5 x i8] c"Tbps\00"
@.str.5 = [5 x i8] c"Pbps\00"
@.str.6 = [5 x i8] c"Ebps\00"
@.str.7 = [5 x i8] c"Zbps\00"
@.str.8 = [5 x i8] c"Ybps\00"
@.str.9 = [8 x i8] c"%.1f %s\00"
define void @format_rate(i8* %0, i64 %1, i32 %2, i32 %3) {
%5 = shl nsw i32 %2, 3
%6 = sdiv i32 %5, %3
%7 = icmp sgt i32 %6, 1023
br i1 %7, label %8, label %14
8: ; preds = %4
%9 = icmp sgt i32 %6, 1048575
br i1 %9, label %10, label %14
10: ; preds = %8
%11 = icmp sgt i32 %6, 1073741823
%12 = select i1 %11, float 0x41D0000000000000, float 0x4130000000000000
%13 = select i1 %11, i64 3, i64 2
br label %14
14: ; preds = %10, %8, %4
%15 = phi float [ 1.000000e+00, %4 ], [ 1.024000e+03, %8 ], [ %12, %10 ]
%16 = phi i64 [ 0, %4 ], [ 1, %8 ], [ %13, %10 ]
%17 = sitofp i32 %6 to float
%18 = fdiv float %17, %15
%19 = shl i64 %16, 2
%20 = call i8* @llvm.load.relative.i64(i8* bitcast ([9 x i32]* @reltable.format_rate to i8*), i64 %19)
%21 = tail call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), float %18, i8* %20)
ret void
}