#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 double uintmax_t;
typedef int /*<<< orphan*/ prefix_1024;
typedef int /*<<< orphan*/ prefix_1000;

/* Variables and functions */
char const *bprintf(char *, double, char const *);
int /*<<< orphan*/ warn(char *);
const char *fmt_human(uintmax_t num, int base) {
  double scaled;
  size_t i, prefixlen;
  const char **prefix;
  const char *prefix_1000[] = {"", "k", "M", "G", "T", "P", "E", "Z", "Y"};
  const char *prefix_1024[] = {"",   "Ki", "Mi", "Gi", "Ti",
                               "Pi", "Ei", "Zi", "Yi"};
  switch (base) {
  case 1000:
    prefix = prefix_1000;
    prefixlen = (sizeof(prefix_1000) / sizeof *(prefix_1000));
    break;
  case 1024:
    prefix = prefix_1024;
    prefixlen = (sizeof(prefix_1024) / sizeof *(prefix_1024));
    break;
  default:
    warn("fmt_human: Invalid base");
    return NULL;
  }
  scaled = num;
  for (i = 0; i < prefixlen && scaled >= base; i++) {
    scaled /= base;
  }
  return bprintf("%.1f %s", scaled, prefix[i]);
}


x86 assembly (clang, -O0):
<fmt_human>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    0x2c86(%rip),%rax
mov    %rax,-0x40(%rbp)
movaps 0x2c6b(%rip),%xmm0
movaps %xmm0,-0x50(%rbp)
movaps 0x2c50(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
movaps 0x2c35(%rip),%xmm0
movaps %xmm0,-0x70(%rbp)
movaps 0x2c1a(%rip),%xmm0
movaps %xmm0,-0x80(%rbp)
mov    0x2c9f(%rip),%rax
mov    %rax,-0x90(%rbp)
movaps 0x2c81(%rip),%xmm0
movaps %xmm0,-0xa0(%rbp)
movaps 0x2c63(%rip),%xmm0
movaps %xmm0,-0xb0(%rbp)
movaps 0x2c45(%rip),%xmm0
movaps %xmm0,-0xc0(%rbp)
movaps 0x2c27(%rip),%xmm0
movaps %xmm0,-0xd0(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xd4(%rbp)
sub    $0x3e8,%eax
je     11df <fmt_human+0xbf>
jmp    11c9 <fmt_human+0xa9>
mov    -0xd4(%rbp),%eax
sub    $0x400,%eax
je     11f4 <fmt_human+0xd4>
jmp    120c <fmt_human+0xec>
lea    -0x80(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x9,-0x30(%rbp)
jmp    1225 <fmt_human+0x105>
lea    -0xd0(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x9,-0x30(%rbp)
jmp    1225 <fmt_human+0x105>
lea    0xe15(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bc <fmt_human+0x19c>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x30(%rbp),%rcx
mov    %al,-0xd5(%rbp)
jae    1264 <fmt_human+0x144>
movsd  -0x20(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0xd5(%rbp)
mov    -0xd5(%rbp),%al
test   $0x1,%al
jne    1277 <fmt_human+0x157>
jmp    129b <fmt_human+0x17b>
cvtsi2sdl -0x14(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1237 <fmt_human+0x117>
movsd  -0x20(%rbp),%xmm0
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd8d(%rip),%rdi
call   1040 <bprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_human>:
sub    $0x98,%rsp
mov    0x2c92(%rip),%rax
mov    %rax,0x90(%rsp)
movaps 0x2c73(%rip),%xmm1
movaps %xmm1,0x80(%rsp)
movaps 0x2c54(%rip),%xmm1
movaps %xmm1,0x70(%rsp)
movaps 0x2c38(%rip),%xmm1
movaps %xmm1,0x60(%rsp)
movaps 0x2c1c(%rip),%xmm1
movaps %xmm1,0x50(%rsp)
mov    0x2ca0(%rip),%rax
mov    %rax,0x40(%rsp)
movaps 0x2c84(%rip),%xmm1
movaps %xmm1,0x30(%rsp)
movaps 0x2c68(%rip),%xmm1
movaps %xmm1,0x20(%rsp)
movaps 0x2c4c(%rip),%xmm1
movaps %xmm1,0x10(%rsp)
movaps 0x2c30(%rip),%xmm1
movaps %xmm1,(%rsp)
cmp    $0x3e8,%edi
je     11b9 <fmt_human+0x99>
cmp    $0x400,%edi
jne    11fe <fmt_human+0xde>
mov    %rsp,%rax
jmp    11be <fmt_human+0x9e>
lea    0x50(%rsp),%rax
xorps  %xmm1,%xmm1
cvtsi2sd %edi,%xmm1
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
ucomisd %xmm1,%xmm0
jb     11e7 <fmt_human+0xc7>
mov    %rcx,%rdx
divsd  %xmm1,%xmm0
add    $0x1,%rcx
cmp    $0x7,%rdx
jbe    11d0 <fmt_human+0xb0>
mov    (%rax,%rcx,8),%rsi
lea    0xe4e(%rip),%rdi
add    $0x98,%rsp
jmp    1040 <bprintf@plt>
lea    0xe23(%rip),%rdi
call   1030 <warn@plt>
xor    %eax,%eax
add    $0x98,%rsp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"k\00"
@.str.2 = [2 x i8] c"M\00"
@.str.3 = [2 x i8] c"G\00"
@.str.4 = [2 x i8] c"T\00"
@.str.5 = [2 x i8] c"P\00"
@.str.6 = [2 x i8] c"E\00"
@.str.7 = [2 x i8] c"Z\00"
@.str.8 = [2 x i8] c"Y\00"
@__const.fmt_human.prefix_1000 = [9 x i8*] [i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)]
@.str.9 = [3 x i8] c"Ki\00"
@.str.10 = [3 x i8] c"Mi\00"
@.str.11 = [3 x i8] c"Gi\00"
@.str.12 = [3 x i8] c"Ti\00"
@.str.13 = [3 x i8] c"Pi\00"
@.str.14 = [3 x i8] c"Ei\00"
@.str.15 = [3 x i8] c"Zi\00"
@.str.16 = [3 x i8] c"Yi\00"
@__const.fmt_human.prefix_1024 = [9 x i8*] [i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i32 0, i32 0)]
@.str.17 = [24 x i8] c"fmt_human: Invalid base\00"
@.str.18 = [8 x i8] c"%.1f %s\00"
define i8* @fmt_human(double %0, i32 %1) {
%3 = alloca i8*
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8**
%10 = alloca [9 x i8*]
%11 = alloca [9 x i8*]
store double %0, double* %4
store i32 %1, i32* %5
%12 = bitcast [9 x i8*]* %10 to i8*
%13 = bitcast [9 x i8*]* %11 to i8*
%14 = load i32, i32* %5
switch i32 %14, label %19 [
i32 1000, label %15
i32 1024, label %17
]
15: ; preds = %2
%16 = getelementptr inbounds [9 x i8*], [9 x i8*]* %10, i64 0, i64 0
store i8** %16, i8*** %9
store i64 9, i64* %8
br label %21
17: ; preds = %2
%18 = getelementptr inbounds [9 x i8*], [9 x i8*]* %11, i64 0, i64 0
store i8** %18, i8*** %9
store i64 9, i64* %8
br label %21
19: ; preds = %2
%20 = call i32 @warn(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.17, i64 0, i64 0))
store i8* null, i8** %3
br label %49
21: ; preds = %17, %15
%22 = load double, double* %4
store double %22, double* %6
store i64 0, i64* %7
br label %23
23: ; preds = %39, %21
%24 = load i64, i64* %7
%25 = load i64, i64* %8
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load double, double* %6
%29 = load i32, i32* %5
%30 = sitofp i32 %29 to double
%31 = fcmp oge double %28, %30
br label %32
32: ; preds = %27, %23
%33 = phi i1 [ false, %23 ], [ %31, %27 ]
br i1 %33, label %34, label %42
34: ; preds = %32
%35 = load i32, i32* %5
%36 = sitofp i32 %35 to double
%37 = load double, double* %6
%38 = fdiv double %37, %36
store double %38, double* %6
br label %39
39: ; preds = %34
%40 = load i64, i64* %7
%41 = add i64 %40, 1
store i64 %41, i64* %7
br label %23
42: ; preds = %32
%43 = load double, double* %6
%44 = load i8**, i8*** %9
%45 = load i64, i64* %7
%46 = getelementptr inbounds i8*, i8** %44, i64 %45
%47 = load i8*, i8** %46
%48 = call i8* @bprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.18, i64 0, i64 0), double %43, i8* %47)
store i8* %48, i8** %3
br label %49
49: ; preds = %42, %19
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"k\00"
@.str.2 = [2 x i8] c"M\00"
@.str.3 = [2 x i8] c"G\00"
@.str.4 = [2 x i8] c"T\00"
@.str.5 = [2 x i8] c"P\00"
@.str.6 = [2 x i8] c"E\00"
@.str.7 = [2 x i8] c"Z\00"
@.str.8 = [2 x i8] c"Y\00"
@__const.fmt_human.prefix_1000 = [9 x i8*] [i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)]
@.str.9 = [3 x i8] c"Ki\00"
@.str.10 = [3 x i8] c"Mi\00"
@.str.11 = [3 x i8] c"Gi\00"
@.str.12 = [3 x i8] c"Ti\00"
@.str.13 = [3 x i8] c"Pi\00"
@.str.14 = [3 x i8] c"Ei\00"
@.str.15 = [3 x i8] c"Zi\00"
@.str.16 = [3 x i8] c"Yi\00"
@__const.fmt_human.prefix_1024 = [9 x i8*] [i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i32 0, i32 0)]
@.str.17 = [24 x i8] c"fmt_human: Invalid base\00"
@.str.18 = [8 x i8] c"%.1f %s\00"
define i8* @fmt_human(double %0, i32 %1) {
%3 = alloca [9 x i8*]
%4 = alloca [9 x i8*]
%5 = bitcast [9 x i8*]* %3 to i8*
%6 = bitcast [9 x i8*]* %4 to i8*
switch i32 %1, label %8 [
i32 1000, label %10
i32 1024, label %7
]
7: ; preds = %2
br label %10
8: ; preds = %2
%9 = tail call i32 @warn(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.17, i64 0, i64 0))
br label %28
10: ; preds = %2, %7
%11 = phi [9 x i8*]* [ %4, %7 ], [ %3, %2 ]
%12 = sitofp i32 %1 to double
%13 = fcmp ugt double %12, %0
br i1 %13, label %22, label %14
14: ; preds = %10, %14
%15 = phi i64 [ %18, %14 ], [ 0, %10 ]
%16 = phi double [ %17, %14 ], [ %0, %10 ]
%17 = fdiv double %16, %12
%18 = add nuw nsw i64 %15, 1
%19 = icmp ult i64 %15, 8
%20 = fcmp oge double %17, %12
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %14, label %22
22: ; preds = %14, %10
%23 = phi double [ %0, %10 ], [ %17, %14 ]
%24 = phi i64 [ 0, %10 ], [ %18, %14 ]
%25 = getelementptr inbounds [9 x i8*], [9 x i8*]* %11, i64 0, i64 %24
%26 = load i8*, i8** %25
%27 = tail call i8* @bprintf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.18, i64 0, i64 0), double %23, i8* %26)
br label %28
28: ; preds = %22, %8
%29 = phi i8* [ null, %8 ], [ %27, %22 ]
ret i8* %29
}