// File: /workspace/github_c_makefile_star2_O1/jsh58[P]Genrich/Genrich.c
// Function: parseBAM
uint64_t parseBAM(gzFile in, char* line, Aln** aln,
    char* readName, int chromLen, Chrom* chrom, int n_ref,
    int idx[], double* totalLen, uint64_t* unmapped,
    uint64_t* paired, uint64_t* single, uint64_t* pairedPr,
    uint64_t* singlePr, uint64_t* supp, uint64_t* skipped,
    uint64_t* lowMapQ, int minMapQ, uint64_t* secPair,
    uint64_t* secSingle, uint64_t* orphan, bool singleOpt,
    bool extendOpt, int extend, bool avgExtOpt,
    Aln*** unpair, int* unpairMem, float asDiff,
    bool atacOpt, int atacLen5, int atacLen3, bool atacAdj,
    File bed, bool bedOpt, bool gzOut, bool ctrl,
    int sample, bool dupsOpt, File dups, bool dupsVerb,
    Read*** readPr, int* readMemPr, Read*** readDc,
    int* readMemDc, Read*** readSn, int* readMemSn,
    HashAln*** table, uint32_t* tableMem,
    HashAln*** tableSn, uint32_t* tableSnMem,
    uint32_t** order, uint32_t** order0, uint32_t** order1,
    uint32_t** order2, uint16_t** qualA, uint16_t** qual0,
    uint16_t** qual1, uint16_t** qual2, uint32_t* arrMem,
    uint64_t* countPr, uint64_t* dupsPr, uint64_t* countDc,
    uint64_t* dupsDc, uint64_t* countSn, uint64_t* dupsSn,
    uint64_t* errCount, bool verbose) {

  // BAM fields to save
  int32_t refID, pos, l_seq, next_refID, next_pos, tlen;
  uint16_t n_cigar_op, flag;
  uint8_t mapq;
  uint32_t* cigar;
  uint8_t* seq;
  char* read_name, *qual, *extra;

  int alnLen = 0;     // number of alignments for this read
  int unpairIdx = 0;  // \ indexes into unpaired array(s)
  int unpairLen = 0;  // /   (with avgExtOpt)
  int readIdxPr = 0;  // \ indexes into read array readPr
  int readLenPr = 0;  // /   (with dupsOpt)
  int readIdxDc = 0;  // \ indexes into read array readDc
  int readLenDc = 0;  // /   (with dupsOpt)
  int readIdxSn = 0;  // \ indexes into read array readSn
  int readLenSn = 0;  // /   (with dupsOpt)
  uint16_t qualR1 = 0, qualR2 = 0; // sums of quality scores
  uint64_t count = 0;
  int32_t block_size;
  while ((block_size = readInt32(in, false)) != EOF) {

    if (block_size < 6 * sizeof(int32_t) + 2 * sizeof(uint32_t))
      exit(error("", ERRBAM));  // min. guaranteed block size

    // copy alignment record
    char* block = line;
    for (int i = 0; i < block_size; i++) {
      int m = gzgetc(in);
      if (m == -1)
        exit(error("", ERRBAM));
      block[i] = m;
    }

    // save BAM fields
    loadBAMfields(&block, &refID, &pos, &mapq, &n_cigar_op,
      &flag, &l_seq, &next_refID, &next_pos, &tlen,
      &read_name, &cigar, &seq, &qual, &extra);
    if (block > line + block_size)
      exit(error("", ERRBAM));  // read off the end of the block

    count++;
    if (flag & 0x4) {
      // skip unmapped
      (*unmapped)++;
      continue;
    }
    if (! strcmp(read_name, "*")
        || refID < 0 || refID >= n_ref
        || idx[refID] < 0 || idx[refID] >= chromLen
        || pos < 0)
      // insufficient alignment info
      exit(error(read_name, ERRSAM));
    if (flag & 0xE00) {
      // skip supplementary/PCR dups/low quality
      (*supp)++;
      continue;
    }
    if (mapq < minMapQ) {
      // skip low MAPQ alignments
      (*lowMapQ)++;
      continue;
    }

    // process previous set of alns
    if (readName[0] == '\0' || strcmp(read_name, readName)) {
      if (readName[0] != '\0')
        processAlns(readName, *aln, alnLen, totalLen,
          pairedPr, singlePr, orphan, singleOpt,
          extendOpt, extend, avgExtOpt, unpair,
          &unpairIdx, &unpairLen, unpairMem, asDiff,
          atacOpt, atacLen5, atacLen3, atacAdj,
          bed, bedOpt, gzOut, ctrl, sample, dupsOpt,
          readPr, &readIdxPr, &readLenPr, readMemPr,
          readDc, &readIdxDc, &readLenDc, readMemDc,
          readSn, &readIdxSn, &readLenSn, readMemSn,
          qualR1, qualR2, errCount, verbose);
      alnLen = 0;
      qualR1 = qualR2 = 0;
      strncpy(readName, read_name, MAX_ALNS);
    }

    // save alignment information
    int length = calcDistBAM(l_seq, n_cigar_op, cigar); // distance to 3' end
    float score = getBAMscore(extra, block_size
      - (int) (extra - line));
    if (! parseAlign(aln, &alnLen, flag, chrom + idx[refID],
        pos, length, next_pos, paired, single, secPair,
        secSingle, skipped, singleOpt, score, dupsOpt,
        qual, l_seq, 0, &qualR1, &qualR2) && verbose)
      fprintf(stderr, "Warning! Read %s has more than %d alignments\n",
        read_name, MAX_ALNS);
    // NOTE: the following BAM fields are ignored:
    //   next_refID, tlen, seq
  }

  // process last set of alns
  if (readName[0] != '\0')
    processAlns(readName, *aln, alnLen, totalLen,
      pairedPr, singlePr, orphan, singleOpt,
      extendOpt, extend, avgExtOpt, unpair,
      &unpairIdx, &unpairLen, unpairMem, asDiff,
      atacOpt, atacLen5, atacLen3, atacAdj,
      bed, bedOpt, gzOut, ctrl, sample, dupsOpt,
      readPr, &readIdxPr, &readLenPr, readMemPr,
      readDc, &readIdxDc, &readLenDc, readMemDc,
      readSn, &readIdxSn, &readLenSn, readMemSn,
      qualR1, qualR2, errCount, verbose);

  if (dupsOpt)
    // remove duplicates and process all alignments
    findDups(*readPr, readIdxPr, readLenPr, *readDc,
      readIdxDc, readLenDc, *readSn, readIdxSn, readLenSn,
      table, tableMem, tableSn, tableSnMem, order, order0,
      order1, order2, qualA, qual0, qual1, qual2, arrMem,
      countPr, dupsPr, countDc, dupsDc, countSn, dupsSn,
      singleOpt, pairedPr, singlePr, totalLen, extendOpt,
      extend, avgExtOpt, asDiff, atacOpt, atacLen5,
      atacLen3, atacAdj, bed, bedOpt, gzOut, dups,
      dupsVerb, ctrl, sample, errCount, verbose);

  else if (avgExtOpt)
    // process single alignments w/ avgExtOpt
    processAvgExt(*unpair, unpairIdx, unpairLen,
      *totalLen, *pairedPr, bed, bedOpt, gzOut,
      ctrl, sample, errCount, verbose);

  return count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movss %xmm0, 0xc(%rsp)
movq %r9, 0xc0(%rsp)
movl %r8d, 0xa4(%rsp)
movq %rcx, 0x60(%rsp)
movq %rdx, 0x90(%rsp)
movq %rsi, %rbp
movq %rdi, %r15
movb 0x308(%rsp), %r12b
movb 0x1b0(%rsp), %bl
movb 0x1a8(%rsp), %r13b
xorl %eax, %eax
movl %eax, 0x8(%rsp)
movl %eax, 0x2c(%rsp)
movl %eax, 0x28(%rsp)
movl %eax, 0x24(%rsp)
movl %eax, 0x20(%rsp)
movl %eax, 0x1c(%rsp)
movl %eax, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
movw %ax, 0x4(%rsp)
movw %ax, 0x2(%rsp)
xorl %esi, %esi
callq 0xd8bb
movl %eax, %r14d
movzbl %r12b, %eax
movq %rax, 0x38(%rsp)
movzbl 0x220(%rsp), %eax
movq %rax, 0x88(%rsp)
movzbl 0x210(%rsp), %eax
movq %rax, 0x48(%rsp)
movzbl 0x208(%rsp), %eax
movq %rax, 0x50(%rsp)
movzbl 0x200(%rsp), %eax
movq %rax, 0x40(%rsp)
movzbl 0x1f0(%rsp), %eax
movq %rax, 0x68(%rsp)
movzbl 0x1d8(%rsp), %eax
movq %rax, 0x70(%rsp)
movzbl 0x1c0(%rsp), %eax
movq %rax, 0x78(%rsp)
movzbl %bl, %eax
movq %rax, 0x80(%rsp)
movzbl %r13b, %eax
movq %rax, 0x58(%rsp)
cmpl $-0x1, %r14d
je 0xe401
xorb $0x1, %r12b
xorl %r13d, %r13d
movb %r12b, 0x6(%rsp)
cmpl $0x1f, %r14d
jbe 0xe752
movslq %r14d, %rbx
movq %rbp, 0xb8(%rsp)
testl %r14d, %r14d
jle 0xdfb9
xorl %r12d, %r12d
movl (%r15), %eax
testl %eax, %eax
je 0xdf9b
decl %eax
movl %eax, (%r15)
incq 0x10(%r15)
movq 0x8(%r15), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r15)
movzbl (%rax), %eax
jmp 0xdfa3
movq %r15, %rdi
callq 0x20a0
cmpl $-0x1, %eax
je 0xe745
movb %al, (%rbp,%r12)
incq %r12
cmpq %r12, %rbx
jne 0xdf7a
subq $0x8, %rsp
leaq 0xc0(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
leaq 0xf(%rsp), %rcx
leaq 0x3a(%rsp), %r8
leaq 0x38(%rsp), %r9
leaq 0xd0(%rsp), %rax
pushq %rax
leaq 0xe0(%rsp), %rax
pushq %rax
leaq 0x108(%rsp), %rax
pushq %rax
leaq 0x100(%rsp), %rax
pushq %rax
leaq 0x100(%rsp), %rax
pushq %rax
leaq 0x118(%rsp), %rax
pushq %rax
leaq 0xe0(%rsp), %rax
pushq %rax
leaq 0x12c(%rsp), %rax
pushq %rax
leaq 0xf4(%rsp), %rax
pushq %rax
callq 0xd953
addq $0x50, %rsp
addq %rbp, %rbx
cmpq %rbx, 0xb8(%rsp)
ja 0xe757
movzwl 0x30(%rsp), %eax
testb $0x4, %al
jne 0xe0de
movl %eax, 0x34(%rsp)
movq 0xd8(%rsp), %r12
movq %r12, %rdi
leaq 0x5445(%rip), %rsi      # 0x134b7
callq 0x21b0
testl %eax, %eax
je 0xe74a
movl 0xb4(%rsp), %ebx
testl %ebx, %ebx
js 0xe74a
cmpl 0x130(%rsp), %ebx
jge 0xe74a
movq 0x138(%rsp), %rax
movl (%rax,%rbx,4), %eax
testl %eax, %eax
js 0xe74a
cmpl 0xa4(%rsp), %eax
jge 0xe74a
movl 0xb0(%rsp), %ecx
testl %ecx, %ecx
js 0xe74a
testl $0xe00, 0x34(%rsp)      # imm = 0xE00
je 0xe107
movq 0x170(%rsp), %rax
jmp 0xe0e6
movq 0x148(%rsp), %rax
incq (%rax)
incq %r13
movq %r15, %rdi
xorl %esi, %esi
callq 0xd8bb
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0xdf5d
jmp 0xe404
movzbl 0x7(%rsp), %eax
cmpl 0x188(%rsp), %eax
jge 0xe11f
movq 0x180(%rsp), %rax
jmp 0xe0e6
movl %ecx, 0xa0(%rsp)
movq %r12, 0x98(%rsp)
movq 0x60(%rsp), %rcx
movb (%rcx), %r12b
testb %r12b, %r12b
je 0xe2a7
movq 0x98(%rsp), %rdi
movq %rcx, %rsi
callq 0x21b0
testl %eax, %eax
je 0xe2d0
testb %r12b, %r12b
movq 0x60(%rsp), %rcx
je 0xe2a7
movq 0x90(%rsp), %rax
movq (%rax), %rsi
movl 0x8(%rsp), %edx
movzwl 0x4(%rsp), %eax
movzwl 0x2(%rsp), %r10d
subq $0x8, %rsp
movq %rcx, %rdi
movq 0x148(%rsp), %rcx
movq 0x168(%rsp), %r8
movq 0x170(%rsp), %r9
movss 0x14(%rsp), %xmm0
pushq 0x40(%rsp)
pushq 0x310(%rsp)
pushq %r10
pushq %rax
pushq 0x288(%rsp)
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x4c(%rsp), %rax
pushq %rax
pushq 0x298(%rsp)
pushq 0x298(%rsp)
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x74(%rsp), %rax
pushq %rax
pushq 0x2a8(%rsp)
pushq 0x2a8(%rsp)
leaq 0x90(%rsp), %rax
pushq %rax
leaq 0x9c(%rsp), %rax
pushq %rax
pushq 0x2b8(%rsp)
pushq 0x110(%rsp)
movl 0x2a8(%rsp), %eax
pushq %rax
pushq 0xe0(%rsp)
pushq 0xf0(%rsp)
pushq 0xe8(%rsp)
pushq 0x2a8(%rsp)
pushq 0x120(%rsp)
movl 0x2a8(%rsp), %eax
pushq %rax
movl 0x2a8(%rsp), %eax
pushq %rax
pushq 0x140(%rsp)
pushq 0x2a8(%rsp)
leaq 0x108(%rsp), %rax
pushq %rax
leaq 0x114(%rsp), %rax
pushq %rax
pushq 0x2b8(%rsp)
pushq 0x170(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0x188(%rsp)
pushq 0x168(%rsp)
pushq 0x2b8(%rsp)
callq 0x9d9a
movq 0x180(%rsp), %rcx
addq $0x120, %rsp            # imm = 0x120
movl $0x0, 0x8(%rsp)
xorl %eax, %eax
movw %ax, 0x2(%rsp)
movw %ax, 0x4(%rsp)
movl $0x80, %edx
movq %rcx, %rdi
movq 0x98(%rsp), %rsi
callq 0x2050
movl 0xac(%rsp), %r8d
movzwl 0x32(%rsp), %eax
movl %r8d, %r12d
testq %rax, %rax
je 0xe31c
movq 0xe0(%rsp), %rcx
xorl %edx, %edx
movl %r8d, %r12d
movl (%rcx,%rdx,4), %esi
movl %esi, %edi
andl $0xf, %edi
shrl $0x4, %esi
cmpl $0x4, %edi
je 0xe30c
cmpl $0x2, %edi
je 0xe311
cmpl $0x1, %edi
jne 0xe314
subl %esi, %r12d
jmp 0xe314
addl %esi, %r12d
incq %rdx
cmpq %rdx, %rax
jne 0xe2f2
movq 0xc8(%rsp), %rdi
movl %ebp, %eax
subl %edi, %eax
addl %eax, %r14d
movl %r14d, %esi
movq %r8, %r14
callq 0xdbb3
movq 0x138(%rsp), %rax
movslq (%rax,%rbx,4), %rax
imulq $0x68, %rax, %rcx
addq 0xc0(%rsp), %rcx
movl 0xa8(%rsp), %eax
subq $0x8, %rsp
movq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl 0x3c(%rsp), %edx
movl 0xa8(%rsp), %r8d
movl %r12d, %r9d
leaq 0xa(%rsp), %r10
pushq %r10
leaq 0x14(%rsp), %r10
pushq %r10
pushq $0x0
pushq %r14
pushq 0xf8(%rsp)
pushq 0xb8(%rsp)
pushq 0x90(%rsp)
pushq 0x1b8(%rsp)
pushq 0x1e0(%rsp)
pushq 0x1e0(%rsp)
pushq 0x1b0(%rsp)
pushq 0x1b0(%rsp)
pushq %rax
callq 0xc396
addq $0x70, %rsp
movb 0x6(%rsp), %r12b
orb %r12b, %al
jne 0xe0e9
movq 0xbc1a(%rip), %rax      # 0x19ff8
movq (%rax), %rdi
leaq 0x579c(%rip), %rsi      # 0x13b84
movq 0x98(%rsp), %rdx
movl $0x80, %ecx
xorl %eax, %eax
callq 0x21d0
jmp 0xe0e9
xorl %r13d, %r13d
movq 0x60(%rsp), %rdi
cmpb $0x0, (%rdi)
je 0xe54e
movq 0x90(%rsp), %rax
movq (%rax), %rsi
movl 0x8(%rsp), %edx
movzwl 0x4(%rsp), %r12d
movq %r13, %r10
movzwl 0x2(%rsp), %r13d
subq $0x8, %rsp
leaq 0x18(%rsp), %rbp
leaq 0x1c(%rsp), %rax
leaq 0x20(%rsp), %r15
leaq 0x24(%rsp), %r14
leaq 0x28(%rsp), %rbx
leaq 0x2c(%rsp), %r11
movq 0x148(%rsp), %rcx
movq 0x168(%rsp), %r8
movq 0x170(%rsp), %r9
movss 0x14(%rsp), %xmm0
pushq 0x40(%rsp)
pushq 0x310(%rsp)
pushq %r13
movq %r10, %r13
pushq %r12
pushq 0x288(%rsp)
pushq %rbp
pushq %rax
pushq 0x298(%rsp)
pushq 0x298(%rsp)
pushq %r15
pushq %r14
pushq 0x2a8(%rsp)
pushq 0x2a8(%rsp)
pushq %rbx
pushq %r11
pushq 0x2b8(%rsp)
pushq 0x110(%rsp)
movl 0x2a8(%rsp), %eax
pushq %rax
pushq 0xe0(%rsp)
pushq 0xf0(%rsp)
pushq 0xe8(%rsp)
pushq 0x2a8(%rsp)
pushq 0x120(%rsp)
movl 0x2a8(%rsp), %eax
pushq %rax
movl 0x2a8(%rsp), %eax
pushq %rax
pushq 0x140(%rsp)
pushq 0x2a8(%rsp)
leaq 0x108(%rsp), %rax
pushq %rax
leaq 0x114(%rsp), %rax
pushq %rax
pushq 0x2b8(%rsp)
pushq 0x170(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0x188(%rsp)
pushq 0x168(%rsp)
pushq 0x2b8(%rsp)
callq 0x9d9a
addq $0x120, %rsp            # imm = 0x120
cmpb $0x0, 0x220(%rsp)
je 0xe6c7
movb 0x230(%rsp), %r11b
movq 0x238(%rsp), %rax
movq (%rax), %rdi
movl 0x24(%rsp), %esi
movl 0x20(%rsp), %edx
movq 0x248(%rsp), %rax
movq (%rax), %rcx
movl 0x1c(%rsp), %r8d
movl 0x18(%rsp), %r9d
movl 0x14(%rsp), %eax
movl 0x10(%rsp), %r10d
movzbl %r11b, %r11d
movss 0xc(%rsp), %xmm0
pushq 0x38(%rsp)
pushq 0x308(%rsp)
movl 0x228(%rsp), %ebx
pushq %rbx
pushq 0x60(%rsp)
pushq %r11
pushq 0x250(%rsp)
pushq 0x80(%rsp)
pushq 0x78(%rsp)
pushq 0x238(%rsp)
pushq 0xb0(%rsp)
movl 0x238(%rsp), %r11d
pushq %r11
movl 0x238(%rsp), %r11d
pushq %r11
pushq 0xd0(%rsp)
pushq 0xe0(%rsp)
movl 0x228(%rsp), %r11d
pushq %r11
pushq 0xf8(%rsp)
pushq 0x1c0(%rsp)
pushq 0x1f0(%rsp)
pushq 0x1f0(%rsp)
pushq 0xf0(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq 0x398(%rsp)
pushq %r10
pushq %rax
movq 0x3a0(%rsp), %rax
pushq (%rax)
callq 0xbb44
addq $0x150, %rsp            # imm = 0x150
jmp 0xe730
cmpb $0x0, 0x1c0(%rsp)
je 0xe730
movq 0x1c8(%rsp), %rax
movq (%rax), %rdi
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
movq 0x140(%rsp), %rax
movsd (%rax), %xmm0
movq 0x160(%rsp), %rax
movq (%rax), %rcx
subq $0x8, %rsp
movq 0x200(%rsp), %r8
movq 0x48(%rsp), %r9
pushq 0x40(%rsp)
pushq 0x310(%rsp)
movl 0x230(%rsp), %eax
pushq %rax
pushq 0x68(%rsp)
pushq 0x78(%rsp)
callq 0x8b22
addq $0x30, %rsp
movq %r13, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x30e4
movq %r12, %rdi
callq 0x30bb
callq 0x30fc
callq 0x30cc