// File: /workspace/github_cpp_makefile_star2_O1/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)
movq %rdi, %r12
movl %esi, 0x4(%rsp)
movslq %esi, %rbx
leaq (,%rbx,4), %rax
testl %ebx, %ebx
movq $-0x1, %r13
cmovnsq %rax, %r13
movq %r13, %rdi
callq 0x1030
movq %rax, %r15
leal -0x1(%rbx), %eax
cmpl $0x2, %ebx
jl 0x326d
movl %eax, %ecx
negq %rcx
movl $0x1, %edx
xorpd %xmm0, %xmm0
movsd -0x8(%r12,%rdx,8), %xmm1
xorl %esi, %esi
ucomisd %xmm0, %xmm1
jbe 0x3258
ucomisd (%r12,%rdx,8), %xmm0
jb 0x3258
movl %edx, %esi
movl %esi, -0x4(%r15,%rdx,4)
leaq (%rcx,%rdx), %rsi
incq %rsi
incq %rdx
cmpq $0x1, %rsi
jne 0x323f
cltq
movl $0x0, (%r15,%rax,4)
movq %r13, %rdi
callq 0x1030
movq %rax, %r13
xorl %ebx, %ebx
movl $0x0, %ebp
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0x32b1
movl %eax, %eax
xorl %ecx, %ecx
xorl %ebp, %ebp
movl (%r15,%rcx,4), %edx
testl %edx, %edx
jle 0x32a9
movslq %ebp, %rsi
incl %ebp
movl %edx, (%r13,%rsi,4)
incq %rcx
cmpq %rcx, %rax
jne 0x3297
cmpl $0x2, %ebp
jl 0x335c
movl %ebp, %ebx
leaq (,%rbx,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, %rbx
jne 0x32cb
decl %ebp
xorl %ecx, %ecx
movsd 0xbd7f(%rip), %xmm0     # 0xf088
movq 0x10(%rsp), %rsi
movsd 0x8(%rsp), %xmm5
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, (%rsi,%rcx,8)
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
divsd %xmm5, %xmm2
movsd %xmm2, (%r14,%rcx,8)
leaq 0x1(%rcx), %rdx
movq %rdx, %rcx
cmpq %rdx, %rbp
jne 0x3314
movq %rax, %rdi
callq 0x1110
movl %ebp, %ebx
movq %r13, %rdi
callq 0x1110
movq %r15, %rdi
callq 0x1110
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq