#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 fabs(double);
int /*<<< orphan*/ snprintf(char *, size_t, char *, double, char const *const);
void si_format(char *str, size_t len, long val) {
  const char *const prefixes[] = {"", "k", "M", "G", "T", "P", "E", "Z", "Y"};
  int prefix_idx = 0;
  double d_val = (double)val;
  while (fabs(d_val) > 1000 && prefix_idx < 9) {
    d_val /= 1000.0;
    prefix_idx++;
  }
  snprintf(str, len, "%.2f %s", d_val, prefixes[prefix_idx]);
}


x86 assembly (clang, -O0):
<si_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
lea    0x2c7e(%rip),%rsi
mov    $0x48,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x64(%rbp)
cvtsi2sdq -0x18(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x70(%rbp),%xmm0
call   1040 <fabs@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x3e8,%ecx
mov    %al,-0x71(%rbp)
jle    1195 <si_format+0x65>
cmpl   $0x9,-0x64(%rbp)
setl   %al
mov    %al,-0x71(%rbp)
mov    -0x71(%rbp),%al
test   $0x1,%al
jne    11a5 <si_format+0x75>
jmp    11c9 <si_format+0x99>
movsd  -0x70(%rbp),%xmm0
movsd  0xe4e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    116e <si_format+0x3e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movsd  -0x70(%rbp),%xmm0
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rcx
lea    0xe32(%rip),%rdx
call   1030 <snprintf@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<si_format>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
cvtsi2sd %rdx,%xmm0
movsd  %xmm0,0x8(%rsp)
call   1040 <fabs@plt>
cmp    $0x3e9,%eax
jl     117e <si_format+0x5e>
xor    %ebx,%ebx
movsd  0x8(%rsp),%xmm0
xchg   %ax,%ax
divsd  0xea8(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1040 <fabs@plt>
lea    0x1(%rbx),%rcx
cmp    $0x3e9,%eax
jl     1180 <si_format+0x60>
cmp    $0x8,%ebx
mov    %rcx,%rbx
movsd  0x8(%rsp),%xmm0
jb     1150 <si_format+0x30>
jmp    1186 <si_format+0x66>
xor    %ecx,%ecx
movsd  0x8(%rsp),%xmm0
lea    0xe93(%rip),%rax
movslq (%rax,%rcx,4),%rcx
add    %rax,%rcx
lea    0xe7d(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <snprintf@plt>

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.si_format.prefixes = [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 = [8 x i8] c"%.2f %s\00"
define void @si_format(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca [9 x i8*]
%8 = alloca i32
%9 = alloca double
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%10 = bitcast [9 x i8*]* %7 to i8*
store i32 0, i32* %8
%11 = load i64, i64* %6
%12 = sitofp i64 %11 to double
store double %12, double* %9
br label %13
13: ; preds = %22, %3
%14 = load double, double* %9
%15 = call i32 @fabs(double %14)
%16 = icmp sgt i32 %15, 1000
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 9
br label %20
20: ; preds = %17, %13
%21 = phi i1 [ false, %13 ], [ %19, %17 ]
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = load double, double* %9
%24 = fdiv double %23, 1.000000e+03
store double %24, double* %9
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
br label %13
27: ; preds = %20
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = load double, double* %9
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [9 x i8*], [9 x i8*]* %7, i64 0, i64 %32
%34 = load i8*, i8** %33
%35 = call i32 @snprintf(i8* %28, i64 %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), double %30, i8* %34)
ret void
}

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"
@reltable.si_format = [9 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([1 x i8]* @.str to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.1 to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.2 to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.3 to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.4 to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.5 to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.6 to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.7 to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.8 to i64), i64 ptrtoint ([9 x i32]* @reltable.si_format to i64)) to i32)]
@.str.9 = [8 x i8] c"%.2f %s\00"
define void @si_format(i8* %0, i64 %1, i64 %2) {
%4 = sitofp i64 %2 to double
%5 = tail call i32 @fabs(double %4)
%6 = icmp sgt i32 %5, 1000
br i1 %6, label %7, label %18
7: ; preds = %3, %7
%8 = phi double [ %10, %7 ], [ %4, %3 ]
%9 = phi i32 [ %11, %7 ], [ 0, %3 ]
%10 = fdiv double %8, 1.000000e+03
%11 = add nuw nsw i32 %9, 1
%12 = tail call i32 @fabs(double %10)
%13 = icmp sgt i32 %12, 1000
%14 = icmp ult i32 %9, 8
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %7, label %16
16: ; preds = %7
%17 = zext i32 %11 to i64
br label %18
18: ; preds = %16, %3
%19 = phi i64 [ 0, %3 ], [ %17, %16 ]
%20 = phi double [ %4, %3 ], [ %10, %16 ]
%21 = shl i64 %19, 2
%22 = call i8* @llvm.load.relative.i64(i8* bitcast ([9 x i32]* @reltable.si_format to i8*), i64 %21)
%23 = tail call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), double %20, i8* %22)
ret void
}