// File: /workspace/github_cpp_makefile_star2_O3/Altius[P]hotspot2/src/hotspot2_part2.cpp
// Function: buildFDRmapping(std::basic_ifstream<char, std::char_traits<char>>&, std::vector<std::pair<int, long double>, std::allocator<std::pair<int, long double>>>&)
bool buildFDRmapping(ifstream& ifs, vector<pair<int, long double> >& p_to_q)
{
  const int BUFSIZE(100);
  char buf[BUFSIZE], *p;
  long numPvalues(0);
  map<int, int> tempMap;
  map<int, int>::iterator it;
  pair<int, int> negLog10PscaledAndNumOccs;
  int linenum(0), fieldnum;

  while (ifs.getline(buf,BUFSIZE))
    {
      linenum++;
      fieldnum = 1;
      if (!(p = strtok(buf, "\t")) || !*p)
	{
	MissingField:
	  cerr << "Error:  Failed to find field " << fieldnum
	       << " on line " << linenum << " of the file of P-values."
	       << endl << endl;
	  return false;
	}
      negLog10PscaledAndNumOccs.first = atoi(p);
      fieldnum++;
      if (!(p = strtok(NULL, "\t")))
	goto MissingField;
      negLog10PscaledAndNumOccs.second = atoi(p);
      
      it = tempMap.lower_bound(negLog10PscaledAndNumOccs.first); // >=
      if (tempMap.end() == it)
	{
	  // no lower bound found in the map; every map element is smaller, or the map is empty
	  if (!tempMap.empty())
	    tempMap.insert(--it, negLog10PscaledAndNumOccs);
	  else
	    tempMap.insert(negLog10PscaledAndNumOccs);
	}
      else
	{
	  if (it->first == negLog10PscaledAndNumOccs.first)
	    it->second += negLog10PscaledAndNumOccs.second;
	  else
	    {
	      if (it != tempMap.begin())
		it--;
	      tempMap.insert(it, negLog10PscaledAndNumOccs);
	    }
	}
      numPvalues += negLog10PscaledAndNumOccs.second;
    }

  if (0 == numPvalues)
    {
      cerr << "Error:  Received an empty file of P-values." << endl << endl;
      return false;
    }

  p_to_q.resize(tempMap.size()); // assume one entry per map entry; final size will almost surely be a bit smaller
  
  it = tempMap.end();
  it--;
  int i = 0, numThisExtremeOrMoreExtreme(0);
  const long double N(static_cast<double>(numPvalues));
  long double prevFDR(-1.), FDR;
  while (it != tempMap.begin())
    {
      p_to_q[i].first = it->first;
      numThisExtremeOrMoreExtreme += it->second;
      FDR = pow(10., -it->first/CHANGE_OF_SCALE) * N / static_cast<long double>(numThisExtremeOrMoreExtreme);
      //      if (FDR < numeric_limits<double>::min())
      //	FDR = numeric_limits<double>::min();
      if (FDR < prevFDR)
	FDR = prevFDR;
      if (FDR > 0.999)
	{
	  p_to_q[i++].second = 1.;
	  p_to_q.resize(i);
	  break;
	}
      p_to_q[i].second = FDR;
      prevFDR = FDR;
      i++;
      it--;
    }

  // It's extremely unlikely, but possible that only the smallest -log10(p) has B-H FDR = 1.
  if (i > 0 && p_to_q[i-1].second < 1.)
    {
      p_to_q[i].first = tempMap.begin()->first;
      p_to_q[i].second = 1.;
    }

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, 0x30(%rsp)
movq %rdi, %r12
leaq 0x58(%rsp), %rax
movl $0x0, (%rax)
xorl %r14d, %r14d
movq %r14, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r14, 0x20(%rax)
leaq 0x28(%rsp), %rax
movq %r14, (%rax)
leaq 0xb0(%rsp), %r13
leaq 0x3d27(%rip), %rbp      # 0x6383
xorl %ebx, %ebx
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %ecx
movl $0x64, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x2300
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2779
incl %r14d
movq %r13, %r15
movq %r13, %rdi
movq %rbp, %r13
movq %rbp, %rsi
callq 0x2250
movl $0x1, %ebp
testq %rax, %rax
je 0x28a4
cmpb $0x0, (%rax)
je 0x28a4
movq %rax, %rdi
callq 0x2240
movl %eax, 0x28(%rsp)
xorl %edi, %edi
movq %r13, %rbp
movq %r13, %rsi
callq 0x2250
testq %rax, %rax
je 0x289f
movq %rax, %rdi
callq 0x2240
movl %eax, 0x2c(%rsp)
movq 0x60(%rsp), %rcx
leaq 0x58(%rsp), %rsi
testq %rcx, %rcx
movq %r15, %r13
je 0x2732
movl 0x28(%rsp), %edx
xorl %edi, %edi
cmpl %edx, 0x20(%rcx)
setl %dil
cmovgeq %rcx, %rsi
movq 0x10(%rcx,%rdi,8), %rcx
testq %rcx, %rcx
jne 0x2707
leaq 0x58(%rsp), %rcx
cmpq %rsi, %rcx
je 0x2732
cmpl %edx, 0x20(%rsi)
jne 0x274b
addl %eax, 0x24(%rsi)
jmp 0x276c
cmpq $0x0, 0x78(%rsp)
jne 0x2752
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x436a
jmp 0x276c
cmpq 0x68(%rsp), %rsi
je 0x275d
movq %rsi, %rdi
callq 0x21c0
movq %rax, %rsi
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x416e
movslq 0x2c(%rsp), %rax
addq %rax, %rbx
jmp 0x265e
testq %rbx, %rbx
je 0x2974
movq 0x78(%rsp), %rsi
movq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x3fae
leaq 0x58(%rsp), %rdi
callq 0x21c0
cvtsi2sd %rbx, %xmm0
movsd %xmm0, 0x80(%rsp)
fldl 0x80(%rsp)
fstpt 0xa4(%rsp)
movb $0x1, %bl
cmpq 0x68(%rsp), %rax
je 0x2956
movq %rax, %r14
movq (%r12), %r13
xorl %r15d, %r15d
movl $0x1, %ebp
fld1
fchs
flds 0x3828(%rip)            # 0x6008
fstpt 0x98(%rsp)
fldl 0x3823(%rip)            # 0x6010
fstpt 0x8c(%rsp)
xorl %r12d, %r12d
fstpt 0x44(%rsp)
movl 0x20(%r14), %eax
movl %eax, (%r13,%r15)
addl 0x24(%r14), %r12d
negl %eax
movl %eax, 0x3c(%rsp)
fildl 0x3c(%rsp)
fdivs 0x37ec(%rip)            # 0x6004
fstpt 0x10(%rsp)
fldt 0x98(%rsp)
fstpt (%rsp)
callq 0x2290
fldt 0xa4(%rsp)
fmulp %st, %st(1)
movl %r12d, 0x40(%rsp)
fidivl 0x40(%rsp)
fldt 0x44(%rsp)
fucomi %st(1), %st
fxch %st(1)
fcmovnbe %st(1), %st
fstp %st(1)
fldt 0x8c(%rsp)
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
ja 0x29d6
movq 0x30(%rsp), %rax
movq (%rax), %r13
fld %st(0)
fstpt 0x10(%r13,%r15)
movq %r14, %rdi
fstpt 0x44(%rsp)
callq 0x21c0
fldt 0x44(%rsp)
movq %rax, %r14
incl %ebp
addq $0x20, %r15
cmpq 0x68(%rsp), %rax
jne 0x27f7
fstp %st(0)
decl %ebp
movq 0x30(%rsp), %r14
fld1
jmp 0x29f3
movl $0x2, %ebp
movq 0x6745(%rip), %rdi      # 0x8ff0
leaq 0x37b6(%rip), %rsi      # 0x6068
movl $0x1d, %edx
callq 0x21e0
movq 0x672d(%rip), %rdi      # 0x8ff0
movl %ebp, %esi
callq 0x22b0
movq %rax, %rbx
leaq 0x37b2(%rip), %rsi      # 0x6086
movl $0x9, %edx
movq %rax, %rdi
callq 0x21e0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x22b0
movq %rax, %rbx
leaq 0x379a(%rip), %rsi      # 0x6090
movl $0x19, %edx
movq %rax, %rdi
callq 0x21e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
xorl %ebx, %ebx
leaq 0x50(%rsp), %rdi
callq 0x40ce
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6675(%rip), %rbx      # 0x8ff0
leaq 0x3728(%rip), %rsi      # 0x60aa
movl $0x2b, %edx
movq %rbx, %rdi
callq 0x21e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x6643(%rip), %rdi      # 0x8ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
jmp 0x2941
fstp %st(0)
movq 0x30(%rsp), %r14
movq (%r14), %rax
fld1
fstpt 0x10(%rax,%r15)
movl %ebp, %esi
movq %r14, %rdi
callq 0x3fae
fld1
movq (%r14), %rax
movslq %ebp, %rcx
shlq $0x5, %rcx
fldt -0x10(%rax,%rcx)
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
jbe 0x2a23
movq 0x68(%rsp), %rcx
movl 0x20(%rcx), %ecx
movl %ebp, %edx
shlq $0x5, %rdx
movl %ecx, (%rax,%rdx)
fstpt 0x10(%rax,%rdx)
jmp 0x2956
fstp %st(0)
jmp 0x2956
fstp %st(0)
jmp 0x2a30
jmp 0x2a30
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x40ce
movq %rbx, %rdi
callq 0x22c0