// File: Sleepwalking[P]world-octave/cheaptrick.cpp
// Function: (anonymous namespace)::GetWindowedWaveform(double*, int, int, double, double, ForwardRealFFT*)
void GetWindowedWaveform(double *x, int x_length, int fs, double current_f0,
    double temporal_position, ForwardRealFFT *forward_real_fft) {
  int half_window_length = matlab_round(1.5 * fs / current_f0);

  int *base_index = new int[half_window_length * 2 + 1];
  int *index = new int[half_window_length * 2 + 1];
  double *window  = new double[half_window_length * 2 + 1];

  SetParametersForGetWindowedWaveform(half_window_length, x_length,
      temporal_position, fs, current_f0, base_index, index, window);

  // F0-adaptive windowing
  double *waveform = forward_real_fft->waveform;
  for (int i = 0; i <= half_window_length * 2; ++i)
    waveform[i] = x[index[i]] * window[i] + randn() * 0.000000000000001;
  double tmp_weight1 = 0;
  double tmp_weight2 = 0;
  for (int i = 0; i <= half_window_length * 2; ++i) {
    tmp_weight1 += waveform[i];
    tmp_weight2 += window[i];
  }
  double weighting_coefficient = tmp_weight1 / tmp_weight2;
  for (int i = 0; i <= half_window_length * 2; ++i)
    waveform[i] -= window[i] * weighting_coefficient;

  delete[] base_index;
  delete[] index;
  delete[] window;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd %xmm1, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
cvtsi2sdl -0x10(%rbp), %xmm1
movsd 0x13ba8(%rip), %xmm0    # 0x1c3b8
mulsd %xmm1, %xmm0
divsd -0x18(%rbp), %xmm0
callq 0x72e0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll %eax
addl $0x1, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
shll %eax
addl $0x1, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x40(%rbp)
movl -0x2c(%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 -0x2c(%rbp), %edi
movl -0xc(%rbp), %esi
movsd -0x20(%rbp), %xmm0
movl -0x10(%rbp), %edx
movsd -0x18(%rbp), %xmm1
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
callq 0x9060
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x2c(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jg 0x896a
movq -0x8(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x54(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x48(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x90(%rbp)
callq 0x7590
movsd -0x90(%rbp), %xmm2
movaps %xmm0, %xmm1
movsd -0x88(%rbp), %xmm0
movsd 0x13a7d(%rip), %xmm3    # 0x1c3c0
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x88e5
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl -0x2c(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jg 0x89c6
movq -0x50(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x48(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x8981
movsd -0x60(%rbp), %xmm0
divsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl -0x2c(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jg 0x8a36
movq -0x48(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd -0x78(%rbp), %xmm2
movq -0x50(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq %xmm0, %rdx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
xorq %rsi, %rdx
movq %rdx, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x89dc
movq -0x38(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x8a53
movq -0x98(%rbp), %rdi
callq 0x1110
movq -0x40(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0x8a70
movq -0xa0(%rbp), %rdi
callq 0x1110
movq -0x48(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x8a8d
movq -0xa8(%rbp), %rdi
callq 0x1110
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)