// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %rbx
movl %ecx, 0x10(%rsp)
movsd %xmm1, (%rsp)
movapd %xmm0, %xmm1
movl %esi, %ebp
movq %rdi, 0x30(%rsp)
cvtsi2sd %edx, %xmm2
movsd 0x6b4f(%rip), %xmm0     # 0xd430
movsd %xmm2, 0x18(%rsp)
mulsd %xmm2, %xmm0
movsd %xmm1, 0x28(%rsp)
divsd %xmm1, %xmm0
mulsd 0x67c3(%rip), %xmm0     # 0xd0c0
callq 0x419e
movq %rax, 0x8(%rsp)
leal 0x1(,%rax,2), %r13d
movslq %r13d, %r12
leaq (,%r12,4), %r15
shlq $0x3, %r12
testl %eax, %eax
pushq $-0x1
popq %rax
cmovsq %rax, %r15
cmovsq %rax, %r12
movq %r15, %rdi
callq 0x1030
movq %rax, %r14
movq %r15, %rdi
callq 0x1030
movq %rax, 0x20(%rsp)
movq %r12, %rdi
callq 0x1030
movq %rax, %r12
movq 0x8(%rsp), %rcx
movslq %ecx, %rax
negl %ecx
movslq %ecx, %rcx
leaq (%r14,%rax,4), %rdx
cmpq %rax, %rcx
jg 0x696c
movl %ecx, (%rdx,%rcx,4)
incq %rcx
jmp 0x695f
decl %ebp
xorl %r15d, %r15d
testl %r13d, %r13d
cmovlel %r15d, %r13d
movsd (%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
cmpq %r15, %r13
je 0x69b9
cvtsi2sdl (%r14,%r15,4), %xmm0
addsd (%rsp), %xmm0
callq 0x419e
testl %eax, %eax
jg 0x69a3
xorl %eax, %eax
movl %ebp, %ecx
cmpl %eax, %ebp
jl 0x69ab
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax,%r15,4)
incq %r15
jmp 0x6988
movsd (%rsp), %xmm0
callq 0x419e
cvtsi2sd %eax, %xmm0
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
cmpl $0x1, 0x10(%rsp)
jne 0x6a33
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x6ab2
cvtsi2sdl (%r14,%r15,4), %xmm0
movsd 0x66ca(%rip), %xmm1     # 0xd0c0
mulsd %xmm1, %xmm0
addsd (%rsp), %xmm0
divsd 0x18(%rsp), %xmm0
mulsd 0x66c3(%rip), %xmm0     # 0xd0d0
mulsd 0x28(%rsp), %xmm0
callq 0x11b0
movsd 0x66a0(%rip), %xmm1     # 0xd0c0
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r12,%r15,8)
incq %r15
jmp 0x69df
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0x6ab2
cvtsi2sdl (%r14,%rbp,4), %xmm1
movsd 0x6678(%rip), %xmm0     # 0xd0c0
mulsd %xmm0, %xmm1
addsd (%rsp), %xmm1
divsd 0x18(%rsp), %xmm1
mulsd 0x6671(%rip), %xmm1     # 0xd0d0
mulsd 0x28(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
movapd %xmm1, %xmm0
callq 0x11b0
mulsd 0x6644(%rip), %xmm0     # 0xd0c0
addsd 0x6964(%rip), %xmm0     # 0xd3e8
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
callq 0x11b0
mulsd 0x694f(%rip), %xmm0     # 0xd3f0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r12,%rbp,8)
incq %rbp
jmp 0x6a35
xorl %r15d, %r15d
movq 0x30(%rsp), %rbp
cmpq %r15, %r13
je 0x6af6
movq 0x20(%rsp), %rax
movslq (%rax,%r15,4), %rax
movsd (%rbp,%rax,8), %xmm0
mulsd (%r12,%r15,8), %xmm0
movsd %xmm0, (%rsp)
callq 0x431e
mulsd 0x660a(%rip), %xmm0     # 0xd0f0
addsd (%rsp), %xmm0
movsd %xmm0, (%rbx,%r15,8)
incq %r15
jmp 0x6aba
xorpd %xmm0, %xmm0
xorl %eax, %eax
xorpd %xmm1, %xmm1
cmpq %rax, %r13
je 0x6b15
addsd (%rbx,%rax,8), %xmm0
addsd (%r12,%rax,8), %xmm1
incq %rax
jmp 0x6b00
divsd %xmm1, %xmm0
xorl %eax, %eax
cmpq %rax, %r13
je 0x6b3d
movsd (%rbx,%rax,8), %xmm1
movsd (%r12,%rax,8), %xmm2
mulsd %xmm0, %xmm2
subsd %xmm2, %xmm1
movsd %xmm1, (%rbx,%rax,8)
incq %rax
jmp 0x6b1b
movq %r14, %rdi
callq 0x1110
movq 0x20(%rsp), %rdi
callq 0x1110
movq %r12, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1110