// File: /workspace/github_c_makefile_star2_O3/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 0x5716(%rip), %rax      # 0x6fd8
movq (%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x164c
leal -0x1(%r14), %r12d
addq $0x8, %rbx
cmpl $0x4, %r14d
jl 0x1991
leaq 0x2855(%rip), %r14      # 0x413d
leaq 0x2853(%rip), %r15      # 0x4142
leaq 0x2876(%rip), %rbp      # 0x416c
movq (%rbx), %r13
cmpb $0x2d, (%r13)
jne 0x199b
movq %r13, %rdi
movq %r14, %rsi
callq 0x1100
testl %eax, %eax
je 0x1976
movq %r13, %rdi
movq %r15, %rsi
callq 0x1100
testl %eax, %eax
je 0x193f
cmpb $0x6f, 0x1(%r13)
jne 0x1930
cmpb $0x0, 0x2(%r13)
je 0x1948
movq %rbp, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x3244
jmp 0x1987
decl %r12d
addq $0x8, %rbx
jmp 0x1987
movq 0x8(%rbx), %rdi
leaq 0x27f8(%rip), %rsi      # 0x414b
callq 0x1190
movq %rax, 0x18(%rsp)
testq %rax, %rax
jne 0x197f
movq 0x8(%rbx), %rsi
leaq 0x27e1(%rip), %rdi      # 0x414e
xorl %eax, %eax
callq 0x3244
jmp 0x197f
movq 0x8(%rbx), %rdi
callq 0x1220
addl $-0x2, %r12d
addq $0x10, %rbx
cmpl $0x2, %r12d
jg 0x18f6
leal -0x3(%r12), %eax
cmpl $-0x3, %eax
ja 0x19a9
leaq 0x27ff(%rip), %rdi      # 0x41a1
xorl %eax, %eax
callq 0x3244
movq (%rbx), %rdi
leaq 0x2709(%rip), %rsi      # 0x40bc
callq 0x1180
movq %rax, 0x28(%rsp)
testq %rax, %rax
jne 0x1a02
movq (%rbx), %rsi
leaq 0x29d3(%rip), %rdi      # 0x439f
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x3244
cmpl $0x2, %r12d
jne 0x1a20
movq 0x8(%rbx), %rdi
leaq 0x26d5(%rip), %rsi      # 0x40bc
callq 0x1180
movq %rax, %r15
movq (%rbx), %rsi
leaq 0x29a6(%rip), %rdi      # 0x439f
xorl %eax, %eax
callq 0x3244
jmp 0x1a20
cmpl $0x2, %r12d
jne 0x1a1d
movq 0x8(%rbx), %rdi
leaq 0x26a9(%rip), %rsi      # 0x40bc
callq 0x1180
movq %rax, %r15
jmp 0x1a20
xorl %r15d, %r15d
xorl %r12d, %r12d
leaq 0xc(%rsp), %rax
movl %r12d, (%rax)
leaq 0x8(%rsp), %rax
movl %r12d, (%rax)
leaq 0x4(%rsp), %rax
movl %r12d, (%rax)
leaq 0x2030(%rsp), %rbp
leaq 0x1c30(%rsp), %r13
leaq 0x1830(%rsp), %rbx
movl $0x0, (%rsp)
movq 0x28(%rsp), %rdi
leaq 0x830(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
leaq 0x8(%rsp), %r9
callq 0x1cd9
testb %al, %al
je 0x1c82
movl 0x8(%rsp), %r14d
movq %rbp, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x16a8
movq %r13, %rdi
leaq 0x30(%rsp), %rsi
movl %r14d, %edx
callq 0x179e
testq %r15, %r15
je 0x1b15
movq %r15, %rdi
leaq 0x430(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x1430(%rsp), %rcx
leaq 0x1030(%rsp), %r8
leaq 0x4(%rsp), %r9
callq 0x1cd9
testb %al, %al
jne 0x1aeb
leaq 0x28d0(%rip), %rdi      # 0x43b1
movl (%rsp), %esi
xorl %eax, %eax
callq 0x3244
testb %r12b, %r12b
je 0x1af5
movb $0x1, %r12b
jmp 0x1b15
leaq 0x830(%rsp), %rdi
leaq 0x430(%rsp), %rsi
callq 0x1100
testl %eax, %eax
jne 0x1c55
xorl %r12d, %r12d
cmpq $0x0, 0x5e3b(%rip)      # 0x7958
je 0x1b2f
movq %rbx, %rdi
callq 0x184c
testb %al, %al
je 0x1a5a
testq %r15, %r15
je 0x1be5
movl 0x4(%rsp), %edx
movl %edx, 0x10(%rsp)
leaq 0x1430(%rsp), %rdi
leaq 0xc30(%rsp), %rsi
callq 0x16a8
leaq 0x1030(%rsp), %rdi
leaq 0x230(%rsp), %rsi
movl 0x10(%rsp), %edx
callq 0x179e
leal 0xf(%r14), %eax
addl $0x1e, %r14d
testl %eax, %eax
cmovnsl %eax, %r14d
sarl $0x4, %r14d
movslq %r14d, %rdx
movl $0x4, %esi
movq %rbx, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rcx
callq 0x11e0
movl 0x8(%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 %r14, 0x18(%rsp)
movq %r14, %rcx
callq 0x11e0
movl 0x4(%rsp), %r14d
leaq 0xc30(%rsp), %rdi
leaq 0x4(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x230(%rsp), %rax
jmp 0x1bf7
movq %rbx, %rdi
leaq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
leal 0xf(%r14), %eax
addl $0x1e, %r14d
testl %eax, %eax
cmovnsl %eax, %r14d
sarl $0x4, %r14d
movslq %r14d, %rdx
movl $0x4, %esi
movq 0x18(%rsp), %r14
movq %r14, %rcx
callq 0x11e0
movq 0x20(%rsp), %rax
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 0x10(%rsp), %rdi
movq %r14, %rcx
callq 0x11e0
incl (%rsp)
jmp 0x1a5a
movq 0x539c(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2774(%rip), %rsi      # 0x43da
leaq 0x830(%rsp), %rdx
leaq 0x430(%rsp), %rcx
xorl %eax, %eax
callq 0x1120
jmp 0x1af0
movq 0x536f(%rip), %rax      # 0x6ff8
movq (%rax), %rdx
movl 0x8(%rsp), %esi
testq %r15, %r15
jne 0x1ca4
movl %esi, %edi
movq %rdx, %rsi
movl (%rsp), %edx
callq 0x142a
jmp 0x1cb1
leaq 0x4(%rsp), %rdi
movl (%rsp), %ecx
callq 0x13d4
cmpq $0x0, 0x5c9f(%rip)      # 0x7958
jne 0x1ccf
xorl %eax, %eax
addq $0x2438, %rsp           # imm = 0x2438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%rsp), %edi
callq 0x1467
jmp 0x1cbb