// File: /workspace/github_c_makefile_star2_O3/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 $0x108, %rsp            # imm = 0x108
movss %xmm0, 0x18(%rsp)
movq %r9, 0xd0(%rsp)
movl %r8d, 0xac(%rsp)
movq %rcx, 0x98(%rsp)
movq %rdx, 0x90(%rsp)
movq %rsi, %rbp
movq %rdi, %r15
movb 0x318(%rsp), %r14b
movb 0x1e8(%rsp), %r12b
movb 0x1c0(%rsp), %bl
movb 0x1b8(%rsp), %r13b
xorl %eax, %eax
movl %eax, 0x14(%rsp)
movl %eax, 0x38(%rsp)
movl %eax, 0x34(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
movl %eax, 0x28(%rsp)
movl %eax, 0x24(%rsp)
movl %eax, 0x20(%rsp)
movl %eax, 0x1c(%rsp)
movw %ax, 0x10(%rsp)
movw %ax, 0xe(%rsp)
xorl %esi, %esi
callq 0xd9a7
movl %r14d, %ecx
movl %eax, %r14d
movzbl %cl, %eax
movq %rax, 0x40(%rsp)
movzbl 0x230(%rsp), %eax
movq %rax, 0x88(%rsp)
movzbl 0x220(%rsp), %eax
movq %rax, 0x50(%rsp)
movzbl 0x218(%rsp), %eax
movq %rax, 0x58(%rsp)
movzbl 0x210(%rsp), %eax
movq %rax, 0x48(%rsp)
movzbl 0x200(%rsp), %eax
movq %rax, 0x68(%rsp)
movzbl %r12b, %eax
movq %rax, 0x70(%rsp)
movzbl 0x1d0(%rsp), %eax
movq %rax, 0x78(%rsp)
movzbl %bl, %eax
movq %rax, 0x80(%rsp)
movzbl %r13b, %eax
movq %rax, 0x60(%rsp)
cmpl $-0x1, %r14d
je 0xe422
xorb $0x1, %cl
movb %cl, 0x12(%rsp)
xorl %r13d, %r13d
cmpl $0x1f, %r14d
jbe 0xe77c
movslq %r14d, %rbx
movq %rbp, 0xc8(%rsp)
testl %r14d, %r14d
jle 0xdfec
xorl %r12d, %r12d
movl (%r15), %eax
testl %eax, %eax
je 0xdfce
decl %eax
movl %eax, (%r15)
incq 0x10(%r15)
movq 0x8(%r15), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r15)
movzbl (%rax), %eax
jmp 0xdfdf
movq %r15, %rdi
callq 0x20a0
cmpl $-0x1, %eax
je 0xe76f
movb %al, (%rbp,%r12)
incq %r12
cmpq %r12, %rbx
jne 0xdfad
subq $0x8, %rsp
leaq 0xd0(%rsp), %rdi
leaq 0xc4(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
leaq 0x1b(%rsp), %rcx
leaq 0x46(%rsp), %r8
leaq 0x44(%rsp), %r9
leaq 0xe0(%rsp), %rax
pushq %rax
leaq 0xf0(%rsp), %rax
pushq %rax
leaq 0x118(%rsp), %rax
pushq %rax
leaq 0x110(%rsp), %rax
pushq %rax
leaq 0x110(%rsp), %rax
pushq %rax
leaq 0x128(%rsp), %rax
pushq %rax
leaq 0xe8(%rsp), %rax
pushq %rax
leaq 0x13c(%rsp), %rax
pushq %rax
leaq 0xfc(%rsp), %rax
pushq %rax
callq 0xda38
addq $0x50, %rsp
addq %rbp, %rbx
cmpq %rbx, 0xc8(%rsp)
ja 0xe781
movzwl 0x3c(%rsp), %ecx
testb $0x4, %cl
jne 0xe107
movq 0xe8(%rsp), %rsi
movb $0x1, %al
cmpb $0x2a, (%rsi)
jne 0xe0a2
cmpb $0x0, 0x1(%rsi)
setne %al
testb %al, %al
je 0xe774
movl 0xbc(%rsp), %ebx
testl %ebx, %ebx
js 0xe774
cmpl 0x140(%rsp), %ebx
jge 0xe774
movq 0x148(%rsp), %rax
movl (%rax,%rbx,4), %eax
testl %eax, %eax
js 0xe774
cmpl 0xac(%rsp), %eax
jge 0xe774
movl 0xb8(%rsp), %edx
testl %edx, %edx
js 0xe774
testl $0xe00, %ecx            # imm = 0xE00
je 0xe130
movq 0x180(%rsp), %rax
jmp 0xe10f
movq 0x158(%rsp), %rax
incq (%rax)
incq %r13
movq %r15, %rdi
xorl %esi, %esi
callq 0xd9a7
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0xdf90
jmp 0xe425
movzbl 0x13(%rsp), %eax
cmpl 0x198(%rsp), %eax
jge 0xe148
movq 0x190(%rsp), %rax
jmp 0xe10f
movl %edx, 0xa4(%rsp)
movl %ecx, 0xa8(%rsp)
movq 0x98(%rsp), %r12
cmpb $0x0, (%r12)
movq %rsi, 0xc0(%rsp)
je 0xe2d1
movq %rsi, %rdi
movq %r12, %rsi
callq 0x21b0
testl %eax, %eax
je 0xe2f2
movq 0x90(%rsp), %rax
movq (%rax), %rsi
movl 0x14(%rsp), %edx
movzwl 0x10(%rsp), %eax
movzwl 0xe(%rsp), %r10d
subq $0x8, %rsp
movq 0xa0(%rsp), %r12
movq %r12, %rdi
movq 0x158(%rsp), %rcx
movq 0x178(%rsp), %r8
movq 0x180(%rsp), %r9
movss 0x20(%rsp), %xmm0
pushq 0x48(%rsp)
pushq 0x320(%rsp)
pushq %r10
pushq %rax
pushq 0x298(%rsp)
leaq 0x4c(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
pushq 0x2a8(%rsp)
pushq 0x2a8(%rsp)
leaq 0x74(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
pushq 0x2b8(%rsp)
pushq 0x2b8(%rsp)
leaq 0x9c(%rsp), %rax
pushq %rax
leaq 0xa8(%rsp), %rax
pushq %rax
pushq 0x2c8(%rsp)
pushq 0x110(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0xe8(%rsp)
pushq 0xf8(%rsp)
pushq 0xf0(%rsp)
pushq 0x2b8(%rsp)
pushq 0x120(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0x140(%rsp)
pushq 0x2b8(%rsp)
leaq 0x114(%rsp), %rax
pushq %rax
leaq 0x120(%rsp), %rax
pushq %rax
pushq 0x2c8(%rsp)
pushq 0x170(%rsp)
movl 0x2c8(%rsp), %eax
pushq %rax
pushq 0x188(%rsp)
pushq 0x170(%rsp)
pushq 0x2c8(%rsp)
callq 0x9f32
addq $0x120, %rsp            # imm = 0x120
movq 0xc0(%rsp), %rsi
movl $0x0, 0x14(%rsp)
xorl %eax, %eax
movw %ax, 0xe(%rsp)
movw %ax, 0x10(%rsp)
movl $0x80, %edx
movq %r12, %rdi
callq 0x2050
movl 0xb4(%rsp), %r8d
movzwl 0x3e(%rsp), %eax
movl %r8d, %r12d
testq %rax, %rax
je 0xe33e
movq 0xf0(%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 0xe32e
cmpl $0x2, %edi
je 0xe333
cmpl $0x1, %edi
jne 0xe336
subl %esi, %r12d
jmp 0xe336
addl %esi, %r12d
incq %rdx
cmpq %rdx, %rax
jne 0xe314
movq 0xd8(%rsp), %rdi
movl %ebp, %eax
subl %edi, %eax
addl %eax, %r14d
movl %r14d, %esi
movq %r8, %r14
callq 0xdc98
movq 0x148(%rsp), %rax
movslq (%rax,%rbx,4), %rax
imulq $0x68, %rax, %rcx
addq 0xd0(%rsp), %rcx
movl 0xb0(%rsp), %eax
subq $0x8, %rsp
movq 0x98(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
movl 0xb0(%rsp), %edx
movl 0xac(%rsp), %r8d
movl %r12d, %r9d
leaq 0x16(%rsp), %r10
pushq %r10
leaq 0x20(%rsp), %r10
pushq %r10
pushq $0x0
pushq %r14
pushq 0x108(%rsp)
pushq 0xb8(%rsp)
pushq 0x98(%rsp)
pushq 0x1c8(%rsp)
pushq 0x1f0(%rsp)
pushq 0x1f0(%rsp)
pushq 0x1c0(%rsp)
pushq 0x1c0(%rsp)
pushq %rax
callq 0xc5c6
addq $0x70, %rsp
orb 0x12(%rsp), %al
jne 0xe112
movq 0xbbf9(%rip), %rax      # 0x19ff8
movq (%rax), %rdi
leaq 0x5771(%rip), %rsi      # 0x13b7a
movq 0xc0(%rsp), %rdx
movl $0x80, %ecx
xorl %eax, %eax
callq 0x21d0
jmp 0xe112
xorl %r13d, %r13d
movq 0x98(%rsp), %rdi
cmpb $0x0, (%rdi)
je 0xe572
movq 0x90(%rsp), %rax
movq (%rax), %rsi
movl 0x14(%rsp), %edx
movzwl 0x10(%rsp), %r12d
movq %r13, %r10
movzwl 0xe(%rsp), %r13d
subq $0x8, %rsp
leaq 0x24(%rsp), %rbp
leaq 0x28(%rsp), %rax
leaq 0x2c(%rsp), %r15
leaq 0x30(%rsp), %r14
leaq 0x34(%rsp), %rbx
leaq 0x38(%rsp), %r11
movq 0x158(%rsp), %rcx
movq 0x178(%rsp), %r8
movq 0x180(%rsp), %r9
movss 0x20(%rsp), %xmm0
pushq 0x48(%rsp)
pushq 0x320(%rsp)
pushq %r13
movq %r10, %r13
pushq %r12
pushq 0x298(%rsp)
pushq %rbp
pushq %rax
pushq 0x2a8(%rsp)
pushq 0x2a8(%rsp)
pushq %r15
pushq %r14
pushq 0x2b8(%rsp)
pushq 0x2b8(%rsp)
pushq %rbx
pushq %r11
pushq 0x2c8(%rsp)
pushq 0x110(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0xe8(%rsp)
pushq 0xf8(%rsp)
pushq 0xf0(%rsp)
pushq 0x2b8(%rsp)
pushq 0x120(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0x140(%rsp)
pushq 0x2b8(%rsp)
leaq 0x114(%rsp), %rax
pushq %rax
leaq 0x120(%rsp), %rax
pushq %rax
pushq 0x2c8(%rsp)
pushq 0x170(%rsp)
movl 0x2c8(%rsp), %eax
pushq %rax
pushq 0x188(%rsp)
pushq 0x170(%rsp)
pushq 0x2c8(%rsp)
callq 0x9f32
addq $0x120, %rsp            # imm = 0x120
cmpb $0x0, 0x230(%rsp)
je 0xe6ee
movb 0x240(%rsp), %r11b
movq 0x248(%rsp), %rax
movq (%rax), %rdi
movl 0x30(%rsp), %esi
movl 0x2c(%rsp), %edx
movq 0x258(%rsp), %rax
movq (%rax), %rcx
movl 0x28(%rsp), %r8d
movl 0x24(%rsp), %r9d
movl 0x20(%rsp), %eax
movl 0x1c(%rsp), %r10d
movzbl %r11b, %r11d
movss 0x18(%rsp), %xmm0
pushq 0x40(%rsp)
pushq 0x318(%rsp)
movl 0x238(%rsp), %ebx
pushq %rbx
pushq 0x68(%rsp)
pushq %r11
pushq 0x260(%rsp)
pushq 0x88(%rsp)
pushq 0x80(%rsp)
pushq 0x248(%rsp)
pushq 0xb0(%rsp)
movl 0x248(%rsp), %r11d
pushq %r11
movl 0x248(%rsp), %r11d
pushq %r11
pushq 0xd0(%rsp)
pushq 0xe0(%rsp)
movl 0x238(%rsp), %r11d
pushq %r11
pushq 0xf8(%rsp)
pushq 0x1d0(%rsp)
pushq 0x200(%rsp)
pushq 0x200(%rsp)
pushq 0xf8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq 0x3a8(%rsp)
pushq %r10
pushq %rax
movq 0x3b0(%rsp), %rax
pushq (%rax)
callq 0xbdaa
addq $0x150, %rsp            # imm = 0x150
jmp 0xe75a
cmpb $0x0, 0x1d0(%rsp)
je 0xe75a
movq 0x1d8(%rsp), %rax
movq (%rax), %rdi
movl 0x38(%rsp), %esi
movl 0x34(%rsp), %edx
movq 0x150(%rsp), %rax
movsd (%rax), %xmm0
movq 0x170(%rsp), %rax
movq (%rax), %rcx
subq $0x8, %rsp
movq 0x210(%rsp), %r8
movq 0x50(%rsp), %r9
pushq 0x48(%rsp)
pushq 0x320(%rsp)
movl 0x240(%rsp), %eax
pushq %rax
pushq 0x70(%rsp)
pushq 0x80(%rsp)
callq 0x8bff
addq $0x30, %rsp
movq %r13, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x310a
movq %rsi, %rdi
callq 0x30e1
callq 0x3122
callq 0x30f2