// File: /workspace/github_cpp_makefile_star2_O2/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, 0x78(%rsp)
movq %rdi, %r12
leaq 0x50(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x2c(%rsp), %r13
andq $0x0, (%r13)
pushq $0x1
popq %rbp
xorl %r15d, %r15d
leaq 0xb0(%rsp), %r14
leaq 0x2ef3(%rip), %rbx      # 0x5383
movq %r12, %rdi
movq %r14, %rsi
pushq $0x64
popq %rdx
callq 0x2070
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x256d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2240
testq %rax, %rax
je 0x2566
cmpb $0x0, (%rax)
je 0x2566
movq %rax, %rdi
callq 0x2230
movl %eax, 0x2c(%rsp)
xorl %edi, %edi
movq %rbx, %rsi
callq 0x2240
testq %rax, %rax
je 0x2679
movq %rax, %rdi
callq 0x2230
movl %eax, 0x30(%rsp)
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
callq 0x381e
leaq 0x50(%rsp), %rcx
cmpq %rax, %rcx
je 0x2521
movl 0x20(%rax), %ecx
cmpl 0x2c(%rsp), %ecx
jne 0x2538
movl 0x30(%rsp), %ecx
addl %ecx, 0x24(%rax)
jmp 0x2557
cmpq $0x0, 0x70(%rsp)
jne 0x253f
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
callq 0x3a80
jmp 0x2557
cmpq 0x60(%rsp), %rax
je 0x2547
movq %rax, %rdi
callq 0x21c0
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x384c
movslq 0x30(%rsp), %rax
addq %rax, %r15
incl %ebp
jmp 0x2490
pushq $0x1
jmp 0x267b
testq %r15, %r15
je 0x26f1
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %r13
movq %r13, %rdi
callq 0x3630
leaq 0x50(%rsp), %rdi
callq 0x21c0
movq %rax, %rbx
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x80(%rsp)
xorl %r14d, %r14d
fld1
fld %st(0)
fchs
fldl 0x80(%rsp)
fstpt 0xa4(%rsp)
flds 0x2a48(%rip)            # 0x5008
fstpt 0x98(%rsp)
fldl 0x2a43(%rip)            # 0x5010
fstpt 0x8c(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq 0x60(%rsp), %rbx
je 0x2701
fstp %st(1)
movl 0x20(%rbx), %eax
movq (%r13), %rcx
movl %eax, (%rcx,%r14)
addl 0x24(%rbx), %ebp
negl %eax
movl %eax, 0x34(%rsp)
fildl 0x34(%rsp)
fdivs 0x2a00(%rip)            # 0x5004
fstpt 0x10(%rsp)
fldt 0x98(%rsp)
fstpt (%rsp)
fstpt 0x3c(%rsp)
callq 0x2280
fldt 0xa4(%rsp)
fmulp %st, %st(1)
movl %ebp, 0x38(%rsp)
fidivl 0x38(%rsp)
fldt 0x3c(%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 0x2710
movq (%r13), %rax
fld %st(0)
fstpt 0x10(%rax,%r14)
movq %rbx, %rdi
fstpt 0x3c(%rsp)
callq 0x21c0
movq %rax, %rbx
decq %r15
addq $0x20, %r14
fldt 0x3c(%rsp)
fld1
fxch %st(1)
jmp 0x25d9
pushq $0x2
popq %rbx
movq 0x596d(%rip), %rdi      # 0x7ff0
leaq 0x29de(%rip), %rsi      # 0x5068
callq 0x2190
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
leaq 0x29e6(%rip), %rsi      # 0x5086
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
movl %ebp, %esi
callq 0x2290
movq %rax, %rdi
leaq 0x29d4(%rip), %rsi      # 0x5090
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
xorl %ebx, %ebx
leaq 0x48(%rsp), %rdi
callq 0x376e
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x29b2(%rip), %rsi      # 0x50aa
movq 0x58f1(%rip), %rdi      # 0x7ff0
jmp 0x26bc
fstp %st(0)
testq %r15, %r15
je 0x276c
negq %r15
movl %r15d, %r12d
jmp 0x2731
fstp %st(0)
pushq $0x1
popq %r12
subq %r15, %r12
movq (%r13), %rax
fld1
fstpt 0x10(%rax,%r14)
movl %r12d, %esi
movq %r13, %rdi
callq 0x3630
fld1
movq (%r13), %rax
movslq %r12d, %rcx
shlq $0x5, %rcx
fldt -0x10(%rax,%rcx)
movb $0x1, %bl
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
jbe 0x2765
movq 0x60(%rsp), %rcx
movl 0x20(%rcx), %ecx
movl %r12d, %edx
shlq $0x5, %rdx
movl %ecx, (%rax,%rdx)
fstpt 0x10(%rax,%rdx)
jmp 0x26d3
fstp %st(0)
jmp 0x26d3
fstp %st(0)
movb $0x1, %bl
jmp 0x26d3
jmp 0x277b
jmp 0x277b
jmp 0x277b
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x376e
movq %rbx, %rdi
callq 0x22a0