// File: /workspace/github_cpp_makefile_star2_O1/Altius[P]hotspot2/src/hotspot2_part1.cpp
// Function: parseAndProcessInput(int const&, int const&, int const&, std::basic_ofstream<char, std::char_traits<char>>&)
bool parseAndProcessInput(const int& windowSize, const int& samplingInterval, const int& MAlength,
			  ofstream& ofsPvalData)
{
  const int BUFSIZE(1000);
  char buf[BUFSIZE], *p;
  long linenum(0);
  int fieldnum;
  const int halfWindowSize(windowSize / 2); // integer division
  SiteRange curSite, prevSite;
  
  BackgroundRegionManager brm(samplingInterval, MAlength);
  SiteManager sm(ofsPvalData);

  prevSite.chrom = NULL;
  prevSite.endPos = -1;
  curSite.ID = NULL;
  curSite.hasPval = false;
  curSite.pval = -1.;
#ifdef DEBUG
  curSite.sampled = false;
#endif

  long start, end;

  while (cin.getline(buf, BUFSIZE))
    {
      linenum++;
      fieldnum = 1;
      p = strtok(buf, "\t");
      curSite.chrom = intern(string(p));
      fieldnum++;
      if (!(p = strtok(NULL, "\t")))
        {
        MissingField:
          cerr << "Error:  Missing required field " << fieldnum
               << " on line " << linenum << "." << endl
               << endl;
          return false;
        }
      start = atol(p);
      fieldnum++;
      if (!(p = strtok(NULL, "\t")))
        goto MissingField;
      end = atol(p);
      fieldnum++;
      if (!(p = strtok(NULL, "\t")))
        goto MissingField;
      //curSite.ID = intern(string(p));
      fieldnum++;
      if (!(p = strtok(NULL, "\t")))
        goto MissingField;
      curSite.count = atoi(p);
      // ignore any further fields

      // When contiguous stretches of sites with identical counts are observed
      // within a line of input, they need to be processed one site at a time,
      // for statistical reasons.
      for (int siteEnd = start + 1; siteEnd <= end; siteEnd++)
        {
          curSite.endPos = siteEnd;
	  curSite.begPos = curSite.endPos - 1;

          if (curSite.chrom != prevSite.chrom || curSite.endPos > prevSite.endPos + halfWindowSize)
            {
              brm.computePandFlush(sm); // Compute P-values for all unprocessed sites in the window.
	      // Writes values to disk.
              // This method removes all count data from brm.
              brm.setBounds(curSite.chrom, curSite.endPos, curSite.endPos + windowSize - 1);
            }

          if (!brm.isSliding() && curSite.endPos < brm.getRightEdge())
            {
              brm.add(curSite);
              sm.addSite(curSite);
            }
          else
            brm.slideAndCompute(curSite, sm); // calls sm.addSite(curSite)

          prevSite = curSite;
        }
    }

  brm.computePandFlush(sm); // See explanatory comment above.
  sm.writeLastUnreportedSite();

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x668, %rsp            # imm = 0x668
movq %rcx, %rbx
movq %rdi, 0x68(%rsp)
movl (%rdi), %ebp
leaq 0xf0(%rsp), %rdi
callq 0x2d34
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rdi
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x705e
movl %ebp, %eax
shrl $0x1f, %eax
addl %ebp, %eax
sarl %eax
leaq 0x90(%rsp), %rcx
movq %rbx, 0x50(%rcx)
leaq 0x20(%rsp), %rcx
movq $0x0, 0x8(%rcx)
movb $0x0, 0x38(%rcx)
fld1
fchs
fstpt 0x30(%rsp)
movslq %eax, %r14
movq $-0x1, %r12
movq $0x0, 0x10(%rsp)
leaq 0x70(%rsp), %r13
xorl %r15d, %r15d
movq 0x752f(%rip), %rbx      # 0xcfd8
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %ecx
movl $0x3e8, %edx            # imm = 0x3E8
movq %rbx, %rdi
leaq 0x280(%rsp), %rsi
callq 0x23e0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x20(%rax,%rcx), %eax
movl %eax, 0x1c(%rsp)
testb $0x5, %al
jne 0x5ce2
leaq 0x280(%rsp), %rdi
leaq 0x3d29(%rip), %rbx      # 0x9824
movq %rbx, %rsi
callq 0x2330
movq %r13, %rdi
movq %rax, %rsi
leaq 0xf(%rsp), %rdx
callq 0x6d58
movq %r13, %rdi
callq 0x27fc
movq 0x10(%rsp), %rbp
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x5b47
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2230
incq %rbp
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2330
testq %rax, %rax
je 0x5d09
movq %rbp, 0x10(%rsp)
movq %rax, %rdi
callq 0x22b0
movq %rax, %rbp
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2330
testq %rax, %rax
je 0x5d10
movq %r14, %r13
movq %rax, %rdi
callq 0x22b0
movq %rbx, %r14
movq %rax, %rbx
xorl %edi, %edi
movq %r14, %rsi
callq 0x2330
testq %rax, %rax
je 0x5d17
xorl %edi, %edi
movq %r14, %rsi
callq 0x2330
testq %rax, %rax
je 0x5d1e
movq %rax, %rdi
callq 0x2320
movl %eax, 0x50(%rsp)
leal 0x1(%rbp), %eax
cltq
cmpq %rax, %rbx
movq %r13, %r14
leaq 0x70(%rsp), %r13
jl 0x5aa2
addl $0x2, %ebp
movq %rax, 0x48(%rsp)
leaq -0x1(%rax), %rcx
movq %rcx, 0x40(%rsp)
cmpq %r15, 0x20(%rsp)
leaq 0xf0(%rsp), %r15
jne 0x5c01
addq %r14, %r12
cmpq %rax, %r12
jge 0x5c2e
movq %r15, %rdi
leaq 0x90(%rsp), %rsi
callq 0x4148
movq 0x20(%rsp), %rsi
movl 0x48(%rsp), %edx
movq 0x68(%rsp), %rax
movl (%rax), %eax
leal (%rdx,%rax), %ecx
decl %ecx
movq %r15, %rdi
callq 0x2ea6
cmpb $0x0, 0x208(%rsp)
jne 0x5c9b
movslq 0xf4(%rsp), %rax
cmpq %rax, 0x48(%rsp)
jge 0x5c9b
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x2f3a
movq 0xc0(%rsp), %rax
movq 0xd0(%rsp), %rcx
addq $-0x40, %rcx
cmpq %rcx, %rax
je 0x5cce
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
movaps 0x50(%rsp), %xmm3
movaps %xmm3, 0x30(%rax)
movaps %xmm2, 0x20(%rax)
movaps %xmm1, 0x10(%rax)
movaps %xmm0, (%rax)
addq $0x40, 0xc0(%rsp)
jmp 0x5cb0
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x4320
movq 0x20(%rsp), %r15
movq 0x48(%rsp), %r12
movslq %ebp, %rax
leal 0x1(%rax), %ebp
cmpq %rax, %rbx
jge 0x5bdc
jmp 0x5aa2
leaq 0x90(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x78b6
jmp 0x5cb0
leaq 0xf0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x4148
leaq 0x90(%rsp), %rdi
callq 0x6ba4
jmp 0x5dd8
movl $0x2, %ebx
jmp 0x5d28
movl $0x3, %ebx
jmp 0x5d23
movl $0x4, %ebx
jmp 0x5d23
movl $0x5, %ebx
movq 0x10(%rsp), %rbp
movq 0x72c1(%rip), %rdi      # 0xcff0
leaq 0x3af0(%rip), %rsi      # 0x9826
movl $0x1f, %edx
callq 0x2270
movq 0x72a9(%rip), %rdi      # 0xcff0
movl %ebx, %esi
callq 0x2390
movq %rax, %rbx
leaq 0x39d7(%rip), %rsi      # 0x972f
movl $0x9, %edx
movq %rax, %rdi
callq 0x2270
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x21f0
movq %rax, %rbx
leaq 0x3a3c(%rip), %rsi      # 0x97b6
movl $0x1, %edx
movq %rax, %rdi
callq 0x2270
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq 0x90(%rsp), %rdi
callq 0x71d6
movq 0x260(%rsp), %rdi
testq %rdi, %rdi
je 0x5e02
movq 0x270(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
leaq 0x210(%rsp), %rdi
callq 0x7226
leaq 0x1a8(%rsp), %rdi
callq 0x7aee
leaq 0x158(%rsp), %rbx
movq %rbx, %rdi
callq 0x7aee
movq -0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x5e45
movq 0x150(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
testb $0x5, 0x1c(%rsp)
setne %al
addq $0x668, %rsp            # imm = 0x668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5ea1
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x5e94
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x5e94
jmp 0x5e91
jmp 0x5e91
jmp 0x5e91
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x71d6
leaq 0xf0(%rsp), %rdi
callq 0x6d96
movq %rbx, %rdi
callq 0x23a0