// File: Sleepwalking[P]world-octave/d4c.cpp
// Function: (anonymous namespace)::GetWindowedWaveform(double*, int, int, double, double, int, double, double*)
void GetWindowedWaveform(double *x, int x_length, int fs, double current_f0,
    double temporal_position, int window_type, double window_length_ratio,
    double *waveform) {
  int half_window_length =
    matlab_round(window_length_ratio * fs / current_f0 / 2.0);

  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_type,
      window);

  // F0-adaptive windowing
  for (int i = 0; i <= half_window_length * 2; ++i)
    waveform[i] =
      x[index[i]] * window[i] + randn() * world::kMySafeGuardMinimum;

  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 $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd %xmm1, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movsd %xmm2, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movsd -0x30(%rbp), %xmm0
cvtsi2sdl -0x10(%rbp), %xmm1
mulsd %xmm1, %xmm0
divsd -0x18(%rbp), %xmm0
movsd 0x105f2(%rip), %xmm1    # 0x1c008
divsd %xmm1, %xmm0
callq 0x72e0
movl %eax, -0x3c(%rbp)
movl -0x3c(%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, -0x48(%rbp)
movl -0x3c(%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, -0x50(%rbp)
movl -0x3c(%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, -0x58(%rbp)
movl -0x3c(%rbp), %edi
movl -0xc(%rbp), %esi
movsd -0x20(%rbp), %xmm0
movl -0x10(%rbp), %edx
movsd -0x18(%rbp), %xmm1
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
movl -0x24(%rbp), %r9d
movq -0x58(%rbp), %rax
movq %rax, (%rsp)
callq 0xbcb0
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl -0x3c(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jg 0xbb67
movq -0x8(%rbp), %rax
movq -0x50(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x58(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x98(%rbp)
callq 0x7590
movsd -0x98(%rbp), %xmm2
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
movsd 0x10568(%rip), %xmm3    # 0x1c0a8
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xbae2
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl -0x3c(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jg 0xbbc3
movq -0x38(%rbp), %rax
movslq -0x74(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x58(%rbp), %rax
movslq -0x74(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xbb7e
movsd -0x68(%rbp), %xmm0
divsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl -0x3c(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jg 0xbc45
movq -0x58(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd -0x80(%rbp), %xmm2
movq -0x38(%rbp), %rax
movslq -0x84(%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 -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xbbdc
movq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0xbc62
movq -0xa0(%rbp), %rdi
callq 0x1110
movq -0x50(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0xbc7f
movq -0xa8(%rbp), %rdi
callq 0x1110
movq -0x58(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0xbc9c
movq -0xb0(%rbp), %rdi
callq 0x1110
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)