#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 SKP_Silk_CLZ16(int const);
int SKP_Silk_CLZ32(int);
int SKP_Silk_int16_array_maxabs(short const *, int const);
int SKP_FIX_P_Ana_find_scaling(const short *signal, const int signal_length,
                               const int sum_sqr_len) {
  int nbits, x_max;
  x_max = SKP_Silk_int16_array_maxabs(signal, signal_length);
  if (x_max < 0x7FFF) {
    nbits =
        32 - SKP_Silk_CLZ32(((int)((short)(x_max)) * (int)((short)(x_max))));
  } else {
    nbits = 30;
  }
  nbits += 17 - SKP_Silk_CLZ16(sum_sqr_len);
  if (nbits < 31) {
    return 0;
  } else {
    return (nbits - 30);
  }
}


x86 assembly (clang, -O0):
<SKP_FIX_P_Ana_find_scaling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <SKP_Silk_int16_array_maxabs@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x7fff,-0x20(%rbp)
jge    1181 <SKP_FIX_P_Ana_find_scaling+0x51>
mov    -0x20(%rbp),%eax
movswl %ax,%edi
mov    -0x20(%rbp),%eax
cwtl
imul   %eax,%edi
call   1030 <SKP_Silk_CLZ32@plt>
mov    %eax,%ecx
mov    $0x20,%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1188 <SKP_FIX_P_Ana_find_scaling+0x58>
movl   $0x1e,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <SKP_Silk_CLZ16@plt>
mov    %eax,%ecx
mov    $0x11,%eax
sub    %ecx,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1f,-0x1c(%rbp)
jge    11b5 <SKP_FIX_P_Ana_find_scaling+0x85>
movl   $0x0,-0x4(%rbp)
jmp    11be <SKP_FIX_P_Ana_find_scaling+0x8e>
mov    -0x1c(%rbp),%eax
sub    $0x1e,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SKP_FIX_P_Ana_find_scaling>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
call   1050 <SKP_Silk_int16_array_maxabs@plt>
mov    $0x1e,%ebx
cmp    $0x7ffe,%eax
jg     1158 <SKP_FIX_P_Ana_find_scaling+0x28>
movswl %ax,%edi
imul   %edi,%edi
call   1030 <SKP_Silk_CLZ32@plt>
mov    $0x20,%ebx
sub    %eax,%ebx
mov    %ebp,%edi
call   1040 <SKP_Silk_CLZ16@plt>
sub    %eax,%ebx
lea    0x11(%rbx),%ecx
add    $0xfffffff3,%ebx
xor    %eax,%eax
cmp    $0x1f,%ecx
cmovge %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @SKP_FIX_P_Ana_find_scaling(i16* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i16*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i16* %0, i16** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i16*, i16** %5
%11 = load i32, i32* %6
%12 = call i32 @SKP_Silk_int16_array_maxabs(i16* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 32767
br i1 %14, label %15, label %25
15: ; preds = %3
%16 = load i32, i32* %9
%17 = trunc i32 %16 to i16
%18 = sext i16 %17 to i32
%19 = load i32, i32* %9
%20 = trunc i32 %19 to i16
%21 = sext i16 %20 to i32
%22 = mul nsw i32 %18, %21
%23 = call i32 @SKP_Silk_CLZ32(i32 %22)
%24 = sub nsw i32 32, %23
store i32 %24, i32* %8
br label %26
25: ; preds = %3
store i32 30, i32* %8
br label %26
26: ; preds = %25, %15
%27 = load i32, i32* %7
%28 = call i32 @SKP_Silk_CLZ16(i32 %27)
%29 = sub nsw i32 17, %28
%30 = load i32, i32* %8
%31 = add nsw i32 %30, %29
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 31
br i1 %33, label %34, label %35
34: ; preds = %26
store i32 0, i32* %4
br label %38
35: ; preds = %26
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 30
store i32 %37, i32* %4
br label %38
38: ; preds = %35, %34
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
define i32 @SKP_FIX_P_Ana_find_scaling(i16* %0, i32 %1, i32 %2) {
%4 = tail call i32 @SKP_Silk_int16_array_maxabs(i16* %0, i32 %1)
%5 = icmp slt i32 %4, 32767
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = shl i32 %4, 16
%8 = ashr exact i32 %7, 16
%9 = mul nsw i32 %8, %8
%10 = tail call i32 @SKP_Silk_CLZ32(i32 %9)
%11 = sub nsw i32 32, %10
br label %12
12: ; preds = %3, %6
%13 = phi i32 [ %11, %6 ], [ 30, %3 ]
%14 = tail call i32 @SKP_Silk_CLZ16(i32 %2)
%15 = sub nsw i32 17, %14
%16 = add nsw i32 %15, %13
%17 = icmp slt i32 %16, 31
%18 = add nsw i32 %16, -30
%19 = select i1 %17, i32 0, i32 %18
ret i32 %19
}