// File: /workspace/github_cpp_makefile_star2_O2/Altius[P]hotspot2/src/hotspot2_part2.cpp
// Function: parseAndProcessInput(std::map<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, std::less<int>, std::allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*>>> const&, std::vector<std::pair<int, long double>, std::allocator<std::pair<int, long double>>> const&, int const&, long double const&, bool const&)
bool parseAndProcessInput(const map<int, string*>& intToChromNameMap, const vector<pair<int, long double> >& PvalToFDRmapping,
			  const int& N, const long double& FDRthreshold, const bool& writePvals)
{
  const int BUFSIZE(1000);
  char buf[BUFSIZE], *p;
  vector<SiteRangeData> vec;
  vector<SiteRangeData>::iterator it;
  int linenum(0), fieldnum;

  vec.resize(N);

  while (cin.getline(buf, BUFSIZE))
    {
      linenum++;
      if (linenum > N)
	{
	  cerr << "Error:  Expected to find exactly " << N
	       << " lines of data in the file of location and P-value data, but at least one additional line was found."
	       << endl << endl;
	  return false;
	}
      if (1 == linenum)
	it = vec.begin();
      else
	it++;
      fieldnum = 1;
      if (!(p = strtok(buf, "\t")) || !*p)
	{
	MissingField:
	  cerr << "Error:  Failed to find required field " << fieldnum
	       << " on line " << linenum << " of the file of location and P-value data."
	       << endl << endl;
	  return false;
	}
      it->chromID = atoi(p);
      fieldnum++;
      if (!(p = strtok(NULL, "\t")) || !*p)
	goto MissingField;
      it->begPos = atoi(p);
      fieldnum++;
      if (!(p = strtok(NULL, "\t")) || !*p)
	goto MissingField;
      it->width = atoi(p);
      fieldnum++;
      if (!(p = strtok(NULL, "\t")) || !*p)
	goto MissingField;
      it->negLog10P_scaled = atoi(p);
    }

  if (linenum < N)
    {
      cerr << "Error:  Expected to find exactly " << N
	   << " lines of data in the file of location and P-value data,"
	   << " but only " << linenum << " lines were found."
	   << endl << endl;
      return false;
    }

  sort(vec.begin(), vec.end(), NegLog10Pscaled_GT);

  if (vec[0].negLog10P_scaled != PvalToFDRmapping[0].first)
    {
      cerr << "Coding error:  Line " << __LINE__
	   << ", expected scaled -log10(P) values in the initial elements\n"
	   << "of the genomic data and P-to-FDR vectors to be equal, but they are not\n"
	   << '(' << vec[0].negLog10P_scaled << " for the former and "
	   << PvalToFDRmapping[0].first << " for the latter.)"
	   << endl << endl;
      return false;
    }

  it = vec.begin();
  for (vector<pair<int, long double> >::const_iterator itPtoQ = PvalToFDRmapping.begin();
       itPtoQ != PvalToFDRmapping.end(); itPtoQ++)
    {
      while (it != vec.end() && it->negLog10P_scaled == itPtoQ->first)
	{
	  it->FDR = itPtoQ->second;
	  it++;
	}
    }
  while (it != vec.end())
    {
      it->FDR = 1.;
      it++;
    }
  
  sort(vec.begin(), vec.end(), OutputOrder_LT);  
  int prevChromID(-1);
  string *prevChromString(NULL);
  
  for (it = vec.begin(); it != vec.end(); it++)
    {
      // Even though the map is tiny, perform as few map lookups as possible.
      if (it->chromID != prevChromID)
	{
	  prevChromString = intToChromNameMap.at(it->chromID);
	  prevChromID = it->chromID;
	}
      if (it->FDR <= FDRthreshold)
	{
	  cout << *prevChromString << '\t'
	       << it->begPos << '\t'
	       << it->begPos + it->width << "\ti\t"
	       << it->FDR;
	  if (writePvals)
	    cout << '\t' << pow(10., -it->negLog10P_scaled/CHANGE_OF_SCALE);
	  cout << '\n';
	}
    }
      
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %r8, 0x58(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, 0x60(%rsp)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movslq (%rdx), %rsi
callq 0x36ee
movq %rbx, 0x48(%rsp)
xorl %ebx, %ebx
movq 0x565b(%rip), %r15      # 0x7fd8
leaq 0x70(%rsp), %r12
leaq 0x29fa(%rip), %r14      # 0x5383
xorl %ebp, %ebp
movl $0x3e8, %edx            # imm = 0x3E8
movq %r15, %rdi
movq %r12, %rsi
callq 0x2070
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2acf
cmpl (%r13), %ebp
jge 0x2b28
testl %ebp, %ebp
jne 0x29c2
movq 0x30(%rsp), %rbx
jmp 0x29c6
addq $0x20, %rbx
movq %r12, %rdi
movq %r14, %rsi
callq 0x2240
testq %rax, %rax
je 0x2a49
cmpb $0x0, (%rax)
je 0x2a49
movq %rax, %rdi
callq 0x2230
movl %eax, 0xc(%rbx)
xorl %edi, %edi
movq %r14, %rsi
callq 0x2240
testq %rax, %rax
je 0x2a4d
cmpb $0x0, (%rax)
je 0x2a4d
movq %rax, %rdi
callq 0x2230
movl %eax, 0x4(%rbx)
xorl %edi, %edi
movq %r14, %rsi
callq 0x2240
testq %rax, %rax
je 0x2a51
cmpb $0x0, (%rax)
je 0x2a51
movq %rax, %rdi
callq 0x2230
movl %eax, 0x8(%rbx)
xorl %edi, %edi
movq %r14, %rsi
callq 0x2240
testq %rax, %rax
je 0x2a55
cmpb $0x0, (%rax)
je 0x2a55
movq %rax, %rdi
callq 0x2230
movl %eax, (%rbx)
incl %ebp
jmp 0x298b
pushq $0x1
jmp 0x2a57
pushq $0x2
jmp 0x2a57
pushq $0x3
jmp 0x2a57
pushq $0x4
popq %rbx
movq 0x5591(%rip), %rdi      # 0x7ff0
leaq 0x27b2(%rip), %rsi      # 0x5218
callq 0x2190
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
leaq 0x260a(%rip), %rsi      # 0x5086
movq %rax, %rdi
callq 0x2190
incl %ebp
movq %rax, %rdi
movl %ebp, %esi
callq 0x2290
leaq 0x27a8(%rip), %rsi      # 0x523f
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
xorl %ebx, %ebx
leaq 0x30(%rsp), %rdi
callq 0x3ff4
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl (%r13), %ebp
jge 0x2b53
movq 0x5514(%rip), %rdi      # 0x7ff0
leaq 0x26ae(%rip), %rsi      # 0x5191
callq 0x2190
movl (%r13), %esi
movq %rax, %rdi
callq 0x2290
leaq 0x276f(%rip), %rsi      # 0x526a
movq %rax, %rdi
callq 0x2190
leaq 0x2799(%rip), %rsi      # 0x52a3
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
movl %ebp, %esi
callq 0x2290
leaq 0x278b(%rip), %rsi      # 0x52ae
jmp 0x2a97
movq 0x54c1(%rip), %rdi      # 0x7ff0
leaq 0x265b(%rip), %rsi      # 0x5191
callq 0x2190
movl (%r13), %esi
movq %rax, %rdi
callq 0x2290
leaq 0x2665(%rip), %rsi      # 0x51b3
jmp 0x2a97
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq -0x748(%rip), %rdx      # 0x241c
callq 0x41f3
movq 0x48(%rsp), %rbx
movq 0x30(%rsp), %rdi
movl (%rdi), %eax
movq (%rbx), %rcx
cmpl (%rcx), %eax
jne 0x2bb0
movq 0x8(%rbx), %rdx
movq 0x38(%rsp), %rsi
movq %rdi, %rax
cmpq %rdx, %rcx
je 0x2c30
cmpq %rsi, %rax
je 0x2baa
movl (%rax), %r8d
cmpl (%rcx), %r8d
jne 0x2baa
fldt 0x10(%rcx)
fstpt 0x10(%rax)
addq $0x20, %rax
jmp 0x2b91
addq $0x20, %rcx
jmp 0x2b88
movq 0x5439(%rip), %rdi      # 0x7ff0
leaq 0x2703(%rip), %rsi      # 0x52c1
callq 0x2190
movq %rax, %rdi
movl $0xff, %esi
callq 0x2290
leaq 0x26ff(%rip), %rsi      # 0x52d6
movq %rax, %rdi
callq 0x2190
leaq 0x272c(%rip), %rsi      # 0x5312
movq %rax, %rdi
callq 0x2190
pushq $0x28
popq %rsi
movq %rax, %rdi
callq 0x21e0
movq 0x30(%rsp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x2290
leaq 0x274b(%rip), %rsi      # 0x535a
movq %rax, %rdi
callq 0x2190
movq (%rbx), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x2290
leaq 0x2744(%rip), %rsi      # 0x536f
jmp 0x2a97
fld1
cmpq %rsi, %rax
je 0x2c42
fld %st(0)
fstpt 0x10(%rax)
addq $0x20, %rax
jmp 0x2c32
fstp %st(0)
leaq -0x827(%rip), %rdx      # 0x2424
callq 0x41f3
movq 0x30(%rsp), %r12
addq $0xc, %r12
pushq $-0x1
popq %r14
xorl %r13d, %r13d
pushq $0x9
popq %rbp
flds 0x239f(%rip)            # 0x5008
fstpt 0x48(%rsp)
leaq -0xc(%r12), %r15
cmpq 0x38(%rsp), %r15
je 0x2d6f
cmpl %r14d, (%r12)
je 0x2c97
movq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x3722
movq (%rax), %r13
movl (%r12), %r14d
fldt 0x4(%r12)
movq 0x68(%rsp), %rax
fldt (%rax)
fucompi %st(1), %st
fstp %st(0)
jb 0x2d66
movq 0x5314(%rip), %rdi      # 0x7fc8
movq %r13, %rsi
callq 0x2180
movq %rax, %rdi
movl %ebp, %esi
callq 0x21e0
movl -0x8(%r12), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
movl %ebp, %esi
callq 0x21e0
movl -0x4(%r12), %esi
addl -0x8(%r12), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
leaq 0x2688(%rip), %rsi      # 0x5381
callq 0x2190
fldt 0x4(%r12)
fstpt (%rsp)
movq %rax, %rdi
callq 0x20e0
movq 0x58(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x2d57
movq 0x52a9(%rip), %rdi      # 0x7fc8
movl %ebp, %esi
callq 0x21e0
movq %rax, %rbx
xorl %eax, %eax
subl (%r15), %eax
movl %eax, 0x2c(%rsp)
fildl 0x2c(%rsp)
fdivs 0x22c8(%rip)            # 0x5004
fldt 0x48(%rsp)
fstpt (%rsp)
fstpt 0x10(%rsp)
callq 0x2280
fstpt (%rsp)
movq %rbx, %rdi
callq 0x20e0
movq 0x526a(%rip), %rdi      # 0x7fc8
pushq $0xa
popq %rsi
callq 0x21e0
addq $0x20, %r12
jmp 0x2c6d
movb $0x1, %bl
jmp 0x2ab1
jmp 0x2d7a
jmp 0x2d7a
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3ff4
movq %rbx, %rdi
callq 0x22a0