// File: Sleepwalking[P]world-octave/stonemask.cpp
// Function: (anonymous namespace)::GetRefinedF0(double*, int, int, double, double)
double GetRefinedF0(double *x, int x_length, int fs, double current_time,
    double current_f0) {
  if (current_f0 == 0.0)
    return 0.0;

  double f0_initial = MyMax(world::kFloorF0,
      MyMin(world::kCeilF0, current_f0));
  int half_window_length = static_cast<int>(3.0 * static_cast<double>(fs)
    / f0_initial / 2.0 + 1.0);
  double window_length_in_time = (2.0 *
    static_cast<double>(half_window_length) + 1) /
    static_cast<double>(fs);
  double *base_time = new double[half_window_length * 2 + 1];
  for (int i = 0; i < half_window_length * 2 + 1; i++) {
    base_time[i] = static_cast<double>(-half_window_length + i) / fs;
  }
  int fft_size = static_cast<int>(pow(2.0, 2.0 +
    static_cast<int>(log(half_window_length * 2.0 + 1.0) / world::kLog2)));

  double mean_f0 = GetMeanF0(x, x_length, fs, current_time,
      f0_initial, fft_size, window_length_in_time, base_time,
      half_window_length * 2 + 1);

  // If amount of correction is overlarge (20 %), initial F0 is employed.
  if (fabs(mean_f0 - f0_initial) / f0_initial > 0.2) mean_f0 = f0_initial;

  delete[] base_time;

  return mean_f0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd %xmm1, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x9349
jp 0x9349
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x9510
movsd -0x28(%rbp), %xmm1
movsd 0x12d42(%rip), %xmm0    # 0x1c098
callq 0xa4b0
movaps %xmm0, %xmm1
movsd 0x12d2a(%rip), %xmm0    # 0x1c090
callq 0xa470
movsd %xmm0, -0x30(%rbp)
cvtsi2sdl -0x18(%rbp), %xmm1
movsd 0x12d4b(%rip), %xmm0    # 0x1c0c8
mulsd %xmm1, %xmm0
divsd -0x30(%rbp), %xmm0
movsd 0x12c7a(%rip), %xmm1    # 0x1c008
divsd %xmm1, %xmm0
movsd 0x12c76(%rip), %xmm1    # 0x1c010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x34(%rbp)
cvtsi2sdl -0x34(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd 0x12c5a(%rip), %xmm1    # 0x1c010
addsd %xmm1, %xmm0
cvtsi2sdl -0x18(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x34(%rbp), %eax
shll %eax
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, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl -0x34(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x943a
xorl %eax, %eax
subl -0x34(%rbp), %eax
addl -0x4c(%rbp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0x18(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x93fe
cvtsi2sdl -0x34(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd 0x12bc5(%rip), %xmm1    # 0x1c010
addsd %xmm1, %xmm0
callq 0x10a0
movsd 0x12bbc(%rip), %xmm1    # 0x1c018
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
movsd 0x12b98(%rip), %xmm1    # 0x1c008
addsd %xmm0, %xmm1
movsd 0x12b8c(%rip), %xmm0    # 0x1c008
callq 0x1180
cvttsd2si %xmm0, %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movsd -0x20(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
movl -0x50(%rbp), %ecx
movsd -0x40(%rbp), %xmm2
movq -0x48(%rbp), %r8
movl -0x34(%rbp), %r9d
shll %r9d
addl $0x1, %r9d
callq 0x9520
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
movaps 0x12c12(%rip), %xmm1    # 0x1c0e0
pand %xmm1, %xmm0
divsd -0x30(%rbp), %xmm0
movsd 0x12f01(%rip), %xmm1    # 0x1c3e0
ucomisd %xmm1, %xmm0
jbe 0x94ef
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x9506
movq -0x60(%rbp), %rdi
callq 0x1110
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)