// File: Sleepwalking[P]world-octave/stonemask.cpp
// Function: (anonymous namespace)::GetMeanF0(double*, int, int, double, double, int, double, double*, int)
double GetMeanF0(double *x, int x_length, int fs, double current_time,
    double f0_initial, int fft_size, double window_length_in_time,
    double *base_time, int base_time_length) {
  ForwardRealFFT forward_real_fft = {0};
  InitializeForwardRealFFT(fft_size, &forward_real_fft);
  fft_complex *main_spectrum = new fft_complex[fft_size];
  fft_complex *diff_spectrum = new fft_complex[fft_size];

  double *power_spectrum = new double[fft_size];
  double *numerator_i = new double[fft_size];
  for (int i = 0; i <= fft_size / 2; ++i) {
    power_spectrum[i] = 0.0;
    numerator_i[i] = 0.0;
  }

  int *index_raw = new int[base_time_length];
  double *main_window = new double[base_time_length];
  double *diff_window = new double[base_time_length];

  double center_location_list[2];
  center_location_list[0] = (1.0 / 4.0 - 0.5) / f0_initial;
  center_location_list[1] = (3.0 / 4.0 - 0.5) / f0_initial;

  for (int i = 0; i < 2; i++) {
    GetIndexRaw(current_time, base_time, base_time_length, fs,
        center_location_list[i], index_raw);
    GetMainWindow(current_time, index_raw, base_time_length, fs,
        center_location_list[i], window_length_in_time, main_window);
    GetDiffWindow(main_window, base_time_length, diff_window);
    GetSpectra(x, x_length, fft_size, index_raw, main_window, diff_window,
        base_time_length, &forward_real_fft, main_spectrum, diff_spectrum);

    for (int j = 0; j <= fft_size / 2; ++j) {
      numerator_i[j] += main_spectrum[j][0] * diff_spectrum[j][1] -
        main_spectrum[j][1] * diff_spectrum[j][0];
      power_spectrum[j] += main_spectrum[j][0] * main_spectrum[j][0] +
        main_spectrum[j][1] * main_spectrum[j][1];
    }
  }

  double tentative_f0 = GetTentativeF0(power_spectrum, numerator_i,
      fft_size, fs, f0_initial);

  delete[] diff_spectrum;
  delete[] diff_window;
  delete[] main_window;
  delete[] index_raw;
  delete[] numerator_i;
  delete[] power_spectrum;
  delete[] main_spectrum;
  DestroyForwardRealFFT(&forward_real_fft);

  return tentative_f0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd %xmm1, -0x28(%rbp)
movl %ecx, -0x2c(%rbp)
movsd %xmm2, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movl %r9d, -0x44(%rbp)
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
movl $0x60, %edx
callq 0x1080
movl -0x2c(%rbp), %edi
leaq -0xa8(%rbp), %rsi
callq 0x1ba0
movslq -0x2c(%rbp), %rax
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0xb0(%rbp)
movslq -0x2c(%rbp), %rax
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0xb8(%rbp)
movslq -0x2c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0xc0(%rbp)
movslq -0x2c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movl %eax, -0x114(%rbp)
movl -0x2c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x114(%rbp), %eax
cmpl %ecx, %eax
jg 0x968c
movq -0xc0(%rbp), %rax
movslq -0xcc(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0xc8(%rbp), %rax
movslq -0xcc(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x962c
movslq -0x44(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0xd8(%rbp)
movslq -0x44(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0xe0(%rbp)
movslq -0x44(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0xe8(%rbp)
movsd 0x12cd8(%rip), %xmm0    # 0x1c3f0
divsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x100(%rbp)
movsd 0x12cbb(%rip), %xmm0    # 0x1c3e8
divsd -0x28(%rbp), %xmm0
movsd %xmm0, -0xf8(%rbp)
movl $0x0, -0x104(%rbp)
cmpl $0x2, -0x104(%rbp)
jge 0x9991
movsd -0x20(%rbp), %xmm0
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x18(%rbp), %edx
movslq -0x104(%rbp), %rax
movsd -0x100(%rbp,%rax,8), %xmm1
movq -0xd8(%rbp), %rcx
callq 0x9ac0
movsd -0x20(%rbp), %xmm0
movq -0xd8(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x18(%rbp), %edx
movslq -0x104(%rbp), %rax
movsd -0x100(%rbp,%rax,8), %xmm1
movsd -0x38(%rbp), %xmm2
movq -0xe0(%rbp), %rcx
callq 0x9b40
movq -0xe0(%rbp), %rdi
movl -0x44(%rbp), %esi
movq -0xe8(%rbp), %rdx
callq 0x9c40
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x2c(%rbp), %edx
movq -0xd8(%rbp), %rcx
movq -0xe0(%rbp), %r8
movq -0xe8(%rbp), %r9
movl -0x44(%rbp), %ebx
movq -0xb0(%rbp), %r10
movq -0xb8(%rbp), %rax
leaq -0xa8(%rbp), %r11
movl %ebx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x9d30
movl $0x0, -0x108(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0x118(%rbp)
movl -0x2c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x118(%rbp), %eax
cmpl %ecx, %eax
jg 0x997b
movq -0xb0(%rbp), %rax
movslq -0x108(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0xb8(%rbp), %rax
movslq -0x108(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm2
movq -0xb0(%rbp), %rax
movslq -0x108(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm1
movq -0xb8(%rbp), %rax
movslq -0x108(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulsd (%rax), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0xc8(%rbp), %rax
movslq -0x108(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0xb0(%rbp), %rax
movslq -0x108(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0xb0(%rbp), %rax
movslq -0x108(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm2
movq -0xb0(%rbp), %rax
movslq -0x108(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm1
movq -0xb0(%rbp), %rax
movslq -0x108(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulsd 0x8(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0xc0(%rbp), %rax
movslq -0x108(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x108(%rbp)
jmp 0x981d
jmp 0x997d
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x9744
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movl -0x2c(%rbp), %edx
movl -0x18(%rbp), %ecx
movsd -0x28(%rbp), %xmm0
callq 0xa130
movsd %xmm0, -0x110(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x120(%rbp)
cmpq $0x0, %rax
je 0x99d7
movq -0x120(%rbp), %rdi
callq 0x1110
movq -0xe8(%rbp), %rax
movq %rax, -0x128(%rbp)
cmpq $0x0, %rax
je 0x99f7
movq -0x128(%rbp), %rdi
callq 0x1110
movq -0xe0(%rbp), %rax
movq %rax, -0x130(%rbp)
cmpq $0x0, %rax
je 0x9a17
movq -0x130(%rbp), %rdi
callq 0x1110
movq -0xd8(%rbp), %rax
movq %rax, -0x138(%rbp)
cmpq $0x0, %rax
je 0x9a37
movq -0x138(%rbp), %rdi
callq 0x1110
movq -0xc8(%rbp), %rax
movq %rax, -0x140(%rbp)
cmpq $0x0, %rax
je 0x9a57
movq -0x140(%rbp), %rdi
callq 0x1110
movq -0xc0(%rbp), %rax
movq %rax, -0x148(%rbp)
cmpq $0x0, %rax
je 0x9a77
movq -0x148(%rbp), %rdi
callq 0x1110
movq -0xb0(%rbp), %rax
movq %rax, -0x150(%rbp)
cmpq $0x0, %rax
je 0x9a97
movq -0x150(%rbp), %rdi
callq 0x1110
leaq -0xa8(%rbp), %rdi
callq 0x1c60
movsd -0x110(%rbp), %xmm0
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)