// File: /workspace/github_c_makefile_star2_O2/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 $0xd8, %rsp
movq %rsi, %rbx
movq %rdi, %rbp
andq $0x0, 0x2d00(%rsi)
movl %edx, 0x2d08(%rsi)
leaq 0x5073a(%rip), %rsi     # 0x7a8c0
callq 0x24528
testq %rax, %rax
jne 0x2a1a8
leaq 0x5072c(%rip), %rsi     # 0x7a8c3
movq %rbp, %rdi
callq 0x24528
testq %rax, %rax
je 0x2a788
movq %rax, %r12
cmpb $0x5a, (%rax)
pushq $-0x1
popq %r13
jne 0x2a1f5
leaq 0x5073a(%rip), %rsi     # 0x7a8f5
movq %rbp, %rdi
callq 0x24528
testq %rax, %rax
je 0x2a20c
movq %rax, %rdi
callq 0x26135
movslq 0x18(%rbp), %rcx
testq %rcx, %rcx
je 0x2a20c
cmpq %rcx, %rax
je 0x2a20c
movq 0x38(%rbp), %rcx
leaq 0x506dd(%rip), %rsi     # 0x7a8c6
leaq 0x50708(%rip), %rdx     # 0x7a8f8
jmp 0x2a73e
movq 0x38(%rbp), %rcx
leaq 0x506c6(%rip), %rsi     # 0x7a8c6
leaq 0x506d2(%rip), %rdx     # 0x7a8d9
jmp 0x2a73e
leaq 0x5071b(%rip), %rsi     # 0x7a92e
movq %rbp, %rdi
callq 0x24528
testq %rax, %rax
jne 0x2a234
leaq 0x5070a(%rip), %rsi     # 0x7a931
movq %rbp, %rdi
callq 0x24528
testq %rax, %rax
je 0x2a26b
cmpb $0x42, (%rax)
jne 0x2a254
cmpb $0x43, 0x1(%rax)
jne 0x2a254
leaq 0x6(%rax), %r10
movl 0x2(%rax), %ecx
addq %rcx, %rax
addq $0x6, %rax
movq %rax, 0x30(%rsp)
jmp 0x2a277
movq 0x38(%rbp), %rcx
leaq 0x50667(%rip), %rsi     # 0x7a8c6
leaq 0x506ce(%rip), %rdx     # 0x7a934
jmp 0x2a73e
movq $0x0, 0x30(%rsp)
xorl %r10d, %r10d
testb $0x10, 0x10(%rbp)
je 0x2a2f7
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movl 0x14(%rbp), %ecx
movl 0x18(%rbp), %eax
shll $0x2, %ecx
addq 0x38(%rbp), %rcx
movzwl 0x12(%rbp), %edx
addq %rcx, %rdx
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %edi
cmovgl %eax, %edi
xorl %r8d, %r8d
cmpl %r8d, %edi
je 0x2a2f0
movl %r8d, %ecx
shrl %ecx
movzbl (%rdx,%rcx), %r9d
movl %esi, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r9d
andl $0xf, %r9d
incl 0x90(%rsp,%r9,4)
incl %r8d
addl $0x4, %esi
jmp 0x2a2c3
movl %eax, 0xcc(%rsp)
movb 0x1(%r12), %al
incq %r12
testb %al, %al
sete %cl
leaq 0x800(%rbx), %rdx
movq %rdx, 0x70(%rsp)
leaq 0x1d00(%rbx), %rdx
movq %rdx, 0x68(%rsp)
pushq $-0x1
popq %r13
movl $0x182045, %esi         # imm = 0x182045
pushq $0x54
popq %rdi
leaq 0x4d5e1(%rip), %r8      # 0x77910
movq $0x0, 0x28(%rsp)
testb $0x1, %cl
jne 0x2a75d
movzbl %al, %ecx
leal -0x41(%rcx), %edx
cmpl $0x14, %edx
ja 0x2a702
btl %edx, %esi
jae 0x2a702
cmpb $0x55, %cl
cmovel %edi, %ecx
movzbl %cl, %eax
movzbl (%r8,%rax), %eax
movq %rax, 0x58(%rsp)
movzbl 0x1(%r12), %eax
cmpl $0x2d, %eax
je 0x2a380
cmpl $0x2b, %eax
jne 0x2a748
movl %eax, 0x20(%rsp)
movq %r10, 0x50(%rsp)
andq $0x0, 0x10(%rsp)
movzbl 0x2(%r12), %r14d
addq $0x2, %r12
callq 0x4510
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r14,2)
movq %r12, %r15
jne 0x2a3c9
testb %r14b, %r14b
je 0x2a748
movzbl %r14b, %ecx
testb $0x4, 0x1(%rax,%rcx,2)
je 0x2a3ef
movb 0x1(%r15), %r14b
incq %r15
jmp 0x2a3ac
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x46d0
movq %rax, 0x40(%rsp)
movq 0x10(%rsp), %rcx
leaq -0x1(%rcx), %r12
movb (%rcx), %r14b
movq %rcx, %r15
jmp 0x2a3fa
xorl %ecx, %ecx
movq $0x0, 0x40(%rsp)
movq %r15, %r13
pushq $0x1
popq %rdx
cmpb $0x2c, %r14b
je 0x2a430
movzbl %r14b, %eax
cmpl $0x3f, %eax
je 0x2a42a
movq %r15, %r13
cmpl $0x3b, %eax
je 0x2a430
cmpl $0x2e, %eax
pushq $-0x1
popq %r13
jne 0x2a748
leaq 0x1(%r15), %r13
jmp 0x2a430
leaq 0x1(%r15), %r13
xorl %edx, %edx
testb $0x10, 0x10(%rbp)
movq %rbx, 0x38(%rsp)
movl %edx, 0x4c(%rsp)
jne 0x2a462
cmpb $0x2c, (%r13)
jne 0x2a4d7
leaq 0x1(%r13), %rdi
leaq 0x10(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x46d0
movq 0x10(%rsp), %rcx
jmp 0x2a4d9
xorl %r14d, %r14d
movq %r13, %rax
xorl %ebx, %ebx
xorl %edx, %edx
cmpb $0x2c, (%rax)
sete %dl
leaq (%rax,%rdx), %r13
movzbl (%rax,%rdx), %eax
testl %eax, %eax
je 0x2a4ad
cmpl $0x3b, %eax
je 0x2a4ad
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x46d0
movq 0x10(%rsp), %rcx
cmpq %r13, %rcx
je 0x2a728
addl %eax, %ebx
incl %ebx
incl %r14d
movq %rcx, %rax
jmp 0x2a46a
movq %rbp, 0x18(%rsp)
leaq 0x505d7(%rip), %rax     # 0x7aa90
movq 0x58(%rsp), %rdx
movslq (%rax,%rdx,4), %rax
movslq 0x90(%rsp,%rax,4), %rax
movslq %ebx, %rdx
subq %rdx, %rax
movq 0x38(%rsp), %rbx
jmp 0x2a4f6
xorl %eax, %eax
movq %rbp, 0x18(%rsp)
testq %rcx, %rcx
cmoveq %r13, %rcx
movq %rcx, 0x10(%rsp)
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmoveq %rdx, %rax
xorl %r14d, %r14d
movq %r15, %rdi
subq %r12, %rdi
movq 0x40(%rsp), %rdx
movl %edx, %r9d
negl %r9d
cmpb $0x2d, 0x20(%rsp)
sete 0xf(%rsp)
leaq 0x1(%r15), %rdx
movq %rdx, 0x80(%rsp)
movq %r14, 0x60(%rsp)
leal -0x1(%r14), %edx
movq %rdi, 0x20(%rsp)
imull %edi, %edx
movslq %edx, %rdx
movq %rdx, 0x78(%rsp)
movslq 0x28(%rsp), %r11
leaq (%rbx,%r11,4), %rbp
movq 0x70(%rsp), %rdx
addq %r11, %rdx
movq %rdx, 0x88(%rsp)
movq 0x68(%rsp), %rdx
leaq (%rdx,%r11,8), %rbx
xorl %edx, %edx
leaq (%r12,%rdx), %r8
cmpq %r15, %r8
jae 0x2a630
movl %r9d, %edi
cmpl $0x0, 0x40(%rsp)
jne 0x2a575
movsbl (%r8), %edi
movl %edi, (%rbp,%rdx,4)
movb 0xf(%rsp), %sil
movq 0x88(%rsp), %rdi
movb %sil, (%rdi,%rdx)
movq 0x58(%rsp), %rsi
movl %esi, 0x400(%rbp,%rdx,4)
movq 0x20(%rsp), %rsi
movl %esi, 0x2500(%rbp,%rdx,4)
movl 0x4c(%rsp), %esi
movl %esi, 0x2900(%rbp,%rdx,4)
testq %rax, %rax
js 0x2a70d
movq %r12, %r14
movl %eax, 0x900(%rbp,%rdx,4)
movq 0x18(%rsp), %rsi
movzwl 0x10(%rsi), %edi
testb $0x10, %dil
movq 0x80(%rsp), %rdi
cmoveq %rcx, %rdi
movq %rcx, %r8
movl $0x0, %esi
cmoveq %rsi, %r8
movq 0x78(%rsp), %r10
cmoveq %rsi, %r10
movq 0x50(%rsp), %r12
addq %r12, %r10
addq %rdx, %r10
movq %rdi, -0x1000(%rbx,%rdx,8)
testq %r12, %r12
cmoveq %r12, %r10
movq %r8, -0x800(%rbx,%rdx,8)
movq %r10, (%rbx,%rdx,8)
leaq (%r11,%rdx), %rsi
cmpq $0xfe, %rsi
jg 0x2a716
incq %rdx
movq %r14, %r12
jmp 0x2a55a
movq 0x50(%rsp), %r10
testq %r10, %r10
je 0x2a67b
movq 0x18(%rsp), %rbp
testb $0x10, 0x10(%rbp)
movq 0x38(%rsp), %rbx
movl $0x182045, %esi         # imm = 0x182045
pushq $0x54
popq %rdi
leaq 0x4d2b9(%rip), %r8      # 0x77910
jne 0x2a6bd
movslq 0x20(%rsp), %rax
movzbl (%r13), %ecx
cmpl $0x2c, %ecx
je 0x2a673
testl %ecx, %ecx
je 0x2a6cc
cmpl $0x3b, %ecx
jne 0x2a676
jmp 0x2a6cc
addq %rax, %r10
incq %r13
jmp 0x2a65e
testq %rcx, %rcx
movq 0x18(%rsp), %rbp
movq 0x38(%rsp), %rbx
movl $0x182045, %esi         # imm = 0x182045
pushq $0x54
popq %rdi
leaq 0x4d279(%rip), %r8      # 0x77910
je 0x2a6aa
testb $0x10, 0x10(%rbp)
je 0x2a6aa
xorl %r10d, %r10d
movq %rcx, %r13
jmp 0x2a6da
incq %r13
movzbl (%r13), %eax
testl %eax, %eax
je 0x2a6b8
cmpl $0x3b, %eax
jne 0x2a6a7
xorl %r10d, %r10d
jmp 0x2a6dc
movq 0x60(%rsp), %rax
imull 0x20(%rsp), %eax
cltq
addq %rax, %r10
movq %r13, %rcx
cmpq 0x30(%rsp), %r10
ja 0x2a78d
movb (%rcx), %al
testb %al, %al
je 0x2a728
movq 0x28(%rsp), %rax
movl %eax, %eax
addq %rdx, %rax
movq %rax, 0x28(%rsp)
movb 0x1(%r13), %al
incq %r13
movq %r13, %r12
pushq $-0x1
popq %r13
jmp 0x2a341
testl %ecx, %ecx
jne 0x2a748
movb $0x1, %cl
jmp 0x2a338
leaq 0x50268(%rip), %rdx     # 0x7a97c
jmp 0x2a71d
leaq 0x50291(%rip), %rdx     # 0x7a9ae
movq 0x18(%rsp), %rbp
pushq $-0x1
popq %r13
jmp 0x2a733
pushq $-0x1
popq %r13
leaq 0x5021f(%rip), %rdx     # 0x7a952
movq 0x38(%rbp), %rcx
leaq 0x50188(%rip), %rsi     # 0x7a8c6
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movl %r13d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r10, %r10
je 0x2a77d
cmpq 0x30(%rsp), %r10
je 0x2a77d
movq 0x38(%rbp), %rcx
leaq 0x50152(%rip), %rsi     # 0x7a8c6
leaq 0x50285(%rip), %rdx     # 0x7aa00
jmp 0x2a73e
movq 0x28(%rsp), %rax
movl %eax, 0x2d04(%rbx)
xorl %r13d, %r13d
jmp 0x2a748
leaq 0x5023f(%rip), %rdx     # 0x7a9d3
jmp 0x2a722