#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*/ sprintf(char *, char *, double);
char *rate(long bytes, long msecs) {
  static char res[15];
  double dlrate;
  if (!msecs)
    ++msecs;
  dlrate = (double)1000 * bytes / msecs;
  if (dlrate < 1024.0)
    sprintf(res, "%.2f B/s", dlrate);
  else if (dlrate < 1024.0 * 1024.0)
    sprintf(res, "%.2f KB/s", dlrate / 1024.0);
  else
    sprintf(res, "%.2f MB/s", dlrate / (1024.0 * 1024.0));
  return res;
}


x86 assembly (clang, -O0):
<rate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1137 <rate+0x27>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
cvtsi2sdq -0x8(%rbp),%xmm1
movsd  0xec3(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsi2sdq -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xea0(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    1188 <rate+0x78>
movsd  -0x18(%rbp),%xmm0
lea    0x2eb2(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11ed <rate+0xdd>
movsd  0xe80(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    11c4 <rate+0xb4>
movsd  -0x18(%rbp),%xmm0
movsd  0xe58(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0x2e76(%rip),%rdi
lea    0xe67(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11e8 <rate+0xd8>
movsd  -0x18(%rbp),%xmm0
movsd  0xe3f(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0x2e4d(%rip),%rdi
lea    0xe48(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11ed <rate+0xdd>
lea    0x2e35(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rate>:
push   %rax
test   %rsi,%rsi
mov    $0x1,%eax
cmovne %rsi,%rax
cvtsi2sd %rdi,%xmm0
mulsd  0xed6(%rip),%xmm0
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
movsd  0xecd(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1151 <rate+0x41>
lea    0x2ee1(%rip),%rdi
lea    0xed9(%rip),%rsi
jmp    118d <rate+0x7d>
movsd  0xeb7(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1177 <rate+0x67>
mulsd  0xeb9(%rip),%xmm0
lea    0x2ebb(%rip),%rdi
lea    0xebc(%rip),%rsi
jmp    118d <rate+0x7d>
mulsd  0xe99(%rip),%xmm0
lea    0x2ea3(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1030 <sprintf@plt>
lea    0x2e90(%rip),%rax
pop    %rcx
ret

LLVM IR (-O0):
@rate.res = internal global [15 x i8] zeroinitializer
@.str = [9 x i8] c"%.2f B/s\00"
@.str.1 = [10 x i8] c"%.2f KB/s\00"
@.str.2 = [10 x i8] c"%.2f MB/s\00"
define i8* @rate(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca double
store i64 %0, i64* %3
store i64 %1, i64* %4
%6 = load i64, i64* %4
%7 = icmp ne i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i64, i64* %4
%10 = add nsw i64 %9, 1
store i64 %10, i64* %4
br label %11
11: ; preds = %8, %2
%12 = load i64, i64* %3
%13 = sitofp i64 %12 to double
%14 = fmul double 1.000000e+03, %13
%15 = load i64, i64* %4
%16 = sitofp i64 %15 to double
%17 = fdiv double %14, %16
store double %17, double* %5
%18 = load double, double* %5
%19 = fcmp olt double %18, 1.024000e+03
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = load double, double* %5
%22 = call i32 @sprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @rate.res, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %21)
br label %35
23: ; preds = %11
%24 = load double, double* %5
%25 = fcmp olt double %24, 0x4130000000000000
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load double, double* %5
%28 = fdiv double %27, 1.024000e+03
%29 = call i32 @sprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @rate.res, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), double %28)
br label %34
30: ; preds = %23
%31 = load double, double* %5
%32 = fdiv double %31, 0x4130000000000000
%33 = call i32 @sprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @rate.res, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), double %32)
br label %34
34: ; preds = %30, %26
br label %35
35: ; preds = %34, %20
ret i8* getelementptr inbounds ([15 x i8], [15 x i8]* @rate.res, i64 0, i64 0)
}

LLVM IR (-O2):
@rate.res = internal global [15 x i8] zeroinitializer
@.str = [9 x i8] c"%.2f B/s\00"
@.str.1 = [10 x i8] c"%.2f KB/s\00"
@.str.2 = [10 x i8] c"%.2f MB/s\00"
define i8* @rate(i64 %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = select i1 %3, i64 1, i64 %1
%5 = sitofp i64 %0 to double
%6 = fmul double %5, 1.000000e+03
%7 = sitofp i64 %4 to double
%8 = fdiv double %6, %7
%9 = fcmp olt double %8, 1.024000e+03
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @rate.res, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %8)
br label %20
12: ; preds = %2
%13 = fcmp olt double %8, 0x4130000000000000
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = fmul double %8, 0x3F50000000000000
%16 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @rate.res, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), double %15)
br label %20
17: ; preds = %12
%18 = fmul double %8, 0x3EB0000000000000
%19 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @rate.res, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), double %18)
br label %20
20: ; preds = %14, %17, %10
ret i8* getelementptr inbounds ([15 x i8], [15 x i8]* @rate.res, i64 0, i64 0)
}