#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 */
double fabs(double);
int log2i(int);
int scale_to_dynrng(double scale) {
  int scale512, logscale, code;
  scale512 = (int)(fabs(scale) * 512.0);
  scale512 = (((((scale512)) > ((8064)) ? ((8064)) : ((scale512)))) > ((32))
                  ? ((((scale512)) > ((8064)) ? ((8064)) : ((scale512))))
                  : ((32)));
  logscale = log2i(scale512) - 5;
  code = (((logscale + 4) & 7) << 5) +
         ((scale512 - (1 << (logscale + 5))) >> logscale);
  return code;
}


x86 assembly (clang, -O0):
<scale_to_dynrng>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movaps 0xee7(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  0xecb(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x1f80,-0xc(%rbp)
jle    115a <scale_to_dynrng+0x4a>
mov    $0x1f80,%eax
mov    %eax,-0x18(%rbp)
jmp    1160 <scale_to_dynrng+0x50>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    $0x20,%eax
jle    1197 <scale_to_dynrng+0x87>
cmpl   $0x1f80,-0xc(%rbp)
jle    1186 <scale_to_dynrng+0x76>
mov    $0x1f80,%eax
mov    %eax,-0x1c(%rbp)
jmp    118c <scale_to_dynrng+0x7c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11a4 <scale_to_dynrng+0x94>
mov    $0x20,%eax
mov    %eax,-0x20(%rbp)
jmp    11a4 <scale_to_dynrng+0x94>
mov    -0x20(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <log2i@plt>
sub    $0x5,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x4,%eax
and    $0x7,%eax
shl    $0x5,%eax
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
add    $0x5,%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    %esi,%ecx
sub    %ecx,%edx
mov    -0x10(%rbp),%ecx
sar    %cl,%edx
mov    %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scale_to_dynrng>:
push   %rbx
andpd  0xee7(%rip),%xmm0
mulsd  0xeef(%rip),%xmm0
cvttsd2si %xmm0,%eax
cmp    $0x1f80,%eax
mov    $0x1f80,%ecx
cmovl  %eax,%ecx
cmp    $0x21,%ecx
mov    $0x20,%ebx
cmovge %ecx,%ebx
mov    %ebx,%edi
call   1030 <log2i@plt>
lea    -0x5(%rax),%edx
mov    %eax,%ecx
shl    $0x5,%ecx
add    $0xe0,%ecx
movzbl %cl,%edi
mov    $0xffffffff,%esi
mov    %eax,%ecx
shl    %cl,%esi
add    %ebx,%esi
mov    %edx,%ecx
sar    %cl,%esi
add    %edi,%esi
mov    %esi,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @scale_to_dynrng(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store double %0, double* %2
%6 = load double, double* %2
%7 = call double @llvm.fabs.f64(double %6)
%8 = fmul double %7, 5.120000e+02
%9 = fptosi double %8 to i32
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 8064
br i1 %11, label %12, label %13
12: ; preds = %1
br label %15
13: ; preds = %1
%14 = load i32, i32* %3
br label %15
15: ; preds = %13, %12
%16 = phi i32 [ 8064, %12 ], [ %14, %13 ]
%17 = icmp sgt i32 %16, 32
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = load i32, i32* %3
%20 = icmp sgt i32 %19, 8064
br i1 %20, label %21, label %22
21: ; preds = %18
br label %24
22: ; preds = %18
%23 = load i32, i32* %3
br label %24
24: ; preds = %22, %21
%25 = phi i32 [ 8064, %21 ], [ %23, %22 ]
br label %27
26: ; preds = %15
br label %27
27: ; preds = %26, %24
%28 = phi i32 [ %25, %24 ], [ 32, %26 ]
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = call i32 @log2i(i32 %29)
%31 = sub nsw i32 %30, 5
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 4
%34 = and i32 %33, 7
%35 = shl i32 %34, 5
%36 = load i32, i32* %3
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 5
%39 = shl i32 1, %38
%40 = sub nsw i32 %36, %39
%41 = load i32, i32* %4
%42 = ashr i32 %40, %41
%43 = add nsw i32 %35, %42
store i32 %43, i32* %5
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
define i32 @scale_to_dynrng(double %0) {
%2 = tail call double @llvm.fabs.f64(double %0)
%3 = fmul double %2, 5.120000e+02
%4 = fptosi double %3 to i32
%5 = icmp slt i32 %4, 8064
%6 = select i1 %5, i32 %4, i32 8064
%7 = icmp sgt i32 %6, 32
%8 = select i1 %7, i32 %6, i32 32
%9 = tail call i32 @log2i(i32 %8)
%10 = add nsw i32 %9, -5
%11 = shl i32 %9, 5
%12 = add i32 %11, 224
%13 = and i32 %12, 224
%14 = shl nsw i32 -1, %9
%15 = add nsw i32 %14, %8
%16 = ashr i32 %15, %10
%17 = add nsw i32 %13, %16
ret i32 %17
}