// File: 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
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0x51f0
testb $0x1, %al
jne 0x3012
jmp 0x3017
jmp 0x395b
movl $0x0, -0x14(%rbp)
movl $0x1, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5280
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
callq 0x52b0
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq -0xa62(%rip), %rdx      # 0x25f0
callq 0x5230
movl -0x18(%rbp), %eax
movl %eax, -0xf4(%rbp)
movq -0x8(%rbp), %rdi
callq 0x52e0
movq %rax, %rcx
movl -0xf4(%rbp), %eax
cmpl %ecx, %eax
jge 0x37bb
jmp 0x307c
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq 0x40(%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq 0x50(%rcx), %rax
jg 0x3600
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq 0x68(%rax), %rax
movq %rax, -0x108(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x108(%rbp), %rax
cmpq 0x40(%rcx), %rax
jge 0x3123
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq 0x40(%rax), %rax
subq $0x1, %rax
movq %rax, -0x110(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x110(%rbp), %rcx
movq %rcx, 0x50(%rax)
jmp 0x35fe
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq 0x68(%rax), %rax
movq %rax, -0x118(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x118(%rbp), %rax
cmpq 0x50(%rcx), %rax
jle 0x3191
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq 0x50(%rax), %rax
addq $0x1, %rax
movq %rax, -0x120(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq -0x120(%rbp), %rcx
movq %rcx, 0x40(%rax)
jmp 0x35fc
testb $0x1, -0x19(%rbp)
jne 0x323f
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq %rax, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x5320
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, -0x128(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq -0x128(%rbp), %rsi
movq %rax, %rdi
callq 0x53a0
jmp 0x31e9
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x53a0
jmp 0x3207
movb $0x1, -0x19(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x5400
jmp 0x307c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x5400
jmp 0x3964
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq 0x40(%rax), %rax
movq %rax, -0x130(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x130(%rbp), %rax
cmpq 0x40(%rcx), %rax
jne 0x33dd
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq 0x50(%rax), %rax
movq %rax, -0x138(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x138(%rbp), %rax
cmpq 0x50(%rcx), %rax
jne 0x33dd
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq 0x70(%rax), %rax
movq %rax, -0x148(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x148(%rbp), %rax
subq 0x68(%rcx), %rax
movq %rax, %rcx
negq %rcx
cmovnsq %rcx, %rax
movq %rax, -0xc8(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq 0x70(%rax), %rax
movq %rax, -0x140(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x140(%rbp), %rax
subq 0x68(%rcx), %rax
movq %rax, %rcx
negq %rcx
cmovnsq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jle 0x3379
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq %rax, -0x150(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x53a0
movq -0x8(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5280
movq %rax, -0xe8(%rbp)
movslq -0x18(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x54a0
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x54e0
movq -0x158(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x5430
movq %rax, -0xf0(%rbp)
jmp 0x35f8
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rsi
movq 0xbbfc(%rip), %rdi      # 0xeff0
callq 0x21e0
movq %rax, %rdi
movl $0x3a, %esi
callq 0x2280
movq %rax, -0x1a0(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1a0(%rbp), %rdi
movq 0x40(%rax), %rsi
subq $0x1, %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x2d, %esi
callq 0x2280
movq %rax, -0x198(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x198(%rbp), %rdi
movq 0x50(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
leaq 0x6bd7(%rip), %rsi      # 0xa040
callq 0x2200
movq %rax, -0x190(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x190(%rbp), %rdi
movq 0x70(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
leaq 0x6bb7(%rip), %rsi      # 0xa053
callq 0x2200
movq %rax, -0x188(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x188(%rbp), %rdi
movq 0x68(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
leaq 0x6b95(%rip), %rsi      # 0xa064
callq 0x2200
movq %rax, -0x180(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq -0x180(%rbp), %rdi
movq %rax, %rsi
callq 0x21e0
movq %rax, %rdi
movl $0x3a, %esi
callq 0x2280
movq %rax, -0x178(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq -0x178(%rbp), %rdi
movq 0x40(%rax), %rsi
subq $0x1, %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x2d, %esi
callq 0x2280
movq %rax, -0x170(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq -0x170(%rbp), %rdi
movq 0x50(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
leaq 0x6ad9(%rip), %rsi      # 0xa040
callq 0x2200
movq %rax, -0x168(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq -0x168(%rbp), %rdi
movq 0x70(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
leaq 0x6ab9(%rip), %rsi      # 0xa053
callq 0x2200
movq %rax, -0x160(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x5300
movq -0x160(%rbp), %rdi
movq 0x68(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x2e, %esi
callq 0x2280
movq %rax, %rdi
movq 0xb9de(%rip), %rsi      # 0xefb8
callq 0x2230
movq %rax, %rdi
movq 0xb9cf(%rip), %rsi      # 0xefb8
callq 0x2230
movl $0x2, %edi
callq 0x22a0
jmp 0x35fa
jmp 0x35fc
jmp 0x35fe
jmp 0x3600
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq 0x50(%rax), %rax
movq %rax, -0x1a8(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x1a8(%rbp), %rax
movq 0x40(%rcx), %rcx
subq $0x1, %rcx
subq %rcx, %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jl 0x37a0
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rsi
movq 0xb96a(%rip), %rdi      # 0xefc8
callq 0x21e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1d8(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1d8(%rbp), %rdi
movq 0x40(%rax), %rsi
subq $0x1, %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1d0(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1d0(%rbp), %rdi
movq 0x50(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1c8(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1c8(%rbp), %rdi
movq %rax, %rsi
addq $0x20, %rsi
callq 0x21e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1c0(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1c0(%rbp), %rdi
movss 0x60(%rax), %xmm0
callq 0x2180
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1b8(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1b8(%rbp), %rdi
movq 0x68(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1b0(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1b0(%rbp), %rdi
movq 0x70(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
movq 0xb81d(%rip), %rsi      # 0xefb8
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
jmp 0x3057
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq 0x50(%rax), %rax
movq %rax, -0x1e0(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq 0x40(%rcx), %rcx
subq $0x1, %rcx
subq %rcx, %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jl 0x395b
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq %rax, %rsi
movq 0xb7af(%rip), %rdi      # 0xefc8
callq 0x21e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x210(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x210(%rbp), %rdi
movq 0x40(%rax), %rsi
subq $0x1, %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x208(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x208(%rbp), %rdi
movq 0x50(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x200(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x200(%rbp), %rdi
movq %rax, %rsi
addq $0x20, %rsi
callq 0x21e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1f8(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1f8(%rbp), %rdi
movss 0x60(%rax), %xmm0
callq 0x2180
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1f0(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1f0(%rbp), %rdi
movq 0x68(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
movl $0x9, %esi
callq 0x2280
movq %rax, -0x1e8(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x5300
movq -0x1e8(%rbp), %rdi
movq 0x70(%rax), %rsi
callq 0x23e0
movq %rax, %rdi
movq 0xb662(%rip), %rsi      # 0xefb8
callq 0x2230
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0xb8(%rbp), %rdi
callq 0x23a0