// File: /workspace/github_cpp_makefile_star2_O3/Sleepwalking[P]world-octave/dio.cpp
// Function: (anonymous namespace)::ZeroCrossingEngine(double*, int, double, double*, double*)
int ZeroCrossingEngine(double *filtered_signal, int y_length, double fs,
    double *interval_locations, double *intervals) {
  int *negative_going_points = new int[y_length];

  for (int i = 0; i < y_length - 1; ++i)
    negative_going_points[i] =
      0.0 < filtered_signal[i] && filtered_signal[i + 1] <= 0.0 ? i + 1 : 0;
  negative_going_points[y_length - 1] = 0;

  int *edges = new int[y_length];
  int count = 0;
  for (int i = 0; i < y_length; ++i)
    if (negative_going_points[i] > 0)
      edges[count++] = negative_going_points[i];

  if (count < 2) {
    delete[] edges;
    delete[] negative_going_points;
    return 0;
  }

  double *fine_edges = new double[count];
  for (int i = 0; i < count; ++i)
    fine_edges[i] =
      edges[i] - filtered_signal[edges[i] - 1] /
      (filtered_signal[edges[i]] - filtered_signal[edges[i] - 1]);

  for (int i = 0; i < count - 1; ++i) {
    intervals[i] = fs / (fine_edges[i + 1] - fine_edges[i]);
    interval_locations[i] = (fine_edges[i] + fine_edges[i + 1]) / 2.0 / fs;
  }

  delete[] fine_edges;
  delete[] edges;
  delete[] negative_going_points;
  return count - 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %r15
movsd %xmm0, 0x8(%rsp)
movl %esi, %ebp
movq %rdi, %r12
movl %esi, %r14d
leaq (,%r14,4), %rax
testl %esi, %esi
movq $-0x1, %r13
cmovnsq %rax, %r13
movq %r13, %rdi
callq 0x1030
movq %rax, %rbx
leal -0x1(%rbp), %eax
cmpl $0x2, %ebp
jl 0x35a4
movl %eax, %ecx
negq %rcx
movl $0x1, %edx
xorpd %xmm0, %xmm0
movsd -0x8(%r12,%rdx,8), %xmm1
xorl %esi, %esi
ucomisd %xmm0, %xmm1
jbe 0x3590
ucomisd (%r12,%rdx,8), %xmm0
jb 0x3590
movl %edx, %esi
movl %esi, -0x4(%rbx,%rdx,4)
leaq (%rcx,%rdx), %rsi
incq %rsi
incq %rdx
cmpq $0x1, %rsi
jne 0x3577
cltq
movl $0x0, (%rbx,%rax,4)
movq %r13, %rdi
callq 0x1030
movq %rax, %r13
testl %ebp, %ebp
jle 0x35de
xorl %eax, %eax
xorl %ebp, %ebp
movl (%rbx,%rax,4), %ecx
testl %ecx, %ecx
jle 0x35d1
movslq %ebp, %rdx
incl %ebp
movl %ecx, (%r13,%rdx,4)
incq %rax
cmpq %rax, %r14
jne 0x35c0
cmpl $0x2, %ebp
jge 0x35e5
xorl %ebp, %ebp
jmp 0x3689
movl %ebp, %r14d
leaq (,%r14,8), %rdi
callq 0x1030
xorl %ecx, %ecx
movslq (%r13,%rcx,4), %rdx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movsd -0x8(%r12,%rdx,8), %xmm1
movsd (%r12,%rdx,8), %xmm2
subsd %xmm1, %xmm2
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x35f7
decl %ebp
movsd (%rax), %xmm1
xorl %ecx, %ecx
movsd 0xbad7(%rip), %xmm0     # 0xf110
movq 0x10(%rsp), %rsi
movsd 0x8(%rsp), %xmm5
movsd 0x8(%rax,%rcx,8), %xmm2
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
movapd %xmm5, %xmm4
divsd %xmm3, %xmm4
movsd %xmm4, (%rsi,%rcx,8)
addsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
divsd %xmm5, %xmm1
movsd %xmm1, (%r15,%rcx,8)
leaq 0x1(%rcx), %rdx
movapd %xmm2, %xmm1
movq %rdx, %rcx
cmpq %rdx, %rbp
jne 0x3644
movq %rax, %rdi
callq 0x1110
movq %r13, %rdi
callq 0x1110
movq %rbx, %rdi
callq 0x1110
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq