// File: /workspace/github_c_makefile_star2_O2/richarddurbin[P]hash10x/fq2b.c
// Function: main
int main (int argc, char *argv[])
{
  BOOL isCheckId = TRUE ;
  FILE *fout = stdout ;
  BOOL isMismatchID = FALSE ;

  packInit () ;
  
  --argc ; ++argv ;		/* eat program name */
  while (argc > 2 && *argv[0] == '-')
    if (!strcmp (*argv, "-10x")) { read10xWhitelist (argv[1]) ; argc -= 2 ; argv += 2 ; }
    else if (!strcmp (*argv, "-checkId")) { isCheckId = TRUE ; --argc ; ++argv ; }
    else if (!strcmp (*argv, "-o"))
      { if (!(fout = fopen (argv[1], "wb"))) die ("failed to open output file %s", argv[1]) ;
	argc -= 2 ; argv += 2 ;
      }
    else die ("Unknown arg %s for fq2b - run without args for usage", *argv) ;
    
  if (argc < 1 || argc > 2)
    die ("Usage: fq2fqb [opts] <fastq.gz> [<fastq.gz>]\n"
	 "  Converts fastq to binary with 2 bits per base, converting N to A (!).\n"
	 "  Throws out read names and qualities. [TODO handle qualities]\n"
	 "  If two fastq files are given they are interleaved.\n"
	 "Opts: -10x <whitelist file>\n"
	 "      -checkId  checks whether id lines match in first and second files\n"
	 "      -o <outfile> [standard output]\n"
	 "  10x option matches first16bp barcode of read 1 to whitelist.\n"
	 "  Only outputs an entry if there is a match after correcting for 1 mismatch\n") ;
  
  gzFile f1 = gzopen (argv[0], "r") ; if (!f1) die ("failed to open %s", argv[0]) ;
  gzFile f2 = 0 ;
  if (argc == 2)
    { f2 = gzopen (argv[1], "r") ; if (!f1) die ("failed to open %s", argv[0]) ; }

  char id1Buf[1024], id2Buf[1024], s1Buf[1024], s2Buf[1024], q1Buf[1024], q2Buf[1024] ;
  U32 u1s[256], u2s[256], u1q[128], u2q[128] ;
  int idLen = 0, s1Len = 0, s2Len = 0 ;
  int n = 0 ;
  while (gzReadFastq (f1, id1Buf, &idLen, s1Buf, q1Buf, &s1Len))
    { seqPack (s1Buf, u1s, s1Len) ; qualPack (q1Buf, u1q, s1Len) ;
      if (f2)
	{ if (!gzReadFastq (f2, id2Buf, &idLen, s2Buf, q2Buf, &s2Len))
	    die ("second fastq file terminated early at %d", n) ;
	  if (!isMismatchID && strcmp (id1Buf, id2Buf))
	    { fprintf (stderr, "proceeding despite paired read ids not matching, e.g. %s %s\n",
		       id1Buf, id2Buf) ;
	      isMismatchID = TRUE ;
	    }
	}
      if (table && !find10xBarcode (u1s)) continue ;
      if (f2) { seqPack (s2Buf, u2s, s2Len) ; qualPack (q2Buf, u2q, s2Len) ; }
      fwrite (u1s, 4, (s1Len+15)/16, fout) ; fwrite (u1q, 4, (s1Len+31)/32, fout) ;
      if (f2) { fwrite (u2s,4,(s2Len+15)/16,fout) ; fwrite (u2q,4,(s2Len+31)/32,fout) ;}
      ++n ;
    }
  if (f2)
    fprintf (stderr, "written %d read pairs %d + %d bp packed in %d word records\n",
	     n, s1Len, s2Len, (s1Len+15)/16 + (s1Len+31)/32 + (s2Len+15)/16 + (s2Len+31)/32) ;
  else
     fprintf (stderr, "written %d reads %d bp packed in %d word records\n",
	     n, s1Len, (s1Len+15)/16 + (s1Len+31)/32) ;
  if (table)
    { fprintf (stderr, "%d (%.1f%%) not matching barcodes were dropped\n",
	       nBad, 100.0 * nBad / (double)(nBad + n)) ;
      fprintf (stderr, "%d (%.1f%%) of those that matched were error corrected\n",
	       nFixed, 100.0 * nFixed/(double)n) ;
      fprintf (stderr, "by base position:") ;
      int i ; for (i = 0 ; i < 16 ; ++i) fprintf (stderr, " %d", nFixBase[i]) ;
      fprintf (stderr, "\n") ;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2448, %rsp           # imm = 0x2448
movq %rsi, %rbx
movl %edi, %r14d
movq 0x5915(%rip), %rax      # 0x6fd8
movq (%rax), %rax
movq %rax, 0x20(%rsp)
callq 0x130c
decl %r14d
addq $0x8, %rbx
leaq 0x2a2f(%rip), %r15      # 0x410d
leaq 0x2a2d(%rip), %r12      # 0x4112
leaq 0x2a50(%rip), %rbp      # 0x413c
cmpl $0x3, %r14d
jl 0x178f
movq (%rbx), %r13
cmpb $0x2d, (%r13)
jne 0x1798
movq %r13, %rdi
movq %r15, %rsi
callq 0x1100
testl %eax, %eax
je 0x173f
movq %r13, %rdi
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
je 0x174a
cmpb $0x6f, 0x1(%r13)
jne 0x1730
cmpb $0x0, 0x2(%r13)
je 0x1753
movq %rbp, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x3060
jmp 0x16ec
movq 0x8(%rbx), %rdi
callq 0x149f
jmp 0x1782
decl %r14d
addq $0x8, %rbx
jmp 0x16ec
movq 0x8(%rbx), %rdi
leaq 0x29bd(%rip), %rsi      # 0x411b
callq 0x1190
movq %rax, %r13
testq %rax, %rax
jne 0x177d
movq 0x8(%rbx), %rsi
leaq 0x29a8(%rip), %rdi      # 0x411e
xorl %eax, %eax
callq 0x3060
movq %r13, 0x20(%rsp)
addl $-0x2, %r14d
addq $0x10, %rbx
jmp 0x16ec
leal -0x3(%r14), %eax
cmpl $-0x3, %eax
ja 0x17a6
leaq 0x29d2(%rip), %rdi      # 0x4171
xorl %eax, %eax
callq 0x3060
movq (%rbx), %rdi
leaq 0x28dc(%rip), %rsi      # 0x408c
callq 0x1180
movq %rax, 0x38(%rsp)
testq %rax, %rax
jne 0x17d0
movq (%rbx), %rsi
leaq 0x2ba6(%rip), %rdi      # 0x436f
xorl %eax, %eax
callq 0x3060
cmpl $0x2, %r14d
jne 0x1804
movq 0x8(%rbx), %rdi
leaq 0x28ab(%rip), %rsi      # 0x408c
callq 0x1180
movq %rax, %r12
cmpq $0x0, 0x38(%rsp)
jne 0x1807
movq (%rbx), %rsi
leaq 0x2b74(%rip), %rdi      # 0x436f
xorl %eax, %eax
callq 0x3060
jmp 0x1807
xorl %r12d, %r12d
leaq 0x1c(%rsp), %rax
andl $0x0, (%rax)
leaq 0xc(%rsp), %rax
andl $0x0, (%rax)
leaq 0x8(%rsp), %rax
andl $0x0, (%rax)
leaq 0x1840(%rsp), %rbx
leaq 0x2040(%rsp), %rbp
leaq 0x1c40(%rsp), %r14
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x840(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
movq %rbx, %rcx
movq %rbp, %r8
leaq 0xc(%rsp), %r9
callq 0x1bcb
testb %al, %al
je 0x1a6c
movl 0xc(%rsp), %r13d
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0x1365
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
movl %r13d, %edx
callq 0x1427
testq %r12, %r12
je 0x1900
movq %r12, %rdi
leaq 0x440(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
leaq 0x1440(%rsp), %rcx
leaq 0x1040(%rsp), %r8
leaq 0x8(%rsp), %r9
callq 0x1bcb
testb %al, %al
jne 0x18d6
leaq 0x2ab7(%rip), %rdi      # 0x4381
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x3060
testb %r15b, %r15b
je 0x18e0
movb $0x1, %r15b
jmp 0x1900
leaq 0x840(%rsp), %rdi
leaq 0x440(%rsp), %rsi
callq 0x1100
testl %eax, %eax
jne 0x1a3f
xorl %r15d, %r15d
cmpq $0x0, 0x6050(%rip)      # 0x7958
je 0x191a
movq %r14, %rdi
callq 0x164f
testb %al, %al
je 0x1843
testq %r12, %r12
je 0x19c9
movl 0x8(%rsp), %ebx
leaq 0x1440(%rsp), %rdi
leaq 0xc40(%rsp), %rsi
movl %ebx, %edx
callq 0x1365
leaq 0x1040(%rsp), %rdi
leaq 0x240(%rsp), %rsi
movl %ebx, %edx
callq 0x1427
addl $0xf, %r13d
movl %r13d, %eax
cltd
pushq $0x10
popq %rcx
idivl %ecx
movslq %eax, %rdx
movq %r14, %rdi
pushq $0x4
popq %rsi
movq %rsi, %rbx
movq 0x20(%rsp), %r13
movq %r13, %rcx
callq 0x11e0
movl 0xc(%rsp), %eax
addl $0x1f, %eax
cltd
pushq $0x20
popq %rcx
idivl %ecx
movslq %eax, %rdx
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %r13, 0x20(%rsp)
movq %r13, %rcx
callq 0x11e0
pushq $0x10
popq %rcx
movl 0x8(%rsp), %r13d
leaq 0xc40(%rsp), %rdi
leaq 0x8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x240(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x19e6
movq %r14, %rdi
leaq 0xc(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
pushq $0x10
popq %rcx
pushq $0x4
popq %rbx
addl $0xf, %r13d
movl %r13d, %eax
cltd
idivl %ecx
movslq %eax, %rdx
movq %rbx, %rsi
movq 0x20(%rsp), %r13
movq %r13, %rcx
callq 0x11e0
movq 0x28(%rsp), %rax
movl (%rax), %eax
addl $0x1f, %eax
cltd
pushq $0x20
popq %rcx
idivl %ecx
movslq %eax, %rdx
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rcx
callq 0x11e0
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
leaq 0x1840(%rsp), %rbx
jmp 0x1843
movq 0x55b2(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x295a(%rip), %rsi      # 0x43aa
leaq 0x840(%rsp), %rdx
leaq 0x440(%rsp), %rcx
xorl %eax, %eax
callq 0x1120
jmp 0x18db
movq 0x5585(%rip), %r13      # 0x6ff8
movq (%r13), %rdi
movl 0xc(%rsp), %ecx
testq %r12, %r12
jne 0x1aad
leal 0xf(%rcx), %eax
cltd
pushq $0x10
popq %rsi
idivl %esi
movl %eax, %r8d
leal 0x1f(%rcx), %eax
cltd
pushq $0x20
popq %rsi
idivl %esi
addl %eax, %r8d
leaq 0x2984(%rip), %rsi      # 0x4423
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1120
jmp 0x1af8
movl 0x8(%rsp), %r8d
leal 0xf(%rcx), %eax
cltd
pushq $0x10
popq %r11
idivl %r11d
movl %eax, %esi
leal 0x1f(%rcx), %eax
cltd
pushq $0x20
popq %r10
idivl %r10d
addl %eax, %esi
leal 0xf(%r8), %eax
cltd
idivl %r11d
movl %eax, %r9d
leal 0x1f(%r8), %eax
cltd
idivl %r10d
addl %eax, %r9d
addl %esi, %r9d
leaq 0x28fb(%rip), %rsi      # 0x43e7
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1120
cmpq $0x0, 0x5e58(%rip)      # 0x7958
jne 0x1b16
xorl %eax, %eax
addq $0x2448, %rsp           # imm = 0x2448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rdi
movl 0x6040(%rip), %edx      # 0x7b60
cvtsi2sd %edx, %xmm0
mulsd 0x24dc(%rip), %xmm0     # 0x4008
movq 0x10(%rsp), %rbx
leal (%rdx,%rbx), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
leaq 0x2912(%rip), %rsi      # 0x4455
movb $0x1, %al
callq 0x1120
movq (%r13), %rdi
movl 0x6010(%rip), %edx      # 0x7b64
cvtsi2sd %edx, %xmm0
mulsd 0x24a8(%rip), %xmm0     # 0x4008
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm0
leaq 0x2916(%rip), %rsi      # 0x4485
movb $0x1, %al
callq 0x1120
movq (%r13), %rcx
leaq 0x293c(%rip), %rdi      # 0x44bd
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x11e0
leaq 0x5fdd(%rip), %r14      # 0x7b70
leaq 0x2d24(%rip), %rbx      # 0x48be
xorl %r15d, %r15d
movq (%r13), %rsi
cmpq $0x40, %r15
je 0x1bbe
movl (%r15,%r14), %edx
movq %rsi, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1120
addq $0x4, %r15
jmp 0x1b9d
pushq $0xa
popq %rdi
callq 0x10e0
jmp 0x1b02