// File: /workspace/github_cpp_makefile_star2_O3/Ahhgust[P]STRaitRazor/str8.cpp
// Function: printReports(_IO_FILE*, std::map<Report, HapCounter, CompareReport, std::allocator<std::pair<Report const, HapCounter>>>&, unsigned int, bool)
void
printReports(FILE *stream, map< Report, HapCounter, CompareReport> &hash, unsigned minCount, bool noRC) {


  //  vector< pair<Report, pair<unsigned, unsigned> > > vec(hash.begin(), hash.end() );

  vector< pair<Report, HapCounter > > vec(hash.begin(), hash.end() );
  sort( vec.begin(), vec.end() , sortKeyAndValue);


  vector< pair<Report, HapCounter > >::iterator it = vec.begin();

  unsigned i, j, stop, mod;
  int offset, period;
  string s;
  unsigned totalSkippedPositive = 0;
  unsigned totalSkippedNegative = 0;
  unsigned prevStrIndex = UINT_MAX;
  Report rep;
  
  if (opt.printHeader) {
    fprintf(stream, "Locus\tLength\tHaplotype\t");
    if (noRC) {
      fprintf(stream, "Count");
    } else {
      fprintf(stream, "ForwardCount\tReverseCount");
    }
    
    if (opt.useQuality) {
      if (noRC) 
        fprintf(stream, "\tForwardQsum");
      else
        fprintf(stream, "\tForwardQsum\tReverseQsum");
    }
    fprintf(stream, "\n");
  }

  
  for ( ; it != vec.end(); ++it) {

    rep = it->first;

    // print out the number of records that were below threshold.    
    // the outer-most sort is by locus, so this approach is correct
    if (rep.strIndex != prevStrIndex) {
      if (totalSkippedPositive + totalSkippedNegative >0) {

        fprintf(stream, "%s:0.0\t0 bases\tSumBelowThreshold",  s.c_str());
        if (noRC) 
          fprintf(stream, "\t\t%u\n",  totalSkippedPositive + totalSkippedNegative);
        else
          fprintf(stream, "\t%u\t%u\n",  totalSkippedPositive , totalSkippedNegative);
        
      }
      prevStrIndex = rep.strIndex;
      totalSkippedPositive = totalSkippedNegative = 0;
    }

    s = (*c)[rep.strIndex].locusName;

    if (it->second.first + it->second.second < minCount) {
      totalSkippedPositive += it->second.second;
      totalSkippedNegative += it->second.first;
      continue;
    }

    // compute the STR nomenclature
    if (opt.includeAnchors) {
      // adjust the nomenclature to account for the inclusion of the anchor sequences in the haplotype.
      period = ( (int)rep.hapLength- (int)(*c)[rep.strIndex].motifOffset - (int)(*c)[rep.strIndex].forwardLength - (int)(*c)[rep.strIndex].reverseLength )/ (int)(*c)[rep.strIndex].motifPeriod;
      offset = ( (int)rep.hapLength-(int)(*c)[rep.strIndex].motifOffset- (int)(*c)[rep.strIndex].forwardLength - (int)(*c)[rep.strIndex].reverseLength ) % (int)(*c)[rep.strIndex].motifPeriod;
    } else {
      period = ( (int)rep.hapLength- (int)(*c)[rep.strIndex].motifOffset)/ (int)(*c)[rep.strIndex].motifPeriod;
      offset = ( (int)rep.hapLength-(int)(*c)[rep.strIndex].motifOffset) % (int)(*c)[rep.strIndex].motifPeriod;
    }
    if (offset) {
      fprintf(stream, "%s:%i.%i\t%u bases\t",  s.c_str(), period, offset,
              rep.hapLength);
    } else {
      fprintf(stream, "%s:%i\t%u bases\t", s.c_str(), period, 
              rep.hapLength);
    }

    if (rep.nonstandardLetters==false) {
      stop = (rep.hapLength / (MAXWORD/2.0));
      mod = rep.hapLength % (MAXWORD/2);
      for (i=0; i < stop; ++i) 
        printBinaryWord(stream, rep.haplotype[i], MAXWORD/2);
      
      if (mod) 
        printBinaryWord(stream, rep.haplotype[i], mod);

    } else {
      fprintf(stream, "%s", (char*)rep.haplotype);
    }

      
    if (noRC)
      fprintf(stream, "\t\t%u", it->second.first + it->second.second );
    else 
      fprintf(stream, "\t%u\t%u", it->second.second, it->second.first );

    if (USE_QVALS) {
      
      if (noRC)
        fprintf(stream, "\t\t%f\n", it->second.fq + it->second.rq );
      else 
        fprintf(stream, "\t%f\t%f\n", it->second.rq, it->second.fq );
      
    } else {
      fprintf(stream, "\n");
    }
  }

  // grab the trailing values (if necessary)
  if (totalSkippedPositive + totalSkippedNegative >0) {
    
    fprintf(stream, "%s:0.0\t0 bases\tSumBelowThreshold",  s.c_str());
    if (noRC) 
      fprintf(stream, "\t\t%u\n",  totalSkippedPositive + totalSkippedNegative);
    else
      fprintf(stream, "\t%u\t%u\n",  totalSkippedPositive , totalSkippedNegative);
    
  }
  
  
  if (opt.verbose) {
    fprintf(stream, "\n\nBias Reporting\nMarkerName\tMissingRightanchor_Counts\tTotalMatches_Count\tRatio\tAvgLeftPos\tAvgRightPos\n");
    unsigned sum, sumt;
    unsigned long leftC, rightC;
    for (j=0; j < numStrs; ++j) {
      sumt=sum=0;
      leftC=rightC=0;
      for (i=0; i < (unsigned)opt.numThreads; ++i) {
        sum += biasCounts[i][j];
        sumt += totalCounts[i][j];
        leftC += leftFlankPosSum[i][j];
        rightC += rightFlankPosSum[i][j];
      }

      fprintf(stream,  "%s\t%u\t%u\t" , (*c)[j].locusName.c_str(), sum, sumt);
      if (sumt + sum) {
        printf("%.4f\t", sum/(double)(sumt+sum));
      } else {
        printf("NaN\t");
      }

      if (sumt) {
        fprintf(stream, "%.1f\t%.1f\n", leftC/(double)sumt, rightC/(double)sumt);
      } else {
        fprintf(stream, "NaN\tNaN\n");
      }
      

    }
    

  }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, 0x8(%rsp)
movl %edx, 0x3c(%rsp)
movq %rdi, %rbx
movq 0x18(%rsi), %rax
leaq 0x8(%rsi), %rdx
leaq 0x60(%rsp), %r14
leaq 0x40(%rsp), %rcx
movq %r14, %rdi
movq %rax, %rsi
callq 0x6ce2
movq (%r14), %r15
movq 0x8(%r14), %r14
cmpq %r14, %r15
je 0x3bb0
movq %r14, %rax
subq %r15, %rax
sarq $0x4, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x142(%rip), %rcx      # 0x3a4c
movq %r15, %rdi
movq %r14, %rsi
callq 0x7181
leaq -0x154(%rip), %rdx      # 0x3a4c
movq %r15, %rdi
movq %r14, %rsi
callq 0x73ae
movq 0x60(%rsp), %r15
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpb $0x1, 0xb706(%rip)      # 0xf2d1
jne 0x3c69
leaq 0x753d(%rip), %rdi      # 0xb115
movl $0x17, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
cmpb $0x0, 0x8(%rsp)
je 0x3c21
leaq 0x7549(%rip), %rdi      # 0xb141
movl $0x5, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
cmpb $0x0, 0xb6c1(%rip)      # 0xf2d2
je 0x3c5c
leaq 0x752d(%rip), %rdi      # 0xb147
movl $0xc, %esi
jmp 0x3c4f
leaq 0x7505(%rip), %rdi      # 0xb12d
movl $0x19, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
cmpb $0x0, 0xb691(%rip)      # 0xf2d2
je 0x3c5c
leaq 0x750a(%rip), %rdi      # 0xb154
movl $0x18, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
movl $0xa, %edi
movq %rbx, %rsi
callq 0x3350
cmpq 0x68(%rsp), %r15
je 0x3f01
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0x0, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
movl (%r15), %r12d
movq 0x8(%r15), %r14
movb 0x10(%r15), %bpl
movl 0x14(%r15), %eax
movq %rax, 0x30(%rsp)
cmpl 0xc(%rsp), %r12d
je 0x3d1c
movl 0x10(%rsp), %r13d
addl 0x20(%rsp), %r13d
je 0x3d07
movq 0x40(%rsp), %rdx
movq %rbx, %rdi
leaq 0x74ab(%rip), %rsi      # 0xb16d
xorl %eax, %eax
callq 0x3380
cmpb $0x0, 0x8(%rsp)
je 0x3cee
movl $0x0, 0x20(%rsp)
movq %rbx, %rdi
leaq 0x74ac(%rip), %rsi      # 0xb18e
movl %r13d, %edx
xorl %eax, %eax
callq 0x3380
jmp 0x3d0f
movq %rbx, %rdi
leaq 0x749c(%rip), %rsi      # 0xb194
movl 0x20(%rsp), %edx
movl 0x10(%rsp), %ecx
xorl %eax, %eax
callq 0x3380
movl $0x0, 0x20(%rsp)
movl $0x0, 0x10(%rsp)
movl %r12d, 0xc(%rsp)
movq 0x626655(%rip), %rax    # 0x62a378
imulq $0xb8, %r12, %r12
movq (%rax), %rsi
addq %r12, %rsi
leaq 0x40(%rsp), %rdi
callq 0x3170
movl 0x18(%r15), %eax
movl 0x1c(%r15), %ecx
leal (%rcx,%rax), %edx
cmpl 0x3c(%rsp), %edx
jae 0x3d60
addl 0x20(%rsp), %ecx
addl 0x10(%rsp), %eax
movl %ecx, 0x20(%rsp)
movl %eax, 0x10(%rsp)
jmp 0x3ef0
movq 0x626611(%rip), %rax    # 0x62a378
movq (%rax), %rax
leaq (%rax,%r12), %rcx
movzwl 0xae(%rax,%r12), %edx
cmpb $0x1, 0xb552(%rip)      # 0xf2d0
jne 0x3d8c
addl 0xa0(%rcx), %edx
addl 0xa4(%rcx), %edx
movq 0x30(%rsp), %r13
movl %r13d, %eax
subl %edx, %eax
movzwl 0xac(%rcx), %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl %edx, %r8d
movq 0x40(%rsp), %rdx
movq %rbx, %rdi
testl %r8d, %r8d
je 0x3dc5
leaq 0x73e3(%rip), %rsi      # 0xb19c
movl %r13d, %r9d
xorl %eax, %eax
callq 0x3380
jmp 0x3dd6
leaq 0x73e3(%rip), %rsi      # 0xb1af
movl %r13d, %r8d
xorl %eax, %eax
callq 0x3380
testb $0x1, %bpl
jne 0x3e26
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
mulsd 0x7234(%rip), %xmm0     # 0xb020
cvttsd2si %xmm0, %rax
leal 0x1f(%r13), %r12d
testl %r13d, %r13d
cmovnsl %r13d, %r12d
testl %eax, %eax
je 0x3e33
movq %rbx, %rbp
movl %eax, %r13d
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rsi
movq %rbp, %rdi
movl $0x20, %edx
callq 0x9737
incq %rbx
cmpq %rbx, %r13
jne 0x3e08
movq %rbp, %rbx
jmp 0x3e36
movq %r14, %rdi
movq %rbx, %rsi
callq 0x32f0
jmp 0x3e5a
xorl %r13d, %r13d
andl $-0x20, %r12d
movq 0x30(%rsp), %rax
subl %r12d, %eax
movq %rax, 0x30(%rsp)
je 0x3e5a
movq (%r14,%r13,8), %rsi
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
callq 0x9737
cmpb $0x0, 0x8(%rsp)
je 0x3ea2
movl 0x1c(%r15), %edx
addl 0x18(%r15), %edx
movq %rbx, %rdi
leaq 0x734c(%rip), %rsi      # 0xb1bf
xorl %eax, %eax
callq 0x3380
cmpb $0x0, 0x6264ef(%rip)    # 0x62a370
je 0x3ee3
movsd 0x20(%r15), %xmm0
addsd 0x28(%r15), %xmm0
movq %rbx, %rdi
leaq 0x7332(%rip), %rsi      # 0xb1cb
movb $0x1, %al
callq 0x3380
jmp 0x3ef0
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %edx
movq %rbx, %rdi
leaq 0x7310(%rip), %rsi      # 0xb1c4
xorl %eax, %eax
callq 0x3380
cmpb $0x0, 0x6264ae(%rip)    # 0x62a370
je 0x3ee3
movsd 0x20(%r15), %xmm1
movsd 0x28(%r15), %xmm0
movq %rbx, %rdi
leaq 0x72f7(%rip), %rsi      # 0xb1d1
movb $0x2, %al
callq 0x3380
jmp 0x3ef0
movl $0xa, %edi
movq %rbx, %rsi
callq 0x3350
addq $0x30, %r15
cmpq 0x68(%rsp), %r15
jne 0x3c8c
jmp 0x3f11
movl $0x0, 0x20(%rsp)
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %ebp
addl 0x20(%rsp), %ebp
je 0x3f66
movq 0x40(%rsp), %rdx
leaq 0x7246(%rip), %rsi      # 0xb16d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3380
cmpb $0x0, 0x8(%rsp)
je 0x3f4d
leaq 0x724f(%rip), %rsi      # 0xb18e
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x3380
jmp 0x3f66
leaq 0x7240(%rip), %rsi      # 0xb194
movq %rbx, %rdi
movl 0x20(%rsp), %edx
movl 0x10(%rsp), %ecx
xorl %eax, %eax
callq 0x3380
cmpb $0x1, 0xb360(%rip)      # 0xf2cd
jne 0x4136
leaq 0x725f(%rip), %rdi      # 0xb1d9
movl $0x66, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
cmpl $0x0, 0x6263ed(%rip)    # 0x62a380
je 0x4136
leaq 0x72a0(%rip), %r14      # 0xb240
leaq 0x72a3(%rip), %rbp      # 0xb24a
xorl %r13d, %r13d
movl 0xb338(%rip), %eax      # 0xf2e8
testq %rax, %rax
je 0x405b
movq 0x6263e0(%rip), %rdx    # 0x62a3a0
movq 0x6263e1(%rip), %rsi    # 0x62a3a8
movq 0x6263e2(%rip), %rdi    # 0x62a3b0
xorl %r8d, %r8d
movq 0x6263e0(%rip), %r9     # 0x62a3b8
xorl %ecx, %ecx
xorl %r10d, %r10d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq (%rdx,%r8,8), %r11
addl (%r11,%r13,4), %r12d
movq (%rsi,%r8,8), %r11
addl (%r11,%r13,4), %r15d
movq (%rdi,%r8,8), %r11
addq (%r11,%r13,8), %r10
movq (%r9,%r8,8), %r11
addq (%r11,%r13,8), %rcx
incq %r8
cmpq %r8, %rax
jne 0x3fe3
movq %r10, %xmm0
movq 0x7018(%rip), %xmm1     # 0xb030
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x701c(%rip), %xmm2     # 0xb040
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movapd %xmm3, 0x20(%rsp)
movq %rcx, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
jmp 0x4071
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movapd %xmm0, 0x10(%rsp)
movq 0x626300(%rip), %rax    # 0x62a378
movq (%rax), %rax
imulq $0xb8, %r13, %rcx
movq (%rax,%rcx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x3380
movl %r15d, %eax
addl %r12d, %eax
je 0x40c6
movl %r12d, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movq %rbp, %rdi
movb $0x1, %al
callq 0x3050
jmp 0x40d4
leaq 0x7183(%rip), %rdi      # 0xb250
xorl %eax, %eax
callq 0x3050
testl %r15d, %r15d
je 0x410b
movl %r15d, %eax
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
movapd 0x20(%rsp), %xmm0
divsd %xmm2, %xmm0
movapd 0x10(%rsp), %xmm1
divsd %xmm2, %xmm1
movq %rbx, %rdi
leaq 0x7153(%rip), %rsi      # 0xb255
movb $0x2, %al
callq 0x3380
jmp 0x4124
movl $0x8, %esi
movl $0x1, %edx
leaq 0x7144(%rip), %rdi      # 0xb260
movq %rbx, %rcx
callq 0x3500
incq %r13
movl 0x626253(%rip), %eax    # 0x62a380
cmpq %rax, %r13
jb 0x3faa
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4152
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4169
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x32d0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x419e
jmp 0x417f
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x419e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x41b5
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x32d0
movq %rbx, %rdi
callq 0x3480