// File: /workspace/github_c_makefile_star2_O2/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, 0xc0(%rsp)
movl %r8d, 0x7c(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, %r12
movq %rdi, %r14
leaq 0x14(%rsp), %rax
andl $0x0, (%rax)
leaq 0x38(%rsp), %rax
andl $0x0, (%rax)
leaq 0x34(%rsp), %rax
andl $0x0, (%rax)
leaq 0x30(%rsp), %rax
andl $0x0, (%rax)
leaq 0x2c(%rsp), %rax
andl $0x0, (%rax)
leaq 0x28(%rsp), %rax
andl $0x0, (%rax)
leaq 0x24(%rsp), %rax
andl $0x0, (%rax)
leaq 0x20(%rsp), %rax
andl $0x0, (%rax)
leaq 0x1c(%rsp), %rax
andl $0x0, (%rax)
leaq 0x10(%rsp), %rax
andw $0x0, (%rax)
leaq 0xe(%rsp), %rax
andw $0x0, (%rax)
movb 0x318(%rsp), %al
xorb $0x1, %al
movb %al, 0x12(%rsp)
xorl %ebx, %ebx
movq %r14, %rdi
xorl %esi, %esi
callq 0xc185
movl %eax, %ebp
movzbl 0x318(%rsp), %eax
movq %rax, 0x98(%rsp)
movzbl 0x230(%rsp), %eax
movq %rax, 0x70(%rsp)
movzbl 0x220(%rsp), %eax
movq %rax, 0x58(%rsp)
movzbl 0x218(%rsp), %eax
movq %rax, 0x48(%rsp)
movzbl 0x210(%rsp), %eax
movq %rax, 0x40(%rsp)
movzbl 0x200(%rsp), %eax
movq %rax, 0x68(%rsp)
movzbl 0x1e8(%rsp), %eax
movq %rax, 0xa0(%rsp)
movzbl 0x1d0(%rsp), %eax
movq %rax, 0xa8(%rsp)
movzbl 0x1c0(%rsp), %r13d
movzbl 0x1b8(%rsp), %r15d
cmpl $-0x1, %ebp
je 0xcc04
movq %r13, 0xc8(%rsp)
movq %r15, 0xb0(%rsp)
movq %rbx, 0xd0(%rsp)
cmpl $0x1f, %ebp
jbe 0xcf24
movslq %ebp, %rbx
movq %r12, 0xb8(%rsp)
testl %ebp, %ebp
movl $0x0, %r15d
cmovgl %ebp, %r15d
xorl %r13d, %r13d
cmpq %r13, %r15
je 0xc825
movl (%r14), %eax
testl %eax, %eax
je 0xc80b
decl %eax
movl %eax, (%r14)
incq 0x10(%r14)
movq 0x8(%r14), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r14)
movzbl (%rax), %eax
jmp 0xc81c
movq %r14, %rdi
callq 0x20a0
cmpl $-0x1, %eax
je 0xcf24
movb %al, (%r12,%r13)
incq %r13
jmp 0xc7e5
subq $0x8, %rsp
leaq 0xc0(%rsp), %rdi
leaq 0x94(%rsp), %rsi
leaq 0x90(%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 0xb8(%rsp), %rax
pushq %rax
leaq 0x13c(%rsp), %rax
pushq %rax
leaq 0xcc(%rsp), %rax
pushq %rax
callq 0xc229
addq $0x50, %rsp
addq %r12, %rbx
cmpq %rbx, 0xb8(%rsp)
ja 0xcf24
movzwl 0x3c(%rsp), %ebx
testb $0x4, %bl
jne 0xc942
movq 0xe8(%rsp), %rcx
movb $0x1, %al
cmpb $0x2a, (%rcx)
jne 0xc8db
cmpb $0x0, 0x1(%rcx)
setne %al
testb %al, %al
je 0xcf3b
movl 0x8c(%rsp), %r13d
testl %r13d, %r13d
js 0xcf3b
cmpl 0x140(%rsp), %r13d
jge 0xcf3b
movq 0x148(%rsp), %rax
movl (%rax,%r13,4), %eax
testl %eax, %eax
js 0xcf3b
cmpl 0x7c(%rsp), %eax
jge 0xcf3b
movl 0x88(%rsp), %r15d
testl %r15d, %r15d
js 0xcf3b
testw $0xe00, %bx             # imm = 0xE00
je 0xc95d
movq 0x180(%rsp), %rax
jmp 0xc94a
movq 0x158(%rsp), %rax
incq (%rax)
movq 0xd0(%rsp), %rbx
incq %rbx
jmp 0xc712
movzbl 0x13(%rsp), %eax
cmpl 0x198(%rsp), %eax
jge 0xc975
movq 0x190(%rsp), %rax
jmp 0xc94a
movq 0x50(%rsp), %rsi
cmpb $0x0, (%rsi)
movq %rcx, 0x90(%rsp)
je 0xcaea
movq %rcx, %rdi
callq 0x21a0
testl %eax, %eax
je 0xcb0b
movq 0x60(%rsp), %rax
movq (%rax), %rsi
movl 0x14(%rsp), %edx
movzwl 0x10(%rsp), %eax
movzwl 0xe(%rsp), %r10d
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
movq 0x158(%rsp), %rcx
movq 0x178(%rsp), %r8
movq 0x180(%rsp), %r9
movss 0x20(%rsp), %xmm0
pushq 0xa0(%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 0xf8(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0xf0(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0x2b8(%rsp)
pushq 0x120(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0x170(%rsp)
pushq 0x2b8(%rsp)
leaq 0x114(%rsp), %rax
pushq %rax
leaq 0x120(%rsp), %rax
pushq %rax
pushq 0x2c8(%rsp)
pushq 0x1a0(%rsp)
movl 0x2c8(%rsp), %eax
pushq %rax
pushq 0x1d0(%rsp)
pushq 0x1c0(%rsp)
pushq 0x2c8(%rsp)
callq 0x8a5a
movq 0x170(%rsp), %rsi
addq $0x120, %rsp            # imm = 0x120
movq 0x90(%rsp), %rcx
andl $0x0, 0x14(%rsp)
andw $0x0, 0xe(%rsp)
andw $0x0, 0x10(%rsp)
movl $0x80, %edx
movq %rsi, %rdi
movq %rcx, %rsi
callq 0x2050
movl 0x84(%rsp), %edi
movq %rdi, 0x48(%rsp)
movzwl 0x3e(%rsp), %esi
movq 0xf0(%rsp), %rdx
callq 0xc327
movl %eax, 0x40(%rsp)
movq 0xd8(%rsp), %rdi
movl %r12d, %eax
subl %edi, %eax
addl %eax, %ebp
movl %ebp, %esi
callq 0xc3f1
movq 0x148(%rsp), %rax
movslq (%rax,%r13,4), %rax
imulq $0x68, %rax, %rcx
addq 0xc0(%rsp), %rcx
movl 0x80(%rsp), %eax
subq $0x8, %rsp
movq 0x68(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
movl %ebx, %edx
movl %r15d, %r8d
movl 0x48(%rsp), %r9d
leaq 0x16(%rsp), %r10
pushq %r10
leaq 0x20(%rsp), %r10
pushq %r10
pushq $0x0
pushq 0x68(%rsp)
pushq 0x108(%rsp)
pushq 0xa0(%rsp)
pushq 0xe8(%rsp)
pushq 0x1c8(%rsp)
pushq 0x1f0(%rsp)
pushq 0x1f0(%rsp)
pushq 0x1c0(%rsp)
pushq 0x1c0(%rsp)
pushq %rax
callq 0xaeb6
addq $0x70, %rsp
orb 0x12(%rsp), %al
jne 0xc94d
movq 0xa417(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x4edf(%rip), %rsi      # 0x11aca
movq 0x90(%rsp), %rdx
movl $0x80, %ecx
xorl %eax, %eax
callq 0x21c0
jmp 0xc94d
movq 0x50(%rsp), %rdi
cmpb $0x0, (%rdi)
movq 0xa8(%rsp), %r12
movq 0xa0(%rsp), %rbp
movq 0x98(%rsp), %r14
je 0xcd4a
movq 0x60(%rsp), %rax
movq (%rax), %rsi
movl 0x14(%rsp), %edx
movzwl 0x10(%rsp), %eax
movzwl 0xe(%rsp), %r10d
subq $0x8, %rsp
movq 0x158(%rsp), %rcx
movq 0x178(%rsp), %r8
movq 0x180(%rsp), %r9
movss 0x20(%rsp), %xmm0
pushq %r14
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 0xf8(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
pushq 0xf0(%rsp)
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
pushq 0x2b8(%rsp)
pushq 0x120(%rsp)
movl 0x2b8(%rsp), %eax
pushq %rax
movl 0x2b8(%rsp), %eax
pushq %rax
pushq %rbp
pushq 0x2b8(%rsp)
leaq 0x114(%rsp), %rax
pushq %rax
leaq 0x120(%rsp), %rax
pushq %rax
pushq 0x2c8(%rsp)
pushq %r12
movl 0x2c8(%rsp), %eax
pushq %rax
pushq %r13
pushq %r15
pushq 0x2c8(%rsp)
callq 0x8a5a
addq $0x120, %rsp            # imm = 0x120
cmpb $0x0, 0x230(%rsp)
je 0xcea8
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 0x240(%rsp), %r11d
movss 0x18(%rsp), %xmm0
pushq %r14
pushq 0x318(%rsp)
movl 0x238(%rsp), %r14d
pushq %r14
pushq 0x70(%rsp)
pushq %r11
pushq 0x260(%rsp)
pushq 0x78(%rsp)
pushq 0x78(%rsp)
pushq 0x248(%rsp)
pushq 0xb0(%rsp)
movl 0x248(%rsp), %r11d
pushq %r11
movl 0x248(%rsp), %r11d
pushq %r11
pushq %rbp
pushq %r12
movl 0x238(%rsp), %r11d
pushq %r11
pushq %r13
pushq 0x1d0(%rsp)
pushq 0x200(%rsp)
pushq 0x200(%rsp)
pushq %r15
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 0xa706
addq $0x150, %rsp            # imm = 0x150
jmp 0xcf0f
cmpb $0x0, 0x1d0(%rsp)
je 0xcf0f
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 0x48(%rsp), %r9
pushq %r14
pushq 0x320(%rsp)
movl 0x240(%rsp), %eax
pushq %rax
pushq 0x78(%rsp)
pushq 0x70(%rsp)
callq 0x77d3
addq $0x30, %rsp
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4be4(%rip), %rdi      # 0x11b0f
pushq $0x13
popq %rsi
callq 0x2724
pushq $0x1
popq %rdi
callq 0x22a0
pushq $0x10
popq %rsi
movq %rcx, %rdi
jmp 0xcf2e