// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %esi, %r13d
movq %rdi, 0x8(%rsp)
leaq 0x802b(%rip), %rdi      # 0xa0a0
xorl %ebx, %ebx
movq %rdx, 0x10(%rsp)
movl %edx, %esi
xorl %eax, %eax
callq 0x10d0
testl %r13d, %r13d
je 0x209b
leaq 0x804a(%rip), %rdi      # 0xa0db
movl %r13d, %esi
xorl %eax, %eax
callq 0x10d0
movl $0xa, %edi
callq 0x1050
movq 0x10(%rsp), %rcx
leal (%rcx,%rcx), %eax
shll $0x5, %ecx
subl %eax, %ecx
movslq %ecx, %rdi
shlq $0x2, %rdi
callq 0x96c0
movq %rax, %r15
movq 0xe2c0(%rip), %rdi      # 0x10388
movl $0x1, %esi
callq 0x9131
movl $0x0, (%rax)
testl %r13d, %r13d
js 0x2223
movq %rax, %r12
xorl %eax, %eax
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %r15, 0x18(%rsp)
movl %r13d, 0x20(%rsp)
testl %eax, %eax
movq %rbx, (%rsp)
je 0x211b
imull $0x78, %eax, %edx
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x11d0
movl (%r12), %r13d
movq %rbx, %r15
jmp 0x211e
xorl %r13d, %r13d
movq 0x10(%rsp), %rbx
subl %r13d, %ebx
testl %ebx, %ebx
jg 0x2138
leaq 0x7fbd(%rip), %rdi      # 0xa0ee
xorl %eax, %eax
callq 0x95f8
leal (,%r13,2), %eax
shll $0x5, %r13d
subl %eax, %r13d
movq 0x18(%rsp), %rax
leaq (%rax,%r13,4), %r13
movq (%rsp), %rdx
leal (%rbx,%rdx), %eax
movl 0x20(%rsp), %esi
movl %esi, %ecx
subl %edx, %ecx
cmpl %esi, %eax
cmovlel %ebx, %ecx
testl %esi, %esi
cmovel %ebx, %ecx
movslq %ecx, %rdx
movl $0x78, %esi
movq %r13, %rdi
movq 0x8(%rsp), %rcx
callq 0x10a0
movq %rax, %rbx
testl %ebx, %ebx
jne 0x21a5
movq 0x8(%rsp), %rdi
callq 0x1160
testl %eax, %eax
jne 0x2228
leaq 0x7f64(%rip), %rdi      # 0xa102
xorl %eax, %eax
callq 0x95f8
testl %ebp, %ebp
jne 0x21ad
movl (%r13), %ebp
testl %ebx, %ebx
movq %rbx, 0x30(%rsp)
jle 0x2203
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
cmpl %ebp, (%r13)
jne 0x21c8
incl (%r12)
jmp 0x21fa
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e2e
movl 0x4(%r12), %eax
addq %rax, %r14
movq 0xe1a6(%rip), %rdi      # 0x10388
movl 0x18(%rdi), %esi
callq 0x9131
movq %rax, %r12
movl $0x1, (%rax)
movl (%r13), %ebp
movq %r13, %r15
addq $0x78, %r13
decq %rbx
jne 0x21bc
movq (%rsp), %rbx
addl 0x30(%rsp), %ebx
movl 0x20(%rsp), %r13d
testl %r13d, %r13d
je 0x221a
cmpl %r13d, %ebx
jge 0x222c
movl (%r12), %eax
jmp 0x20f7
xorl %r14d, %r14d
jmp 0x222c
movq (%rsp), %rbx
movq 0x8(%rsp), %rdi
callq 0x10b0
movq 0xe153(%rip), %rdi      # 0x10390
movq 0xe144(%rip), %rax      # 0x10388
movl 0x18(%rax), %ecx
decl %ecx
cvtsi2sd %ebx, %xmm0
movsd %xmm0, (%rsp)
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0x7eb3(%rip), %rsi      # 0xa114
movl %ebx, %edx
movb $0x1, %al
callq 0x1150
movq 0xe11f(%rip), %rdi      # 0x10390
movq %r14, %xmm0
punpckldq 0x7d92(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7d9a(%rip), %xmm0     # 0xa020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
divsd (%rsp), %xmm0
movq 0xe0e6(%rip), %rax      # 0x10388
movl 0x18(%rax), %eax
decl %eax
cvtsi2sd %eax, %xmm2
movapd %xmm1, 0x20(%rsp)
divsd %xmm2, %xmm1
leaq 0x7ea7(%rip), %rsi      # 0xa163
movq %r14, %rdx
movapd %xmm0, 0x30(%rsp)
movb $0x2, %al
callq 0x1150
movq 0xe0bd(%rip), %rax      # 0x10390
movq 0xccf6(%rip), %rcx      # 0xefd0
cmpq (%rcx), %rax
je 0x234c
movq 0xe0a2(%rip), %rax      # 0x10388
movl 0x18(%rax), %edx
decl %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movsd (%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x7e12(%rip), %rdi      # 0xa114
movl %ebx, %esi
movb $0x1, %al
callq 0x10d0
movq 0xe076(%rip), %rax      # 0x10388
movl 0x18(%rax), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
leaq 0x7e34(%rip), %rdi      # 0xa163
movq %r14, %rsi
movaps 0x30(%rsp), %xmm0
movb $0x2, %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10d0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq