// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
cvtsi2sdl -0x10(%rbp), %xmm1
movsd 0x11bab(%rip), %xmm0    # 0x1c0d0
mulsd %xmm1, %xmm0
movsd 0x11b5f(%rip), %xmm1    # 0x1c090
divsd %xmm1, %xmm0
movsd 0x11ad3(%rip), %xmm1    # 0x1c010
addsd %xmm1, %xmm0
callq 0x10a0
movsd 0x11aca(%rip), %xmm1    # 0x1c018
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
movsd 0x11aae(%rip), %xmm1    # 0x1c010
addsd %xmm0, %xmm1
movsd 0x11a9a(%rip), %xmm0    # 0x1c008
callq 0x1180
cvttsd2si %xmm0, %eax
movl %eax, -0x28(%rbp)
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x60, %edx
callq 0x1080
movl -0x28(%rbp), %edi
leaq -0x88(%rbp), %rsi
callq 0x1ba0
cvtsi2sdl -0x10(%rbp), %xmm1
movsd 0x11a5f(%rip), %xmm0    # 0x1c008
divsd %xmm0, %xmm1
movsd 0x11e5b(%rip), %xmm0    # 0x1c410
subsd %xmm0, %xmm1
movsd 0x11e57(%rip), %xmm0    # 0x1c418
callq 0xa4b0
movsd 0x11e42(%rip), %xmm1    # 0x1c410
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x8c(%rbp)
cvtsi2sdl -0x28(%rbp), %xmm1
movsd 0x11e27(%rip), %xmm0    # 0x1c410
mulsd %xmm1, %xmm0
cvtsi2sdl -0x10(%rbp), %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
movslq -0x90(%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, -0x98(%rbp)
movl -0x90(%rbp), %edi
movq -0x98(%rbp), %rsi
callq 0x1ac0
movl -0x8c(%rbp), %eax
addl $0x2, %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, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movsd 0x11d80(%rip), %xmm0    # 0x1c408
movsd %xmm0, (%rax)
movq -0xa0(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x8c(%rbp), %eax
addl $0x2, %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, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jg 0xa72a
cvtsi2sdl -0xac(%rbp), %xmm0
movsd 0x11d0e(%rip), %xmm1    # 0x1c410
mulsd %xmm1, %xmm0
movq -0xa8(%rbp), %rax
movslq -0xac(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xa6e4
cvtsi2sdl -0x10(%rbp), %xmm0
movsd 0x118d1(%rip), %xmm1    # 0x1c008
divsd %xmm1, %xmm0
movq -0xa8(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x10(%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, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl 0x10(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xcc(%rbp), %eax
cmpl %ecx, %eax
jg 0xa7f6
cvtsi2sdl -0xbc(%rbp), %xmm0
cvtsi2sdl -0x10(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl 0x10(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0xb8(%rbp), %rax
movslq -0xbc(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0xa795
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0xaa1e
movq -0x20(%rbp), %rax
movslq -0xc0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xa88b
jp 0xa88b
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0xd0(%rbp)
movl 0x10(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xd0(%rbp), %eax
cmpl %ecx, %eax
jg 0xa886
movq 0x18(%rbp), %rax
movslq -0xc0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0xc4(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xa834
jmp 0xaa0a
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0xd8(%rbp)
movq -0x20(%rbp), %rax
movslq -0xc0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x117d0(%rip), %xmm1    # 0x1c090
callq 0xa470
movq -0xe8(%rbp), %rdi
movl -0xdc(%rbp), %esi
movl -0xd8(%rbp), %edx
movl -0x28(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0xc0(%rbp), %r8
movsd (%rax,%r8,8), %xmm1
movl -0x8c(%rbp), %r8d
movq -0x98(%rbp), %r9
movl -0x90(%rbp), %r11d
movq -0xa0(%rbp), %rax
addq $0x8, %rax
leaq -0x88(%rbp), %r10
movl %r11d, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xaac0
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movl -0x8c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0xd4(%rbp)
movq -0xb8(%rbp), %rcx
movl 0x10(%rbp), %eax
movl $0x2, %r8d
cltd
idivl %r8d
movl -0xd4(%rbp), %edx
movl %eax, %r8d
addl $0x1, %r8d
movq 0x18(%rbp), %rax
movslq -0xc0(%rbp), %r9
movq (%rax,%r9,8), %r9
callq 0x68d0
movl $0x0, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0xec(%rbp)
movl 0x10(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xec(%rbp), %eax
cmpl %ecx, %eax
jg 0xaa08
movq 0x18(%rbp), %rax
movslq -0xc0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0xc8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd 0x11a60(%rip), %xmm0    # 0x1c428
divsd %xmm0, %xmm1
movsd 0x11a4c(%rip), %xmm0    # 0x1c420
callq 0x1180
movq 0x18(%rbp), %rax
movslq -0xc0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0xc8(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0xa982
jmp 0xaa0a
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0xa800
leaq -0x88(%rbp), %rdi
callq 0x1c60
movq -0xa8(%rbp), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, %rax
je 0xaa4a
movq -0xf8(%rbp), %rdi
callq 0x1110
movq -0xa0(%rbp), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, %rax
je 0xaa6a
movq -0x100(%rbp), %rdi
callq 0x1110
movq -0x98(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, %rax
je 0xaa8a
movq -0x108(%rbp), %rdi
callq 0x1110
movq -0xb8(%rbp), %rax
movq %rax, -0x110(%rbp)
cmpq $0x0, %rax
je 0xaaaa
movq -0x110(%rbp), %rdi
callq 0x1110
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)