// File: /workspace/github_cpp_makefile_star2_O1/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 0x2906
movq %rsi, %r12
movq %rdx, 0x10(%rsp)
leaq -0x328(%rip), %rsi      # 0x246c
movq %r14, %rdi
callq 0x3f38
movq (%r14), %r13
cmpq %r14, %r13
je 0x287a
leaq 0x8(%rsp), %r15
movl 0x5c(%r13), %eax
movl %eax, 0xc(%rsp)
movl 0x6c(%r13), %eax
movl %eax, 0x8(%rsp)
movss 0x58(%r13), %xmm0
movss %xmm0, 0x70(%r13)
movq 0x60(%r13), %rax
addq 0x50(%r13), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x384f(%rip), %xmm0     # 0x6030
callq 0x22a0
cvttsd2si %xmm0, %eax
movslq %eax, %rbp
movq %rbp, 0x78(%r13)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x251a
testb %al, %al
je 0x285f
leaq 0x50(%r13), %rax
leaq 0x60(%r13), %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(%r13)
subq %rax, %rbp
movq %rbp, %rax
negq %rax
cmovsq %rbp, %rax
movq %rax, 0x88(%r13)
movq (%r13), %r13
jmp 0x2871
movq (%r13), %rbp
movq %r14, %rdi
movq %r13, %rsi
callq 0x4486
movq %rbp, %r13
cmpq %r14, %r13
jne 0x27ad
movq (%r14), %r15
cmpq %r14, %r15
je 0x2923
movq (%r15), %rax
cmpq %r14, %rax
je 0x28b2
movq 0x88(%r15), %rcx
movq 0x88(%rax), %rdx
cmpq %rcx, %rdx
cmovlq %rax, %r15
movq (%rax), %rsi
cmovlq %rdx, %rcx
movq %rsi, %rax
cmpq %r14, %rsi
jne 0x2895
leaq 0x10(%r15), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x20e0
leaq 0x20(%rbx), %rsi
addq $0x30, %r15
movq %r15, %rdi
callq 0x20e0
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x41aa
movq 0x48(%rbx), %rax
cmpq %rax, 0x50(%rbx)
je 0x28ec
movq %rax, 0x50(%rbx)
movq %r14, %rdi
callq 0x4344
movq %r14, 0x68(%rbx)
movq %r14, 0x60(%rbx)
movq $0x0, 0x70(%rbx)
jmp 0x2914
movq 0x48(%rbx), %rax
cmpq 0x50(%rbx), %rax
je 0x2914
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 0x2910
jmp 0x2914