// File: /workspace/github_cpp_makefile_star2_O1/Altius[P]hotspot2/src/findVarWidthPeaks.cpp
// Function: cleanUpAnyOverlapsAndWriteOutput(std::vector<Range, std::allocator<Range>>&, long const&)
void cleanUpAnyOverlapsAndWriteOutput(vector<Range>& peaks, const long& minWidth)
{
  if (peaks.empty())
    return;

  int idxL(0), idxR(1);
  bool triedSwapping(false);

  sort(peaks.begin(), peaks.end(), GenomicOrder_LT);

  while (idxR < static_cast<int>(peaks.size()))
    {
    TopOfLoop:
      if (peaks[idxR].beg.x <= peaks[idxL].end.x) // then these two peaks overlap
	{
	  if (peaks[idxL].x_of_maxY < peaks[idxR].beg.x) // L's summit is left of R's beg, so shorten the L peak
	    peaks[idxL].end.x = peaks[idxR].beg.x - 1;
	  else
	    {
	      if (peaks[idxR].x_of_maxY > peaks[idxL].end.x) // R's summit is right of L's end, so shorten the R peak
		peaks[idxR].beg.x = peaks[idxL].end.x + 1;
	      else // these two overlapping peaks are confusing; swap (L,R) --> (R,L) and try again
		{
		  if (!triedSwapping)
		    {
		      Range temp = peaks[idxR];
		      peaks[idxR] = peaks[idxL];
		      peaks[idxL] = temp;
		      triedSwapping = true;
		      goto TopOfLoop;
		    }
		  else
		    {
		      if (peaks[idxL].beg.x == peaks[idxR].beg.x && peaks[idxL].end.x == peaks[idxR].end.x)
			{
			  // Two adjacent wavelet peaks yielded the same FWHM peak.
			  // Delete the one whose wavelet summit (coordinate, x-value) is farther from the FWHM summit.
			  // Do this by enforcing the idxL version to be the one we keep
			  // and deleting the idxR version.
			  long distL(labs(peaks[idxL].inputSummit - peaks[idxL].x_of_maxY)),
			    distR(labs(peaks[idxR].inputSummit - peaks[idxR].x_of_maxY));
			  if (distL > distR)
			    peaks[idxL] = peaks[idxR];
			  peaks.erase(peaks.begin() + idxR);
			}
		      else
			{
			  cerr << peaks[idxL].chrom << ':' << peaks[idxL].beg.x-1 << '-' << peaks[idxL].end.x
			       << ", waveletSummit = " << peaks[idxL].inputSummit
			       << ", FWHM summit = " << peaks[idxL].x_of_maxY
			       << ",\nunsure how to resolve overlap with "
			       << peaks[idxR].chrom << ':' << peaks[idxR].beg.x-1 << '-' << peaks[idxR].end.x
			       << ", waveletSummit = " << peaks[idxR].inputSummit
			       << ", FWHM summit = " << peaks[idxR].x_of_maxY << '.' << endl << endl;
			  exit(2);
			}
		    }
		}
	    }
	}
      // We may have shortened peaks[idxL], so ensure it's sufficiently wide before reporting it.
      if (peaks[idxL].end.x - (peaks[idxL].beg.x - 1) >= minWidth)
	cout << peaks[idxL].chrom << '\t' << peaks[idxL].beg.x - 1 << '\t'
	     << peaks[idxL].end.x << '\t' << peaks[idxL].id << '\t'
	     << peaks[idxL].maxY << '\t' << peaks[idxL].x_of_maxY
	     << '\t' << peaks[idxL].inputSummit << endl;
      idxL++;
      idxR++;
      triedSwapping = false;
    }
  // Write the last element in the vector.
  if (peaks[idxL].end.x - (peaks[idxL].beg.x - 1) >= minWidth)
    cout << peaks[idxL].chrom << '\t' << peaks[idxL].beg.x - 1 << '\t'
	 << peaks[idxL].end.x << '\t' << peaks[idxL].id << '\t'
	 << peaks[idxL].maxY << '\t' << peaks[idxL].x_of_maxY
	 << '\t' << peaks[idxL].inputSummit << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, 0x8(%rsp)
movq (%rdi), %r14
movq 0x8(%rdi), %r15
cmpq %r15, %r14
je 0x2f02
movq %rdi, %rbx
movq %r15, %rax
subq %r14, %rax
sarq $0x7, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x4c3(%rip), %r12      # 0x24b3
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x483e
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x49b0
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
shrq $0x7, %rcx
cmpl $0x2, %ecx
jl 0x2d9e
leaq 0x38(%rsp), %r15
movl $0x1, %ebp
xorl %r13d, %r13d
movq %rbx, (%rsp)
movq (%rbx), %r12
movq %rbp, 0x18(%rsp)
shlq $0x7, %rbp
movq 0x40(%r12,%rbp), %rcx
movq %r13, 0x20(%rsp)
shlq $0x7, %r13
movq 0x50(%r12,%r13), %rax
cmpq %rax, %rcx
jg 0x2c1a
leaq (%r12,%rbp), %r14
leaq (%r12,%rbp), %rbx
addq $0x40, %rbx
addq %r13, %r12
xorl %edx, %edx
movq 0x68(%r12), %rdi
cmpq %rcx, %rdi
jl 0x2b6b
movq 0x68(%r14), %rsi
cmpq %rax, %rsi
jg 0x2b78
testb $0x1, %dl
jne 0x2b83
movq %r15, 0x28(%rsp)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0x42a8
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0x42a8
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups 0x30(%rbx), %xmm3
movups %xmm3, 0x60(%r15)
movups %xmm2, 0x50(%r15)
movups %xmm1, 0x40(%r15)
movups %xmm0, 0x30(%r15)
movq (%rsp), %rbx
movq (%rbx), %r14
leaq (%r14,%r13), %r12
addq %rbp, %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x20e0
leaq 0x20(%r14), %rdi
leaq 0x20(%r12), %rsi
callq 0x20e0
movups 0x40(%r12), %xmm0
movups 0x50(%r12), %xmm1
movups 0x60(%r12), %xmm2
movups 0x70(%r12), %xmm3
movups %xmm3, 0x70(%r14)
movups %xmm2, 0x60(%r14)
movups %xmm1, 0x50(%r14)
movups %xmm0, 0x40(%r14)
movq (%rbx), %r14
addq %r13, %r14
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
callq 0x20e0
leaq 0x20(%r14), %rdi
leaq 0x48(%rsp), %rsi
callq 0x20e0
movups 0x30(%r15), %xmm0
movups 0x40(%r15), %xmm1
movups 0x50(%r15), %xmm2
movups 0x60(%r15), %xmm3
movups %xmm3, 0x70(%r14)
movups %xmm2, 0x60(%r14)
movups %xmm1, 0x50(%r14)
movups %xmm0, 0x40(%r14)
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2b2a
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x2b41
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21a0
movq (%rbx), %r12
leaq (%r12,%rbp), %r14
leaq (%r12,%rbp), %rbx
addq $0x40, %rbx
movq (%rbx), %rcx
movq 0x50(%r12,%r13), %rax
addq %r13, %r12
movb $0x1, %dl
cmpq %rax, %rcx
jle 0x29f3
jmp 0x2c1a
decq %rcx
movq %rcx, 0x50(%r12)
jmp 0x2c1a
incq %rax
movq %rax, (%rbx)
jmp 0x2c1a
cmpq %rcx, 0x40(%r12)
jne 0x2f14
cmpq 0x50(%r14), %rax
jne 0x2f14
movq 0x70(%r12), %rax
subq %rdi, %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
movq 0x70(%r14), %rax
subq %rsi, %rax
movq %rax, %rdx
negq %rdx
cmovsq %rax, %rdx
cmpq %rdx, %rcx
jbe 0x2c0b
leaq 0x40(%r12), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x20e0
addq $0x20, %r12
addq $0x20, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x20e0
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups 0x30(%rbx), %xmm3
movq 0x10(%rsp), %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq (%rsp), %rdi
addq (%rdi), %rbp
movq %rbp, %rsi
callq 0x590a
movq (%rsp), %rbx
movq (%rbx), %rax
movq 0x50(%rax,%r13), %rcx
subq 0x40(%rax,%r13), %rcx
incq %rcx
movq 0x8(%rsp), %rdx
cmpq (%rdx), %rcx
jl 0x2d6f
addq %r13, %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x637b(%rip), %rdi      # 0x8fc8
callq 0x21c0
movb $0x9, %bpl
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x40(%rcx,%r13), %rsi
decq %rsi
movq %rax, %rdi
callq 0x2160
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x50(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2160
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x20(%rcx,%r13), %rsi
movq 0x28(%rcx,%r13), %rdx
movq %rax, %rdi
callq 0x21c0
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%rbx), %rcx
xorps %xmm0, %xmm0
cvtss2sd 0x60(%rcx,%r13), %xmm0
movq %rax, %rdi
callq 0x2260
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x68(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2160
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x70(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2160
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movq 0x20(%rsp), %r13
incq %r13
movq 0x18(%rsp), %rbp
incq %rbp
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
shrq $0x7, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jl 0x29ba
movl %r13d, %r15d
jmp 0x2da1
xorl %r15d, %r15d
shlq $0x7, %r15
movq 0x50(%rax,%r15), %rcx
subq 0x40(%rax,%r15), %rcx
incq %rcx
movq 0x8(%rsp), %rdx
cmpq (%rdx), %rcx
jl 0x2f02
addq %r15, %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x61f7(%rip), %rdi      # 0x8fc8
callq 0x21c0
movq %rbx, %r12
movb $0x9, %bl
leaq 0x28(%rsp), %r14
movb %bl, (%r14)
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%r12), %rcx
movq 0x40(%rcx,%r15), %rsi
decq %rsi
movq %rax, %rdi
callq 0x2160
movb %bl, (%r14)
leaq 0x28(%rsp), %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%r12), %rcx
movq 0x50(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2160
movb %bl, (%r14)
leaq 0x28(%rsp), %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%r12), %rcx
movq 0x20(%rcx,%r15), %rsi
movq 0x28(%rcx,%r15), %rdx
movq %rax, %rdi
callq 0x21c0
movb %bl, (%r14)
leaq 0x28(%rsp), %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%r12), %rcx
cvtss2sd 0x60(%rcx,%r15), %xmm0
movq %rax, %rdi
callq 0x2260
movb %bl, (%r14)
leaq 0x28(%rsp), %r14
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%r12), %rcx
movq 0x68(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2160
movb %bl, (%r14)
leaq 0x28(%rsp), %rsi
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
movq (%r12), %rcx
movq 0x70(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x60d5(%rip), %rdi      # 0x8ff0
movq %r12, %rsi
callq 0x2140
movq %rax, %rdi
movl $0x3a, %esi
callq 0x21d0
movq (%rsp), %rbx
movq (%rbx), %rcx
movq 0x40(%rcx,%r13), %rsi
decq %rsi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
movl $0x2d, %esi
callq 0x21d0
movq (%rbx), %rcx
movq 0x50(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2300
leaq 0x30d5(%rip), %r14      # 0x6040
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
movq (%rbx), %rcx
movq 0x70(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2300
leaq 0x30c6(%rip), %r15      # 0x6053
movq %rax, %rdi
movq %r15, %rsi
callq 0x2180
movq (%rbx), %rcx
movq 0x68(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2300
leaq 0x30b5(%rip), %rsi      # 0x6064
movq %rax, %rdi
callq 0x2180
movq (%rbx), %rsi
addq %rbp, %rsi
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
movl $0x3a, %esi
callq 0x21d0
movq (%rbx), %rcx
movq 0x40(%rcx,%rbp), %rsi
decq %rsi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
movl $0x2d, %esi
callq 0x21d0
movq (%rbx), %rcx
movq 0x50(%rcx,%rbp), %rsi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
movq (%rbx), %rcx
movq 0x70(%rcx,%rbp), %rsi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
movq %r15, %rsi
callq 0x2180
movq (%rbx), %rcx
movq 0x68(%rcx,%rbp), %rsi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
movl $0x2e, %esi
callq 0x21d0
movq %rax, %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x20b0
movl $0x2, %edi
callq 0x21f0
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x3088
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x3088
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x41d4
movq %rbx, %rdi
callq 0x22d0