// File: Sleepwalking[P]world-octave/d4c.cpp
// Function: (anonymous namespace)::GetCoarseAperiodicity(double*, int, double, int, int, double*, int, ForwardRealFFT*, double*)
void GetCoarseAperiodicity(double *static_group_delay, int fs,
    double current_f0, int fft_size, int number_of_aperiodicities,
    double *window, int window_length, ForwardRealFFT *forward_real_fft,
    double *coarse_aperiodicity) {
  int boundary =
    matlab_round(fft_size * 8.0 / window_length);
  int half_window_length = static_cast<int>(window_length / 2);

  double *power_spectrum = new double[fft_size / 2 + 1];
  int center;
  for (int i = 0; i < fft_size; ++i) forward_real_fft->waveform[i] = 0.0;

  for (int i = 0; i < number_of_aperiodicities; ++i) {
    center =
      static_cast<int>(world::kFrequencyInterval * (i + 1) * fft_size / fs);
    for (int j = 0; j <= half_window_length * 2; ++j)
      forward_real_fft->waveform[j] =
        static_group_delay[center - half_window_length + j] * window[j];
    fft_execute(forward_real_fft->forward_fft);
    for (int j = 0 ; j <= fft_size / 2; ++j)
      power_spectrum[j] =
        forward_real_fft->spectrum[j][0] * forward_real_fft->spectrum[j][0] +
        forward_real_fft->spectrum[j][1] * forward_real_fft->spectrum[j][1];
    std::sort(power_spectrum, power_spectrum + fft_size / 2 + 1);
    for (int j = 1 ; j <= fft_size / 2; ++j)
      power_spectrum[j] += power_spectrum[j - 1];
    coarse_aperiodicity[i] =
      10 * log10(power_spectrum[fft_size / 2 - boundary - 1] /
                 power_spectrum[fft_size / 2]);
  }
  delete[] power_spectrum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movsd %xmm0, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd 0x1124e(%rip), %xmm1    # 0x1c438
mulsd %xmm1, %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x72e0
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x34(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x40(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xb270
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x48(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xb249
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0xb528
movl -0x4c(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x1117b(%rip), %xmm0    # 0x1c410
mulsd %xmm1, %xmm0
cvtsi2sdl -0x1c(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl -0xc(%rbp), %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl -0x34(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jg 0xb303
movq -0x8(%rbp), %rax
movl -0x44(%rbp), %ecx
subl -0x34(%rbp), %ecx
addl -0x50(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movslq -0x50(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xb2b9
movq 0x10(%rbp), %rsi
addq $0x18, %rsi
leaq -0x98(%rbp), %rdi
movl $0x48, %edx
callq 0x10b0
movq -0x58(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x40(%rax)
movups -0x98(%rbp), %xmm0
movups -0x88(%rbp), %xmm1
movups -0x78(%rbp), %xmm2
movups -0x68(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0xd500
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xa4(%rbp), %eax
cmpl %ecx, %eax
jg 0xb418
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x9c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x9c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm2
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x9c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm1
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x9c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulsd 0x8(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x40(%rbp), %rax
movslq -0x9c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0xb35b
movq -0x40(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cltq
shlq $0x3, %rax
addq %rax, %rsi
addq $0x8, %rsi
callq 0xbf40
movl $0x1, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xa8(%rbp), %eax
cmpl %ecx, %eax
jg 0xb4a5
movq -0x40(%rbp), %rax
movl -0xa0(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x40(%rbp), %rax
movslq -0xa0(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0xb447
movq -0x40(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0xb8(%rbp), %rax
subl -0x30(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0xb0(%rbp), %rax
movslq %ecx, %rcx
divsd (%rax,%rcx,8), %xmm0
callq 0x1070
movaps %xmm0, %xmm1
movsd 0x10f17(%rip), %xmm0    # 0x1c420
mulsd %xmm1, %xmm0
movq 0x18(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xb277
movq -0x40(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0xb545
movq -0xc0(%rbp), %rdi
callq 0x1110
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop