// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rsi, (%rsp)
movq %rdi, %rbx
movq (%rdi), %rdi
movq 0x8(%rbx), %rsi
cmpq %rsi, %rdi
je 0x2bcd
leaq -0x480(%rip), %rdx      # 0x2473
callq 0x3edb
pushq $0x1
popq %rbp
xorl %r15d, %r15d
leaq 0x8(%rsp), %r12
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
shrq $0x7, %rax
cltq
cmpq %rax, %rbp
jge 0x2aee
xorl %edi, %edi
movq %rbp, %r14
shlq $0x7, %r14
movq %r15, %r13
shlq $0x7, %r13
movq 0x40(%rsi,%r14), %r9
movq 0x50(%rsi,%r13), %rdx
cmpq %rdx, %r9
jg 0x2a09
leaq (%rsi,%r13), %rax
movq 0x68(%rax), %r10
cmpq %r9, %r10
jl 0x2997
leaq (%rsi,%r14), %rcx
movq 0x68(%rcx), %r8
cmpq %rdx, %r8
jg 0x29a3
testb $0x1, %dil
jne 0x29ad
movq %r12, %rdi
movq %rcx, %rsi
callq 0x38c6
movq (%rbx), %rdi
leaq (%rdi,%r13), %rsi
addq %r14, %rdi
callq 0x3922
movq (%rbx), %rdi
addq %r13, %rdi
movq %r12, %rsi
callq 0x3922
movq %r12, %rdi
callq 0x396e
movq (%rbx), %rsi
movb $0x1, %dil
jmp 0x292c
decq %r9
movq %r9, 0x50(%rax)
movq %r9, %rdx
jmp 0x2a09
leaq 0x1(%rdx), %rax
movq %rax, 0x40(%rcx)
jmp 0x2a09
cmpq %r9, 0x40(%rax)
jne 0x2bdf
cmpq 0x50(%rcx), %rdx
jne 0x2bdf
movq 0x70(%rax), %rdx
subq %r10, %rdx
movq %rdx, %rdi
negq %rdi
cmovsq %rdx, %rdi
movq 0x70(%rcx), %rdx
subq %r8, %rdx
movq %rdx, %r8
negq %r8
cmovsq %rdx, %r8
cmpq %r8, %rdi
jbe 0x29f6
movq %rax, %rdi
movq %rcx, %rsi
callq 0x3922
movq (%rbx), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0x3984
movq (%rbx), %rsi
movq 0x50(%rsi,%r13), %rdx
subq 0x40(%rsi,%r13), %rdx
incq %rdx
movq (%rsp), %rax
cmpq (%rax), %rdx
jl 0x2ae3
addq %r13, %rsi
movq 0x55a0(%rip), %rdi      # 0x7fc8
callq 0x2160
movq %rax, %rdi
pushq $0x9
popq %r14
movl %r14d, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x40(%rcx,%r13), %rsi
decq %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
movl %r14d, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x50(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
movl %r14d, %esi
callq 0x21c0
movq (%rbx), %rcx
leaq (%rcx,%r13), %rsi
addq $0x20, %rsi
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
movl %r14d, %esi
callq 0x21c0
movq (%rbx), %rcx
movss 0x60(%rcx,%r13), %xmm0
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
movl %r14d, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x68(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
movl %r14d, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x70(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20d0
incq %r15
incq %rbp
jmp 0x2903
movl %r15d, %r14d
shlq $0x7, %r14
movq 0x50(%rsi,%r14), %rax
subq 0x40(%rsi,%r14), %rax
incq %rax
movq (%rsp), %rcx
cmpq (%rcx), %rax
jl 0x2bcd
addq %r14, %rsi
movq 0x54af(%rip), %rdi      # 0x7fc8
callq 0x2160
pushq $0x9
popq %rbp
movq %rax, %rdi
movl %ebp, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x40(%rcx,%r14), %rsi
decq %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
movl %ebp, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x50(%rcx,%r14), %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
movl %ebp, %esi
callq 0x21c0
movq (%rbx), %rcx
leaq (%rcx,%r14), %rsi
addq $0x20, %rsi
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
movl %ebp, %esi
callq 0x21c0
movq (%rbx), %rcx
movss 0x60(%rcx,%r14), %xmm0
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
movl %ebp, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x68(%rcx,%r14), %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
movl %ebp, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x70(%rcx,%r14), %rsi
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20d0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x540a(%rip), %rdi      # 0x7ff0
movq %rax, %rsi
callq 0x2160
pushq $0x3a
popq %rsi
movq %rax, %rdi
callq 0x21c0
movq (%rbx), %rcx
movq 0x40(%rcx,%r13), %rsi
decq %rsi
movq %rax, %rdi
callq 0x22f0
pushq $0x2d
popq %rbp
movq %rax, %rdi
movl %ebp, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x50(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x22f0
leaq 0x2410(%rip), %r15      # 0x5040
movq %rax, %rdi
movq %r15, %rsi
callq 0x2190
movq (%rbx), %rcx
movq 0x70(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x22f0
leaq 0x2401(%rip), %r12      # 0x5053
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
movq (%rbx), %rcx
movq 0x68(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x22f0
leaq 0x23f0(%rip), %rsi      # 0x5064
movq %rax, %rdi
callq 0x2190
movq (%rbx), %rsi
addq %r14, %rsi
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
pushq $0x3a
popq %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x40(%rcx,%r14), %rsi
decq %rsi
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
movl %ebp, %esi
callq 0x21c0
movq (%rbx), %rcx
movq 0x50(%rcx,%r14), %rsi
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2190
movq (%rbx), %rcx
movq 0x70(%rcx,%r14), %rsi
movq %rax, %rdi
callq 0x22f0
movq %rax, %rdi
movq %r12, %rsi
callq 0x2190
movq (%rbx), %rcx
movq 0x68(%rcx,%r14), %rsi
movq %rax, %rdi
callq 0x22f0
pushq $0x2e
popq %rsi
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
pushq $0x2
popq %rdi
callq 0x21e0
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x396e
movq %rbx, %rdi
callq 0x22c0