// File: /workspace/github_c_makefile_star2_O2/richarddurbin[P]hash10x/hash10x.c
// Function: readFQB
void readFQB (FILE *f, int N, int chunkSize)
{
  int nReads = 0 ;	/* this group for statistics accumulation */
  U64 nHashes = 0 ;
  
  printf ("  reading and processing sorted fqb file with chunkSize %d", chunkSize) ;
  if (N) printf (", first %d records", N) ;
  printf ("\n") ;
  
  U32 *uBuf = new (chunkSize*30, U32) ; /* read and process readPairs in chunks */
  U32 *uStart = uBuf ;			/* beginning of records for current block */
  U32 barcode = 0 ;
  ClusterBlock *b = arrayp(clusterBlocks, 1, ClusterBlock) ; /* start at 1 */
  b->nRead = 0 ;
  while (!N || nReads < N)
    { if (b->nRead) { memmove (uBuf, uStart, 120*b->nRead) ; uStart = uBuf ; }

      U32 *u = &uBuf[30*b->nRead] ;
      int thisChunk = chunkSize - b->nRead ; if (thisChunk <= 0) die ("chunkSize too small") ;
      if (N && (nReads + thisChunk > N)) { thisChunk = N - nReads ; }
      int nRec = fread (u, 120, thisChunk, f) ;
      if (!nRec) { if (feof (f)) break ; else die ("file read problem") ; } /* loop exit here */
      
      int i ;
      if (!barcode) barcode = u[0] ; /* needed to start barcode matching */
      for (i = 0 ; i < nRec ; ++i)
	if (u[30*i] == barcode) ++b->nRead ;
	else
	  { processBlock (uStart, b) ;
	    nHashes += b->nHash ;
	    b = arrayp(clusterBlocks, arrayMax(clusterBlocks), ClusterBlock) ; b->nRead = 1 ;
	    barcode = u[30*i] ; uStart = &u[30*i] ;
	  }

      nReads += nRec ;
    }
  fclose (f) ;

  fprintf (outFile, "  read %d read pair records for %d barcodes, mean %.2f read pairs per barcode\n",
	  nReads, arrayMax(clusterBlocks)-1, nReads/(double)(arrayMax(clusterBlocks)-1)) ;
  fprintf (outFile, "  created %ld hashes, mean %.2f hashes per read pair, %.2f per barcode\n",
	  (long)nHashes, nHashes/(double)nReads, nHashes/(double)(arrayMax(clusterBlocks)-1)) ;
  if (outFile != stdin)
    { printf ("  read %d read pair records for %d barcodes, mean %.2f read pairs per barcode\n",
	  nReads, arrayMax(clusterBlocks)-1, nReads/(double)(arrayMax(clusterBlocks)-1)) ;
      printf ("  created %ld hashes, mean %.2f hashes per read pair, %.2f per barcode\n",
	  (long)nHashes, nHashes/(double)nReads, nHashes/(double)(arrayMax(clusterBlocks)-1)) ;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %r13d
movq %rdi, 0x18(%rsp)
leaq 0x7903(%rip), %rdi      # 0x9060
movl %edx, 0x14(%rsp)
movl %edx, %esi
xorl %eax, %eax
callq 0x10d0
testl %r13d, %r13d
je 0x1780
leaq 0x7925(%rip), %rdi      # 0x909b
movl %r13d, %esi
xorl %eax, %eax
callq 0x10d0
pushq $0xa
popq %rdi
callq 0x1050
imull $0x1e, 0x14(%rsp), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x8afc
movq %rax, %r15
movq 0xebf5(%rip), %rdi      # 0x10398
pushq $0x1
popq %rsi
callq 0x8560
movq %rax, %rbp
andl $0x0, (%rax)
movq %r15, 0x38(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r14d, %r14d
movl %r13d, 0x20(%rsp)
testl %r13d, %r13d
je 0x17d3
cmpl %r14d, 0x20(%rsp)
jle 0x18fb
movl (%rbp), %eax
testl %eax, %eax
movq %r14, 0x8(%rsp)
je 0x17fb
imull $0x78, %eax, %edx
movq 0x38(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x11e0
movl (%rbp), %r13d
movq %r14, %r15
jmp 0x17fe
xorl %r13d, %r13d
movl 0x14(%rsp), %eax
movl %eax, %r14d
subl %r13d, %r14d
testl %r14d, %r14d
jg 0x181b
leaq 0x789a(%rip), %rdi      # 0x90ae
xorl %eax, %eax
callq 0x8a3c
imull $0x1e, %r13d, %eax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax,4), %r13
movq 0x8(%rsp), %rdx
leal (%r14,%rdx), %eax
movl 0x20(%rsp), %esi
movl %esi, %ecx
subl %edx, %ecx
cmpl %esi, %eax
cmovlel %r14d, %ecx
testl %esi, %esi
cmovel %r14d, %ecx
movslq %ecx, %rdx
movq %r13, %rdi
pushq $0x78
popq %rsi
movq 0x18(%rsp), %rcx
callq 0x10a0
movq %rax, 0x40(%rsp)
testl %eax, %eax
jne 0x1886
movq 0x18(%rsp), %rdi
callq 0x1160
testl %eax, %eax
movq 0x8(%rsp), %r14
jne 0x18fb
leaq 0x7843(%rip), %rdi      # 0x90c2
xorl %eax, %eax
callq 0x8a3c
testl %r12d, %r12d
jne 0x188f
movl (%r13), %r12d
movq 0x40(%rsp), %rax
testl %eax, %eax
movl $0x0, %r14d
cmovgl %eax, %r14d
subq $0x1, %r14
jb 0x18e7
cmpl %r12d, (%r13)
jne 0x18b1
incl (%rbp)
jmp 0x18e1
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1527
movl 0x4(%rbp), %eax
addq %rax, %rbx
movq 0xeacf(%rip), %rdi      # 0x10398
movl 0x18(%rdi), %esi
callq 0x8560
movq %rax, %rbp
movl $0x1, (%rax)
movl (%r13), %r12d
movq %r13, %r15
addq $0x78, %r13
jmp 0x18a0
movq 0x8(%rsp), %r14
addl 0x40(%rsp), %r14d
movl 0x20(%rsp), %r13d
jmp 0x17c3
movq 0x18(%rsp), %rdi
callq 0x10b0
movq 0xea94(%rip), %rdi      # 0x103a0
movq 0xea85(%rip), %rax      # 0x10398
movl 0x18(%rax), %ecx
decl %ecx
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x8(%rsp)
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0x77a2(%rip), %rsi      # 0x90d4
movl %r14d, %edx
movb $0x1, %al
callq 0x1150
movq 0xea5d(%rip), %rdi      # 0x103a0
movq %rbx, %xmm0
punpckldq 0x76c0(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x76c8(%rip), %xmm0     # 0x9020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
divsd 0x8(%rsp), %xmm0
movq 0xea23(%rip), %rax      # 0x10398
movl 0x18(%rax), %eax
decl %eax
cvtsi2sd %eax, %xmm2
movapd %xmm1, 0x40(%rsp)
divsd %xmm2, %xmm1
leaq 0x7794(%rip), %rsi      # 0x9123
movq %rbx, %rdx
movapd %xmm0, 0x20(%rsp)
movb $0x2, %al
callq 0x1150
movq 0xe9fa(%rip), %rax      # 0x103a0
movq 0xd623(%rip), %rcx      # 0xefd0
cmpq (%rcx), %rax
je 0x1a1b
movq 0xe9df(%rip), %rax      # 0x10398
movl 0x18(%rax), %edx
decl %edx
cvtsi2sd %edx, %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x7701(%rip), %rdi      # 0x90d4
movl %r14d, %esi
movb $0x1, %al
callq 0x10d0
movq 0xe9b4(%rip), %rax      # 0x10398
movl 0x18(%rax), %eax
decl %eax
cvtsi2sd %eax, %xmm0
movapd 0x40(%rsp), %xmm1
divsd %xmm0, %xmm1
leaq 0x7725(%rip), %rdi      # 0x9123
movq %rbx, %rsi
movaps 0x20(%rsp), %xmm0
movb $0x2, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10d0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq