#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 int64_t;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, double);
void bytes_to_human_string(int64_t bytes, char *dest) {
  if (bytes < 1024)
    sprintf(dest, "%d bytes", (int)bytes);
  else if ((bytes >> 10) < 1024)
    sprintf(dest, "%.2lf KiB", (double)bytes / 1024);
  else if ((bytes >> 20) < 1024)
    sprintf(dest, "%.2lf MiB", (double)bytes / 1024 / 1024);
  else
    sprintf(dest, "%.2lf GiB", (double)bytes / 1024 / 1024 / 1024);
}


x86 assembly (clang, -O0):
<bytes_to_human_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x400,-0x4(%rbp)
jge    1146 <bytes_to_human_string+0x36>
mov    -0x10(%rbp),%rdi
cvtsi2sdl -0x4(%rbp),%xmm0
lea    0xecc(%rip),%rsi
call   1030 <sprintf@plt>
jmp    1203 <bytes_to_human_string+0xf3>
mov    -0x4(%rbp),%eax
sar    $0xa,%eax
cmp    $0x400,%eax
jge    117d <bytes_to_human_string+0x6d>
mov    -0x10(%rbp),%rdi
cvtsi2sdl -0x4(%rbp),%xmm0
movsd  0xe98(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xe9e(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11fe <bytes_to_human_string+0xee>
mov    -0x4(%rbp),%eax
sar    $0x14,%eax
cmp    $0x400,%eax
jge    11c0 <bytes_to_human_string+0xb0>
mov    -0x10(%rbp),%rdi
cvtsi2sdl -0x4(%rbp),%xmm0
movsd  0xe61(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe55(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xe65(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11f9 <bytes_to_human_string+0xe9>
mov    -0x10(%rbp),%rdi
cvtsi2sdl -0x4(%rbp),%xmm0
movsd  0xe2f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe23(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe17(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xe31(%rip),%rsi
call   1030 <sprintf@plt>
jmp    11fe <bytes_to_human_string+0xee>
jmp    1203 <bytes_to_human_string+0xf3>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes_to_human_string>:
mov    %rsi,%rax
cmp    $0x3ff,%edi
jg     112e <bytes_to_human_string+0x1e>
cvtsi2sd %edi,%xmm0
lea    0xee2(%rip),%rsi
mov    %rax,%rdi
jmp    1030 <sprintf@plt>
cmp    $0xfffff,%edi
ja     1151 <bytes_to_human_string+0x41>
cvtsi2sd %edi,%xmm0
mulsd  0xebe(%rip),%xmm0
lea    0xec8(%rip),%rsi
mov    %rax,%rdi
jmp    1030 <sprintf@plt>
cvtsi2sd %edi,%xmm0
movsd  0xea3(%rip),%xmm1
mulsd  %xmm1,%xmm0
mulsd  %xmm1,%xmm0
cmp    $0x3fffffff,%edi
ja     117c <bytes_to_human_string+0x6c>
lea    0xea7(%rip),%rsi
mov    %rax,%rdi
jmp    1030 <sprintf@plt>
mulsd  0xe7c(%rip),%xmm0
lea    0xe9a(%rip),%rsi
mov    %rax,%rdi
jmp    1030 <sprintf@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"%d bytes\00"
@.str.1 = [10 x i8] c"%.2lf KiB\00"
@.str.2 = [10 x i8] c"%.2lf MiB\00"
@.str.3 = [10 x i8] c"%.2lf GiB\00"
define void @bytes_to_human_string(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 1024
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i8*, i8** %4
%9 = load i32, i32* %3
%10 = sitofp i32 %9 to double
%11 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %10)
br label %43
12: ; preds = %2
%13 = load i32, i32* %3
%14 = ashr i32 %13, 10
%15 = icmp slt i32 %14, 1024
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = load i32, i32* %3
%19 = sitofp i32 %18 to double
%20 = fdiv double %19, 1.024000e+03
%21 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), double %20)
br label %42
22: ; preds = %12
%23 = load i32, i32* %3
%24 = ashr i32 %23, 20
%25 = icmp slt i32 %24, 1024
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i8*, i8** %4
%28 = load i32, i32* %3
%29 = sitofp i32 %28 to double
%30 = fdiv double %29, 1.024000e+03
%31 = fdiv double %30, 1.024000e+03
%32 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), double %31)
br label %41
33: ; preds = %22
%34 = load i8*, i8** %4
%35 = load i32, i32* %3
%36 = sitofp i32 %35 to double
%37 = fdiv double %36, 1.024000e+03
%38 = fdiv double %37, 1.024000e+03
%39 = fdiv double %38, 1.024000e+03
%40 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), double %39)
br label %41
41: ; preds = %33, %26
br label %42
42: ; preds = %41, %16
br label %43
43: ; preds = %42, %7
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"%d bytes\00"
@.str.1 = [10 x i8] c"%.2lf KiB\00"
@.str.2 = [10 x i8] c"%.2lf MiB\00"
@.str.3 = [10 x i8] c"%.2lf GiB\00"
define void @bytes_to_human_string(i32 %0, i8* nocapture writeonly %1) {
%3 = icmp slt i32 %0, 1024
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = sitofp i32 %0 to double
%6 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %5)
br label %23
7: ; preds = %2
%8 = icmp ult i32 %0, 1048576
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = sitofp i32 %0 to double
%11 = fmul double %10, 0x3F50000000000000
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), double %11)
br label %23
13: ; preds = %7
%14 = icmp ult i32 %0, 1073741824
%15 = sitofp i32 %0 to double
%16 = fmul double %15, 0x3F50000000000000
%17 = fmul double %16, 0x3F50000000000000
br i1 %14, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), double %17)
br label %23
20: ; preds = %13
%21 = fmul double %17, 0x3F50000000000000
%22 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), double %21)
br label %23
23: ; preds = %9, %20, %18, %4
ret void
}