// File: Altius[P]hotspot2/src/hotspot2_part1.cpp
// Function: BackgroundRegionManager::add(SiteRange const&)
void BackgroundRegionManager::add(const SiteRange& s)
{
  if (s.endPos > m_posR)
    {
      cerr << "Coding error:  BRM::add(), region " << *m_pCurChrom << ':' << m_posL << '-' << m_posR
           << " received out-of-bounds position " << s.endPos
           << " (line " << __LINE__ << " of the code)." << endl
           << endl;
      exit(1);
    }
  SiteData sd;
  sd.pos = s.endPos;
  sd.count = s.count;
  sd.hasPval = false;
  sd.sampled = false;

  if (1 != m_samplingInterval && s.endPos > m_nextPosToSample)
    m_nextPosToSample = s.endPos;

  // Alternatively, "sampling" could be performed strictly at multiples of m_samplingInterval.
  // If we intend to sample at, say, positions 200, 400, 600, 800, 1000, etc.,
  // and regions 390-402 and 598-620 are excluded (unmappable),
  // then the current code will sample at 200, 403, 621, 821, etc.
  // Sampling strictly at multiples of m_samplingInterval would in this case
  // sample at 200, 800, 1000, etc.
  // Use the following code if strict multiples of m_samplingInterval are desired.
  //
  // m_nextPosToSample += static_cast<int>(ceil(static_cast<double>(s.endPos - m_nextPosToSample)/static_cast<double>(m_samplingInterval))) * m_samplingInterval;
  //
  // The above is a one-step version of "while(s.endPos > m_nextPosToSample){m_nextPosToSample += m_samplingInterval;}."
  // Typically, m_nextPosToSample will only need to increase by m_samplingInterval;
  // it will need to increase by a greater multiple of m_samplingInterval when a large gap is present in the data
  // (an unmappable region or otherwise restricted/withheld region).

  if (1 == m_samplingInterval || s.endPos == m_nextPosToSample)
    {
      sd.sampled = true;
      m_nextPosToSample += m_samplingInterval;
      // Add the incoming site's count to the distribution of counts observed in this region.
      if (s.count < static_cast<int>(m_distn.size()))
        m_distn[s.count].numOccs++;
      else
        {
          StatsForCount sc;
          sc.numOccs = 0;
          sc.pmf = sc.pval = -1.;
          sc.MAxN = -1; // don't compute moving averages until/unless we're sliding, for efficiency's sake
          while (static_cast<int>(m_distn.size()) < s.count)
            {
              m_distn.push_back(sc); // create bins for unobserved interior values, e.g., count = 5 but only 0,1,2 have been observed so far
              m_sampledDataDistnSize++;
            }
          sc.numOccs = 1;
          m_distn.push_back(sc);
          m_sampledDataDistnSize++;
        }
      if ( static_cast<int>(m_distn.size()) >= m_MAlength && m_distn[s.count].MAxN != -1 && m_distn[s.count].MAxN >= m_modeYval)
        {
          m_modeXval = s.count;
          m_modeYval = m_distn[s.count].MAxN;
        }
    }

  // Add the incoming site to the appropriate deque of observed positions.
  if (s.endPos < m_posC)
    m_sitesInRegion_leftHalf.push_back(sd);
  else
    m_sitesInRegion_rightHalf.push_back(sd);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jle 0x442c
movq 0x16c8c(%rip), %rdi     # 0x1aff0
leaq 0xf05a(%rip), %rsi      # 0x133c5
callq 0x3240
movq %rax, %rdi
movq -0x68(%rbp), %rax
movq 0x188(%rax), %rsi
callq 0x3230
movq %rax, %rdi
movl $0x3a, %esi
callq 0x3310
movq %rax, %rdi
movq -0x68(%rbp), %rax
movl (%rax), %esi
callq 0x3420
movq %rax, %rdi
movl $0x2d, %esi
callq 0x3310
movq %rax, %rdi
movq -0x68(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x3420
movq %rax, %rdi
leaq 0xf024(%rip), %rsi      # 0x133e8
callq 0x3240
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x3470
movq %rax, %rdi
leaq 0xf027(%rip), %rsi      # 0x1340a
callq 0x3240
movq %rax, %rdi
movl $0x163, %esi            # imm = 0x163
callq 0x3420
movq %rax, %rdi
leaq 0xf013(%rip), %rsi      # 0x13412
callq 0x3240
movq %rax, %rdi
movq 0x16b9a(%rip), %rsi     # 0x1afa8
callq 0x3280
movq %rax, %rdi
movq 0x16b8b(%rip), %rsi     # 0x1afa8
callq 0x3280
movl $0x1, %edi
callq 0x3330
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x18(%rbp)
movb $0x0, -0x14(%rbp)
movb $0x0, -0x13(%rbp)
movl $0x1, %eax
cmpl 0x158(%rcx), %eax
je 0x4484
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq 0x15c(%rcx), %rcx
cmpq %rcx, %rax
jle 0x4484
movq -0x68(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl %ecx, 0x15c(%rax)
movq -0x68(%rbp), %rcx
movl $0x1, %eax
cmpl 0x158(%rcx), %eax
je 0x44b1
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq 0x15c(%rcx), %rcx
cmpq %rcx, %rax
jne 0x4613
movq -0x68(%rbp), %rdi
movb $0x1, -0x13(%rbp)
movl 0x158(%rdi), %eax
addl 0x15c(%rdi), %eax
movl %eax, 0x15c(%rdi)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x6c(%rbp)
addq $0x50, %rdi
callq 0xa930
movq %rax, %rcx
movl -0x6c(%rbp), %eax
cmpl %ecx, %eax
jge 0x4509
movq -0x68(%rbp), %rdi
addq $0x50, %rdi
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rsi
callq 0xa950
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x458a
movl $0x0, -0x60(%rbp)
fld1
fchs
fld %st(0)
fstpt -0x40(%rbp)
fstpt -0x50(%rbp)
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movq -0x68(%rbp), %rdi
addq $0x50, %rdi
callq 0xa930
movq -0x10(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x455f
movq -0x68(%rbp), %rdi
addq $0x50, %rdi
leaq -0x60(%rbp), %rsi
callq 0xa970
movq -0x68(%rbp), %rax
movl 0x160(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x160(%rax)
jmp 0x4523
movq -0x68(%rbp), %rdi
movl $0x1, -0x60(%rbp)
addq $0x50, %rdi
leaq -0x60(%rbp), %rsi
callq 0xa970
movq -0x68(%rbp), %rax
movl 0x160(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x160(%rax)
movq -0x68(%rbp), %rdi
addq $0x50, %rdi
callq 0xa930
movq -0x68(%rbp), %rcx
cmpl 0x3c(%rcx), %eax
jl 0x4611
movq -0x68(%rbp), %rdi
addq $0x50, %rdi
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rsi
callq 0xa950
cmpl $-0x1, 0x30(%rax)
je 0x4611
movq -0x68(%rbp), %rdi
addq $0x50, %rdi
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rsi
callq 0xa950
movq -0x68(%rbp), %rcx
movl 0x30(%rax), %eax
cmpl 0x10c(%rcx), %eax
jl 0x4611
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, 0x108(%rdi)
addq $0x50, %rdi
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rsi
callq 0xa950
movq %rax, %rcx
movq -0x68(%rbp), %rax
movl 0x30(%rcx), %ecx
movl %ecx, 0x10c(%rax)
jmp 0x4613
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq 0x8(%rcx), %rcx
cmpq %rcx, %rax
jge 0x463b
movq -0x68(%rbp), %rdi
addq $0x68, %rdi
leaq -0x1c(%rbp), %rsi
callq 0xa9e0
jmp 0x464f
movq -0x68(%rbp), %rdi
addq $0xb8, %rdi
leaq -0x1c(%rbp), %rsi
callq 0xa9e0
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)