// File: /workspace/github_cpp_makefile_star2_O1/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), %r15
leaq 0x40(%rsp), %rcx
movq %r15, %rdi
movq %rax, %rsi
callq 0x6c42
movq (%r15), %r14
movq 0x8(%r15), %r15
cmpq %r15, %r14
je 0x3bcb
movq %r15, %rax
subq %r14, %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      # 0x3a6c
movq %r14, %rdi
movq %r15, %rsi
callq 0x6fd1
leaq -0x154(%rip), %rdx      # 0x3a6c
movq %r14, %rdi
movq %r15, %rsi
callq 0x71fe
movq 0x60(%rsp), %r15
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpb $0x1, 0xb6e6(%rip)      # 0xf2d1
jne 0x3c7c
leaq 0x74dd(%rip), %rdi      # 0xb0d5
movl $0x17, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
cmpb $0x0, 0x8(%rsp)
je 0x3c1f
leaq 0x74e9(%rip), %rdi      # 0xb101
movl $0x5, %esi
jmp 0x3c2b
leaq 0x74c7(%rip), %rdi      # 0xb0ed
movl $0x19, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
cmpb $0x0, 0xb693(%rip)      # 0xf2d2
je 0x3c6f
cmpb $0x0, 0x8(%rsp)
je 0x3c56
leaq 0x74b8(%rip), %rdi      # 0xb107
movl $0xc, %esi
jmp 0x3c62
leaq 0x74b7(%rip), %rdi      # 0xb114
movl $0x18, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
movl $0xa, %edi
movq %rbx, %rsi
callq 0x3350
cmpq 0x68(%rsp), %r15
je 0x3f10
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0x0, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
movl (%r15), %ebp
movq 0x8(%r15), %r14
movb 0x10(%r15), %r12b
movl 0x14(%r15), %eax
movq %rax, 0x30(%rsp)
cmpl 0xc(%rsp), %ebp
je 0x3d2d
movl 0x10(%rsp), %r13d
addl 0x20(%rsp), %r13d
je 0x3d19
movq 0x40(%rsp), %rdx
movq %rbx, %rdi
leaq 0x7459(%rip), %rsi      # 0xb12d
xorl %eax, %eax
callq 0x3380
cmpb $0x0, 0x8(%rsp)
je 0x3d00
movl $0x0, 0x20(%rsp)
movq %rbx, %rdi
leaq 0x745a(%rip), %rsi      # 0xb14e
movl %r13d, %edx
xorl %eax, %eax
callq 0x3380
jmp 0x3d21
movq %rbx, %rdi
leaq 0x744a(%rip), %rsi      # 0xb154
movl 0x20(%rsp), %edx
movl 0x10(%rsp), %ecx
xorl %eax, %eax
callq 0x3380
movl $0x0, 0x20(%rsp)
movl $0x0, 0x10(%rsp)
movl %ebp, 0xc(%rsp)
movq 0x626644(%rip), %rax    # 0x62a378
imulq $0xb8, %rbp, %r13
movq (%rax), %rsi
addq %r13, %rsi
leaq 0x40(%rsp), %rdi
callq 0x3170
movl 0x18(%r15), %eax
movl 0x1c(%r15), %ecx
leal (%rcx,%rax), %edx
cmpl 0x3c(%rsp), %edx
jae 0x3d71
addl 0x20(%rsp), %ecx
addl 0x10(%rsp), %eax
movl %ecx, 0x20(%rsp)
movl %eax, 0x10(%rsp)
jmp 0x3eff
movq 0x626600(%rip), %rax    # 0x62a378
movq (%rax), %rax
leaq (%rax,%r13), %rcx
movzwl 0xae(%rax,%r13), %edx
cmpb $0x1, 0xb541(%rip)      # 0xf2d0
jne 0x3d9d
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 0x3dd6
leaq 0x7392(%rip), %rsi      # 0xb15c
movl %r13d, %r9d
xorl %eax, %eax
callq 0x3380
jmp 0x3de7
leaq 0x7392(%rip), %rsi      # 0xb16f
movl %r13d, %r8d
xorl %eax, %eax
callq 0x3380
testb $0x1, %r12b
jne 0x3e34
movq %rbx, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
mulsd 0x7220(%rip), %xmm0     # 0xb020
cvttsd2si %xmm0, %rax
leal 0x1f(%r13), %ebp
testl %r13d, %r13d
cmovnsl %r13d, %ebp
testl %eax, %eax
je 0x3e41
movl %eax, %ebx
xorl %r13d, %r13d
movq (%r14,%r13,8), %rsi
movq %r12, %rdi
movl $0x20, %edx
callq 0x9657
incq %r13
cmpq %r13, %rbx
jne 0x3e19
jmp 0x3e44
movq %r14, %rdi
movq %rbx, %rsi
callq 0x32f0
jmp 0x3e69
xorl %r13d, %r13d
andl $-0x20, %ebp
movq 0x30(%rsp), %rax
subl %ebp, %eax
movq %rax, 0x30(%rsp)
movq %r12, %rbx
je 0x3e69
movq (%r14,%r13,8), %rsi
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
callq 0x9657
cmpb $0x0, 0x8(%rsp)
je 0x3e8b
movl 0x1c(%r15), %edx
addl 0x18(%r15), %edx
movq %rbx, %rdi
leaq 0x72fd(%rip), %rsi      # 0xb17f
xorl %eax, %eax
callq 0x3380
jmp 0x3ea4
movl 0x18(%r15), %ecx
movl 0x1c(%r15), %edx
movq %rbx, %rdi
leaq 0x72e7(%rip), %rsi      # 0xb184
xorl %eax, %eax
callq 0x3380
cmpb $0x0, 0x6264c5(%rip)    # 0x62a370
je 0x3ed3
cmpb $0x0, 0x8(%rsp)
je 0x3ee2
movsd 0x20(%r15), %xmm0
addsd 0x28(%r15), %xmm0
movq %rbx, %rdi
leaq 0x72c1(%rip), %rsi      # 0xb18b
movb $0x1, %al
callq 0x3380
jmp 0x3eff
movl $0xa, %edi
movq %rbx, %rsi
callq 0x3350
jmp 0x3eff
movsd 0x20(%r15), %xmm1
movsd 0x28(%r15), %xmm0
movq %rbx, %rdi
leaq 0x7299(%rip), %rsi      # 0xb191
movb $0x2, %al
callq 0x3380
addq $0x30, %r15
cmpq 0x68(%rsp), %r15
jne 0x3c9f
jmp 0x3f20
movl $0x0, 0x20(%rsp)
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %r14d
addl 0x20(%rsp), %r14d
je 0x3f78
movq 0x40(%rsp), %rdx
leaq 0x71f5(%rip), %rsi      # 0xb12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3380
cmpb $0x0, 0x8(%rsp)
je 0x3f5f
leaq 0x71fe(%rip), %rsi      # 0xb14e
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x3380
jmp 0x3f78
leaq 0x71ee(%rip), %rsi      # 0xb154
movq %rbx, %rdi
movl 0x20(%rsp), %edx
movl 0x10(%rsp), %ecx
xorl %eax, %eax
callq 0x3380
cmpb $0x1, 0xb34e(%rip)      # 0xf2cd
jne 0x4148
leaq 0x720d(%rip), %rdi      # 0xb199
movl $0x66, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x3500
cmpl $0x0, 0x6263db(%rip)    # 0x62a380
je 0x4148
leaq 0x724e(%rip), %r14      # 0xb200
leaq 0x7251(%rip), %rbp      # 0xb20a
xorl %r13d, %r13d
movl 0xb326(%rip), %eax      # 0xf2e8
testq %rax, %rax
je 0x406d
movq 0x6263ce(%rip), %rdx    # 0x62a3a0
movq 0x6263cf(%rip), %rsi    # 0x62a3a8
movq 0x6263d0(%rip), %rdi    # 0x62a3b0
xorl %r8d, %r8d
movq 0x6263ce(%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 0x3ff5
movq %r10, %xmm0
movq 0x7006(%rip), %xmm1     # 0xb030
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x700a(%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 0x4083
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movapd %xmm0, 0x10(%rsp)
movq 0x6262ee(%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 0x40d8
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 0x40e6
leaq 0x7131(%rip), %rdi      # 0xb210
xorl %eax, %eax
callq 0x3050
testl %r15d, %r15d
je 0x411d
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 0x7101(%rip), %rsi      # 0xb215
movb $0x2, %al
callq 0x3380
jmp 0x4136
movl $0x8, %esi
movl $0x1, %edx
leaq 0x70f2(%rip), %rdi      # 0xb220
movq %rbx, %rcx
callq 0x3500
incq %r13
movl 0x626241(%rip), %eax    # 0x62a380
cmpq %rax, %r13
jb 0x3fbc
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4164
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x417b
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 0x41b0
jmp 0x4191
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x41b0
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32d0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x41c7
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x32d0
movq %rbx, %rdi
callq 0x3480