#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 log10(double);
int v_magsq(float *, int);
float gain_ana(float sigin[], float pitch, int minlength, int maxlength) {
  int length;
  float flength, gain;
  flength = pitch;
  while (flength < minlength)
    flength += pitch;
  length = flength + 0.5;
  if (length > maxlength)
    length = (length / 2);
  gain = 10.0 * log10(0.01 + (v_magsq(&sigin[-(length / 2)], length) / length));
  if (gain < 0.0)
    gain = 0.0;
  return (gain);
}


x86 assembly (clang, -O0):
<gain_ana>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movss  %xmm0,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm1
cvtsi2ssl -0x10(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jbe    1168 <gain_ana+0x48>
movss  -0xc(%rbp),%xmm0
addss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
jmp    1141 <gain_ana+0x21>
movss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe87(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    119e <gain_ana+0x7e>
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
call   1040 <v_magsq@plt>
cltd
idivl  -0x18(%rbp)
cvtsi2sd %eax,%xmm1
movsd  0xe3c(%rip),%xmm0
addsd  %xmm1,%xmm0
call   1030 <log10@plt>
movaps %xmm0,%xmm1
movsd  0xe20(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x20(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    1213 <gain_ana+0xf3>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x20(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gain_ana>:
cvtsi2ss %esi,%xmm1
ucomiss %xmm0,%xmm1
jbe    113b <gain_ana+0x1b>
movaps %xmm0,%xmm2
nopl   0x0(%rax)
addss  %xmm0,%xmm2
ucomiss %xmm2,%xmm1
ja     1130 <gain_ana+0x10>
jmp    113e <gain_ana+0x1e>
movaps %xmm0,%xmm2
push   %rbx
xorps  %xmm0,%xmm0
cvtss2sd %xmm2,%xmm0
addsd  0xeb2(%rip),%xmm0
cvttsd2si %xmm0,%ebx
cmp    %edx,%ebx
jle    1161 <gain_ana+0x41>
mov    %ebx,%eax
shr    $0x1f,%eax
add    %ebx,%eax
sar    %eax
mov    %eax,%ebx
mov    %ebx,%eax
shr    $0x1f,%eax
add    %ebx,%eax
sar    %eax
neg    %eax
cltq
lea    (%rdi,%rax,4),%rdi
mov    %ebx,%esi
call   1040 <v_magsq@plt>
cltd
idiv   %ebx
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
addsd  0xe7d(%rip),%xmm0
call   1030 <log10@plt>
mulsd  0xe78(%rip),%xmm0
xorps  %xmm1,%xmm1
cvtsd2ss %xmm0,%xmm1
xorps  %xmm0,%xmm0
maxss  %xmm1,%xmm0
pop    %rbx
ret

LLVM IR (-O0):
define float @gain_ana(float* %0, float %1, i32 %2, i32 %3) {
%5 = alloca float*
%6 = alloca float
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca float
%11 = alloca float
store float* %0, float** %5
store float %1, float* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load float, float* %6
store float %12, float* %10
br label %13
13: ; preds = %18, %4
%14 = load float, float* %10
%15 = load i32, i32* %7
%16 = sitofp i32 %15 to float
%17 = fcmp olt float %14, %16
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load float, float* %6
%20 = load float, float* %10
%21 = fadd float %20, %19
store float %21, float* %10
br label %13
22: ; preds = %13
%23 = load float, float* %10
%24 = fpext float %23 to double
%25 = fadd double %24, 5.000000e-01
%26 = fptosi double %25 to i32
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = icmp sgt i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = load i32, i32* %9
%32 = sdiv i32 %31, 2
store i32 %32, i32* %9
br label %33
33: ; preds = %30, %22
%34 = load float*, float** %5
%35 = load i32, i32* %9
%36 = sdiv i32 %35, 2
%37 = sub nsw i32 0, %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds float, float* %34, i64 %38
%40 = load i32, i32* %9
%41 = call i32 @v_magsq(float* %39, i32 %40)
%42 = load i32, i32* %9
%43 = sdiv i32 %41, %42
%44 = sitofp i32 %43 to double
%45 = fadd double 1.000000e-02, %44
%46 = call double @log10(double %45)
%47 = fmul double 1.000000e+01, %46
%48 = fptrunc double %47 to float
store float %48, float* %11
%49 = load float, float* %11
%50 = fpext float %49 to double
%51 = fcmp olt double %50, 0.000000e+00
br i1 %51, label %52, label %53
52: ; preds = %33
store float 0.000000e+00, float* %11
br label %53
53: ; preds = %52, %33
%54 = load float, float* %11
ret float %54
}

LLVM IR (-O2):
define float @gain_ana(float* %0, float %1, i32 %2, i32 %3) {
%5 = sitofp i32 %2 to float
%6 = fcmp ogt float %5, %1
br i1 %6, label %7, label %11
7: ; preds = %4, %7
%8 = phi float [ %9, %7 ], [ %1, %4 ]
%9 = fadd float %8, %1
%10 = fcmp olt float %9, %5
br i1 %10, label %7, label %11
11: ; preds = %7, %4
%12 = phi float [ %1, %4 ], [ %9, %7 ]
%13 = fpext float %12 to double
%14 = fadd double %13, 5.000000e-01
%15 = fptosi double %14 to i32
%16 = icmp sgt i32 %15, %3
%17 = sdiv i32 %15, 2
%18 = select i1 %16, i32 %17, i32 %15
%19 = sdiv i32 %18, -2
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds float, float* %0, i64 %20
%22 = tail call i32 @v_magsq(float* %21, i32 %18)
%23 = sdiv i32 %22, %18
%24 = sitofp i32 %23 to double
%25 = fadd double %24, 1.000000e-02
%26 = tail call double @log10(double %25)
%27 = fmul double %26, 1.000000e+01
%28 = fptrunc double %27 to float
%29 = fcmp olt float %28, 0.000000e+00
%30 = select i1 %29, float 0.000000e+00, float %28
ret float %30
}