// File: /workspace/github_cpp_makefile_star2_O3/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), %r15
movq 0x8(%rdi), %r12
cmpq %r12, %r15
je 0x2fa8
movq %rdi, %rbx
movq %r12, %rax
subq %r15, %rax
sarq $0x7, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x559(%rip), %r13      # 0x24b3
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rcx
callq 0x4896
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x49ea
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
shrq $0x7, %rax
cmpl $0x2, %eax
jl 0x2e41
leaq 0x38(%rsp), %r14
movl $0x1, %r13d
xorl %r15d, %r15d
movq %rbx, (%rsp)
movq %r13, 0x10(%rsp)
shlq $0x7, %r13
movq 0x40(%rsi,%r13), %rax
movq %r15, 0x20(%rsp)
shlq $0x7, %r15
movq 0x50(%rsi,%r15), %rcx
cmpq %rcx, %rax
jg 0x2cbc
leaq (%rsi,%r13), %rbp
leaq (%rsi,%r13), %rbx
addq $0x40, %rbx
leaq (%rsi,%r15), %r12
xorl %edx, %edx
movq 0x68(%r12), %r8
cmpq %rax, %r8
jl 0x2c03
movq 0x68(%rbp), %rdi
cmpq %rcx, %rdi
jg 0x2c10
testb $0x1, %dl
jne 0x2c1b
movq %r14, 0x28(%rsp)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0x42fc
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x20(%rbp), %rsi
movq 0x28(%rbp), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0x42fc
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups 0x30(%rbx), %xmm3
movups %xmm3, 0x60(%r14)
movups %xmm2, 0x50(%r14)
movups %xmm1, 0x40(%r14)
movups %xmm0, 0x30(%r14)
movq (%rsp), %rbx
movq (%rbx), %r12
leaq (%r12,%r15), %rbp
addq %r13, %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x20e0
leaq 0x20(%r12), %rdi
leaq 0x20(%rbp), %rsi
callq 0x20e0
movups 0x40(%rbp), %xmm0
movups 0x50(%rbp), %xmm1
movups 0x60(%rbp), %xmm2
movups 0x70(%rbp), %xmm3
movups %xmm3, 0x70(%r12)
movups %xmm2, 0x60(%r12)
movups %xmm1, 0x50(%r12)
movups %xmm0, 0x40(%r12)
movq (%rbx), %r12
addq %r15, %r12
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
callq 0x20e0
leaq 0x20(%r12), %rdi
leaq 0x48(%rsp), %rsi
callq 0x20e0
movups 0x30(%r14), %xmm0
movups 0x40(%r14), %xmm1
movups 0x50(%r14), %xmm2
movups 0x60(%r14), %xmm3
movups %xmm3, 0x70(%r12)
movups %xmm2, 0x60(%r12)
movups %xmm1, 0x50(%r12)
movups %xmm0, 0x40(%r12)
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2bc1
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0x2bd8
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21a0
movq (%rbx), %rsi
leaq (%rsi,%r13), %rbp
leaq (%rsi,%r13), %rbx
addq $0x40, %rbx
movq (%rbx), %rax
leaq (%rsi,%r15), %r12
movq 0x50(%rsi,%r15), %rcx
movb $0x1, %dl
cmpq %rcx, %rax
jle 0x2a88
jmp 0x2cbc
decq %rax
movq %rax, 0x50(%r12)
jmp 0x2cbc
incq %rcx
movq %rcx, (%rbx)
jmp 0x2cbc
cmpq %rax, 0x40(%r12)
jne 0x2fba
cmpq 0x50(%rbp), %rcx
jne 0x2fba
movq 0x70(%r12), %rax
subq %r8, %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
movq 0x70(%rbp), %rax
subq %rdi, %rax
movq %rax, %rdx
negq %rdx
cmovsq %rax, %rdx
cmpq %rdx, %rcx
jbe 0x2caa
leaq 0x40(%r12), %rax
movq %rax, 0x18(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x20e0
addq $0x20, %r12
addq $0x20, %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x20e0
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups 0x30(%rbx), %xmm3
movq 0x18(%rsp), %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq (%rsp), %rax
movq (%rax), %rsi
addq %r13, %rsi
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x5918
movq (%rbx), %rsi
movq 0x50(%rsi,%r15), %rax
subq 0x40(%rsi,%r15), %rax
incq %rax
movq 0x8(%rsp), %rcx
cmpq (%rcx), %rax
jge 0x2cdc
movq (%rsp), %rbx
jmp 0x2e16
addq %r15, %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
movq 0x62db(%rip), %rdi      # 0x8fc8
movq %rax, %rsi
callq 0x21c0
movb $0x9, %bpl
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x28(%rsp), %r12
movq %r12, %rsi
callq 0x21c0
movq (%rsp), %rbx
movq (%rbx), %rcx
movq 0x40(%rcx,%r15), %rsi
decq %rsi
movq %rax, %rdi
callq 0x2160
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x50(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2160
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x20(%rcx,%r15), %rsi
movq 0x28(%rcx,%r15), %rdx
movq %rax, %rdi
callq 0x21c0
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x21c0
movq (%rbx), %rcx
cvtss2sd 0x60(%rcx,%r15), %xmm0
movq %rax, %rdi
callq 0x2260
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x68(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2160
movb %bpl, 0x28(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x21c0
movq (%rbx), %rcx
movq 0x70(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2160
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movq (%rbx), %rsi
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r15
incq %r15
incq %r13
movq 0x8(%rbx), %rax
subq %rsi, %rax
shrq $0x7, %rax
cltq
cmpq %rax, %r13
jl 0x2a51
movl %r15d, %r15d
jmp 0x2e44
xorl %r15d, %r15d
shlq $0x7, %r15
movq 0x50(%rsi,%r15), %rax
subq 0x40(%rsi,%r15), %rax
incq %rax
movq 0x8(%rsp), %rcx
cmpq (%rcx), %rax
jl 0x2fa8
addq %r15, %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
movq 0x6154(%rip), %rdi      # 0x8fc8
movq %rax, %rsi
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 0x602f(%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,%r15), %rsi
decq %rsi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
movl $0x2d, %esi
callq 0x21d0
movq (%rbx), %rcx
movq 0x50(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2300
leaq 0x302f(%rip), %r14      # 0x6040
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
movq (%rbx), %rcx
movq 0x70(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2300
leaq 0x3020(%rip), %r12      # 0x6053
movq %rax, %rdi
movq %r12, %rsi
callq 0x2180
movq (%rbx), %rcx
movq 0x68(%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x2300
leaq 0x300f(%rip), %rsi      # 0x6064
movq %rax, %rdi
callq 0x2180
movq (%rbx), %rsi
addq %r13, %rsi
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
movl $0x3a, %esi
callq 0x21d0
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
movq %rax, %rdi
movq %r14, %rsi
callq 0x2180
movq (%rbx), %rcx
movq 0x70(%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
movq %r12, %rsi
callq 0x2180
movq (%rbx), %rcx
movq 0x68(%rcx,%r13), %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 %r14, %rdi
je 0x312e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x312e
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4228
movq %rbx, %rdi
callq 0x22d0