// File: Sleepwalking[P]world-octave/d4c.cpp
// Function: (anonymous namespace)::GetCentroid(double*, int, int, double, int, double, ForwardRealFFT*, double*)
void GetCentroid(double *x, int x_length, int fs, double current_f0,
    int fft_size, double temporal_position, ForwardRealFFT *forward_real_fft,
    double *centroid) {
  for (int i = 0; i < fft_size; ++i) forward_real_fft->waveform[i] = 0.0;
  GetWindowedWaveform(x, x_length, fs, current_f0,
      temporal_position, world::kBlackman, 4.0, forward_real_fft->waveform);
  double power = 0.0;
  for (int i = 0; i <= matlab_round(2.0 * fs / current_f0) * 2; ++i)
    power += forward_real_fft->waveform[i] * forward_real_fft->waveform[i];
  for (int i = 0; i <= matlab_round(2.0 * fs / current_f0) * 2; ++i)
    forward_real_fft->waveform[i] /= sqrt(power);

  fft_execute(forward_real_fft->forward_fft);
  double *tmp_real = new double[fft_size / 2 + 1];
  double *tmp_imag = new double[fft_size / 2 + 1];
  for (int i = 0; i <= fft_size / 2; ++i) {
    tmp_real[i] = forward_real_fft->spectrum[i][0];
    tmp_imag[i] = forward_real_fft->spectrum[i][1];
  }

  for (int i = 0; i < fft_size; ++i)
    forward_real_fft->waveform[i] *= i + 1.0;
  fft_execute(forward_real_fft->forward_fft);
  for (int i = 0; i <= fft_size / 2; ++i)
    centroid[i] = forward_real_fft->spectrum[i][0] * tmp_real[i] +
      tmp_imag[i] * forward_real_fft->spectrum[i][1];

  delete[] tmp_real;
  delete[] tmp_imag;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movsd %xmm1, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xb5a8
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xb581
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movsd -0x18(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
movq -0x30(%rbp), %rax
movq 0x8(%rax), %r8
movl $0x2, %ecx
movsd 0x10aff(%rip), %xmm2    # 0x1c0d0
callq 0xb9d0
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x100(%rbp)
cvtsi2sdl -0x10(%rbp), %xmm1
movsd 0x10a0d(%rip), %xmm0    # 0x1c008
mulsd %xmm1, %xmm0
divsd -0x18(%rbp), %xmm0
callq 0x72e0
movl %eax, %ecx
movl -0x100(%rbp), %eax
shll %ecx
cmpl %ecx, %eax
jg 0xb656
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x48(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xb5e5
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x104(%rbp)
cvtsi2sdl -0x10(%rbp), %xmm1
movsd 0x10995(%rip), %xmm0    # 0x1c008
mulsd %xmm1, %xmm0
divsd -0x18(%rbp), %xmm0
callq 0x72e0
movl %eax, %ecx
movl -0x104(%rbp), %eax
shll %ecx
cmpl %ecx, %eax
jg 0xb6c1
movsd -0x48(%rbp), %xmm0
callq 0x1090
movaps %xmm0, %xmm1
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xb65d
movq -0x30(%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 -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, -0xa0(%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, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0x108(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x108(%rbp), %eax
cmpl %ecx, %eax
jg 0xb81b
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xac(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0xa0(%rbp), %rax
movslq -0xac(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xac(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %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 0xb789
movl $0x0, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xb86e
cvtsi2sdl -0xb0(%rbp), %xmm0
movsd 0x107d0(%rip), %xmm1    # 0x1c010
addsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0xb0(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0xb825
movq -0x30(%rbp), %rsi
addq $0x18, %rsi
leaq -0xf8(%rbp), %rdi
movl $0x48, %edx
callq 0x10b0
movq -0xb8(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x40(%rax)
movups -0xf8(%rbp), %xmm0
movups -0xe8(%rbp), %xmm1
movups -0xd8(%rbp), %xmm2
movups -0xc8(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0xd500
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, -0x10c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x10c(%rbp), %eax
cmpl %ecx, %eax
jg 0xb97d
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xfc(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0xa0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0xa8(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xfc(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulsd 0x8(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0xb8cf
movq -0xa0(%rbp), %rax
movq %rax, -0x118(%rbp)
cmpq $0x0, %rax
je 0xb99d
movq -0x118(%rbp), %rdi
callq 0x1110
movq -0xa8(%rbp), %rax
movq %rax, -0x120(%rbp)
cmpq $0x0, %rax
je 0xb9bd
movq -0x120(%rbp), %rdi
callq 0x1110
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw %cs:(%rax,%rax)