#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, long double, char *);
scalar_t__ strtoll(char *, int /*<<< orphan*/ *, int);
char *calculate_size(char *field_total, int byte_idx) {
  char *byte_totals[] = {"B", "KiB", "MiB", "GiB"};
  long double field_value = (long double)strtoll(field_total, NULL, 10);
  for (; byte_idx < 4 && field_value > 1024; byte_idx++)
    field_value /= 1024;
  free(field_total);
  char *field_str = malloc(sizeof(char) * 16);
  snprintf(field_str, 15, "%.2Lf %s", field_value, byte_totals[byte_idx]);
  return field_str;
}


x86 assembly (clang, -O0):
<calculate_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movaps 0x2cba(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
movaps 0x2c9f(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtoll@plt>
mov    %rax,-0x50(%rbp)
fildll -0x50(%rbp)
fstpt  -0x40(%rbp)
xor    %eax,%eax
cmpl   $0x4,-0xc(%rbp)
mov    %al,-0x51(%rbp)
jge    11a5 <calculate_size+0x65>
fldt   -0x40(%rbp)
flds   0xe67(%rip)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
seta   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    11b5 <calculate_size+0x75>
jmp    11d1 <calculate_size+0x91>
fldt   -0x40(%rbp)
flds   0xe42(%rip)
fdivrp %st,%st(1)
fstpt  -0x40(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1181 <calculate_size+0x41>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    $0x10,%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
fldt   -0x40(%rbp)
movslq -0xc(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rcx
mov    %rsp,%rax
fstpt  (%rax)
lea    0xe0c(%rip),%rdx
mov    $0xf,%esi
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_size>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoll@plt>
mov    %rax,0x10(%rsp)
fildll 0x10(%rsp)
cmp    $0x3,%ebx
jg     119c <calculate_size+0x5c>
cmp    $0x401,%rax
jl     119c <calculate_size+0x5c>
flds   0xe90(%rip)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%eax
fxch   %st(1)
fmuls  0xe76(%rip)
lea    0x1(%rax),%ebx
cmp    $0x2,%eax
jg     119a <calculate_size+0x5a>
fucomi %st(1),%st
fxch   %st(1)
ja     1180 <calculate_size+0x40>
jmp    119e <calculate_size+0x5e>
fstp   %st(1)
fldz
fstp   %st(0)
fstpt  0x1c(%rsp)
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x10,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
movslq %ebx,%rax
lea    0xe5d(%rip),%rdx
movslq (%rdx,%rax,4),%rcx
add    %rdx,%rcx
fldt   0x1c(%rsp)
fstpt  (%rsp)
lea    0xe3c(%rip),%rdx
mov    %r14,%rdi
mov    $0xf,%esi
call   1040 <snprintf@plt>
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"B\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
@.str.3 = [4 x i8] c"GiB\00"
@__const.calculate_size.byte_totals = [4 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i32 0, i32 0)]
@.str.4 = [9 x i8] c"%.2Lf %s\00"
define i8* @calculate_size(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [4 x i8*]
%6 = alloca x86_fp80
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = bitcast [4 x i8*]* %5 to i8*
%9 = load i8*, i8** %3
%10 = call i64 @strtoll(i8* %9, i32* null, i32 10)
%11 = sitofp i64 %10 to x86_fp80
store x86_fp80 %11, x86_fp80* %6
br label %12
12: ; preds = %23, %2
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load x86_fp80, x86_fp80* %6
%17 = fcmp ogt x86_fp80 %16, 0xK40098000000000000000
br label %18
18: ; preds = %15, %12
%19 = phi i1 [ false, %12 ], [ %17, %15 ]
br i1 %19, label %20, label %26
20: ; preds = %18
%21 = load x86_fp80, x86_fp80* %6
%22 = fdiv x86_fp80 %21, 0xK40098000000000000000
store x86_fp80 %22, x86_fp80* %6
br label %23
23: ; preds = %20
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %12
26: ; preds = %18
%27 = load i8*, i8** %3
%28 = call i32 @free(i8* %27)
%29 = call i8* @malloc(i32 16)
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = load x86_fp80, x86_fp80* %6
%32 = load i32, i32* %4
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 @snprintf(i8* %30, i32 15, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), x86_fp80 %31, i8* %35)
%37 = load i8*, i8** %7
ret i8* %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"B\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
@.str.3 = [4 x i8] c"GiB\00"
@reltable.calculate_size = [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str to i64), i64 ptrtoint ([4 x i32]* @reltable.calculate_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([4 x i32]* @reltable.calculate_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.2 to i64), i64 ptrtoint ([4 x i32]* @reltable.calculate_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.3 to i64), i64 ptrtoint ([4 x i32]* @reltable.calculate_size to i64)) to i32)]
@.str.4 = [9 x i8] c"%.2Lf %s\00"
define i8* @calculate_size(i8* nocapture %0, i32 %1) {
%3 = tail call i64 @strtoll(i8* nocapture %0, i32* null, i32 10)
%4 = sitofp i64 %3 to x86_fp80
%5 = icmp slt i32 %1, 4
%6 = icmp sgt i64 %3, 1024
%7 = select i1 %5, i1 %6, i1 false
br i1 %7, label %8, label %16
8: ; preds = %2, %8
%9 = phi i32 [ %12, %8 ], [ %1, %2 ]
%10 = phi x86_fp80 [ %11, %8 ], [ %4, %2 ]
%11 = fmul x86_fp80 %10, 0xK3FF58000000000000000
%12 = add nsw i32 %9, 1
%13 = icmp slt i32 %9, 3
%14 = fcmp ogt x86_fp80 %11, 0xK40098000000000000000
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %8, label %16
16: ; preds = %8, %2
%17 = phi x86_fp80 [ %4, %2 ], [ %11, %8 ]
%18 = phi i32 [ %1, %2 ], [ %12, %8 ]
%19 = tail call i32 @free(i8* %0)
%20 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
%21 = sext i32 %18 to i64
%22 = shl i64 %21, 2
%23 = call i8* @llvm.load.relative.i64(i8* bitcast ([4 x i32]* @reltable.calculate_size to i8*), i64 %22)
%24 = tail call i32 @snprintf(i8* %20, i32 15, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), x86_fp80 %17, i8* %23)
ret i8* %20
}