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

/* Variables and functions */
int asprintf(char **, char *, double, char *);
char *format_capstr(uint64_t size, unsigned int blksize) {
  double cap_abbr;
  char *capstr;
  uint64_t cap;
  char *abbr;
  int ret;
  cap = size * blksize;
  cap_abbr = cap / (1024.0 * 1024.0);
  abbr = "MiB";
  if (cap_abbr >= 1024) {
    cap_abbr /= 1024.0;
    abbr = "GiB";
  }
  if (cap_abbr >= 1024) {
    cap_abbr /= 1024.0;
    abbr = "TiB";
  }
  if (cap_abbr >= 1024) {
    cap_abbr /= 1024.0;
    abbr = "PiB";
  }
  ret = asprintf(&capstr, "%0.2f %s", cap_abbr, abbr);
  if (ret == -1)
    return "Unknown";
  return capstr;
}


x86 assembly (clang, -O0):
<format_capstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
imul   %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cvtsi2sd %rax,%xmm0
movsd  0xece(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    0xec6(%rip),%rax
mov    %rax,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xea5(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1186 <format_capstr+0x76>
movsd  -0x18(%rbp),%xmm0
movsd  0xe8e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    0xe92(%rip),%rax
mov    %rax,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe6d(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11be <format_capstr+0xae>
movsd  -0x18(%rbp),%xmm0
movsd  0xe56(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    0xe5e(%rip),%rax
mov    %rax,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe35(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11f6 <format_capstr+0xe6>
movsd  -0x18(%rbp),%xmm0
movsd  0xe1e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    0xe2a(%rip),%rax
mov    %rax,-0x30(%rbp)
movsd  -0x18(%rbp),%xmm0
mov    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1030 <asprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    122c <format_capstr+0x11c>
lea    0xe06(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <format_capstr+0x124>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_capstr>:
push   %rax
imul   %esi,%edi
cvtsi2sd %rdi,%xmm0
mulsd  0xedf(%rip),%xmm0
movsd  0xedf(%rip),%xmm2
ucomisd %xmm2,%xmm0
movsd  0xedb(%rip),%xmm1
jb     113b <format_capstr+0x2b>
mulsd  %xmm1,%xmm0
lea    0xed6(%rip),%rcx
lea    0xed3(%rip),%rax
cmovb  %rcx,%rax
ucomisd %xmm2,%xmm0
jb     1157 <format_capstr+0x47>
mulsd  %xmm1,%xmm0
lea    0xec2(%rip),%rcx
cmovb  %rax,%rcx
ucomisd %xmm2,%xmm0
jb     116c <format_capstr+0x5c>
mulsd  %xmm1,%xmm0
lea    0xeb1(%rip),%rdx
cmovb  %rcx,%rdx
lea    0xeaa(%rip),%rsi
mov    %rsp,%rdi
call   1030 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1191 <format_capstr+0x81>
mov    (%rsp),%rax
pop    %rcx
ret
lea    0xe99(%rip),%rax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"MiB\00"
@.str.1 = [4 x i8] c"GiB\00"
@.str.2 = [4 x i8] c"TiB\00"
@.str.3 = [4 x i8] c"PiB\00"
@.str.4 = [9 x i8] c"%0.2f %s\00"
@.str.5 = [8 x i8] c"Unknown\00"
define i8* @format_capstr(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = load i32, i32* %4
%12 = load i32, i32* %5
%13 = mul i32 %11, %12
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = uitofp i32 %14 to double
%16 = fdiv double %15, 0x4130000000000000
store double %16, double* %6
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %9
%17 = load double, double* %6
%18 = fcmp oge double %17, 1.024000e+03
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load double, double* %6
%21 = fdiv double %20, 1.024000e+03
store double %21, double* %6
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %9
br label %22
22: ; preds = %19, %2
%23 = load double, double* %6
%24 = fcmp oge double %23, 1.024000e+03
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load double, double* %6
%27 = fdiv double %26, 1.024000e+03
store double %27, double* %6
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %9
br label %28
28: ; preds = %25, %22
%29 = load double, double* %6
%30 = fcmp oge double %29, 1.024000e+03
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load double, double* %6
%33 = fdiv double %32, 1.024000e+03
store double %33, double* %6
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %9
br label %34
34: ; preds = %31, %28
%35 = load double, double* %6
%36 = load i8*, i8** %9
%37 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), double %35, i8* %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %34
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %3
br label %43
41: ; preds = %34
%42 = load i8*, i8** %7
store i8* %42, i8** %3
br label %43
43: ; preds = %41, %40
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
@.str = [4 x i8] c"MiB\00"
@.str.1 = [4 x i8] c"GiB\00"
@.str.2 = [4 x i8] c"TiB\00"
@.str.3 = [4 x i8] c"PiB\00"
@.str.4 = [9 x i8] c"%0.2f %s\00"
@.str.5 = [8 x i8] c"Unknown\00"
define i8* @format_capstr(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = mul i32 %1, %0
%6 = uitofp i32 %5 to double
%7 = fmul double %6, 0x3EB0000000000000
%8 = fcmp ult double %7, 1.024000e+03
%9 = fmul double %7, 0x3F50000000000000
%10 = select i1 %8, double %7, double %9
%11 = select i1 %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%12 = fcmp ult double %10, 1.024000e+03
%13 = fmul double %10, 0x3F50000000000000
%14 = select i1 %12, double %10, double %13
%15 = select i1 %12, i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
%16 = fcmp ult double %14, 1.024000e+03
%17 = fmul double %14, 0x3F50000000000000
%18 = select i1 %16, double %14, double %17
%19 = select i1 %16, i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%20 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), double %18, i8* %19)
%21 = icmp eq i32 %20, -1
%22 = load i8*, i8** %3
%23 = select i1 %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %22
ret i8* %23
}