// File: /workspace/github_cpp_makefile_star2_O3/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 0x2d1e
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 0x738c
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 0x71f1(%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 0x602b
leaq 0x280(%rsp), %rdi
leaq 0x3a1b(%rip), %rbx      # 0x9854
movq %rbx, %rsi
callq 0x2330
movq %r13, %rdi
movq %rax, %rsi
leaq 0xf(%rsp), %rdx
callq 0x7086
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 0x5e85
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2230
incq %rbp
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2330
testq %rax, %rax
je 0x6052
movq %rbp, 0x10(%rsp)
movq %rax, %rdi
callq 0x22b0
movq %rax, %rbp
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2330
testq %rax, %rax
je 0x6059
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 0x6060
xorl %edi, %edi
movq %r14, %rsi
callq 0x2330
testq %rax, %rax
je 0x6067
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 0x5de0
movq 0x20(%rsp), %rcx
addl $0x2, %ebp
movq %rax, 0x48(%rsp)
leaq -0x1(%rax), %rdx
movq %rdx, 0x40(%rsp)
cmpq %r15, %rcx
leaq 0xf0(%rsp), %r15
jne 0x5f42
addq %r14, %r12
cmpq %rax, %r12
jge 0x5f6f
movq %r15, %rdi
leaq 0x90(%rsp), %rsi
callq 0x40f2
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 0x2e90
cmpb $0x0, 0x208(%rsp)
jne 0x5fdc
movslq 0xf4(%rsp), %rax
cmpq %rax, 0x48(%rsp)
jge 0x5fdc
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x2f24
movq 0xc0(%rsp), %rax
movq 0xd0(%rsp), %rcx
addq $-0x40, %rcx
cmpq %rcx, %rax
je 0x600f
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 0x5ff1
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x42b2
movq 0x20(%rsp), %rcx
movq 0x48(%rsp), %r12
movslq %ebp, %rax
leal 0x1(%rax), %ebp
movq %rcx, %r15
cmpq %rax, %rbx
jge 0x5f1f
jmp 0x6023
leaq 0x90(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x7bf2
jmp 0x5ff1
movq %rcx, %r15
jmp 0x5de0
leaq 0xf0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x40f2
leaq 0x90(%rsp), %rdi
callq 0x6ed2
jmp 0x6121
movl $0x2, %ebx
jmp 0x6071
movl $0x3, %ebx
jmp 0x606c
movl $0x4, %ebx
jmp 0x606c
movl $0x5, %ebx
movq 0x10(%rsp), %rbp
movq 0x6f78(%rip), %rdi      # 0xcff0
leaq 0x37d7(%rip), %rsi      # 0x9856
movl $0x1f, %edx
callq 0x2270
movq 0x6f60(%rip), %rdi      # 0xcff0
movl %ebx, %esi
callq 0x2390
movq %rax, %rbx
leaq 0x36be(%rip), %rsi      # 0x975f
movl $0x9, %edx
movq %rax, %rdi
callq 0x2270
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x21f0
movq %rax, %rbx
leaq 0x3723(%rip), %rsi      # 0x97e6
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 0x7504
movq 0x260(%rsp), %rdi
testq %rdi, %rdi
je 0x614b
movq 0x270(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
leaq 0x210(%rsp), %rdi
callq 0x755c
leaq 0x1a8(%rsp), %rdi
callq 0x7e2a
leaq 0x158(%rsp), %rbx
movq %rbx, %rdi
callq 0x7e2a
movq -0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x618e
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 0x61ea
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x61dd
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x61dd
jmp 0x61da
jmp 0x61da
jmp 0x61da
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x7504
leaq 0xf0(%rsp), %rdi
callq 0x70c4
movq %rbx, %rdi
callq 0x23a0