// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/sam_mods.c
// Function: bam_parse_basemod2
int bam_parse_basemod2(const bam1_t *b, hts_base_mod_state *state,
                       uint32_t flags) {
    // Reset position, else upcoming calls may fail on
    // seq pos - length comparison
    state->seq_pos = 0;
    state->nmods = 0;
    state->flags = flags;

    // Read MM and ML tags
    uint8_t *mm = bam_aux_get(b, "MM");
    if (!mm) mm = bam_aux_get(b, "Mm");
    if (!mm)
        return 0;
    if (mm[0] != 'Z') {
        hts_log_error("%s: MM tag is not of type Z", bam_get_qname(b));
        return -1;
    }

    uint8_t *mi = bam_aux_get(b, "MN");
    if (mi && bam_aux2i(mi) != b->core.l_qseq && b->core.l_qseq) {
        // bam_aux2i with set errno = EINVAL and return 0 if the tag
        // isn't integer, but 0 will be a seq-length mismatch anyway so
        // triggers an error here too.
        hts_log_error("%s: MM/MN data length is incompatible with"
                      " SEQ length", bam_get_qname(b));
        return -1;
    }

    uint8_t *ml = bam_aux_get(b, "ML");
    if (!ml) ml = bam_aux_get(b, "Ml");
    if (ml && (ml[0] != 'B' || ml[1] != 'C')) {
        hts_log_error("%s: ML tag is not of type B,C", bam_get_qname(b));
        return -1;
    }
    uint8_t *ml_end = ml ? ml+6 + le_to_u32(ml+2) : NULL;
    if (ml) ml += 6;

    // Aggregate freqs of ACGTN if reversed, to get final-delta (later)
    int freq[16];
    if (b->core.flag & BAM_FREVERSE)
        seq_freq(b, freq);

    char *cp = (char *)mm+1;
    int mod_num = 0;
    int implicit = 1;
    while (*cp) {
        for (; *cp; cp++) {
            // cp should be [ACGTNU][+-]([a-zA-Z]+|[0-9]+)[.?]?(,\d+)*;
            unsigned char btype = *cp++;

            if (btype != 'A' && btype != 'C' &&
                btype != 'G' && btype != 'T' &&
                btype != 'U' && btype != 'N')
                return -1;
            if (btype == 'U') btype = 'T';

            btype = seq_nt16_table[btype];

            // Strand
            if (*cp != '+' && *cp != '-')
                return -1; // malformed
            char strand = *cp++;

            // List of modification types
            char *ms = cp, *me; // mod code start and end
            char *cp_end = NULL;
            int chebi = 0;
            if (isdigit_c(*cp)) {
                chebi = strtol(cp, &cp_end, 10);
                cp = cp_end;
                ms = cp-1;
            } else {
                while (*cp && isalpha_c(*cp))
                    cp++;
                if (*cp == '\0')
                    return -1;
            }

            me = cp;

            // Optional explicit vs implicit marker
            implicit = 1;
            if (*cp == '.') {
                // default is implicit = 1;
                cp++;
            } else if (*cp == '?') {
                implicit = 0;
                cp++;
            } else if (*cp != ',' && *cp != ';') {
                // parse error
                return -1;
            }

            long delta;
            int n = 0; // nth symbol in a multi-mod string
            int stride = me-ms;
            int ndelta = 0;

            if (b->core.flag & BAM_FREVERSE) {
                // We process the sequence in left to right order,
                // but delta is successive count of bases to skip
                // counting right to left.  This also means the number
                // of bases to skip at left edge is unrecorded (as it's
                // the remainder).
                //
                // To output mods in left to right, we step through the
                // MM list in reverse and need to identify the left-end
                // "remainder" delta.
                int total_seq = 0;
                for (;;) {
                    cp += (*cp == ',');
                    if (*cp == 0 || *cp == ';')
                        break;

                    delta = strtol(cp, &cp_end, 10);
                    if (cp_end == cp) {
                        hts_log_error("%s: Hit end of MM tag. Missing "
                                      "semicolon?", bam_get_qname(b));
                        return -1;
                    }

                    cp = cp_end;
                    total_seq += delta+1;
                    ndelta++;
                }
                delta = freq[seqi_rc[btype]] - total_seq; // remainder
            } else {
                delta = *cp == ','
                    ? strtol(cp+1, &cp_end, 10)
                    : 0;
                if (!cp_end) {
                    // empty list
                    delta = INT_MAX;
                    cp_end = cp;
                }
            }
            // Now delta is first in list or computed remainder,
            // and cp_end is either start or end of the MM list.
            while (ms < me) {
                state->type     [mod_num] = chebi ? -chebi : *ms;
                state->strand   [mod_num] = (strand == '-');
                state->canonical[mod_num] = btype;
                state->MLstride [mod_num] = stride;
                state->implicit [mod_num] = implicit;

                if (delta < 0) {
                    hts_log_error("%s: MM tag refers to bases beyond sequence "
                                  "length", bam_get_qname(b));
                    return -1;
                }
                state->MMcount  [mod_num] = delta;
                if (b->core.flag & BAM_FREVERSE) {
                    state->MM   [mod_num] = me+1;
                    state->MMend[mod_num] = cp_end;
                    state->ML   [mod_num] = ml ? ml+n +(ndelta-1)*stride: NULL;
                } else {
                    state->MM   [mod_num] = cp_end;
                    state->MMend[mod_num] = NULL;
                    state->ML   [mod_num] = ml ? ml+n : NULL;
                }

                if (++mod_num >= MAX_BASE_MOD) {
                    hts_log_error("%s: Too many base modification types",
                                  bam_get_qname(b));
                    return -1;
                }
                ms++; n++;
            }

            // Skip modification deltas
            if (ml) {
                if (b->core.flag & BAM_FREVERSE) {
                    ml += ndelta*stride;
                } else {
                    while (*cp && *cp != ';') {
                        if (*cp == ',')
                            ml+=stride;
                        cp++;
                    }
                }
                if (ml > ml_end) {
                    hts_log_error("%s: Insufficient number of entries in ML "
                                  "tag", bam_get_qname(b));
                    return -1;
                }
            } else {
                // cp_end already known if FREVERSE
                if (cp_end && (b->core.flag & BAM_FREVERSE))
                    cp = cp_end;
                else
                    while (*cp && *cp != ';')
                        cp++;
            }
            if (!*cp) {
                hts_log_error("%s: Hit end of MM tag. Missing semicolon?",
                              bam_get_qname(b));
                return -1;
            }
        }
    }
    if (ml && ml != ml_end) {
        hts_log_error("%s: Too many entries in ML tag", bam_get_qname(b));
        return -1;
    }

    state->nmods = mod_num;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq $0x0, 0x2d00(%rsi)
movl %edx, 0x2d08(%rsi)
leaq 0x64cc5(%rip), %rsi     # 0x98960
callq 0x2beb1
testq %rax, %rax
jne 0x33cbd
leaq 0x64cb7(%rip), %rsi     # 0x98963
movq %rbx, %rdi
callq 0x2beb1
testq %rax, %rax
je 0x34230
movq %rax, %r13
cmpb $0x5a, (%rax)
jne 0x33d03
leaq 0x64cc9(%rip), %rsi     # 0x98995
movq %rbx, %rdi
callq 0x2beb1
testq %rax, %rax
je 0x33d17
movq %rax, %rdi
callq 0x2e8bf
movslq 0x18(%rbx), %rcx
testq %rcx, %rcx
je 0x33d17
cmpq %rcx, %rax
je 0x33d17
movq 0x38(%rbx), %rcx
leaq 0x64c6c(%rip), %rsi     # 0x98966
leaq 0x64c97(%rip), %rdx     # 0x98998
jmp 0x33d6d
movq 0x38(%rbx), %rcx
leaq 0x64c58(%rip), %rsi     # 0x98966
leaq 0x64c64(%rip), %rdx     # 0x98979
jmp 0x33d6d
leaq 0x64cb0(%rip), %rsi     # 0x989ce
movq %rbx, %rdi
callq 0x2beb1
testq %rax, %rax
jne 0x33d3f
leaq 0x64c9f(%rip), %rsi     # 0x989d1
movq %rbx, %rdi
callq 0x2beb1
testq %rax, %rax
je 0x33d92
cmpb $0x42, (%rax)
jne 0x33d5b
cmpb $0x43, 0x1(%rax)
jne 0x33d5b
leaq 0x6(%rax), %r10
movl 0x2(%rax), %ecx
leaq (%rax,%rcx), %r9
addq $0x6, %r9
jmp 0x33d98
movq 0x38(%rbx), %rcx
leaq 0x64c00(%rip), %rsi     # 0x98966
leaq 0x64c67(%rip), %rdx     # 0x989d4
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r9d, %r9d
xorl %r10d, %r10d
testb $0x10, 0x10(%rbx)
je 0x33e0c
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movl 0x18(%rbx), %eax
testl %eax, %eax
jle 0x33e05
movl 0x14(%rbx), %ecx
shll $0x2, %ecx
addq 0x38(%rbx), %rcx
movzwl 0x12(%rbx), %edx
addq %rcx, %rdx
xorl %esi, %esi
xorl %edi, %edi
movl %edi, %ecx
shrl %ecx
movzbl (%rdx,%rcx), %r8d
movl %esi, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r8d
andl $0xf, %r8d
incl 0x80(%rsp,%r8,4)
incl %edi
addl $0x4, %esi
cmpl %edi, %eax
jne 0x33ddd
movl %eax, 0xbc(%rsp)
movb 0x1(%r13), %al
testb %al, %al
je 0x341fc
incq %r13
leaq 0x800(%r14), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x1d00(%r14), %rcx
movq %rcx, 0x50(%rsp)
movl $0x182045, %edx         # imm = 0x182045
movl $0x54, %esi
leaq 0x61b1c(%rip), %rdi     # 0x95960
movl $0x0, 0xc(%rsp)
movq %r14, 0x38(%rsp)
movq %r9, 0x60(%rsp)
movzbl %al, %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leal -0x41(%rax), %ecx
cmpl $0x14, %ecx
ja 0x34276
btl %ecx, %edx
jae 0x34276
cmpb $0x55, %al
cmovel %esi, %eax
movzbl %al, %eax
movzbl (%rdi,%rax), %eax
movq %rax, 0x40(%rsp)
movzbl 0x1(%r13), %eax
cmpl $0x2d, %eax
je 0x33e97
cmpl $0x2b, %eax
jne 0x33d7e
movl %eax, 0x18(%rsp)
movq %r10, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
movzbl 0x2(%r13), %r15d
addq $0x2, %r13
callq 0x4510
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r15,2)
jne 0x33ee3
movq %r13, %r12
testb %r15b, %r15b
je 0x33d7e
movzbl %r15b, %ecx
testb $0x4, 0x1(%rax,%rcx,2)
je 0x33f0b
movb 0x1(%r12), %r15b
incq %r12
jmp 0x33ec5
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rsi
leaq -0x1(%rsi), %r13
movb (%rsi), %r15b
movq %rsi, %r12
jmp 0x33f16
xorl %esi, %esi
movq $0x0, 0x20(%rsp)
movzbl %r15b, %eax
movl $0x1, 0x1c(%rsp)
cmpl $0x3a, %eax
jg 0x33f3c
cmpl $0x2c, %eax
je 0x33f5c
cmpl $0x2e, %eax
jne 0x33d7e
leaq 0x1(%r12), %r15
jmp 0x33f5f
movq %r12, %r15
cmpl $0x3b, %eax
je 0x33f5f
cmpl $0x3f, %eax
jne 0x33d7e
leaq 0x1(%r12), %r15
movl $0x0, 0x1c(%rsp)
jmp 0x33f5f
movq %r12, %r15
testb $0x10, 0x10(%rbx)
jne 0x33f8c
cmpb $0x2c, (%r15)
jne 0x3400a
leaq 0x1(%r15), %rdi
leaq 0x10(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq 0x10(%rsp), %rsi
jmp 0x3400c
xorl %edi, %edi
movq %r15, %rax
xorl %r14d, %r14d
xorl %edx, %edx
cmpb $0x2c, (%rax)
sete %dl
leaq (%rax,%rdx), %r15
movzbl (%rax,%rdx), %eax
testl %eax, %eax
je 0x33fe0
cmpl $0x3b, %eax
je 0x33fe0
movq %rdi, %rbp
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq 0x10(%rsp), %rsi
cmpq %r15, %rsi
je 0x34237
addl %eax, %r14d
incl %r14d
movq %rbp, %rdi
incl %edi
movq %rsi, %rax
jmp 0x33f94
leaq 0x64b49(%rip), %rax     # 0x98b30
movq 0x40(%rsp), %rcx
movslq (%rax,%rcx,4), %rax
movslq 0x80(%rsp,%rax,4), %rax
movslq %r14d, %rdx
subq %rdx, %rax
movq 0x38(%rsp), %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x34023
xorl %eax, %eax
testq %rsi, %rsi
cmoveq %r15, %rsi
movq %rsi, 0x10(%rsp)
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmoveq %rcx, %rax
xorl %edi, %edi
movq %r12, %rdx
subq %r13, %rdx
jbe 0x34165
movq 0x20(%rsp), %rcx
negl %ecx
movl %ecx, 0x34(%rsp)
cmpb $0x2d, 0x18(%rsp)
sete 0x18(%rsp)
incq %r12
movq %rdi, 0x48(%rsp)
decl %edi
imull %edx, %edi
movslq %edi, %rcx
movq %rcx, 0x70(%rsp)
movslq 0xc(%rsp), %r8
cmpl $0x100, %r8d            # imm = 0x100
movl $0xff, %ecx
cmovgel %r8d, %ecx
subl %r8d, %ecx
movq %rcx, 0x68(%rsp)
leaq (%r14,%r8,4), %r10
movq 0x58(%rsp), %rcx
addq %r8, %rcx
movq %rcx, 0x78(%rsp)
movq 0x50(%rsp), %rcx
leaq (%rcx,%r8,8), %r8
xorl %r11d, %r11d
movl 0x34(%rsp), %r14d
cmpl $0x0, 0x20(%rsp)
jne 0x340a5
movsbl (%r13,%r11), %r14d
movl %r14d, (%r10,%r11,4)
movb 0x18(%rsp), %cl
movq 0x78(%rsp), %rdi
movb %cl, (%rdi,%r11)
movq 0x40(%rsp), %rcx
movl %ecx, 0x400(%r10,%r11,4)
movl %edx, 0x2500(%r10,%r11,4)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x2900(%r10,%r11,4)
testq %rax, %rax
js 0x34245
movl %eax, 0x900(%r10,%r11,4)
movzwl 0x10(%rbx), %r14d
testb $0x10, %r14b
movq %r12, %r14
cmoveq %rsi, %r14
movq %rsi, %rcx
movl $0x0, %edi
cmoveq %rdi, %rcx
movq 0x70(%rsp), %rbp
cmoveq %rdi, %rbp
movq 0x28(%rsp), %r9
addq %r9, %rbp
addq %r11, %rbp
testq %r9, %r9
movq %r14, -0x1000(%r8,%r11,8)
movq %rcx, -0x800(%r8,%r11,8)
cmoveq %r9, %rbp
movq %rbp, (%r8,%r11,8)
cmpq %r11, 0x68(%rsp)
je 0x3424e
incq %r11
cmpq %r11, %rdx
jne 0x34093
addl %r11d, 0xc(%rsp)
movq 0x38(%rsp), %r14
movq 0x28(%rsp), %r10
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x48(%rsp), %rdi
jmp 0x3416a
movq 0x28(%rsp), %r10
testq %r10, %r10
movq 0x60(%rsp), %r9
je 0x34199
testb $0x10, 0x10(%rbx)
jne 0x341c1
movslq %edx, %rax
movzbl (%r15), %ecx
cmpl $0x2c, %ecx
je 0x34191
testl %ecx, %ecx
je 0x341ca
cmpl $0x3b, %ecx
jne 0x34194
jmp 0x341ca
addq %rax, %r10
incq %r15
jmp 0x3417d
testq %rsi, %rsi
je 0x341af
testb $0x10, 0x10(%rbx)
je 0x341af
xorl %r10d, %r10d
movq %rsi, %r15
jmp 0x341d6
incq %r15
movzbl (%r15), %eax
testl %eax, %eax
je 0x341bc
cmpl $0x3b, %eax
jne 0x341ac
xorl %r10d, %r10d
jmp 0x341d8
imull %edx, %edi
movslq %edi, %rax
addq %rax, %r10
movq %r15, %rsi
cmpq %r9, %r10
ja 0x34280
movb (%rsi), %al
testb %al, %al
je 0x3423c
movb 0x1(%r15), %al
incq %r15
movq %r15, %r13
movl $0x182045, %edx         # imm = 0x182045
movl $0x54, %esi
leaq 0x61769(%rip), %rdi     # 0x95960
jmp 0x33e56
movl $0x0, 0xc(%rsp)
testq %r10, %r10
je 0x34225
cmpq %r9, %r10
je 0x34225
movq 0x38(%rbx), %rcx
leaq 0x6474d(%rip), %rsi     # 0x98966
leaq 0x64880(%rip), %rdx     # 0x98aa0
jmp 0x33d6d
movl 0xc(%rsp), %eax
movl %eax, 0x2d04(%r14)
xorl %ebp, %ebp
jmp 0x33d7e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq 0x647af(%rip), %rdx     # 0x989f2
jmp 0x3425a
leaq 0x647d0(%rip), %rdx     # 0x98a1c
jmp 0x34255
leaq 0x647f9(%rip), %rdx     # 0x98a4e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x38(%rbx), %rcx
leaq 0x64701(%rip), %rsi     # 0x98966
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x33d7e
testl %eax, %eax
jne 0x33d7e
jmp 0x34204
leaq 0x647ec(%rip), %rdx     # 0x98a73
jmp 0x3425a