#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*/ snprintf(char *, int, char *, ...);
char *gen_str_speed(long speed, char unit, int threshold) {
  char *str_speed = malloc(16 * sizeof(char));
  if (speed != -1) {
    if (speed >= threshold * 1024) {
      if (speed > 1048576)
        snprintf(str_speed, 16, "%.1f Mi%c/s", (float)speed / 1024 / 1024,
                 unit);
      else
        snprintf(str_speed, 16, "%ld Ki%c/s", speed / 1024, unit);
    } else
      snprintf(str_speed, 16, "0 Ki%c/s", unit);
  } else
    snprintf(str_speed, 16, "Error");
  return str_speed;
}


x86 assembly (clang, -O0):
<gen_str_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x8(%rbp)
je     1206 <gen_str_speed+0xe6>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
shl    $0xa,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jl     11e6 <gen_str_speed+0xc6>
cmpq   $0x100000,-0x8(%rbp)
jle    11b4 <gen_str_speed+0x94>
mov    -0x18(%rbp),%rdi
cvtsi2ssq -0x8(%rbp),%xmm0
movss  0xe7c(%rip),%xmm1
divss  %xmm1,%xmm0
movss  0xe70(%rip),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsbl -0x9(%rbp),%ecx
mov    $0x10,%esi
lea    0xe5c(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    11e1 <gen_str_speed+0xc1>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
movsbl -0x9(%rbp),%r8d
mov    $0x10,%esi
lea    0xe36(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    1201 <gen_str_speed+0xe1>
mov    -0x18(%rbp),%rdi
movsbl -0x9(%rbp),%ecx
mov    $0x10,%esi
lea    0xe21(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    121d <gen_str_speed+0xfd>
mov    -0x18(%rbp),%rdi
mov    $0x10,%esi
lea    0xe0e(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_str_speed>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
cmp    $0xffffffffffffffff,%rbp
je     116c <gen_str_speed+0x4c>
shl    $0xa,%ebx
movslq %ebx,%rax
cmp    %rbp,%rax
jle    1184 <gen_str_speed+0x64>
movsbl %r14b,%ecx
lea    0xec3(%rip),%rdx
mov    %r15,%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11ee <gen_str_speed+0xce>
lea    0xeb1(%rip),%rdx
mov    %r15,%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11ee <gen_str_speed+0xce>
cmp    $0x100001,%rbp
jl     11c2 <gen_str_speed+0xa2>
cvtsi2ss %rbp,%xmm0
movss  0xe66(%rip),%xmm1
mulss  %xmm1,%xmm0
mulss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsbl %r14b,%ecx
lea    0xe53(%rip),%rdx
mov    %r15,%rdi
mov    $0x10,%esi
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    11ee <gen_str_speed+0xce>
lea    0x3ff(%rbp),%rcx
test   %rbp,%rbp
cmovns %rbp,%rcx
sar    $0xa,%rcx
movsbl %r14b,%r8d
lea    0xe31(%rip),%rdx
mov    %r15,%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%.1f Mi%c/s\00"
@.str.1 = [11 x i8] c"%ld Ki%c/s\00"
@.str.2 = [9 x i8] c"0 Ki%c/s\00"
@.str.3 = [6 x i8] c"Error\00"
define i8* @gen_str_speed(i64 %0, i8 signext %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8
%6 = alloca i32
%7 = alloca i8*
store i64 %0, i64* %4
store i8 %1, i8* %5
store i32 %2, i32* %6
%8 = call i8* @malloc(i32 16)
store i8* %8, i8** %7
%9 = load i64, i64* %4
%10 = icmp ne i64 %9, -1
br i1 %10, label %11, label %44
11: ; preds = %3
%12 = load i64, i64* %4
%13 = load i32, i32* %6
%14 = mul nsw i32 %13, 1024
%15 = sext i32 %14 to i64
%16 = icmp sge i64 %12, %15
br i1 %16, label %17, label %38
17: ; preds = %11
%18 = load i64, i64* %4
%19 = icmp sgt i64 %18, 1048576
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = load i64, i64* %4
%23 = sitofp i64 %22 to float
%24 = fdiv float %23, 1.024000e+03
%25 = fdiv float %24, 1.024000e+03
%26 = fpext float %25 to double
%27 = load i8, i8* %5
%28 = sext i8 %27 to i32
%29 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %21, i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), double %26, i32 %28)
br label %37
30: ; preds = %17
%31 = load i8*, i8** %7
%32 = load i64, i64* %4
%33 = sdiv i64 %32, 1024
%34 = load i8, i8* %5
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %31, i32 16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 %33, i32 %35)
br label %37
37: ; preds = %30, %20
br label %43
38: ; preds = %11
%39 = load i8*, i8** %7
%40 = load i8, i8* %5
%41 = sext i8 %40 to i32
%42 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %39, i32 16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %38, %37
br label %47
44: ; preds = %3
%45 = load i8*, i8** %7
%46 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %45, i32 16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %44, %43
%48 = load i8*, i8** %7
ret i8* %48
}

LLVM IR (-O2):
@.str = [12 x i8] c"%.1f Mi%c/s\00"
@.str.1 = [11 x i8] c"%ld Ki%c/s\00"
@.str.2 = [9 x i8] c"0 Ki%c/s\00"
@.str.3 = [6 x i8] c"Error\00"
define i8* @gen_str_speed(i64 %0, i8 signext %1, i32 %2) {
%4 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
%5 = icmp eq i64 %0, -1
br i1 %5, label %26, label %6
6: ; preds = %3
%7 = shl nsw i32 %2, 10
%8 = sext i32 %7 to i64
%9 = icmp sgt i64 %8, %0
br i1 %9, label %23, label %10
10: ; preds = %6
%11 = icmp sgt i64 %0, 1048576
br i1 %11, label %12, label %19
12: ; preds = %10
%13 = sitofp i64 %0 to float
%14 = fmul float %13, 0x3F50000000000000
%15 = fmul float %14, 0x3F50000000000000
%16 = fpext float %15 to double
%17 = sext i8 %1 to i32
%18 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), double %16, i32 %17)
br label %28
19: ; preds = %10
%20 = sdiv i64 %0, 1024
%21 = sext i8 %1 to i32
%22 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 %20, i32 %21)
br label %28
23: ; preds = %6
%24 = sext i8 %1 to i32
%25 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %28
26: ; preds = %3
%27 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %23, %19, %12, %26
ret i8* %4
}