// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x2460, %rsp           # imm = 0x2460
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x1, -0x11(%rbp)
movq 0x6664(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
callq 0x1310
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
xorl %eax, %eax
cmpl $0x2, -0x8(%rbp)
movb %al, -0x2455(%rbp)
jle 0x19bd
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
sete %al
movb %al, -0x2455(%rbp)
movb -0x2455(%rbp), %al
testb $0x1, %al
jne 0x19cc
jmp 0x1abf
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x3713(%rip), %rsi      # 0x50ed
callq 0x1100
cmpl $0x0, %eax
jne 0x1a0b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1620
movl -0x8(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x10(%rbp)
jmp 0x1aba
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x36d9(%rip), %rsi      # 0x50f2
callq 0x1100
cmpl $0x0, %eax
jne 0x1a3e
movb $0x1, -0x11(%rbp)
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x1ab8
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x36af(%rip), %rsi      # 0x50fb
callq 0x1100
cmpl $0x0, %eax
jne 0x1aa1
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3699(%rip), %rsi      # 0x50fe
callq 0x1190
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x1a8a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x367e(%rip), %rdi      # 0x5101
movb $0x0, %al
callq 0x4700
movl -0x8(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x10(%rbp)
jmp 0x1ab6
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x3670(%rip), %rdi      # 0x511f
movb $0x0, %al
callq 0x4700
jmp 0x1ab8
jmp 0x1aba
jmp 0x1999
cmpl $0x1, -0x8(%rbp)
jl 0x1acb
cmpl $0x2, -0x8(%rbp)
jle 0x1ad9
leaq 0x3682(%rip), %rdi      # 0x5154
movb $0x0, %al
callq 0x4700
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x3585(%rip), %rsi      # 0x506c
callq 0x1180
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1b0c
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x384d(%rip), %rdi      # 0x5352
movb $0x0, %al
callq 0x4700
movq $0x0, -0x38(%rbp)
cmpl $0x2, -0x8(%rbp)
jne 0x1b50
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3543(%rip), %rsi      # 0x506c
callq 0x1180
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1b4e
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x380b(%rip), %rdi      # 0x5352
movb $0x0, %al
callq 0x4700
jmp 0x1b50
movl $0x0, -0x2444(%rbp)
movl $0x0, -0x2448(%rbp)
movl $0x0, -0x244c(%rbp)
movl $0x0, -0x2450(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x440(%rbp), %rsi
leaq -0xc40(%rbp), %rcx
leaq -0x1440(%rbp), %r8
leaq -0x2444(%rbp), %rdx
leaq -0x2448(%rbp), %r9
callq 0x1fa0
cmpb $0x0, %al
je 0x1d8e
leaq -0xc40(%rbp), %rdi
leaq -0x1c40(%rbp), %rsi
movl -0x2448(%rbp), %edx
callq 0x13a0
leaq -0x1440(%rbp), %rdi
leaq -0x2240(%rbp), %rsi
movl -0x2448(%rbp), %edx
callq 0x1520
cmpq $0x0, -0x38(%rbp)
je 0x1c77
movq -0x38(%rbp), %rdi
leaq -0x840(%rbp), %rsi
leaq -0x1040(%rbp), %rcx
leaq -0x1840(%rbp), %r8
leaq -0x2444(%rbp), %rdx
leaq -0x244c(%rbp), %r9
callq 0x1fa0
cmpb $0x0, %al
jne 0x1c2d
movl -0x2450(%rbp), %esi
leaq 0x373e(%rip), %rdi      # 0x5364
movb $0x0, %al
callq 0x4700
cmpb $0x0, -0x21(%rbp)
jne 0x1c75
leaq -0x440(%rbp), %rdi
leaq -0x840(%rbp), %rsi
callq 0x1100
cmpl $0x0, %eax
je 0x1c75
movq 0x63a6(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq -0x440(%rbp), %rdx
leaq -0x840(%rbp), %rcx
leaq 0x3723(%rip), %rsi      # 0x538d
movb $0x0, %al
callq 0x1120
movb $0x1, -0x21(%rbp)
jmp 0x1c77
cmpq $0x0, 0x6d09(%rip)      # 0x8988
je 0x1c96
leaq -0x1c40(%rbp), %rdi
callq 0x1880
cmpb $0x0, %al
jne 0x1c96
jmp 0x1b78
cmpq $0x0, -0x38(%rbp)
je 0x1ccf
leaq -0x1040(%rbp), %rdi
leaq -0x2040(%rbp), %rsi
movl -0x244c(%rbp), %edx
callq 0x13a0
leaq -0x1840(%rbp), %rdi
leaq -0x2440(%rbp), %rsi
movl -0x244c(%rbp), %edx
callq 0x1520
leaq -0x1c40(%rbp), %rdi
movl -0x2448(%rbp), %eax
addl $0xf, %eax
movl $0x10, %ecx
cltd
idivl %ecx
movslq %eax, %rdx
movq -0x20(%rbp), %rcx
movl $0x4, %esi
callq 0x11e0
leaq -0x2240(%rbp), %rdi
movl -0x2448(%rbp), %eax
addl $0x1f, %eax
movl $0x20, %ecx
cltd
idivl %ecx
movslq %eax, %rdx
movq -0x20(%rbp), %rcx
movl $0x4, %esi
callq 0x11e0
cmpq $0x0, -0x38(%rbp)
je 0x1d7a
leaq -0x2040(%rbp), %rdi
movl -0x244c(%rbp), %eax
addl $0xf, %eax
movl $0x10, %ecx
cltd
idivl %ecx
movslq %eax, %rdx
movq -0x20(%rbp), %rcx
movl $0x4, %esi
callq 0x11e0
leaq -0x2440(%rbp), %rdi
movl -0x244c(%rbp), %eax
addl $0x1f, %eax
movl $0x20, %ecx
cltd
idivl %ecx
movslq %eax, %rdx
movq -0x20(%rbp), %rcx
movl $0x4, %esi
callq 0x11e0
movl -0x2450(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2450(%rbp)
jmp 0x1b78
cmpq $0x0, -0x38(%rbp)
je 0x1e22
movq 0x6258(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x2450(%rbp), %eax
movl %eax, -0x245c(%rbp)
movl -0x2448(%rbp), %ecx
movl -0x244c(%rbp), %r8d
movl -0x2448(%rbp), %eax
addl $0xf, %eax
movl $0x10, %esi
cltd
idivl %esi
movl %eax, %r9d
movl -0x2448(%rbp), %eax
addl $0x1f, %eax
movl $0x20, %esi
cltd
idivl %esi
addl %eax, %r9d
movl -0x244c(%rbp), %eax
addl $0xf, %eax
movl $0x10, %esi
cltd
idivl %esi
addl %eax, %r9d
movl -0x244c(%rbp), %eax
addl $0x1f, %eax
movl $0x20, %esi
cltd
idivl %esi
movl -0x245c(%rbp), %edx
addl %eax, %r9d
leaq 0x35b1(%rip), %rsi      # 0x53ca
movb $0x0, %al
callq 0x1120
jmp 0x1e7a
movq 0x61cf(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl -0x2450(%rbp), %eax
movl %eax, -0x2460(%rbp)
movl -0x2448(%rbp), %ecx
movl -0x2448(%rbp), %eax
addl $0xf, %eax
movl $0x10, %esi
cltd
idivl %esi
movl %eax, %r8d
movl -0x2448(%rbp), %eax
addl $0x1f, %eax
movl $0x20, %esi
cltd
idivl %esi
movl -0x2460(%rbp), %edx
addl %eax, %r8d
leaq 0x3593(%rip), %rsi      # 0x5406
movb $0x0, %al
callq 0x1120
cmpq $0x0, 0x6b06(%rip)      # 0x8988
je 0x1f89
movq 0x6169(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl 0x6cf8(%rip), %edx      # 0x8b90
cvtsi2sdl 0x6cf0(%rip), %xmm1 # 0x8b90
movsd 0x3160(%rip), %xmm0     # 0x5008
mulsd %xmm1, %xmm0
movl 0x6cde(%rip), %eax      # 0x8b90
addl -0x2450(%rbp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
leaq 0x3571(%rip), %rsi      # 0x5438
movb $0x1, %al
callq 0x1120
movq 0x6123(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl 0x6cb6(%rip), %edx      # 0x8b94
cvtsi2sdl 0x6cae(%rip), %xmm1 # 0x8b94
movsd 0x311a(%rip), %xmm0     # 0x5008
mulsd %xmm1, %xmm0
cvtsi2sdl -0x2450(%rbp), %xmm1
divsd %xmm1, %xmm0
leaq 0x3563(%rip), %rsi      # 0x5468
movb $0x1, %al
callq 0x1120
movq 0x60e5(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x3583(%rip), %rsi      # 0x54a0
movb $0x0, %al
callq 0x1120
movl $0x0, -0x2454(%rbp)
cmpl $0x10, -0x2454(%rbp)
jge 0x1f71
movq 0x60ba(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movslq -0x2454(%rbp), %rcx
leaq 0x6c51(%rip), %rax      # 0x8ba0
movl (%rax,%rcx,4), %edx
leaq 0x3961(%rip), %rsi      # 0x58ba
movb $0x0, %al
callq 0x1120
movl -0x2454(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2454(%rbp)
jmp 0x1f2e
movq 0x6080(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x3651(%rip), %rsi      # 0x55d3
movb $0x0, %al
callq 0x1120
movl -0x4(%rbp), %eax
addq $0x2460, %rsp           # imm = 0x2460
popq %rbp
retq
nopw %cs:(%rax,%rax)