// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movsd %xmm0, 0x8(%rsp)
movl %esi, %ebp
movq %rdi, %r12
movl %esi, %eax
shlq $0x2, %rax
testl %esi, %esi
pushq $-0x1
popq %r13
cmovnsq %rax, %r13
movq %r13, %rdi
callq 0x1030
leal -0x1(%rbp), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovgl %ecx, %edx
movq %rax, %r15
negq %rdx
pushq $0x1
popq %rax
xorpd %xmm0, %xmm0
leaq (%rdx,%rax), %rsi
cmpq $0x1, %rsi
je 0x2f86
movsd -0x8(%r12,%rax,8), %xmm1
xorl %esi, %esi
ucomisd %xmm0, %xmm1
jbe 0x2f7c
ucomisd (%r12,%rax,8), %xmm0
jb 0x2f7c
movl %eax, %esi
movl %esi, -0x4(%r15,%rax,4)
incq %rax
jmp 0x2f59
movslq %ecx, %rax
andl $0x0, (%r15,%rax,4)
movq %r13, %rdi
callq 0x1030
movq %rax, %r13
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
xorl %ebx, %ebx
cmpq %rax, %rbp
je 0x2fbe
movl (%r15,%rax,4), %ecx
testl %ecx, %ecx
jle 0x2fb9
movslq %ebx, %rdx
incl %ebx
movl %ecx, (%r13,%rdx,4)
incq %rax
jmp 0x2fa2
xorl %ebp, %ebp
cmpl $0x2, %ebx
jl 0x3068
movl %ebx, %ebp
leaq (,%rbp,8), %rdi
callq 0x1030
xorl %ecx, %ecx
movsd 0x8(%rsp), %xmm5
cmpq %rcx, %rbp
je 0x3011
movslq (%r13,%rcx,4), %rdx
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
jmp 0x2fe0
decl %ebx
xorl %ecx, %ecx
movsd 0xa0a3(%rip), %xmm0     # 0xd0c0
movq 0x10(%rsp), %rdx
cmpq %rcx, %rbx
je 0x305e
movsd (%rax,%rcx,8), %xmm1
movsd 0x8(%rax,%rcx,8), %xmm2
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
movapd %xmm5, %xmm4
divsd %xmm3, %xmm4
movsd %xmm4, (%rdx,%rcx,8)
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
divsd %xmm5, %xmm2
movsd %xmm2, (%r14,%rcx,8)
incq %rcx
jmp 0x3022
movq %rax, %rdi
callq 0x1110
movl %ebx, %ebp
movq %r13, %rdi
callq 0x1110
movq %r15, %rdi
callq 0x1110
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq