#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*/ fabs(double);
int log10f(int /*<<< orphan*/);
double powf(double, int);
double _si_scaling(double val, int max_num, char *prefix, int *exp) {
  const char prefixes[5] = {' ', 'k', 'M', 'G', 'T'};
  int exponent = log10f(fabs(val));
  int d = exponent >= max_num ? ((exponent - max_num + 3) / 3) : 0;
  if (prefix)
    *prefix = prefixes[d];
  if (exp)
    *exp = d * 3;
  return val / powf(10.0, d * 3);
}


x86 assembly (clang, -O0):
<_si_scaling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0xeba(%rip),%eax
mov    %eax,-0x25(%rbp)
mov    0xeb5(%rip),%al
mov    %al,-0x21(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1040 <fabs@plt>
mov    %eax,%edi
call   1050 <log10f@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1193 <_si_scaling+0x63>
mov    -0x2c(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x3,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
jmp    119d <_si_scaling+0x6d>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    119d <_si_scaling+0x6d>
mov    -0x34(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11bc <_si_scaling+0x8c>
movslq -0x30(%rbp),%rax
mov    -0x25(%rbp,%rax,1),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     11d1 <_si_scaling+0xa1>
imul   $0x3,-0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
imul   $0x3,-0x30(%rbp),%edi
movsd  0xe19(%rip),%xmm0
call   1030 <powf@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
divsd  %xmm1,%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_si_scaling>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
movsd  %xmm0,0x8(%rsp)
call   1040 <fabs@plt>
mov    %eax,%edi
call   1050 <log10f@plt>
xor    %ecx,%ecx
sub    %ebp,%eax
jl     1171 <_si_scaling+0x41>
add    $0x3,%eax
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
shr    $0x20,%rcx
add    %eax,%ecx
test   %rbx,%rbx
je     1185 <_si_scaling+0x55>
movslq %ecx,%rax
lea    0xe88(%rip),%rdx
mov    (%rax,%rdx,1),%al
mov    %al,(%rbx)
lea    (%rcx,%rcx,2),%edi
test   %r14,%r14
je     1190 <_si_scaling+0x60>
mov    %edi,(%r14)
movsd  0xe68(%rip),%xmm0
call   1030 <powf@plt>
movsd  0x8(%rsp),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const._si_scaling.prefixes = [5 x i8] c" kMGT"
define double @_si_scaling(double %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca double
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [5 x i8]
%10 = alloca i32
%11 = alloca i32
store double %0, double* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32* %3, i32** %8
%12 = bitcast [5 x i8]* %9 to i8*
%13 = load double, double* %5
%14 = call i32 @fabs(double %13)
%15 = call i32 @log10f(i32 %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = load i32, i32* %6
%18 = icmp sge i32 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %4
%20 = load i32, i32* %10
%21 = load i32, i32* %6
%22 = sub nsw i32 %20, %21
%23 = add nsw i32 %22, 3
%24 = sdiv i32 %23, 3
br label %26
25: ; preds = %4
br label %26
26: ; preds = %25, %19
%27 = phi i32 [ %24, %19 ], [ 0, %25 ]
store i32 %27, i32* %11
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i32, i32* %11
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = load i8*, i8** %7
store i8 %34, i8* %35
br label %36
36: ; preds = %30, %26
%37 = load i32*, i32** %8
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i32, i32* %11
%41 = mul nsw i32 %40, 3
%42 = load i32*, i32** %8
store i32 %41, i32* %42
br label %43
43: ; preds = %39, %36
%44 = load double, double* %5
%45 = load i32, i32* %11
%46 = mul nsw i32 %45, 3
%47 = call double @powf(double 1.000000e+01, i32 %46)
%48 = fdiv double %44, %47
ret double %48
}

LLVM IR (-O2):
@__const._si_scaling.prefixes = [5 x i8] c" kMGT"
define double @_si_scaling(double %0, i32 %1, i8* writeonly %2, i32* writeonly %3) {
%5 = tail call i32 @fabs(double %0)
%6 = tail call i32 @log10f(i32 %5)
%7 = icmp slt i32 %6, %1
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = sub i32 3, %1
%10 = add i32 %9, %6
%11 = sdiv i32 %10, 3
br label %12
12: ; preds = %4, %8
%13 = phi i32 [ %11, %8 ], [ 0, %4 ]
%14 = icmp eq i8* %2, null
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = sext i32 %13 to i64
%17 = getelementptr inbounds [5 x i8], [5 x i8]* @__const._si_scaling.prefixes, i64 0, i64 %16
%18 = load i8, i8* %17
store i8 %18, i8* %2
br label %19
19: ; preds = %15, %12
%20 = icmp eq i32* %3, null
%21 = mul nsw i32 %13, 3
br i1 %20, label %23, label %22
22: ; preds = %19
store i32 %21, i32* %3
br label %23
23: ; preds = %19, %22
%24 = tail call double @powf(double 1.000000e+01, i32 %21)
%25 = fdiv double %0, %24
ret double %25
}