// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movsd %xmm0, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movslq -0x14(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x4314
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x42f4
movq -0x10(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
xorps %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jb 0x42f4
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x42fb
xorl %eax, %eax
movl %eax, -0x64(%rbp)
jmp 0x42fb
movl -0x64(%rbp), %edx
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x42af
movq -0x38(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
movslq -0x14(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x43a2
movq -0x38(%rbp), %rax
movslq -0x50(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x4395
movq -0x38(%rbp), %rax
movslq -0x50(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x48(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x4c(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x4397
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x435f
cmpl $0x2, -0x4c(%rbp)
jge 0x43e2
movq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x43bf
movq -0x70(%rbp), %rdi
callq 0x1110
movq -0x38(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0x43d6
movq -0x78(%rbp), %rdi
callq 0x1110
movl $0x0, -0x4(%rbp)
jmp 0x4582
movslq -0x4c(%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, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x4490
movq -0x48(%rbp), %rax
movslq -0x5c(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
subsd (%rax,%rcx,8), %xmm2
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movq -0x58(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x4412
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl -0x4c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x4528
movsd -0x20(%rbp), %xmm0
movq -0x58(%rbp), %rax
movl -0x60(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x58(%rbp), %rax
movslq -0x60(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm1
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movslq -0x60(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movslq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x58(%rbp), %rax
movl -0x60(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
addsd (%rax,%rcx,8), %xmm0
movsd 0x17b04(%rip), %xmm1    # 0x1c008
divsd %xmm1, %xmm0
divsd -0x20(%rbp), %xmm0
movq -0x28(%rbp), %rax
movslq -0x60(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x4497
movq -0x58(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x453f
movq -0x80(%rbp), %rdi
callq 0x1110
movq -0x48(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x455c
movq -0x88(%rbp), %rdi
callq 0x1110
movq -0x38(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x4579
movq -0x90(%rbp), %rdi
callq 0x1110
movl -0x4c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop