// File: /workspace/github_cpp_makefile_star2_O3/Altius[P]hotspot2/src/findVarWidthPeaks.cpp
// Function: processRegionAndFlushIt(Region&, long const&, std::vector<Range, std::allocator<Range>>&)
void processRegionAndFlushIt(Region& reg, const long& minWidth, vector<Range>& peaks)
{
  if (reg.localMaxima.empty())
    {
      if (!reg.posns.empty())
	reg.posns.clear();
      return;
    }

  reg.localMaxima.sort(Height_GT);
  // Local maxima are now sorted in descending order by y-value (height), if there are 2 or more of them.
  // Local maxima with equal y-values, if any, are sorted from L to R in genomic order.
  // We traverse the list of local maxima in this order, tracing the shape of each down to half of its maximum.
  list<Range>::iterator it1 = reg.localMaxima.begin(), it2;
  while (it1 != reg.localMaxima.end())
    {
      int idxL(it1->beg.idx), idxR(it1->end.idx);
 
      it1->maxY = it1->beg.y;
      it1->x_of_maxY = static_cast<int>(floor(0.5*static_cast<double>(it1->beg.x + it1->end.x))); // important to use double, since x can have many digits
      if (!getPeakBoundaries(reg, idxL, idxR, minWidth)) // idxL and idxR will be changed upon return
	{
	  // The criteria for being called a "peak" were not met by this local maximum:
	  // it's too narrow (narrower than midWidth), or it doesn't contain reg.posSummit.
	  it2 = it1;
	  it1++;
	  reg.localMaxima.erase(it2);
	  continue;
	}
      it1->beg = reg.posns[idxL];
      it1->end = reg.posns[idxR];
      it1->inputSummit = reg.posSummit;
      it1->summitSeparation = labs(it1->x_of_maxY - it1->inputSummit);
      it1++;
    }

  // We now have 0, 1, or more peaks that are at least minWidth bp wide
  // and contain reg.posSummit.
  if (reg.localMaxima.empty())
    {
      reg.posns.clear();
      return;
    }

  long minSeparation = reg.localMaxima.begin()->summitSeparation;
  it1 = it2 = reg.localMaxima.begin();
  it2++;
  while (it2 != reg.localMaxima.end())
    {
      if (it2->summitSeparation < minSeparation)
	{
	  minSeparation = it2->summitSeparation;
	  it1 = it2;
	}
      it2++;
    }

  it1->chrom = reg.chrom;
  it1->id = reg.id;
  peaks.push_back(*it1);
  reg.posns.clear();
  reg.localMaxima.clear();
  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x60(%rdi), %r14
cmpq %r14, 0x60(%rdi)
je 0x299c
movq %rdx, %rbp
movq %rsi, %r13
leaq -0x3a8(%rip), %rsi      # 0x246c
movq %r14, %rdi
callq 0x3faa
movq (%r14), %r12
cmpq %r14, %r12
je 0x2912
movq %rbp, 0x10(%rsp)
leaq 0x8(%rsp), %r15
movl 0x5c(%r12), %eax
movl %eax, 0xc(%rsp)
movl 0x6c(%r12), %eax
movl %eax, 0x8(%rsp)
movss 0x58(%r12), %xmm0
movss %xmm0, 0x70(%r12)
movq 0x60(%r12), %rax
addq 0x50(%r12), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x37c4(%rip), %xmm0     # 0x6030
callq 0x22a0
cvttsd2si %xmm0, %eax
movslq %eax, %rbp
movq %rbp, 0x78(%r12)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x251a
testb %al, %al
je 0x28ef
leaq 0x50(%r12), %rax
leaq 0x60(%r12), %rcx
movslq 0xc(%rsp), %rdx
movq 0x48(%rbx), %rsi
shlq $0x4, %rdx
movups (%rsi,%rdx), %xmm0
movups %xmm0, (%rax)
movslq 0x8(%rsp), %rax
movq 0x48(%rbx), %rdx
shlq $0x4, %rax
movupd (%rdx,%rax), %xmm0
movupd %xmm0, (%rcx)
movq 0x40(%rbx), %rax
movq %rax, 0x80(%r12)
subq %rax, %rbp
movq %rbp, %rax
negq %rax
cmovsq %rbp, %rax
movq %rax, 0x88(%r12)
movq (%r12), %r12
jmp 0x2901
movq (%r12), %rbp
movq %r14, %rdi
movq %r12, %rsi
callq 0x44de
movq %rbp, %r12
cmpq %r14, %r12
jne 0x2832
movq (%r14), %r12
movq 0x10(%rsp), %rbp
cmpq %r14, %r12
je 0x29b9
movq (%r12), %rax
cmpq %r14, %rax
je 0x2949
movq 0x88(%r12), %rcx
movq 0x88(%rax), %rdx
cmpq %rcx, %rdx
cmovlq %rax, %r12
movq (%rax), %rsi
cmovlq %rdx, %rcx
movq %rsi, %rax
cmpq %r14, %rsi
jne 0x292c
leaq 0x10(%r12), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20e0
leaq 0x20(%rbx), %rsi
addq $0x30, %r12
movq %r12, %rdi
callq 0x20e0
movq %rbp, %rdi
movq %r15, %rsi
callq 0x41fe
movq 0x48(%rbx), %rax
cmpq %rax, 0x50(%rbx)
je 0x2982
movq %rax, 0x50(%rbx)
movq %r14, %rdi
callq 0x439c
movq %r14, 0x68(%rbx)
movq %r14, 0x60(%rbx)
movq $0x0, 0x70(%rbx)
jmp 0x29aa
movq 0x48(%rbx), %rax
cmpq 0x50(%rbx), %rax
je 0x29aa
movq %rax, 0x50(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rbx), %rax
cmpq %rax, 0x50(%rbx)
jne 0x29a6
jmp 0x29aa