// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
leaq 0x7fdd(%rip), %rdi      # 0xa060
movq %rdx, 0x18(%rsp)
movl %edx, %esi
xorl %eax, %eax
callq 0x10d0
testl %r13d, %r13d
je 0x20a7
leaq 0x7ffe(%rip), %rdi      # 0xa09b
movl %r13d, %esi
xorl %eax, %eax
callq 0x10d0
movl $0xa, %edi
callq 0x1050
movq 0x18(%rsp), %rcx
leal (%rcx,%rcx), %eax
shll $0x5, %ecx
subl %eax, %ecx
movslq %ecx, %rdi
shlq $0x2, %rdi
callq 0x94d0
movq %rax, %r15
movq 0xe2b4(%rip), %rdi      # 0x10388
movl $0x1, %esi
callq 0x8f2b
movq %rax, %r12
movl $0x0, (%rax)
movq %r15, 0x20(%rsp)
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r14d, %r14d
movl %r13d, 0x30(%rsp)
testl %r13d, %r13d
je 0x2106
cmpl %r14d, %r13d
jle 0x223f
movl (%r12), %eax
testl %eax, %eax
movq %r14, 0x8(%rsp)
je 0x2129
imull $0x78, %eax, %edx
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x11d0
movq %r14, %r15
movl (%r12), %r13d
movq 0x18(%rsp), %r14
subl %r13d, %r14d
testl %r14d, %r14d
jg 0x2148
leaq 0x7f6d(%rip), %rdi      # 0xa0ae
xorl %eax, %eax
callq 0x9408
leal (,%r13,2), %eax
shll $0x5, %r13d
subl %eax, %r13d
movq 0x20(%rsp), %rax
leaq (%rax,%r13,4), %r13
movq 0x8(%rsp), %rdx
leal (%r14,%rdx), %eax
movl 0x30(%rsp), %esi
movl %esi, %ecx
subl %edx, %ecx
cmpl %esi, %eax
cmovlel %r14d, %ecx
testl %esi, %esi
cmovel %r14d, %ecx
movslq %ecx, %rdx
movl $0x78, %esi
movq %r13, %rdi
movq 0x10(%rsp), %rcx
callq 0x10a0
movq %rax, %r14
testl %r14d, %r14d
je 0x2208
testl %ebp, %ebp
jne 0x21a2
movl (%r13), %ebp
testl %r14d, %r14d
movq %r14, 0x40(%rsp)
jle 0x21fa
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpl %ebp, (%r13)
jne 0x21bf
incl (%r12)
jmp 0x21f1
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e1e
movl 0x4(%r12), %eax
addq %rax, %rbx
movq 0xe1af(%rip), %rdi      # 0x10388
movl 0x18(%rdi), %esi
callq 0x8f2b
movq %rax, %r12
movl $0x1, (%rax)
movl (%r13), %ebp
movq %r13, %r15
addq $0x78, %r13
decq %r14
jne 0x21b3
movq 0x8(%rsp), %r14
addl 0x40(%rsp), %r14d
movb $0x1, %al
jmp 0x221d
movq 0x10(%rsp), %rdi
callq 0x1160
testl %eax, %eax
je 0x222c
xorl %eax, %eax
movq 0x8(%rsp), %r14
testb %al, %al
movl 0x30(%rsp), %r13d
jne 0x20f8
jmp 0x223f
leaq 0x7e8f(%rip), %rdi      # 0xa0c2
xorl %eax, %eax
callq 0x9408
jmp 0x219a
movq 0x10(%rsp), %rdi
callq 0x10b0
movq 0xe140(%rip), %rdi      # 0x10390
movq 0xe131(%rip), %rax      # 0x10388
movl 0x18(%rax), %ecx
decl %ecx
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x8(%rsp)
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
leaq 0x7e5e(%rip), %rsi      # 0xa0d4
movl %r14d, %edx
movb $0x1, %al
callq 0x1150
movq 0xe109(%rip), %rdi      # 0x10390
movq %rbx, %xmm0
punpckldq 0x7d7c(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7d84(%rip), %xmm0     # 0xa020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
divsd 0x8(%rsp), %xmm0
movq 0xe0cf(%rip), %rax      # 0x10388
movl 0x18(%rax), %eax
decl %eax
cvtsi2sd %eax, %xmm2
movapd %xmm1, 0x30(%rsp)
divsd %xmm2, %xmm1
leaq 0x7e50(%rip), %rsi      # 0xa123
movq %rbx, %rdx
movapd %xmm0, 0x20(%rsp)
movb $0x2, %al
callq 0x1150
movq 0xe0a6(%rip), %rax      # 0x10390
movq 0xccdf(%rip), %rcx      # 0xefd0
cmpq (%rcx), %rax
je 0x2365
movq 0xe08b(%rip), %rax      # 0x10388
movl 0x18(%rax), %edx
decl %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
leaq 0x7dba(%rip), %rdi      # 0xa0d4
movl %r14d, %esi
movb $0x1, %al
callq 0x10d0
movq 0xe05d(%rip), %rax      # 0x10388
movl 0x18(%rax), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movapd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
leaq 0x7ddb(%rip), %rdi      # 0xa123
movq %rbx, %rsi
movaps 0x20(%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