#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 *, ...);
char *rate2str(double rate) {
  char *buf;
  static char r2sbuf[8][16];
  static int idx = 0;
  buf = r2sbuf[idx++];
  if (idx == 8)
    idx = 0;
  if (rate == 0.0)
    snprintf(buf, 16, "0");
  else if (rate >= 1000000.0)
    snprintf(buf, 16, "%.2fM", rate / 1000000.0);
  else
    snprintf(buf, 16, "%.2fk", rate / 1000.0);
  return (buf);
}


x86 assembly (clang, -O0):
<rate2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    0x2f9d(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x2f92(%rip)
movslq %eax,%rcx
lea    0x2f08(%rip),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x8,0x2f76(%rip)
jne    115a <rate2str+0x4a>
movl   $0x0,0x2f66(%rip)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    118e <rate2str+0x7e>
jp     118e <rate2str+0x7e>
mov    -0x10(%rbp),%rdi
mov    $0x10,%esi
lea    0xe8e(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11ff <rate2str+0xef>
movsd  -0x8(%rbp),%xmm0
movsd  0xe65(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11d2 <rate2str+0xc2>
mov    -0x10(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
movsd  0xe4a(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0x10,%esi
lea    0xe4c(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    11fa <rate2str+0xea>
mov    -0x10(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
movsd  0xe25(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    $0x10,%esi
lea    0xe25(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    11ff <rate2str+0xef>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rate2str>:
push   %rbx
movslq 0x2fa8(%rip),%rax
lea    0x1(%rax),%ecx
shl    $0x4,%rax
lea    0x2f1a(%rip),%rbx
add    %rax,%rbx
xor    %eax,%eax
cmp    $0x8,%ecx
cmovne %ecx,%eax
mov    %eax,0x2f89(%rip)
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    115b <rate2str+0x4b>
jp     115b <rate2str+0x4b>
lea    0xec6(%rip),%rdx
mov    %rbx,%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    1194 <rate2str+0x84>
ucomisd 0xe9d(%rip),%xmm0
jae    1176 <rate2str+0x66>
divsd  0xe9b(%rip),%xmm0
lea    0xea4(%rip),%rdx
jmp    1185 <rate2str+0x75>
divsd  0xe82(%rip),%xmm0
lea    0xe8d(%rip),%rdx
mov    %rbx,%rdi
mov    $0x10,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@rate2str.r2sbuf = internal global [8 x [16 x i8]] zeroinitializer
@rate2str.idx = internal global i32 0
@.str = [2 x i8] c"0\00"
@.str.1 = [6 x i8] c"%.2fM\00"
@.str.2 = [6 x i8] c"%.2fk\00"
define i8* @rate2str(double %0) {
%2 = alloca double
%3 = alloca i8*
store double %0, double* %2
%4 = load i32, i32* @rate2str.idx
%5 = add nsw i32 %4, 1
store i32 %5, i32* @rate2str.idx
%6 = sext i32 %4 to i64
%7 = getelementptr inbounds [8 x [16 x i8]], [8 x [16 x i8]]* @rate2str.r2sbuf, i64 0, i64 %6
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
store i8* %8, i8** %3
%9 = load i32, i32* @rate2str.idx
%10 = icmp eq i32 %9, 8
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* @rate2str.idx
br label %12
12: ; preds = %11, %1
%13 = load double, double* %2
%14 = fcmp oeq double %13, 0.000000e+00
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %16, i32 16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %32
18: ; preds = %12
%19 = load double, double* %2
%20 = fcmp oge double %19, 1.000000e+06
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8*, i8** %3
%23 = load double, double* %2
%24 = fdiv double %23, 1.000000e+06
%25 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %22, i32 16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), double %24)
br label %31
26: ; preds = %18
%27 = load i8*, i8** %3
%28 = load double, double* %2
%29 = fdiv double %28, 1.000000e+03
%30 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %27, i32 16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %29)
br label %31
31: ; preds = %26, %21
br label %32
32: ; preds = %31, %15
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@rate2str.r2sbuf = internal global [8 x [16 x i8]] zeroinitializer
@rate2str.idx = internal unnamed_addr global i32 0
@.str = [2 x i8] c"0\00"
@.str.1 = [6 x i8] c"%.2fM\00"
@.str.2 = [6 x i8] c"%.2fk\00"
define i8* @rate2str(double %0) {
%2 = load i32, i32* @rate2str.idx
%3 = add nsw i32 %2, 1
%4 = sext i32 %2 to i64
%5 = getelementptr inbounds [8 x [16 x i8]], [8 x [16 x i8]]* @rate2str.r2sbuf, i64 0, i64 %4, i64 0
%6 = icmp eq i32 %3, 8
%7 = select i1 %6, i32 0, i32 %3
store i32 %7, i32* @rate2str.idx
%8 = fcmp oeq double %0, 0.000000e+00
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %19
11: ; preds = %1
%12 = fcmp ult double %0, 1.000000e+06
br i1 %12, label %16, label %13
13: ; preds = %11
%14 = fdiv double %0, 1.000000e+06
%15 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), double %14)
br label %19
16: ; preds = %11
%17 = fdiv double %0, 1.000000e+03
%18 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %17)
br label %19
19: ; preds = %13, %16, %9
ret i8* %5
}