// File: /workspace/github_c_makefile_star2_O2/jsh58[P]Genrich/Genrich.c
// Function: readSAM
uint64_t readSAM(File in, bool gz, char* line, Aln** aln,
    char* readName, 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, int xcount,
    char** xchrList, int xBedLen, Bed* xBed,
    uint64_t* secPair, uint64_t* secSingle,
    uint64_t* orphan, int* chromLen, Chrom** chrom,
    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 sortOpt, bool verbose) {

  // SAM fields to save
  char* qname, *rname, *cigar, *rnext, *seq, *qual, *extra;
  uint16_t flag;
  uint32_t pos, pnext;
  int32_t tlen;
  uint8_t mapq;

  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
  bool pastHeader = false;    // to check for misplaced header lines
  uint64_t count = 0;
  while (getLine(line, MAX_SIZE, in, gz) != NULL) {

    if (line[0] == '@') {
      if (pastHeader)
        exit(error(line, ERRHEAD));
      checkHeader(line, chromLen, chrom, xcount, xchrList,
        xBedLen, xBed, ctrl, sortOpt, verbose);
      continue;
    }
    pastHeader = true;

    // parse SAM record
    qname = strtok(line, TAB);
    if (qname == NULL)
      exit(error(line, ERRSAM));
    if (! loadFields(&flag, &rname, &pos, &mapq, &cigar,
        &rnext, &pnext, &tlen, &seq, &qual, &extra))
      exit(error(qname, ERRSAM));

    count++;
    if (flag & 0x4) {
      // skip unmapped
      (*unmapped)++;
      continue;
    }
    if (! strcmp(qname, "*") || ! strcmp(rname, "*")
        || pos < 0)
      // insufficient alignment info
      exit(error(qname, ERRSAM));
    if (flag & 0xE00) {
      // skip supplementary/PCR dups/low quality
      (*supp)++;
      continue;
    }
    // find matching Chrom (reference sequence)
    Chrom* ref = NULL;
    for (int i = 0; i < *chromLen; i++)
      if (! strcmp((*chrom + i)->name, rname)) {
        ref = *chrom + i;
        break;
      }
    if (ref == NULL)
      // cannot find reference sequence
      exit(error(rname, ERRCHROM));
    if (mapq < minMapQ) {
      // skip low MAPQ alignments
      (*lowMapQ)++;
      continue;
    }

    // process previous set of alns, if starting a new set
    if (readName[0] == '\0' || strcmp(qname, 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, qname, MAX_ALNS);
    }

    // save alignment information
    int length = calcDist(qname, seq, cigar); // distance to 3' end
    float score = getScore(extra);
    if (! parseAlign(aln, &alnLen, flag, ref, pos, length,
        pnext, paired, single, secPair, secSingle, skipped,
        singleOpt, score, dupsOpt, qual, strlen(qual),
        SAMQUAL, &qualR1, &qualR2) && verbose)
      fprintf(stderr, "Warning! Read %s has more than %d alignments\n",
        qname, MAX_ALNS);
    // NOTE: the following SAM fields are ignored:
    //   rnext, tlen
  }

  // 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, 0x20(%rsp)
movq %r9, 0x60(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, 0x80(%rsp)
movl %esi, 0x90(%rsp)
movq %rdi, 0xc8(%rsp)
leaq 0x14(%rsp), %rax
andl $0x0, (%rax)
leaq 0x40(%rsp), %rax
andl $0x0, (%rax)
leaq 0x3c(%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 0x10(%rsp), %rax
andw $0x0, (%rax)
leaq 0xe(%rsp), %rax
andw $0x0, (%rax)
movb 0x338(%rsp), %al
xorb $0x1, %al
movb %al, 0x12(%rsp)
movq $0x0, 0x88(%rsp)
xorl %r12d, %r12d
movq %rdx, 0x48(%rsp)
movzbl 0x90(%rsp), %ecx
movq %rdx, %rdi
movl $0xfff0, %esi           # imm = 0xFFF0
movq %rdx, %rbp
movq 0xc8(%rsp), %rdx
callq 0x283f
movzbl 0x338(%rsp), %r13d
movzbl 0x248(%rsp), %ebx
movzbl 0x238(%rsp), %r14d
movzbl 0x230(%rsp), %r8d
movzbl 0x228(%rsp), %r9d
movzbl 0x218(%rsp), %r15d
movzbl 0x200(%rsp), %esi
movzbl 0x1e8(%rsp), %edx
movzbl 0x1d8(%rsp), %ecx
movzbl 0x1d0(%rsp), %r11d
testq %rax, %rax
je 0xbe33
cmpb $0x40, (%rbp)
movq %rbp, %rdi
jne 0xb9f4
testb $0x1, %r12b
jne 0xc159
movzbl 0x330(%rsp), %eax
movq 0x1c0(%rsp), %rsi
movq 0x1c8(%rsp), %rdx
movl 0x188(%rsp), %ecx
movq 0x190(%rsp), %r8
movl 0x198(%rsp), %r9d
pushq %r13
pushq %rax
pushq %r14
pushq 0x1b8(%rsp)
callq 0xb3e1
movq 0x68(%rsp), %rdx
addq $0x20, %rsp
jmp 0xb914
movq %r9, 0xc0(%rsp)
movq %r8, 0xb8(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x78(%rsp)
movq %r11, 0xb0(%rsp)
leaq 0x608f(%rip), %rsi      # 0x11ab1
callq 0x2280
testq %rax, %rax
je 0xc17b
movq %rax, %r12
subq $0x8, %rsp
leaq 0x4e(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
leaq 0xac(%rsp), %rdx
leaq 0x1b(%rsp), %rcx
leaq 0xf0(%rsp), %r8
leaq 0x108(%rsp), %r9
leaq 0xd8(%rsp), %rax
pushq %rax
leaq 0xe8(%rsp), %rax
pushq %rax
leaq 0xf8(%rsp), %rax
pushq %rax
leaq 0x11c(%rsp), %rax
pushq %rax
leaq 0xc8(%rsp), %rax
pushq %rax
callq 0xb506
addq $0x30, %rsp
testb %al, %al
je 0xc173
movq 0x88(%rsp), %rax
incq %rax
movzwl 0x46(%rsp), %ecx
testb $0x4, %cl
movq 0x48(%rsp), %rdx
movq %rax, 0x88(%rsp)
jne 0xbb06
cmpb $0x2a, (%r12)
jne 0xbad4
cmpb $0x0, 0x1(%r12)
je 0xc173
movq 0xf0(%rsp), %rsi
movq %rsi, 0x18(%rsp)
cmpb $0x2a, (%rsi)
jne 0xbaf5
movq 0x18(%rsp), %rsi
cmpb $0x0, 0x1(%rsi)
je 0xc173
testw $0xe00, %cx             # imm = 0xE00
je 0xbb19
movq 0x168(%rsp), %rax
jmp 0xbb0e
movq 0x140(%rsp), %rax
incq (%rax)
movb $0x1, %r12b
jmp 0xb914
movl %ecx, 0x9c(%rsp)
movq %r12, 0xa8(%rsp)
movq 0x1c0(%rsp), %rax
movl (%rax), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
imulq $0x68, %rax, %rax
movq %rax, 0x50(%rsp)
pushq $-0x68
popq %rcx
leaq 0x68(%rcx), %r12
cmpq %r12, 0x50(%rsp)
je 0xc169
movq 0x1c8(%rsp), %rax
movq (%rax), %rbp
movq 0x68(%rbp,%rcx), %rdi
movq 0x18(%rsp), %rsi
callq 0x21a0
movq %r12, %rcx
testl %eax, %eax
jne 0xbb48
movq %rbp, 0x18(%rsp)
movzbl 0x13(%rsp), %eax
cmpl 0x180(%rsp), %eax
jge 0xbba3
movq 0x178(%rsp), %rax
incq (%rax)
movb $0x1, %r12b
movq 0x48(%rsp), %rdx
jmp 0xb914
movq %r12, 0x50(%rsp)
movq 0x58(%rsp), %rsi
cmpb $0x0, (%rsi)
movq 0x60(%rsp), %r12
je 0xbd03
movq 0xa8(%rsp), %rdi
movq %rdi, %rbp
callq 0x21a0
testl %eax, %eax
je 0xbd2c
movq 0x80(%rsp), %rax
movq (%rax), %rsi
movl 0x14(%rsp), %edx
movzwl 0x10(%rsp), %eax
movzwl 0xe(%rsp), %r10d
subq $0x8, %rsp
movq 0x60(%rsp), %rdi
movq %r12, %rcx
movq 0x160(%rsp), %r8
movq 0x168(%rsp), %r9
movss 0x28(%rsp), %xmm0
pushq %r13
pushq 0x338(%rsp)
pushq %r10
pushq %rax
pushq 0x2b0(%rsp)
leaq 0x54(%rsp), %rax
pushq %rax
leaq 0x60(%rsp), %rax
pushq %rax
pushq 0x2c0(%rsp)
pushq 0x2c0(%rsp)
leaq 0x7c(%rsp), %rax
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
pushq 0x2d0(%rsp)
pushq 0x2d0(%rsp)
leaq 0xa4(%rsp), %rax
pushq %rax
leaq 0xb0(%rsp), %rax
pushq %rax
pushq 0x2e0(%rsp)
pushq %rbx
movl 0x2d0(%rsp), %eax
pushq %rax
pushq %r14
pushq 0x158(%rsp)
pushq 0x168(%rsp)
pushq 0x2d0(%rsp)
pushq %r15
movl 0x2d0(%rsp), %eax
pushq %rax
movl 0x2d0(%rsp), %eax
pushq %rax
pushq 0x138(%rsp)
pushq 0x2d0(%rsp)
leaq 0x11c(%rsp), %rax
pushq %rax
leaq 0x128(%rsp), %rax
pushq %rax
pushq 0x2e0(%rsp)
pushq 0x168(%rsp)
movl 0x2e0(%rsp), %eax
pushq %rax
pushq 0x180(%rsp)
pushq 0x1c0(%rsp)
pushq 0x2d0(%rsp)
callq 0x8a5a
movq 0x178(%rsp), %rsi
addq $0x120, %rsp            # imm = 0x120
andl $0x0, 0x14(%rsp)
andw $0x0, 0xe(%rsp)
andw $0x0, 0x10(%rsp)
movl $0x80, %edx
movq %rsi, %rdi
movq 0xa8(%rsp), %rsi
movq %rsi, %rbp
callq 0x2050
movq 0x18(%rsp), %r13
addq 0x50(%rsp), %r13
movq 0xe0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
movq %rbp, %rdi
callq 0xb7db
movl %eax, 0x18(%rsp)
movq 0xd0(%rsp), %rdi
callq 0xb61b
movss %xmm0, 0x50(%rsp)
movl 0xa4(%rsp), %r12d
movl 0xa0(%rsp), %r15d
movq 0xd8(%rsp), %r14
movq %r14, %rdi
callq 0x20f0
subq $0x8, %rsp
movq 0x88(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
movl 0xa4(%rsp), %edx
movq %r13, %rcx
movl %r12d, %r8d
movl 0x20(%rsp), %r9d
movss 0x58(%rsp), %xmm0
leaq 0x16(%rsp), %r10
pushq %r10
leaq 0x20(%rsp), %r10
pushq %r10
pushq $0x21
pushq %rax
pushq %r14
pushq %rbx
pushq 0xe8(%rsp)
pushq 0x1b0(%rsp)
pushq 0x1f8(%rsp)
pushq 0x1f8(%rsp)
pushq 0x1a8(%rsp)
pushq 0x1a8(%rsp)
pushq %r15
callq 0xaeb6
addq $0x70, %rsp
orb 0x12(%rsp), %al
movb $0x1, %r12b
movq 0x48(%rsp), %rdx
jne 0xb914
movq 0xb1e8(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x5cb0(%rip), %rsi      # 0x11aca
movq %rbp, %rdx
movl $0x80, %ecx
xorl %eax, %eax
callq 0x21c0
movq 0x48(%rsp), %rdx
jmp 0xb914
movq %r9, %rbp
movq %r8, %r12
movq %rsi, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x78(%rsp)
movq 0x58(%rsp), %rdi
cmpb $0x0, (%rdi)
je 0xbf71
movq 0x80(%rsp), %rax
movq (%rax), %rsi
movl 0x14(%rsp), %edx
movzwl 0x10(%rsp), %eax
movzwl 0xe(%rsp), %r10d
subq $0x8, %rsp
movq 0x68(%rsp), %rcx
movq 0x160(%rsp), %r8
movq 0x168(%rsp), %r9
movss 0x28(%rsp), %xmm0
pushq %r13
pushq 0x338(%rsp)
pushq %r10
pushq %rax
pushq 0x2b0(%rsp)
leaq 0x54(%rsp), %rax
pushq %rax
leaq 0x60(%rsp), %rax
pushq %rax
pushq 0x2c0(%rsp)
pushq 0x2c0(%rsp)
leaq 0x7c(%rsp), %rax
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
pushq 0x2d0(%rsp)
pushq 0x2d0(%rsp)
leaq 0xa4(%rsp), %rax
pushq %rax
leaq 0xb0(%rsp), %rax
pushq %rax
pushq 0x2e0(%rsp)
pushq %rbx
movl 0x2d0(%rsp), %eax
pushq %rax
pushq %r14
pushq %r12
pushq %rbp
pushq 0x2d0(%rsp)
pushq %r15
movl 0x2d0(%rsp), %eax
pushq %rax
movl 0x2d0(%rsp), %eax
pushq %rax
pushq 0x138(%rsp)
pushq 0x2d0(%rsp)
leaq 0x11c(%rsp), %rax
pushq %rax
leaq 0x128(%rsp), %rax
pushq %rax
pushq 0x2e0(%rsp)
pushq 0x168(%rsp)
movl 0x2e0(%rsp), %eax
pushq %rax
pushq 0x180(%rsp)
pushq %r11
pushq 0x2d0(%rsp)
movq %r11, %rbx
callq 0x8a5a
movq %rbx, %r11
addq $0x120, %rsp            # imm = 0x120
cmpb $0x0, 0x248(%rsp)
je 0xc0e1
movq 0x260(%rsp), %rax
movq (%rax), %rdi
movl 0x38(%rsp), %esi
movl 0x34(%rsp), %edx
movq 0x270(%rsp), %rax
movq (%rax), %rcx
movl 0x30(%rsp), %r8d
movl 0x2c(%rsp), %r9d
movl 0x28(%rsp), %eax
movq %rax, 0x90(%rsp)
movl 0x24(%rsp), %r10d
movq %r11, %rax
movzbl 0x258(%rsp), %r11d
movss 0x20(%rsp), %xmm0
pushq %r13
pushq 0x330(%rsp)
movl 0x250(%rsp), %ebx
pushq %rbx
pushq %r14
pushq %r11
pushq 0x278(%rsp)
pushq %r12
pushq %rbp
pushq 0x260(%rsp)
pushq %r15
movl 0x260(%rsp), %r11d
pushq %r11
movl 0x260(%rsp), %r11d
pushq %r11
pushq 0xc8(%rsp)
pushq 0xd8(%rsp)
movl 0x250(%rsp), %r11d
pushq %r11
pushq 0xf0(%rsp)
pushq 0xe0(%rsp)
pushq 0x1e8(%rsp)
pushq 0x1e8(%rsp)
pushq %rax
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq 0x3c0(%rsp)
pushq %r10
pushq 0x1d0(%rsp)
movq 0x3c8(%rsp), %rax
pushq (%rax)
callq 0xa706
addq $0x150, %rsp            # imm = 0x150
jmp 0xc13f
cmpb $0x0, 0x1e8(%rsp)
je 0xc13f
movq 0x1f0(%rsp), %rax
movq (%rax), %rdi
movl 0x40(%rsp), %esi
movl 0x3c(%rsp), %edx
movq 0x60(%rsp), %rax
movsd (%rax), %xmm0
movq 0x158(%rsp), %rax
movq (%rax), %rcx
subq $0x8, %rsp
movq 0x228(%rsp), %r8
movl %ebp, %r9d
pushq %r13
pushq 0x338(%rsp)
movl 0x258(%rsp), %eax
pushq %rax
pushq %r14
pushq %r12
callq 0x77d3
addq $0x30, %rsp
movq 0x88(%rsp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x12
popq %rsi
callq 0x2724
pushq $0x1
popq %rdi
callq 0x22a0
pushq $0x11
popq %rsi
movq 0x18(%rsp), %rdi
jmp 0xc15c
pushq $0x10
popq %rsi
movq %r12, %rdi
jmp 0xc15c
pushq $0x10
popq %rsi
movq 0x48(%rsp), %rdi
jmp 0xc15c