// File: /workspace/github_cpp_makefile_star2_O2/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 0x289b
movq %rsi, %r12
movq %rdx, 0x10(%rsp)
leaq -0x30c(%rip), %rsi      # 0x242c
movq %r14, %rdi
callq 0x36be
movq 0x60(%rbx), %r15
leaq 0x8(%rsp), %rbp
cmpq %r14, %r15
je 0x2819
movl 0x5c(%r15), %eax
movl %eax, 0xc(%rsp)
movl 0x6c(%r15), %eax
movl %eax, 0x8(%rsp)
movss 0x58(%r15), %xmm0
movss %xmm0, 0x70(%r15)
movq 0x60(%r15), %rax
addq 0x50(%r15), %rax
cvtsi2sd %rax, %xmm0
mulsd 0x28ad(%rip), %xmm0     # 0x5030
callq 0x22a0
cvttsd2si %xmm0, %eax
movslq %eax, %r13
movq %r13, 0x78(%r15)
movq %rbx, %rdi
leaq 0xc(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x24bf
testb %al, %al
je 0x2803
leaq 0x50(%r15), %rax
leaq 0x60(%r15), %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(%r15)
subq %rax, %r13
movq %r13, %rax
negq %rax
cmovsq %r13, %rax
movq %rax, 0x88(%r15)
movq (%r15), %r15
jmp 0x2749
movq (%r15), %r13
movq %r14, %rdi
movq %r15, %rsi
callq 0x3874
movq %r13, %r15
jmp 0x2749
movq (%r14), %rax
cmpq %r14, %rax
je 0x28b8
movq 0x88(%rax), %rcx
movq %rax, %r15
movq (%rax), %rax
cmpq %r14, %rax
je 0x284b
movq 0x88(%rax), %rdx
cmpq %rcx, %rdx
cmovlq %rax, %r15
cmovlq %rdx, %rcx
jmp 0x282f
leaq 0x10(%r15), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2100
leaq 0x20(%rbx), %rsi
addq $0x30, %r15
movq %r15, %rdi
callq 0x2100
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3882
movq 0x48(%rbx), %rax
cmpq %rax, 0x50(%rbx)
je 0x2885
movq %rax, 0x50(%rbx)
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x38ae
movq 0x48(%rbx), %rax
cmpq 0x50(%rbx), %rax
je 0x28a9
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 0x28a5
jmp 0x28a9