// File: /workspace/github_c_makefile_star2_O1/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 $0x2438, %rsp           # imm = 0x2438
movq %rsi, %rbx
movl %edi, %r14d
movq 0x570d(%rip), %rax      # 0x6fd8
movq (%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x165c
leal -0x1(%r14), %ebp
addq $0x8, %rbx
cmpl $0x4, %r14d
jl 0x199c
leaq 0x281c(%rip), %r14      # 0x410d
leaq 0x281a(%rip), %r15      # 0x4112
leaq 0x281c(%rip), %r12      # 0x411b
movq (%rbx), %r13
cmpb $0x2d, (%r13)
jne 0x199c
movq %r13, %rdi
movq %r14, %rsi
callq 0x1100
testl %eax, %eax
je 0x1983
movq %r13, %rdi
movq %r15, %rsi
callq 0x1100
testl %eax, %eax
je 0x194d
movq %r13, %rdi
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
je 0x1955
leaq 0x27fe(%rip), %rdi      # 0x413f
movq %r13, %rsi
xorl %eax, %eax
callq 0x3264
jmp 0x1993
decl %ebp
addq $0x8, %rbx
jmp 0x1993
movq 0x8(%rbx), %rdi
leaq 0x27be(%rip), %rsi      # 0x411e
callq 0x1190
movq %rax, 0x18(%rsp)
testq %rax, %rax
jne 0x198c
movq 0x8(%rbx), %rsi
leaq 0x27a7(%rip), %rdi      # 0x4121
xorl %eax, %eax
callq 0x3264
jmp 0x198c
movq 0x8(%rbx), %rdi
callq 0x1220
addl $-0x2, %ebp
addq $0x10, %rbx
cmpl $0x2, %ebp
jg 0x18ff
leal -0x3(%rbp), %eax
cmpl $-0x3, %eax
ja 0x19b2
leaq 0x27c9(%rip), %rdi      # 0x4174
xorl %eax, %eax
callq 0x3264
movq (%rbx), %rdi
leaq 0x26d0(%rip), %rsi      # 0x408c
callq 0x1180
movq %rax, 0x28(%rsp)
testq %rax, %rax
jne 0x19dc
movq (%rbx), %rsi
leaq 0x299d(%rip), %rdi      # 0x4372
xorl %eax, %eax
callq 0x3264
xorl %r14d, %r14d
cmpl $0x2, %ebp
jne 0x1a12
movq 0x8(%rbx), %rdi
leaq 0x269d(%rip), %rsi      # 0x408c
callq 0x1180
movq %rax, %r15
cmpq $0x0, 0x28(%rsp)
jne 0x1a15
movq (%rbx), %rsi
leaq 0x2969(%rip), %rdi      # 0x4372
xorl %eax, %eax
callq 0x3264
jmp 0x1a15
xorl %r15d, %r15d
leaq 0x14(%rsp), %rax
movl %r14d, (%rax)
leaq 0x10(%rsp), %rax
movl %r14d, (%rax)
leaq 0xc(%rsp), %rax
movl %r14d, (%rax)
leaq 0x2030(%rsp), %rbp
leaq 0x1c30(%rsp), %r13
leaq 0x1830(%rsp), %rbx
xorl %r14d, %r14d
movl $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x830(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
leaq 0x10(%rsp), %r9
callq 0x1cd5
testb %al, %al
je 0x1c7b
movl 0x10(%rsp), %r12d
movq %rbp, %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x16b3
movq %r13, %rdi
leaq 0x30(%rsp), %rsi
movl %r12d, %edx
callq 0x17a6
testq %r15, %r15
je 0x1b0c
movq %r15, %rdi
leaq 0x430(%rsp), %rsi
leaq 0x14(%rsp), %rdx
leaq 0x1430(%rsp), %rcx
leaq 0x1030(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x1cd5
testb %al, %al
jne 0x1ae2
leaq 0x28ad(%rip), %rdi      # 0x4384
movl 0x8(%rsp), %esi
xorl %eax, %eax
callq 0x3264
testb %r14b, %r14b
je 0x1aec
movb $0x1, %r14b
jmp 0x1b0c
leaq 0x830(%rsp), %rdi
leaq 0x430(%rsp), %rsi
callq 0x1100
testl %eax, %eax
jne 0x1c4e
xorl %r14d, %r14d
cmpq $0x0, 0x5e44(%rip)      # 0x7958
je 0x1b26
movq %rbx, %rdi
callq 0x184d
testb %al, %al
je 0x1a50
testq %r15, %r15
je 0x1bf0
movl 0xc(%rsp), %edx
movl %edx, 0x24(%rsp)
leaq 0x1430(%rsp), %rdi
leaq 0xc30(%rsp), %rsi
callq 0x16b3
leaq 0x1030(%rsp), %rdi
leaq 0x230(%rsp), %rsi
movl 0x24(%rsp), %edx
callq 0x17a6
leal 0xf(%r12), %eax
addl $0x1e, %r12d
testl %eax, %eax
cmovnsl %eax, %r12d
sarl $0x4, %r12d
movslq %r12d, %rdx
movl $0x4, %esi
movq %rbx, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rcx
callq 0x11e0
movl 0x10(%rsp), %eax
leal 0x1f(%rax), %ecx
addl $0x3e, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x5, %eax
movslq %eax, %rdx
movl $0x4, %esi
leaq 0x30(%rsp), %rdi
movq %r12, %rcx
callq 0x11e0
movl 0xc(%rsp), %eax
leal 0xf(%rax), %ecx
addl $0x1e, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x4, %eax
movslq %eax, %rdx
movl $0x4, %esi
leaq 0xc30(%rsp), %rdi
movq %r12, %rcx
callq 0x11e0
leaq 0xc(%rsp), %rax
leaq 0x230(%rsp), %rdi
jmp 0x1c25
leal 0xf(%r12), %eax
addl $0x1e, %r12d
testl %eax, %eax
cmovnsl %eax, %r12d
sarl $0x4, %r12d
movslq %r12d, %rdx
movl $0x4, %esi
movq %rbx, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rcx
callq 0x11e0
leaq 0x10(%rsp), %rax
leaq 0x30(%rsp), %rdi
movl (%rax), %eax
leal 0x1f(%rax), %ecx
addl $0x3e, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x5, %eax
movslq %eax, %rdx
movl $0x4, %esi
movq %r12, %rcx
callq 0x11e0
incl 0x8(%rsp)
jmp 0x1a50
movq 0x53a3(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x274e(%rip), %rsi      # 0x43ad
leaq 0x830(%rsp), %rdx
leaq 0x430(%rsp), %rcx
xorl %eax, %eax
callq 0x1120
jmp 0x1ae7
movq 0x5376(%rip), %rax      # 0x6ff8
movq (%rax), %rdx
movl 0x10(%rsp), %esi
testq %r15, %r15
jne 0x1c9e
movl %esi, %edi
movq %rdx, %rsi
movl 0x8(%rsp), %edx
callq 0x1431
jmp 0x1cac
leaq 0xc(%rsp), %rdi
movl 0x8(%rsp), %ecx
callq 0x13db
cmpq $0x0, 0x5ca4(%rip)      # 0x7958
jne 0x1cca
xorl %eax, %eax
addq $0x2438, %rsp           # imm = 0x2438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%rsp), %edi
callq 0x146e
jmp 0x1cb6