// File: Sleepwalking[P]world-octave/dio.cpp
// Function: (anonymous namespace)::GetSpectrumForEstimation(double*, int, int, double, int, int, double (*) [2])
void GetSpectrumForEstimation(double *x, int x_length, int y_length,
    double actual_fs, int fft_size, int decimation_ratio,
    fft_complex *y_spectrum) {
  double *y = new double[fft_size];

  // Initialization
  for (int i = 0; i < fft_size; ++i) y[i] = 0.0;

  // Downsampling
  if (decimation_ratio != 1) {
    decimate(x, x_length, decimation_ratio, y);
  } else {
    for (int i = 0; i < x_length; ++i) y[i] = x[i];
  }

  // Removal of the DC component (y = y - mean value of y)
  double mean_y = 0.0;
  for (int i = 0; i < y_length; ++i) mean_y += y[i];
  mean_y /= y_length;
  for (int i = 0; i < y_length; ++i) y[i] -= mean_y;
  for (int i = y_length; i < fft_size; ++i) y[i] = 0.0;

  fft_plan forwardFFT =
    fft_plan_dft_r2c_1d(fft_size, y, y_spectrum, FFT_ESTIMATE);
  fft_execute(forwardFFT);

  // Low cut filtering (from 0.1.4)
  int cutoff_in_sample = matlab_round(actual_fs / 50.0);  // Cutoff is 50.0 Hz
  DesignLowCutFilter(cutoff_in_sample * 2 + 1, fft_size, y);

  fft_complex *filter_spectrum = new fft_complex[fft_size];
  forwardFFT.c_out = filter_spectrum;
  fft_execute(forwardFFT);

  double tmp = 0;
  for (int i = 0; i <= fft_size / 2; ++i) {
    tmp = y_spectrum[i][0] * filter_spectrum[i][0] -
      y_spectrum[i][1] * filter_spectrum[i][1];
    y_spectrum[i][1] = y_spectrum[i][0] * filter_spectrum[i][1] +
      y_spectrum[i][1] * filter_spectrum[i][0];
    y_spectrum[i][0] = tmp;
  }

  fft_destroy_plan(forwardFFT);
  delete[] y;
  delete[] filter_spectrum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movslq -0x1c(%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, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2ab8
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2a95
cmpl $0x1, -0x20(%rbp)
je 0x2ad3
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x20(%rbp), %edx
movq -0x30(%rbp), %rcx
callq 0x6b90
jmp 0x2b09
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x2b07
movq -0x8(%rbp), %rax
movslq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2ada
jmp 0x2b09
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x2b42
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2b18
cvtsi2sdl -0x10(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x2b8a
movsd -0x40(%rbp), %xmm1
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x2b5c
movl -0x10(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2bb3
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x2b90
movl -0x1c(%rbp), %esi
movq -0x30(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x98(%rbp), %rdi
movl $0x3, %r8d
callq 0xd3b0
leaq -0xe0(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movl $0x48, %edx
callq 0x10b0
movq -0xa0(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x40(%rax)
movups -0xe0(%rbp), %xmm0
movups -0xd0(%rbp), %xmm1
movups -0xc0(%rbp), %xmm2
movups -0xb0(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0xd500
movsd -0x18(%rbp), %xmm0
movsd 0x1946d(%rip), %xmm1    # 0x1c0a0
divsd %xmm1, %xmm0
callq 0x72e0
movl %eax, -0xe4(%rbp)
movl -0xe4(%rbp), %edi
shll %edi
addl $0x1, %edi
movl -0x1c(%rbp), %esi
movq -0x30(%rbp), %rdx
callq 0x33a0
movslq -0x1c(%rbp), %rax
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movl $0x48, %edx
callq 0x10b0
movq -0xf8(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x40(%rax)
movups -0x138(%rbp), %xmm0
movups -0x128(%rbp), %xmm1
movups -0x118(%rbp), %xmm2
movups -0x108(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0xd500
xorps %xmm0, %xmm0
movsd %xmm0, -0x140(%rbp)
movl $0x0, -0x144(%rbp)
movl -0x144(%rbp), %eax
movl %eax, -0x194(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x194(%rbp), %eax
cmpl %ecx, %eax
jg 0x2e5a
movq -0x28(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0xf0(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm2
movq -0x28(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm1
movq -0xf0(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulsd 0x8(%rax), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x140(%rbp)
movq -0x28(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq -0xf0(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm2
movq -0x28(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm1
movq -0xf0(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq -0x28(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd %xmm0, 0x8(%rax)
movsd -0x140(%rbp), %xmm0
movq -0x28(%rbp), %rax
movslq -0x144(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd %xmm0, (%rax)
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x144(%rbp)
jmp 0x2cfb
leaq -0x190(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movl $0x48, %edx
callq 0x10b0
movq -0x150(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x40(%rax)
movups -0x190(%rbp), %xmm0
movups -0x180(%rbp), %xmm1
movups -0x170(%rbp), %xmm2
movups -0x160(%rbp), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0xda70
movq -0x30(%rbp), %rax
movq %rax, -0x1a0(%rbp)
cmpq $0x0, %rax
je 0x2ecd
movq -0x1a0(%rbp), %rdi
callq 0x1110
movq -0xf0(%rbp), %rax
movq %rax, -0x1a8(%rbp)
cmpq $0x0, %rax
je 0x2eed
movq -0x1a8(%rbp), %rdi
callq 0x1110
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
nopw %cs:(%rax,%rax)