// File: /workspace/github_cpp_makefile_star2_O1/Sleepwalking[P]world-octave/d4c.cpp
// Function: D4C(double*, int, int, double*, double*, int, int, double**)
void D4C(double *x, int x_length, int fs, double *time_axis, double *f0,
    int f0_length, int fft_size, double **aperiodicity) {
  int fft_size_d4c = static_cast<int>(pow(2.0, 1.0 +
      static_cast<int>(log(4.0 * fs / world::kFloorF0 + 1) / world::kLog2)));

  ForwardRealFFT forward_real_fft = {0};
  InitializeForwardRealFFT(fft_size_d4c, &forward_real_fft);

  int number_of_aperiodicities =
    static_cast<int>(MyMin(world::kUpperLimit, fs / 2.0 -
      world::kFrequencyInterval) / world::kFrequencyInterval);
  // Since the window function is common in D4CGeneralBody(),
  // it is designed here to speed up.
  int window_length =
    static_cast<int>(world::kFrequencyInterval * fft_size_d4c / fs) * 2 + 1;
  double *window =  new double[window_length];
  NuttallWindow(window_length, window);

  double *coarse_aperiodicity = new double[number_of_aperiodicities + 2];
  coarse_aperiodicity[0] = -60.0;
  coarse_aperiodicity[number_of_aperiodicities + 1] = 0.0;
  double *coarse_frequency_axis = new double[number_of_aperiodicities + 2];
  for (int i = 0; i <= number_of_aperiodicities; ++i)
    coarse_frequency_axis[i] =
      static_cast<double>(i) * world::kFrequencyInterval;
  coarse_frequency_axis[number_of_aperiodicities + 1] = fs / 2.0;

  double *frequency_axis = new double[fft_size / 2 + 1];
  for (int i = 0; i <= fft_size / 2; ++i)
    frequency_axis[i] = static_cast<double>(i) * fs / fft_size;
  for (int i = 0; i < f0_length; ++i) {
    if (f0[i] == 0) {
      for (int j = 0; j <= fft_size / 2; ++j) aperiodicity[i][j] = 0.0;
      continue;
    }
//    D4CGeneralBody(x, x_length, fs, f0[i], fft_size_d4c, time_axis[i],
    D4CGeneralBody(x, x_length, fs, MyMax(f0[i], world::kFloorF0),
        fft_size_d4c, time_axis[i], number_of_aperiodicities, window,
        window_length, &forward_real_fft, &coarse_aperiodicity[1]);
    // Linear interpolation to convert the coarse aperiodicity into its
    // spectral representation.
    interp1(coarse_frequency_axis, coarse_aperiodicity,
        number_of_aperiodicities + 2, frequency_axis, fft_size / 2 + 1,
        aperiodicity[i]);
    for (int j = 0; j <= fft_size / 2; ++j)
      aperiodicity[i][j] = pow(10.0, aperiodicity[i][j] / 20.0);
  }

  DestroyForwardRealFFT(&forward_real_fft);
  delete[] coarse_frequency_axis;
  delete[] coarse_aperiodicity;
  delete[] window;
  delete[] frequency_axis;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movl %r9d, %r15d
movq %r8, 0x130(%rsp)
movq %rcx, 0x120(%rsp)
movl %esi, 0x70(%rsp)
movq %rdi, 0x90(%rsp)
movl %edx, 0x64(%rsp)
cvtsi2sd %edx, %xmm1
movsd %xmm1, 0xa0(%rsp)
movsd 0x8e57(%rip), %xmm0     # 0xf3e0
mulsd %xmm1, %xmm0
divsd 0x8de3(%rip), %xmm0     # 0xf378
addsd 0x8a73(%rip), %xmm0     # 0xf010
callq 0x10a0
divsd 0x8a5e(%rip), %xmm0     # 0xf008
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
addsd 0x8a56(%rip), %xmm0     # 0xf010
callq 0x10c0
movapd %xmm0, 0xc0(%rsp)
cvttsd2si %xmm0, %edi
xorpd %xmm0, %xmm0
leaq 0x150(%rsp), %rsi
movapd %xmm0, 0x50(%rsi)
movapd %xmm0, 0x40(%rsi)
movapd %xmm0, 0x30(%rsi)
movapd %xmm0, 0x20(%rsi)
movapd %xmm0, 0x10(%rsi)
movapd %xmm0, (%rsi)
movl %edi, 0x4c(%rsp)
callq 0x1817
movsd 0x8a82(%rip), %xmm1     # 0xf088
movsd 0xa0(%rsp), %xmm2
mulsd %xmm2, %xmm1
movsd 0x8dcd(%rip), %xmm0     # 0xf3e8
movsd %xmm1, 0x58(%rsp)
addsd %xmm1, %xmm0
movsd 0x8dc3(%rip), %xmm1     # 0xf3f0
minsd %xmm0, %xmm1
movsd 0x8dbf(%rip), %xmm3     # 0xf3f8
divsd %xmm3, %xmm1
cvttsd2si %xmm1, %ebp
cvttpd2dq 0xc0(%rsp), %xmm0
cvtdq2pd %xmm0, %xmm0
movapd %xmm0, 0x140(%rsp)
mulsd %xmm3, %xmm0
divsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
leal 0x1(%rax,%rax), %ecx
movl %ecx, 0x74(%rsp)
movslq %ecx, %rbx
leaq (,%rbx,8), %rdi
testl %eax, %eax
movq $-0x1, %r12
cmovsq %r12, %rdi
callq 0x1030
movq %rax, %r14
movl %ebx, %edi
movq %rax, %rsi
callq 0x1755
cmpl $-0x2, %ebp
movslq %ebp, %r13
leaq 0x10(,%r13,8), %rbx
cmovlq %r12, %rbx
movq %rbx, %rdi
callq 0x1030
movabsq $-0x3fb2000000000000, %rcx # imm = 0xC04E000000000000
movq %rcx, (%rax)
movq %rax, 0x80(%rsp)
movq $0x0, 0x8(%rax,%r13,8)
incq %r13
movq %rbx, %rdi
callq 0x1030
movsd 0x8d18(%rip), %xmm1     # 0xf3f8
movq %rax, %rdx
movq %rbp, 0x78(%rsp)
testl %ebp, %ebp
js 0x670d
movq 0x78(%rsp), %rax
incl %eax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x66f5
movq %rdx, 0xd8(%rsp)
movsd 0x58(%rsp), %xmm0
movsd %xmm0, (%rdx,%r13,8)
movl 0x1f0(%rsp), %ebx
movl %ebx, %r13d
shrl $0x1f, %r13d
addl %ebx, %r13d
sarl %r13d
movslq %r13d, %rax
incl %r13d
cmpl $-0x3, %ebx
leaq 0x8(,%rax,8), %rdi
cmovlq %r12, %rdi
callq 0x1030
cmpl $-0x1, %ebx
movsd 0xa0(%rsp), %xmm2
jl 0x6787
xorps %xmm0, %xmm0
cvtsi2sdl 0x1f0(%rsp), %xmm0
xorl %ecx, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd %xmm2, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r13
jne 0x676b
movq %rax, 0xd0(%rsp)
testl %r15d, %r15d
jle 0x6efa
movq 0x1f8(%rsp), %rsi
movq 0x78(%rsp), %rdx
leal 0x2(%rdx), %eax
movl %eax, 0xb8(%rsp)
movl 0x4c(%rsp), %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
leal 0x1(%rax), %ebp
cmpl $-0x3, %ecx
cltq
movq %rax, 0x108(%rsp)
leaq 0x8(,%rax,8), %rax
movq $-0x1, %rdi
movq %rax, 0x100(%rsp)
cmovgeq %rax, %rdi
movq %rdi, 0x88(%rsp)
movl %ecx, %eax
shlq $0x3, %rax
movq %rax, 0xe0(%rsp)
movsd 0x8bfb(%rip), %xmm1     # 0xf400
movl 0x74(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x140(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x118(%rsp)
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
movl %r12d, %eax
sarl %eax
movl %eax, 0xbc(%rsp)
xorl %ecx, %ecx
andl $-0x2, %r12d
cmovlel %ecx, %r12d
movl %edx, %eax
movq %rax, 0xf8(%rsp)
leaq (,%r13,8), %rax
movq %rax, 0x110(%rsp)
movl %r15d, %eax
movq %rax, 0x128(%rsp)
movq %rsi, %r15
incq %r12
movq %r13, 0xe8(%rsp)
movq 0x130(%rsp), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x882a(%rip), %xmm0     # 0xf0b8
movq %rcx, 0xc0(%rsp)
jne 0x68c8
jp 0x68c8
cmpl $-0x1, 0x1f0(%rsp)
jl 0x6ee9
movq (%r15,%rcx,8), %rdi
xorl %esi, %esi
movq 0x110(%rsp), %rdx
callq 0x1080
movq 0xc0(%rsp), %rcx
jmp 0x6ee9
maxsd 0x8aa8(%rip), %xmm0     # 0xf378
movsd %xmm0, 0x50(%rsp)
movq 0x120(%rsp), %rax
movsd (%rax,%rcx,8), %xmm1
movsd %xmm1, 0x58(%rsp)
movq 0x88(%rsp), %r15
movq %r15, %rdi
callq 0x1030
movq %rax, 0x98(%rsp)
movq %r15, %rdi
callq 0x1030
movq %rax, 0xf0(%rsp)
movq %r15, %rdi
callq 0x1030
movq %rax, 0x68(%rsp)
movq %r15, %rdi
callq 0x1030
movq %rax, %rbx
movq %r15, %rdi
callq 0x1030
movq %rax, %r15
movsd 0x8764(%rip), %xmm2     # 0xf0a0
movsd 0x50(%rsp), %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0xb0(%rsp)
movsd 0x58(%rsp), %xmm1
subsd %xmm2, %xmm1
movq 0x90(%rsp), %rdi
movl 0x70(%rsp), %esi
movl 0x64(%rsp), %edx
movl 0x4c(%rsp), %r13d
movl %r13d, %ecx
leaq 0x150(%rsp), %r8
movq %rbx, %r9
callq 0x6f48
movsd 0xb0(%rsp), %xmm1
addsd 0x58(%rsp), %xmm1
movq 0x90(%rsp), %rdi
movl 0x70(%rsp), %esi
movl 0x64(%rsp), %edx
movsd 0x50(%rsp), %xmm0
movl %r13d, %ecx
leaq 0x150(%rsp), %r8
movq %r15, %r9
callq 0x6f48
movq 0x98(%rsp), %rdi
cmpl $-0x1, %r13d
jl 0x69e1
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm0
addsd (%r15,%rax,8), %xmm0
movsd %xmm0, (%rdi,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x69c9
movsd 0x50(%rsp), %xmm0
movl 0x64(%rsp), %esi
movl %r13d, %edx
movq %rdi, %rcx
callq 0x12f9
movq %rbx, %rdi
callq 0x1110
movq %r15, %rdi
callq 0x1110
testl %r13d, %r13d
jle 0x6a22
movq 0x158(%rsp), %rdi
xorl %esi, %esi
movq 0xe0(%rsp), %rdx
callq 0x1080
movq 0x158(%rsp), %r8
movq 0x90(%rsp), %rdi
movl 0x70(%rsp), %esi
movl 0x64(%rsp), %r15d
movl %r15d, %edx
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movl $0x1, %ecx
callq 0x71d0
movl $0x9, %ecx
movq %rsp, %rdi
leaq 0x168(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x7f1a
movl 0x4c(%rsp), %ebx
cmpl $-0x1, %ebx
movq 0xf0(%rsp), %r13
jge 0x6aae
movq %r13, %rdi
movsd 0x50(%rsp), %xmm0
movl %r15d, %esi
movl %ebx, %edx
movq %r13, %rcx
callq 0x12f9
movq %r13, %rdi
movsd 0x50(%rsp), %xmm0
movl %r15d, %esi
movl %ebx, %edx
movq %r13, %rcx
callq 0x1426
jmp 0x6b38
movq 0x160(%rsp), %rax
addq $0x8, %rax
xorl %ecx, %ecx
movsd -0x8(%rax), %xmm0
movsd (%rax), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r13,%rcx,8)
incq %rcx
addq $0x10, %rax
cmpq %rcx, %rbp
jne 0x6abc
movq %r13, %rdi
movsd 0x50(%rsp), %xmm0
movl %r15d, %esi
movl %ebx, %edx
movq %r13, %rcx
callq 0x12f9
movq %r13, %rdi
movsd 0x50(%rsp), %xmm0
movl %r15d, %esi
movl %ebx, %edx
movq %r13, %rcx
callq 0x1426
xorl %eax, %eax
movq 0x98(%rsp), %rcx
movq 0x68(%rsp), %rdx
movsd (%rcx,%rax,8), %xmm0
divsd (%r13,%rax,8), %xmm0
movsd %xmm0, (%rdx,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x6b1f
movsd 0x50(%rsp), %xmm0
mulsd 0x8542(%rip), %xmm0     # 0xf088
movq 0x68(%rsp), %rdi
movl %r15d, %esi
movl 0x4c(%rsp), %r13d
movl %r13d, %edx
movq 0x68(%rsp), %rcx
callq 0x1426
movq 0x88(%rsp), %rdi
callq 0x1030
movq %rax, %rbx
movq 0x68(%rsp), %rdi
movsd 0x50(%rsp), %xmm0
movl %r15d, %esi
movl %r13d, %edx
movq %rax, %rcx
callq 0x1426
movq 0x68(%rsp), %rcx
cmpl $-0x1, %r13d
jl 0x6bad
xorl %eax, %eax
movsd (%rcx,%rax,8), %xmm0
subsd (%rbx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x6b96
movq %rbx, %rdi
callq 0x1110
movsd 0x118(%rsp), %xmm0
callq 0x45c3
movl %eax, 0xa8(%rsp)
movq 0x88(%rsp), %rdi
callq 0x1030
movq %rax, %rbx
cmpl $0x0, 0x4c(%rsp)
movq 0xe8(%rsp), %r13
movq 0x1f8(%rsp), %r15
jle 0x6c08
movq 0x158(%rsp), %rdi
xorl %esi, %esi
movq 0xe0(%rsp), %rdx
callq 0x1080
cmpl $0x0, 0x78(%rsp)
jle 0x6e24
movq 0x108(%rsp), %rcx
leaq (%rbx,%rcx,8), %rax
movq %rax, 0xb0(%rsp)
leaq (%rbx,%rcx,8), %rax
addq $0x8, %rax
movq %rax, 0x58(%rsp)
movq 0x100(%rsp), %rax
sarq $0x3, %rax
bsrq %rax, %rax
xorl $0x3f, %eax
addl %eax, %eax
xorq $0x7e, %rax
movq %rax, 0x138(%rsp)
movl 0xa8(%rsp), %eax
notl %eax
addl %ecx, %eax
cltq
movq %rax, 0xa8(%rsp)
xorl %r15d, %r15d
leaq 0x1(%r15), %r13
cmpl $-0x1, 0x74(%rsp)
jl 0x6cd3
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
mulsd 0x8770(%rip), %xmm0     # 0xf3f8
mulsd 0x140(%rsp), %xmm0
divsd 0xa0(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
subl 0xbc(%rsp), %ecx
movq 0x158(%rsp), %rax
movslq %ecx, %rcx
movq 0x68(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm0
mulsd (%r14,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %r12
jne 0x6cbb
movl $0x9, %ecx
movq %rsp, %rdi
leaq 0x168(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x7f1a
movl 0x4c(%rsp), %edx
cmpl $-0x1, %edx
jl 0x6d28
movq 0x160(%rsp), %rax
addq $0x8, %rax
xorl %ecx, %ecx
movsd -0x8(%rax), %xmm0
movsd (%rax), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rbx,%rcx,8)
incq %rcx
addq $0x10, %rax
cmpq %rcx, %rbp
jne 0x6d02
cmpq 0x58(%rsp), %rbx
je 0x6d55
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movq 0x138(%rsp), %rdx
callq 0x75a5
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
callq 0x769c
movl 0x4c(%rsp), %edx
cmpl $0x2, %edx
jl 0x6d75
movsd (%rbx), %xmm0
movl $0x1, %eax
addsd (%rbx,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x6d63
movq 0xa8(%rsp), %rax
movsd (%rbx,%rax,8), %xmm0
movq 0xb0(%rsp), %rax
divsd (%rax), %xmm0
callq 0x1070
mulsd 0x866d(%rip), %xmm0     # 0xf408
movq 0x80(%rsp), %rax
movsd %xmm0, 0x8(%rax,%r15,8)
movq %r13, %r15
cmpq 0xf8(%rsp), %r13
jne 0x6c6d
movq %rbx, %rdi
callq 0x1110
cmpl $0x0, 0x78(%rsp)
movq 0x80(%rsp), %rbx
movq 0xe8(%rsp), %r13
movq 0x1f8(%rsp), %r15
movq 0xf8(%rsp), %rcx
movsd 0x50(%rsp), %xmm2
jle 0x6e34
addsd 0x8618(%rip), %xmm2     # 0xf410
divsd 0x8290(%rip), %xmm2     # 0xf090
xorl %eax, %eax
movsd 0x8(%rbx,%rax,8), %xmm0
addsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
minsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rbx,%rax,8)
incq %rax
cmpq %rax, %rcx
jne 0x6e02
jmp 0x6e34
movq %rbx, %rdi
callq 0x1110
movq 0x80(%rsp), %rbx
movq 0x98(%rsp), %rdi
callq 0x1110
movq 0xf0(%rsp), %rdi
callq 0x1110
movq 0x68(%rsp), %rdi
callq 0x1110
movq 0xc0(%rsp), %rax
movq (%r15,%rax,8), %r9
movq 0xd8(%rsp), %rdi
movq %rbx, %rsi
movl 0xb8(%rsp), %edx
movq 0xd0(%rsp), %rcx
movl %r13d, %r8d
callq 0x41a9
movq 0xc0(%rsp), %rcx
cmpl $-0x1, 0x1f0(%rsp)
movsd 0x856a(%rip), %xmm2     # 0xf408
movsd 0x8572(%rip), %xmm0     # 0xf418
jl 0x6ee9
xorl %ebx, %ebx
movq (%r15,%rcx,8), %rax
movsd (%rax,%rbx,8), %xmm1
divsd %xmm0, %xmm1
movapd %xmm2, %xmm0
callq 0x1190
movsd 0x8540(%rip), %xmm2     # 0xf408
movq 0xc0(%rsp), %rcx
movq (%r15,%rcx,8), %rax
movsd %xmm0, (%rax,%rbx,8)
movsd 0x8537(%rip), %xmm0     # 0xf418
incq %rbx
cmpq %rbx, %r13
jne 0x6eaa
incq %rcx
cmpq 0x128(%rsp), %rcx
jne 0x6879
leaq 0x150(%rsp), %rdi
callq 0x189b
movq 0xd8(%rsp), %rdi
callq 0x1110
movq 0x80(%rsp), %rdi
callq 0x1110
movq %r14, %rdi
callq 0x1110
movq 0xd0(%rsp), %rdi
callq 0x1110
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq