// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0x0, (%rax)
xorl %r14d, %r14d
movq %r14, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r14, 0x20(%rax)
leaq 0x2c(%rsp), %rax
movq %r14, (%rax)
leaq 0xb0(%rsp), %r13
leaq 0x3d27(%rip), %rbx      # 0x6383
xorl %ebp, %ebp
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 0x277e
incl %r14d
movq %r13, %r15
movq %r13, %rdi
movq %rbx, %r13
movq %rbx, %rsi
callq 0x2250
movl $0x1, %ebx
testq %rax, %rax
je 0x289a
cmpb $0x0, (%rax)
je 0x289a
movq %rax, %rdi
callq 0x2240
movl %eax, 0x2c(%rsp)
xorl %edi, %edi
movq %r13, %rbx
movq %r13, %rsi
callq 0x2250
testq %rax, %rax
je 0x2895
movq %rax, %rdi
callq 0x2240
movl %eax, 0x30(%rsp)
movq 0x58(%rsp), %rcx
leaq 0x50(%rsp), %r8
movq %r8, %rsi
testq %rcx, %rcx
je 0x2721
movl 0x2c(%rsp), %edx
movq %r8, %rsi
xorl %edi, %edi
cmpl %edx, 0x20(%rcx)
setl %dil
cmovgeq %rcx, %rsi
movq 0x10(%rcx,%rdi,8), %rcx
testq %rcx, %rcx
jne 0x270a
cmpq %rsi, %r8
movq %r15, %r13
je 0x2737
movl 0x20(%rsi), %ecx
cmpl 0x2c(%rsp), %ecx
jne 0x2750
addl %eax, 0x24(%rsi)
jmp 0x2771
cmpq $0x0, 0x70(%rsp)
jne 0x2757
leaq 0x48(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x43b0
jmp 0x2771
cmpq 0x60(%rsp), %rsi
je 0x2762
movq %rsi, %rdi
callq 0x21c0
movq %rax, %rsi
leaq 0x48(%rsp), %rdi
leaq 0x2c(%rsp), %rdx
callq 0x41ac
movslq 0x30(%rsp), %rax
addq %rax, %rbp
jmp 0x265e
testq %rbp, %rbp
je 0x296a
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %r12
movq %r12, %rdi
callq 0x3fe8
leaq 0x50(%rsp), %rdi
callq 0x21c0
cvtsi2sd %rbp, %xmm0
movsd %xmm0, 0x80(%rsp)
fldl 0x80(%rsp)
fstpt 0xa4(%rsp)
cmpq 0x60(%rsp), %rax
je 0x29cc
movq %rax, %rbx
xorl %r14d, %r14d
movl $0x1, %ebp
fld1
fchs
flds 0x3829(%rip)            # 0x6008
fstpt 0x98(%rsp)
fldl 0x3824(%rip)            # 0x6010
fstpt 0x8c(%rsp)
xorl %r15d, %r15d
fstpt 0x3c(%rsp)
movl 0x20(%rbx), %eax
movq (%r12), %rcx
movl %eax, (%rcx,%r14)
addl 0x24(%rbx), %r15d
negl %eax
movl %eax, 0x34(%rsp)
fildl 0x34(%rsp)
fdivs 0x37eb(%rip)            # 0x6004
fstpt 0x10(%rsp)
fldt 0x98(%rsp)
fstpt (%rsp)
callq 0x2290
fldt 0xa4(%rsp)
fmulp %st, %st(1)
movl %r15d, 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 0x29d0
movq (%r12), %rax
fld %st(0)
fstpt 0x10(%rax,%r14)
movq %rbx, %rdi
fstpt 0x3c(%rsp)
callq 0x21c0
fldt 0x3c(%rsp)
movq %rax, %rbx
addq $0x20, %r14
incl %ebp
cmpq 0x60(%rsp), %rax
jne 0x27f6
fstp %st(0)
decl %ebp
jmp 0x29e7
movl $0x2, %ebx
movq 0x674f(%rip), %rdi      # 0x8ff0
leaq 0x37c0(%rip), %rsi      # 0x6068
movl $0x1d, %edx
callq 0x21e0
movq 0x6737(%rip), %rdi      # 0x8ff0
movl %ebx, %esi
callq 0x22b0
movq %rax, %rbx
leaq 0x37bc(%rip), %rsi      # 0x6086
movl $0x9, %edx
movq %rax, %rdi
callq 0x21e0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x22b0
movq %rax, %rbx
leaq 0x37a4(%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 0x48(%rsp), %rdi
callq 0x410c
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x667f(%rip), %rbx      # 0x8ff0
leaq 0x3732(%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 0x664d(%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 0x2937
xorl %ebp, %ebp
jmp 0x29e7
fstp %st(0)
movq (%r12), %rax
fld1
fstpt 0x10(%rax,%r14)
movl %ebp, %esi
movq %r12, %rdi
callq 0x3fe8
movb $0x1, %bl
testl %ebp, %ebp
jle 0x294c
movq (%r12), %rax
movl %ebp, %ecx
movq %rcx, %rdx
shlq $0x5, %rdx
fldt -0x10(%rax,%rdx)
fld1
fucomi %st(1), %st
fstp %st(1)
jbe 0x2a22
movq 0x60(%rsp), %rdx
movl 0x20(%rdx), %edx
shlq $0x5, %rcx
movl %edx, (%rax,%rcx)
fstpt 0x10(%rax,%rcx)
jmp 0x294c
fstp %st(0)
jmp 0x294c
jmp 0x2a2d
jmp 0x2a2d
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x410c
movq %rbx, %rdi
callq 0x22c0