// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movl %ecx, 0x10(%rsp)
movl %edx, 0x1c(%rsp)
movq %rdi, %rbx
movq 0x18(%rsi), %rax
leaq 0x8(%rsi), %rdx
leaq 0x40(%rsp), %r14
leaq 0x20(%rsp), %rcx
movq %r14, %rdi
movq %rax, %rsi
callq 0x5f9a
movq (%r14), %rdi
movq 0x8(%r14), %rsi
leaq -0x105(%rip), %rdx      # 0x38d3
callq 0x63a1
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq 0x40(%rsp), %r12
movb $0x0, (%rax)
cmpb $0x1, 0xb8b7(%rip)      # 0xf2b1
jne 0x3a83
leaq 0x66ce(%rip), %rdi      # 0xa0d5
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x34c0
cmpb $0x0, 0x10(%rsp)
je 0x3a45
leaq 0x66de(%rip), %rdi      # 0xa101
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x34c0
cmpb $0x0, 0xb87a(%rip)      # 0xf2b2
je 0x3a78
leaq 0x66c6(%rip), %rdi      # 0xa107
pushq $0xc
jmp 0x3a6c
leaq 0x66a1(%rip), %rdi      # 0xa0ed
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x34c0
cmpb $0x0, 0xb851(%rip)      # 0xf2b2
je 0x3a78
leaq 0x66aa(%rip), %rdi      # 0xa114
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x34c0
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x32f0
pushq $-0x1
popq %rax
movl %eax, 0x14(%rsp)
movl $0x0, 0x8(%rsp)
xorl %r15d, %r15d
cmpq 0x48(%rsp), %r12
je 0x3cfb
movl (%r12), %r13d
movq 0x8(%r12), %rbp
movb 0x10(%r12), %al
movl 0x14(%r12), %r14d
cmpl 0x14(%rsp), %r13d
movb %al, 0xf(%rsp)
je 0x3b2e
movl %r15d, 0x18(%rsp)
addl 0x8(%rsp), %r15d
je 0x3b1e
movq 0x20(%rsp), %rdx
movq %rbx, %rdi
leaq 0x6654(%rip), %rsi      # 0xa12d
xorl %eax, %eax
callq 0x3340
cmpb $0x0, 0x10(%rsp)
je 0x3b05
movl $0x0, 0x8(%rsp)
movq %rbx, %rdi
leaq 0x6655(%rip), %rsi      # 0xa14e
movl %r15d, %edx
xorl %eax, %eax
callq 0x3340
jmp 0x3b26
movq %rbx, %rdi
leaq 0x6645(%rip), %rsi      # 0xa154
movl 0x8(%rsp), %edx
movl 0x18(%rsp), %ecx
xorl %eax, %eax
callq 0x3340
movl $0x0, 0x8(%rsp)
xorl %r15d, %r15d
movl %r13d, 0x14(%rsp)
movq 0x626823(%rip), %rax    # 0x62a358
imulq $0xb8, %r13, %r13
movq (%rax), %rsi
addq %r13, %rsi
leaq 0x20(%rsp), %rdi
callq 0x3160
movl 0x18(%r12), %eax
movl 0x1c(%r12), %ecx
leal (%rcx,%rax), %edx
cmpl 0x1c(%rsp), %edx
jae 0x3b72
addl 0x8(%rsp), %ecx
addl %r15d, %eax
movl %ecx, 0x8(%rsp)
movl %eax, %r15d
jmp 0x3cf2
movq 0x6267df(%rip), %rax    # 0x62a358
movq (%rax), %rax
leaq (%rax,%r13), %rcx
movzwl 0xae(%rax,%r13), %edx
cmpb $0x1, 0xb720(%rip)      # 0xf2b0
jne 0x3b9e
addl 0xa0(%rcx), %edx
addl 0xa4(%rcx), %edx
movl %r14d, %eax
subl %edx, %eax
movzwl 0xac(%rcx), %ecx
cltd
idivl %ecx
movl %edx, %r8d
movq 0x20(%rsp), %rdx
movq %rbx, %rdi
testl %r8d, %r8d
je 0x3bd2
leaq 0x6598(%rip), %rsi      # 0xa15c
movl %eax, %ecx
movl %r14d, %r9d
xorl %eax, %eax
callq 0x3340
jmp 0x3be5
leaq 0x6596(%rip), %rsi      # 0xa16f
movl %eax, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x3340
testb $0x1, 0xf(%rsp)
jne 0x3c2f
movl %r15d, 0x18(%rsp)
cvtsi2sd %r14d, %xmm0
mulsd 0x6422(%rip), %xmm0     # 0xa020
cvttsd2si %xmm0, %rcx
movl %r14d, %eax
cltd
pushq $0x20
popq %rsi
idivl %esi
movl %edx, %r14d
movl %ecx, %r13d
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x3c3c
movq (%rbp,%r15,8), %rsi
movq %rbx, %rdi
pushq $0x20
popq %rdx
callq 0x852d
incq %r15
jmp 0x3c15
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3290
jmp 0x3c56
testl %r14d, %r14d
movl 0x18(%rsp), %r15d
je 0x3c56
movq (%rbp,%r13,8), %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x852d
cmpb $0x0, 0x10(%rsp)
je 0x3ca2
movl 0x1c(%r12), %edx
addl 0x18(%r12), %edx
movq %rbx, %rdi
leaq 0x650e(%rip), %rsi      # 0xa17f
xorl %eax, %eax
callq 0x3340
cmpb $0x0, 0x6266d1(%rip)    # 0x62a350
je 0x3ce7
movsd 0x20(%r12), %xmm0
addsd 0x28(%r12), %xmm0
movq %rbx, %rdi
leaq 0x64f2(%rip), %rsi      # 0xa18b
movb $0x1, %al
callq 0x3340
jmp 0x3cf2
movl 0x18(%r12), %ecx
movl 0x1c(%r12), %edx
movq %rbx, %rdi
leaq 0x64ce(%rip), %rsi      # 0xa184
xorl %eax, %eax
callq 0x3340
cmpb $0x0, 0x62668c(%rip)    # 0x62a350
je 0x3ce7
movsd 0x20(%r12), %xmm1
movsd 0x28(%r12), %xmm0
movq %rbx, %rdi
leaq 0x64b3(%rip), %rsi      # 0xa191
movb $0x2, %al
callq 0x3340
jmp 0x3cf2
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x32f0
addq $0x30, %r12
jmp 0x3a95
movl %r15d, %ebp
addl 0x8(%rsp), %ebp
je 0x3d4e
movq 0x20(%rsp), %rdx
leaq 0x641d(%rip), %rsi      # 0xa12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3340
cmpb $0x0, 0x10(%rsp)
je 0x3d36
leaq 0x6426(%rip), %rsi      # 0xa14e
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x3340
jmp 0x3d4e
leaq 0x6417(%rip), %rsi      # 0xa154
movq %rbx, %rdi
movl 0x8(%rsp), %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x3340
cmpb $0x1, 0xb558(%rip)      # 0xf2ad
jne 0x3ec5
leaq 0x6437(%rip), %rdi      # 0xa199
pushq $0x66
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x34c0
xorl %r12d, %r12d
movl 0x6265e7(%rip), %eax    # 0x62a360
cmpq %rax, %r12
jae 0x3ec5
movl 0xb540(%rip), %eax      # 0xf2c8
movq 0x6265f1(%rip), %rcx    # 0x62a380
movq 0x6265f2(%rip), %rdx    # 0x62a388
movq 0x6265f3(%rip), %rsi    # 0x62a390
movq 0x6265f4(%rip), %rdi    # 0x62a398
xorl %r8d, %r8d
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r13d, %r13d
cmpq %r8, %rax
je 0x3ddc
movq (%rcx,%r8,8), %r9
addl (%r9,%r12,4), %r14d
movq (%rdx,%r8,8), %r9
addl (%r9,%r12,4), %ebp
movq (%rsi,%r8,8), %r9
addq (%r9,%r12,8), %r15
movq (%rdi,%r8,8), %r9
addq (%r9,%r12,8), %r13
incq %r8
jmp 0x3db2
movq 0x626575(%rip), %rax    # 0x62a358
movq (%rax), %rax
imulq $0xb8, %r12, %rcx
movq (%rax,%rcx), %rdx
movq %rbx, %rdi
leaq 0x6405(%rip), %rsi      # 0xa200
movl %r14d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x3340
movl %ebp, %eax
addl %r14d, %eax
je 0x3e32
movl %r14d, %ecx
cvtsi2sd %rcx, %xmm0
movl %eax, %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
leaq 0x63e1(%rip), %rdi      # 0xa20a
movb $0x1, %al
callq 0x3040
jmp 0x3e40
leaq 0x63d7(%rip), %rdi      # 0xa210
xorl %eax, %eax
callq 0x3040
testl %ebp, %ebp
je 0x3ea8
movq %r15, %xmm1
movq 0x61df(%rip), %xmm4     # 0xa030
punpckldq %xmm4, %xmm1    # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
movapd 0x61e3(%rip), %xmm5     # 0xa040
subpd %xmm5, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movl %ebp, %eax
cvtsi2sd %rax, %xmm2
divsd %xmm2, %xmm0
movq %r13, %xmm3
punpckldq %xmm4, %xmm3    # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
subpd %xmm5, %xmm3
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
addsd %xmm3, %xmm1
divsd %xmm2, %xmm1
movq %rbx, %rdi
leaq 0x6376(%rip), %rsi      # 0xa215
movb $0x2, %al
callq 0x3340
jmp 0x3ebd
leaq 0x6371(%rip), %rdi      # 0xa220
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x34c0
incq %r12
jmp 0x3d73
leaq 0x20(%rsp), %rdi
callq 0x3110
leaq 0x40(%rsp), %rdi
callq 0x628e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x3efc
jmp 0x3eef
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3110
leaq 0x40(%rsp), %rdi
callq 0x628e
movq %rbx, %rdi
callq 0x3440