#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 *);
void hr_filesize(char *buf, double filesize) {
  if (!buf)
    return;
  char *sizes[] = {"B", "KB", "MB", "GB", "TB"};
  int i = 0;
  for (i = 0; i < 4 && filesize >= 1024; i++) {
    filesize /= 1024;
  }
  sprintf(buf, "%.02f %s", filesize, sizes[i]);
}


x86 assembly (clang, -O0):
<hr_filesize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1141 <hr_filesize+0x21>
jmp    120e <hr_filesize+0xee>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
lea    0xeb7(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xea5(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe9d(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe95(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe8d(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x44(%rbp)
xor    %eax,%eax
cmpl   $0x4,-0x44(%rbp)
mov    %al,-0x45(%rbp)
jge    11bc <hr_filesize+0x9c>
movsd  -0x10(%rbp),%xmm0
movsd  0xe4e(%rip),%xmm1
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    11cc <hr_filesize+0xac>
jmp    11f0 <hr_filesize+0xd0>
movsd  -0x10(%rbp),%xmm0
movsd  0xe27(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1196 <hr_filesize+0x76>
mov    -0x8(%rbp),%rdi
movsd  -0x10(%rbp),%xmm0
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdx
lea    0xe0b(%rip),%rsi
call   1040 <sprintf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hr_filesize>:
test   %rdi,%rdi
je     11c8 <hr_filesize+0xb8>
sub    $0x28,%rsp
lea    0xef3(%rip),%rdx
mov    %rdx,(%rsp)
lea    0xee1(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xed8(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xecf(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xec6(%rip),%rax
mov    %rax,0x20(%rsp)
ucomisd 0xea0(%rip),%xmm0
jb     11b8 <hr_filesize+0xa8>
mulsd  0xe9e(%rip),%xmm0
mov    $0x1,%eax
ucomisd 0xe89(%rip),%xmm0
jb     11b4 <hr_filesize+0xa4>
mulsd  0xe87(%rip),%xmm0
mov    $0x2,%eax
ucomisd 0xe72(%rip),%xmm0
jb     11b4 <hr_filesize+0xa4>
mulsd  0xe70(%rip),%xmm0
mov    $0x3,%eax
ucomisd 0xe5b(%rip),%xmm0
jb     11b4 <hr_filesize+0xa4>
mulsd  0xe59(%rip),%xmm0
mov    $0x4,%eax
mov    (%rsp,%rax,8),%rdx
lea    0xe5d(%rip),%rsi
call   1030 <sprintf@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"B\00"
@.str.1 = [3 x i8] c"KB\00"
@.str.2 = [3 x i8] c"MB\00"
@.str.3 = [3 x i8] c"GB\00"
@.str.4 = [3 x i8] c"TB\00"
@.str.5 = [9 x i8] c"%.02f %s\00"
define void @hr_filesize(i8* %0, double %1) {
%3 = alloca i8*
%4 = alloca double
%5 = alloca [5 x i8*]
%6 = alloca i32
store i8* %0, i8** %3
store double %1, double* %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
br label %40
10: ; preds = %2
%11 = bitcast [5 x i8*]* %5 to i8*
%12 = bitcast i8* %11 to [5 x i8*]*
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8** %15
%16 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8** %16
%17 = getelementptr inbounds [5 x i8*], [5 x i8*]* %12, i32 0, i32 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8** %17
store i32 0, i32* %6
store i32 0, i32* %6
br label %18
18: ; preds = %29, %10
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 4
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load double, double* %4
%23 = fcmp oge double %22, 1.024000e+03
br label %24
24: ; preds = %21, %18
%25 = phi i1 [ false, %18 ], [ %23, %21 ]
br i1 %25, label %26, label %32
26: ; preds = %24
%27 = load double, double* %4
%28 = fdiv double %27, 1.024000e+03
store double %28, double* %4
br label %29
29: ; preds = %26
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %18
32: ; preds = %24
%33 = load i8*, i8** %3
%34 = load double, double* %4
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i64 0, i64 %36
%38 = load i8*, i8** %37
%39 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), double %34, i8* %38)
br label %40
40: ; preds = %32, %9
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"B\00"
@.str.1 = [3 x i8] c"KB\00"
@.str.2 = [3 x i8] c"MB\00"
@.str.3 = [3 x i8] c"GB\00"
@.str.4 = [3 x i8] c"TB\00"
@.str.5 = [9 x i8] c"%.02f %s\00"
define void @hr_filesize(i8* writeonly %0, double %1) {
%3 = alloca [5 x i8*]
%4 = icmp eq i8* %0, null
br i1 %4, label %33, label %5
5: ; preds = %2
%6 = bitcast [5 x i8*]* %3 to i8*
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %11
%12 = fcmp ult double %1, 1.024000e+03
br i1 %12, label %29, label %13
13: ; preds = %5
%14 = fmul double %1, 0x3F50000000000000
%15 = fcmp ult double %14, 1.024000e+03
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = fmul double %14, 0x3F50000000000000
%18 = fcmp ult double %17, 1.024000e+03
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = fmul double %17, 0x3F50000000000000
%21 = fcmp ult double %20, 1.024000e+03
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = fmul double %20, 0x3F50000000000000
br label %24
24: ; preds = %22, %19, %16, %13
%25 = phi double [ %14, %13 ], [ %17, %16 ], [ %20, %19 ], [ %23, %22 ]
%26 = phi i64 [ 1, %13 ], [ 2, %16 ], [ 3, %19 ], [ 4, %22 ]
%27 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %26
%28 = load i8*, i8** %27
br label %29
29: ; preds = %5, %24
%30 = phi i8* [ %28, %24 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %5 ]
%31 = phi double [ %25, %24 ], [ %1, %5 ]
%32 = tail call i32 @sprintf(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), double %31, i8* %30)
br label %33
33: ; preds = %2, %29
ret void
}