// File: 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
movq %rsp, %rbp
subq $0x6b0, %rsp            # imm = 0x6B0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x3e8, -0x34(%rbp)     # imm = 0x3E8
leaq -0x440(%rbp), %rdi
movq %rdi, -0x508(%rbp)
callq 0x5110
leaq -0x448(%rbp), %rdi
callq 0x5130
movq -0x508(%rbp), %rdi
movl $0x0, -0x44c(%rbp)
movq -0x20(%rbp), %rax
movslq (%rax), %rsi
callq 0x5150
jmp 0x30b4
jmp 0x30b6
movq 0xdf1b(%rip), %rdi      # 0x10fd8
leaq -0x420(%rbp), %rsi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2090
movq %rax, -0x510(%rbp)
jmp 0x30d7
movq -0x510(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x22b0
movb %al, -0x511(%rbp)
jmp 0x30f5
movb -0x511(%rbp), %al
testb $0x1, %al
jne 0x3104
jmp 0x3485
movl -0x44c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44c(%rbp)
movl -0x44c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jle 0x31df
movq 0xdec4(%rip), %rdi      # 0x10ff0
leaq 0x800e(%rip), %rsi      # 0xb141
callq 0x21e0
movq %rax, -0x520(%rbp)
jmp 0x3141
movq -0x520(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %esi
callq 0x2370
movq %rax, -0x528(%rbp)
jmp 0x315c
movq -0x528(%rbp), %rdi
leaq 0x7ff9(%rip), %rsi      # 0xb163
callq 0x21e0
movq %rax, -0x530(%rbp)
jmp 0x3178
movq -0x530(%rbp), %rdi
movq 0xde22(%rip), %rsi      # 0x10fa8
callq 0x2220
movq %rax, -0x538(%rbp)
jmp 0x3194
movq -0x538(%rbp), %rdi
movq 0xde06(%rip), %rsi      # 0x10fa8
callq 0x2220
jmp 0x31a9
movb $0x0, -0x1(%rbp)
movl $0x1, -0x460(%rbp)
jmp 0x3bcf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x458(%rbp)
movl %eax, -0x45c(%rbp)
leaq -0x440(%rbp), %rdi
callq 0x5510
jmp 0x3be9
movl $0x1, %eax
cmpl -0x44c(%rbp), %eax
jne 0x320f
leaq -0x440(%rbp), %rdi
callq 0x51e0
movq %rax, -0x468(%rbp)
movq -0x468(%rbp), %rax
movq %rax, -0x448(%rbp)
jmp 0x3224
leaq -0x448(%rbp), %rdi
xorl %esi, %esi
callq 0x5210
movq %rax, -0x470(%rbp)
movl $0x1, -0x450(%rbp)
leaq -0x420(%rbp), %rdi
leaq 0x80f7(%rip), %rsi      # 0xb333
callq 0x2310
movq %rax, -0x428(%rbp)
cmpq $0x0, %rax
je 0x325e
movq -0x428(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x332e
jmp 0x3260
movq 0xdd89(%rip), %rdi      # 0x10ff0
leaq 0x7f5a(%rip), %rsi      # 0xb1c8
callq 0x21e0
movq %rax, -0x540(%rbp)
jmp 0x327c
movq -0x540(%rbp), %rdi
movl -0x450(%rbp), %esi
callq 0x2370
movq %rax, -0x548(%rbp)
jmp 0x3297
movq -0x548(%rbp), %rdi
leaq 0x7d91(%rip), %rsi      # 0xb036
callq 0x21e0
movq %rax, -0x550(%rbp)
jmp 0x32b3
movq -0x550(%rbp), %rdi
movl -0x44c(%rbp), %esi
callq 0x2370
movq %rax, -0x558(%rbp)
jmp 0x32ce
movq -0x558(%rbp), %rdi
leaq 0x7f13(%rip), %rsi      # 0xb1ef
callq 0x21e0
movq %rax, -0x560(%rbp)
jmp 0x32ea
movq -0x560(%rbp), %rdi
movq 0xdcb0(%rip), %rsi      # 0x10fa8
callq 0x2220
movq %rax, -0x568(%rbp)
jmp 0x3306
movq -0x568(%rbp), %rdi
movq 0xdc94(%rip), %rsi      # 0x10fa8
callq 0x2220
jmp 0x331b
movb $0x0, -0x1(%rbp)
movl $0x1, -0x460(%rbp)
jmp 0x3bcf
movq -0x428(%rbp), %rdi
callq 0x2300
movl %eax, -0x56c(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x5250
movl -0x56c(%rbp), %ecx
movl %ecx, 0xc(%rax)
movl -0x450(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x450(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x7fc4(%rip), %rsi      # 0xb333
callq 0x2310
movq %rax, -0x428(%rbp)
cmpq $0x0, %rax
je 0x338d
movq -0x428(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x3392
jmp 0x3260
movq -0x428(%rbp), %rdi
callq 0x2300
movl %eax, -0x570(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x5250
movl -0x570(%rbp), %ecx
movl %ecx, 0x4(%rax)
movl -0x450(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x450(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x7f60(%rip), %rsi      # 0xb333
callq 0x2310
movq %rax, -0x428(%rbp)
cmpq $0x0, %rax
je 0x33f1
movq -0x428(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x33f6
jmp 0x3260
movq -0x428(%rbp), %rdi
callq 0x2300
movl %eax, -0x574(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x5250
movl -0x574(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl -0x450(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x450(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x7efc(%rip), %rsi      # 0xb333
callq 0x2310
movq %rax, -0x428(%rbp)
cmpq $0x0, %rax
je 0x3455
movq -0x428(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x345a
jmp 0x3260
movq -0x428(%rbp), %rdi
callq 0x2300
movl %eax, -0x578(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x5250
movl -0x578(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x30b6
movl -0x44c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x3581
movq 0xdb52(%rip), %rdi      # 0x10ff0
leaq 0x7c9c(%rip), %rsi      # 0xb141
callq 0x21e0
movq %rax, -0x580(%rbp)
jmp 0x34b3
movq -0x580(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %esi
callq 0x2370
movq %rax, -0x588(%rbp)
jmp 0x34ce
movq -0x588(%rbp), %rdi
leaq 0x7d3e(%rip), %rsi      # 0xb21a
callq 0x21e0
movq %rax, -0x590(%rbp)
jmp 0x34ea
movq -0x590(%rbp), %rdi
leaq 0x7d5b(%rip), %rsi      # 0xb253
callq 0x21e0
movq %rax, -0x598(%rbp)
jmp 0x3506
movq -0x598(%rbp), %rdi
movl -0x44c(%rbp), %esi
callq 0x2370
movq %rax, -0x5a0(%rbp)
jmp 0x3521
movq -0x5a0(%rbp), %rdi
leaq 0x7d2f(%rip), %rsi      # 0xb25e
callq 0x21e0
movq %rax, -0x5a8(%rbp)
jmp 0x353d
movq -0x5a8(%rbp), %rdi
movq 0xda5d(%rip), %rsi      # 0x10fa8
callq 0x2220
movq %rax, -0x5b0(%rbp)
jmp 0x3559
movq -0x5b0(%rbp), %rdi
movq 0xda41(%rip), %rsi      # 0x10fa8
callq 0x2220
jmp 0x356e
movb $0x0, -0x1(%rbp)
movl $0x1, -0x460(%rbp)
jmp 0x3bcf
leaq -0x440(%rbp), %rdi
movq %rdi, -0x5b8(%rbp)
callq 0x51e0
movq -0x5b8(%rbp), %rdi
movq %rax, -0x478(%rbp)
callq 0x52c0
movq %rax, -0x480(%rbp)
movq -0x478(%rbp), %rdi
movq -0x480(%rbp), %rsi
leaq -0x10a3(%rip), %rdx     # 0x2520
callq 0x5270
jmp 0x35ca
leaq -0x440(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x52f0
movl (%rax), %eax
movl %eax, -0x5bc(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5310
movq %rax, %rcx
movl -0x5bc(%rbp), %eax
cmpl (%rcx), %eax
je 0x374f
movq 0xd9e9(%rip), %rdi      # 0x10ff0
leaq 0x7c63(%rip), %rsi      # 0xb271
callq 0x21e0
movq %rax, -0x5c8(%rbp)
jmp 0x361c
movq -0x5c8(%rbp), %rdi
movl $0xff, %esi
callq 0x2370
movq %rax, -0x5d0(%rbp)
jmp 0x3636
movq -0x5d0(%rbp), %rdi
leaq 0x7c42(%rip), %rsi      # 0xb286
callq 0x21e0
movq %rax, -0x5d8(%rbp)
jmp 0x3652
movq -0x5d8(%rbp), %rdi
leaq 0x7c62(%rip), %rsi      # 0xb2c2
callq 0x21e0
movq %rax, -0x5e0(%rbp)
jmp 0x366e
movq -0x5e0(%rbp), %rdi
movl $0x28, %esi
callq 0x2290
movq %rax, -0x5e8(%rbp)
jmp 0x3688
xorl %eax, %eax
movl %eax, %esi
leaq -0x440(%rbp), %rdi
callq 0x52f0
movq -0x5e8(%rbp), %rdi
movl (%rax), %esi
callq 0x2370
movq %rax, -0x5f0(%rbp)
jmp 0x36af
movq -0x5f0(%rbp), %rdi
leaq 0x7c4d(%rip), %rsi      # 0xb30a
callq 0x21e0
movq %rax, -0x5f8(%rbp)
jmp 0x36cb
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5310
movq -0x5f8(%rbp), %rdi
movl (%rax), %esi
callq 0x2370
movq %rax, -0x600(%rbp)
jmp 0x36ef
movq -0x600(%rbp), %rdi
leaq 0x7c22(%rip), %rsi      # 0xb31f
callq 0x21e0
movq %rax, -0x608(%rbp)
jmp 0x370b
movq -0x608(%rbp), %rdi
movq 0xd88f(%rip), %rsi      # 0x10fa8
callq 0x2220
movq %rax, -0x610(%rbp)
jmp 0x3727
movq -0x610(%rbp), %rdi
movq 0xd873(%rip), %rsi      # 0x10fa8
callq 0x2220
jmp 0x373c
movb $0x0, -0x1(%rbp)
movl $0x1, -0x460(%rbp)
jmp 0x3bcf
leaq -0x440(%rbp), %rdi
callq 0x51e0
movq %rax, -0x488(%rbp)
movq -0x488(%rbp), %rax
movq %rax, -0x448(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5330
movq %rax, -0x490(%rbp)
movq -0x18(%rbp), %rdi
callq 0x53a0
movq %rax, -0x498(%rbp)
leaq -0x490(%rbp), %rdi
leaq -0x498(%rbp), %rsi
callq 0x5360
testb $0x1, %al
jne 0x37ac
jmp 0x3885
jmp 0x37ae
leaq -0x440(%rbp), %rdi
callq 0x52c0
movq %rax, -0x4a0(%rbp)
leaq -0x448(%rbp), %rdi
leaq -0x4a0(%rbp), %rsi
callq 0x53d0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x611(%rbp)
jne 0x37e5
jmp 0x3819
leaq -0x448(%rbp), %rdi
callq 0x5250
movl (%rax), %eax
movl %eax, -0x618(%rbp)
leaq -0x490(%rbp), %rdi
callq 0x5410
movq %rax, %rcx
movl -0x618(%rbp), %eax
cmpl (%rcx), %eax
sete %al
movb %al, -0x611(%rbp)
movb -0x611(%rbp), %al
testb $0x1, %al
jne 0x3825
jmp 0x3869
leaq -0x490(%rbp), %rdi
callq 0x5410
fldt 0x10(%rax)
fstpt -0x624(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x5250
fldt -0x624(%rbp)
fstpt 0x10(%rax)
leaq -0x448(%rbp), %rdi
xorl %esi, %esi
callq 0x5210
movq %rax, -0x4a8(%rbp)
jmp 0x37ae
jmp 0x386b
leaq -0x490(%rbp), %rdi
xorl %esi, %esi
callq 0x5430
movq %rax, -0x4b0(%rbp)
jmp 0x3780
jmp 0x3887
leaq -0x440(%rbp), %rdi
callq 0x52c0
movq %rax, -0x4b8(%rbp)
leaq -0x448(%rbp), %rdi
leaq -0x4b8(%rbp), %rsi
callq 0x53d0
testb $0x1, %al
jne 0x38b3
jmp 0x38db
leaq -0x448(%rbp), %rdi
callq 0x5250
fld1
fstpt 0x10(%rax)
leaq -0x448(%rbp), %rdi
xorl %esi, %esi
callq 0x5210
movq %rax, -0x4c0(%rbp)
jmp 0x3887
leaq -0x440(%rbp), %rdi
movq %rdi, -0x630(%rbp)
callq 0x51e0
movq -0x630(%rbp), %rdi
movq %rax, -0x4c8(%rbp)
callq 0x52c0
movq %rax, -0x4d0(%rbp)
movq -0x4c8(%rbp), %rdi
movq -0x4d0(%rbp), %rsi
leaq -0x13dd(%rip), %rdx     # 0x2540
callq 0x5270
jmp 0x3924
movl $0xffffffff, -0x4d4(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x4e0(%rbp)
leaq -0x440(%rbp), %rdi
callq 0x51e0
movq %rax, -0x4e8(%rbp)
movq -0x4e8(%rbp), %rax
movq %rax, -0x448(%rbp)
leaq -0x440(%rbp), %rdi
callq 0x52c0
movq %rax, -0x4f0(%rbp)
leaq -0x448(%rbp), %rdi
leaq -0x4f0(%rbp), %rsi
callq 0x53d0
testb $0x1, %al
jne 0x3989
jmp 0x3bc1
leaq -0x448(%rbp), %rdi
callq 0x5250
movl 0xc(%rax), %eax
cmpl -0x4d4(%rbp), %eax
je 0x39f9
movq -0x10(%rbp), %rax
movq %rax, -0x640(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x5250
movq -0x640(%rbp), %rdi
movq %rax, %rsi
addq $0xc, %rsi
callq 0x5470
movq %rax, -0x638(%rbp)
jmp 0x39d3
movq -0x638(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x4e0(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x5250
movl 0xc(%rax), %eax
movl %eax, -0x4d4(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x5250
fldt 0x10(%rax)
movq -0x28(%rbp), %rax
fldt (%rax)
fucompi %st(1), %st
fstp %st(0)
jb 0x3ba5
jmp 0x3a1a
movq -0x4e0(%rbp), %rsi
movq 0xd5a0(%rip), %rdi      # 0x10fc8
callq 0x21d0
movq %rax, -0x648(%rbp)
jmp 0x3a36
movq -0x648(%rbp), %rdi
movl $0x9, %esi
callq 0x2290
movq %rax, -0x650(%rbp)
jmp 0x3a50
leaq -0x448(%rbp), %rdi
callq 0x5250
movq -0x650(%rbp), %rdi
movl 0x4(%rax), %esi
callq 0x2370
movq %rax, -0x658(%rbp)
jmp 0x3a74
movq -0x658(%rbp), %rdi
movl $0x9, %esi
callq 0x2290
movq %rax, -0x660(%rbp)
jmp 0x3a8e
leaq -0x448(%rbp), %rdi
movq %rdi, -0x678(%rbp)
callq 0x5250
movq -0x678(%rbp), %rdi
movl 0x4(%rax), %eax
movl %eax, -0x66c(%rbp)
callq 0x5250
movl -0x66c(%rbp), %esi
movq -0x660(%rbp), %rdi
movl 0x8(%rax), %eax
addl %eax, %esi
callq 0x2370
movq %rax, -0x668(%rbp)
jmp 0x3ad6
movq -0x668(%rbp), %rdi
leaq 0x784d(%rip), %rsi      # 0xb331
callq 0x21e0
movq %rax, -0x680(%rbp)
jmp 0x3af2
leaq -0x448(%rbp), %rdi
callq 0x5250
movq -0x680(%rbp), %rdi
fldt 0x10(%rax)
movq %rsp, %rax
fstpt (%rax)
callq 0x2280
jmp 0x3b14
movq -0x30(%rbp), %rax
testb $0x1, (%rax)
je 0x3b90
movq 0xd4a4(%rip), %rdi      # 0x10fc8
movl $0x9, %esi
callq 0x2290
movq %rax, -0x688(%rbp)
jmp 0x3b37
leaq -0x448(%rbp), %rdi
callq 0x5250
movl (%rax), %eax
negl %eax
movl %eax, -0x4fc(%rbp)
fildl -0x4fc(%rbp)
flds 0x74ab(%rip)            # 0xb004
fdivrp %st, %st(1)
movq %rsp, %rax
fstpt (%rax)
movsd 0x74a0(%rip), %xmm0     # 0xb008
callq 0x4e60
fstpt -0x694(%rbp)
jmp 0x3b75
movq -0x688(%rbp), %rdi
fldt -0x694(%rbp)
movq %rsp, %rax
fstpt (%rax)
callq 0x2280
jmp 0x3b8e
jmp 0x3b90
movq 0xd431(%rip), %rdi      # 0x10fc8
movl $0xa, %esi
callq 0x2290
jmp 0x3ba3
jmp 0x3ba5
jmp 0x3ba7
leaq -0x448(%rbp), %rdi
xorl %esi, %esi
callq 0x5210
movq %rax, -0x4f8(%rbp)
jmp 0x395a
movb $0x1, -0x1(%rbp)
movl $0x1, -0x460(%rbp)
leaq -0x440(%rbp), %rdi
callq 0x5510
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x6b0, %rsp            # imm = 0x6B0
popq %rbp
retq
movq -0x458(%rbp), %rdi
callq 0x2380
nopw %cs:(%rax,%rax)