// File: Sleepwalking[P]world-octave/synthesis.cpp
// Function: (anonymous namespace)::GetPulseLocationsForTimeBase(double*, double*, int, int, double*, int*)
int GetPulseLocationsForTimeBase(double *interpolated_f0, double *time_axis,
    int y_length, int fs, double *pulse_locations,
    int *pulse_locations_index) {

  double *total_phase = new double[y_length];
  total_phase[0] = 2.0 * world::kPi * interpolated_f0[0] / fs;
  for (int i = 1; i < y_length; ++i)
    total_phase[i] = total_phase[i - 1] +
      2.0 * world::kPi * interpolated_f0[i] / fs;

  double *wrap_phase = new double[y_length];
  for (int i = 0; i < y_length; ++i)
    wrap_phase[i] = fmod(total_phase[i], 2.0 * world::kPi);

  double *wrap_phase_abs = new double[y_length];
  for (int i = 0; i < y_length - 1; ++i)
    wrap_phase_abs[i] = fabs(wrap_phase[i + 1] - wrap_phase[i]);

  int number_of_pulses = 0;
  for (int i = 0; i < y_length - 1; ++i) {
    if (wrap_phase_abs[i] > world::kPi) {
      pulse_locations[number_of_pulses] = time_axis[i];
      pulse_locations_index[number_of_pulses] = static_cast<int>
        (matlab_round(pulse_locations[number_of_pulses] * fs));
      ++number_of_pulses;
    }
  }

  delete[] wrap_phase_abs;
  delete[] wrap_phase;
  delete[] total_phase;

  return number_of_pulses;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movslq -0x14(%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)
movq -0x8(%rbp), %rax
movsd 0x1650a(%rip), %xmm0    # 0x1c060
mulsd (%rax), %xmm0
cvtsi2sdl -0x18(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5bc6
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd 0x164c4(%rip), %xmm1    # 0x1c060
mulsd (%rax,%rcx,8), %xmm1
cvtsi2sdl -0x18(%rbp), %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5b72
movslq -0x14(%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, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5c30
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x1644d(%rip), %xmm1    # 0x1c060
callq 0x1120
movq -0x40(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5bf6
movslq -0x14(%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, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5caf
movq -0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x40(%rbp), %rax
movslq -0x54(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movaps 0x1644d(%rip), %xmm1    # 0x1c0e0
pand %xmm1, %xmm0
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x5c60
movl $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5d3d
movq -0x50(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x163d9(%rip), %xmm1    # 0x1c0b8
ucomisd %xmm1, %xmm0
jbe 0x5d30
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl -0x18(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x72e0
movl %eax, %edx
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x5d32
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x5cbd
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0x5d54
movq -0x68(%rbp), %rdi
callq 0x1110
movq -0x40(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x5d6b
movq -0x70(%rbp), %rdi
callq 0x1110
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0x5d82
movq -0x78(%rbp), %rdi
callq 0x1110
movl -0x58(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop