// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq $0x0, 0x2d00(%rsi)
movl %edx, 0x2d08(%rsi)
leaq 0x669e5(%rip), %rsi     # 0x9b950
callq 0x2c960
movq %rax, %rbp
testq %rax, %rax
jne 0x34f8a
leaq 0x669d4(%rip), %rsi     # 0x9b953
movq %r13, %rdi
callq 0x2c960
movq %rax, %rbp
testq %rbp, %rbp
je 0x35616
cmpb $0x5a, (%rbp)
jne 0x34fd7
leaq 0x669e5(%rip), %rsi     # 0x9b985
movq %r13, %rdi
callq 0x2c960
testq %rax, %rax
je 0x34feb
movq %rax, %rdi
callq 0x2f5cb
movslq 0x18(%r13), %rcx
testq %rcx, %rcx
je 0x34feb
cmpq %rcx, %rax
je 0x34feb
movq 0x38(%r13), %rcx
leaq 0x66988(%rip), %rsi     # 0x9b956
leaq 0x669b3(%rip), %rdx     # 0x9b988
jmp 0x35041
movq 0x38(%r13), %rcx
leaq 0x66974(%rip), %rsi     # 0x9b956
leaq 0x66980(%rip), %rdx     # 0x9b969
jmp 0x35041
leaq 0x669cc(%rip), %rsi     # 0x9b9be
movq %r13, %rdi
callq 0x2c960
testq %rax, %rax
jne 0x3500e
leaq 0x669bb(%rip), %rsi     # 0x9b9c1
movq %r13, %rdi
callq 0x2c960
testq %rax, %rax
je 0x35057
cmpb $0x42, (%rax)
jne 0x3502f
cmpb $0x43, 0x1(%rax)
jne 0x3502f
movl 0x2(%rax), %ecx
addq %rax, %rcx
addq $0x6, %rcx
movq %rcx, 0x38(%rsp)
jmp 0x35060
movq 0x38(%r13), %rcx
leaq 0x6691c(%rip), %rsi     # 0x9b956
leaq 0x66983(%rip), %rdx     # 0x9b9c4
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x35618
movq $0x0, 0x38(%rsp)
leaq 0x6(%rax), %rcx
testq %rax, %rax
cmoveq %rax, %rcx
movq %rcx, 0x28(%rsp)
testb $0x10, 0x10(%r13)
je 0x350e8
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movl 0x18(%r13), %eax
testl %eax, %eax
jle 0x350e1
movl 0x14(%r13), %ecx
shll $0x2, %ecx
addq 0x38(%r13), %rcx
movzwl 0x12(%r13), %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 0x90(%rsp,%r8,4)
incl %edi
addl $0x4, %esi
cmpl %edi, %eax
jne 0x350b9
movl %eax, 0xcc(%rsp)
cmpb $0x0, 0x1(%rbp)
movq %rbx, 0x58(%rsp)
je 0x355d5
incq %rbp
leaq 0x1d00(%rbx), %rax
movq %rax, 0x70(%rsp)
leaq 0x800(%rbx), %rax
movq %rax, 0x68(%rsp)
movq $0x0, 0x10(%rsp)
movq %r13, 0x18(%rsp)
movb (%rbp), %al
testb %al, %al
je 0x355c9
leaq 0x1(%rbp), %rbx
movzbl %al, %eax
xorl %r12d, %r12d
leal -0x41(%rax), %ecx
cmpl $0x14, %ecx
ja 0x355b2
movl $0x182045, %edx         # imm = 0x182045
btl %ecx, %edx
jae 0x355b2
cmpb $0x55, %al
movl $0x54, %ecx
cmovel %ecx, %eax
movzbl %al, %eax
leaq 0x6374d(%rip), %rcx     # 0x988b0
movzbl (%rcx,%rax), %eax
movq %rax, 0x60(%rsp)
movzbl (%rbx), %r15d
cmpl $0x2d, %r15d
je 0x3517c
cmpl $0x2b, %r15d
jne 0x351ea
leaq 0x2(%rbp), %rbx
movq $0x0, 0x8(%rsp)
movzbl 0x2(%rbp), %r14d
callq 0x4510
movq (%rax), %rax
testb $0x8, 0x1(%rax,%r14,2)
jne 0x351f2
movzbl (%rbx), %edx
testq %rdx, %rdx
sete %cl
je 0x351ea
testb $0x4, 0x1(%rax,%rdx,2)
movq %rbx, %r14
je 0x351d7
addq $0x3, %rbp
movzbl (%rbp), %edx
testq %rdx, %rdx
sete %cl
je 0x35526
incq %rbp
testb $0x4, 0x1(%rax,%rdx,2)
jne 0x351b7
decq %rbp
movq %rbp, %r14
xorl %r12d, %r12d
movl $0x0, %eax
movq %rax, 0x48(%rsp)
testb %cl, %cl
je 0x35212
jmp 0x35256
xorl %r12d, %r12d
jmp 0x355b2
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq %rax, 0x48(%rsp)
movq 0x8(%rsp), %r14
leaq -0x1(%r14), %rbx
movzbl (%r14), %eax
movl $0x1, 0x34(%rsp)
xorl %r12d, %r12d
cmpl $0x3a, %eax
movl %r15d, 0x24(%rsp)
jg 0x3523b
cmpl $0x2c, %eax
je 0x3525e
cmpl $0x2e, %eax
jne 0x35256
leaq 0x1(%r14), %r15
jmp 0x35261
movq %r14, %r15
cmpl $0x3b, %eax
je 0x35261
cmpl $0x3f, %eax
jne 0x35256
leaq 0x1(%r14), %r15
movl $0x0, 0x34(%rsp)
jmp 0x35261
movq %r14, %rbx
jmp 0x355b2
movq %r14, %r15
testb $0x10, 0x10(%r13)
jne 0x3528a
cmpb $0x2c, (%r15)
jne 0x352fb
leaq 0x1(%r15), %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq %rax, %r12
jmp 0x352fe
movq %r14, 0x40(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %eax, %eax
cmpb $0x2c, (%r15)
sete %al
leaq (%r15,%rax), %r13
movzbl (%r15,%rax), %eax
testl %eax, %eax
je 0x352d8
cmpl $0x3b, %eax
je 0x352d8
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x46d0
movq %rax, %r12
movq 0x8(%rsp), %r15
cmpq %r13, %r15
je 0x3531c
addl %r12d, %r14d
incl %r14d
incl %ebp
jmp 0x35294
leaq 0x66841(%rip), %rax     # 0x9bb20
movq 0x60(%rsp), %rcx
movslq (%rax,%rcx,4), %rax
movslq 0x90(%rsp,%rax,4), %r12
movslq %r14d, %rax
subq %rax, %r12
movb $0x1, %r14b
jmp 0x35342
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
testq %rax, %rax
cmoveq %r15, %rax
movq %rax, 0x8(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmoveq %rax, %r12
xorl %ebp, %ebp
jmp 0x35358
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rcx
xorl %r14d, %r14d
movl $0x1, %edi
leaq 0x66622(%rip), %rsi     # 0x9b956
leaq 0x666a7(%rip), %rdx     # 0x9b9e2
xorl %eax, %eax
callq 0x14378
testb %r14b, %r14b
je 0x354dc
movq %r13, %r15
movq 0x18(%rsp), %r13
movq 0x40(%rsp), %r14
movq %r14, %rax
subq %rbx, %rax
jbe 0x354ad
movq 0x10(%rsp), %rdx
movl %edx, %ecx
movq %rcx, 0x80(%rsp)
movq 0x48(%rsp), %rcx
negl %ecx
movl %ecx, 0x54(%rsp)
cmpb $0x2d, 0x24(%rsp)
sete 0x24(%rsp)
incq %r14
movq %r14, 0x40(%rsp)
movq 0x8(%rsp), %rsi
movq %rbp, 0x78(%rsp)
leal -0x1(%rbp), %ecx
imull %eax, %ecx
movslq %ecx, %rcx
movq %rcx, 0x88(%rsp)
movslq %edx, %r9
cmpl $0x100, %r9d            # imm = 0x100
movl $0xff, %r10d
cmovgel %edx, %r10d
subl %r9d, %r10d
movq 0x70(%rsp), %rcx
leaq (%rcx,%r9,8), %r13
movq 0x58(%rsp), %rcx
leaq (%rcx,%r9,4), %rcx
addq 0x68(%rsp), %r9
xorl %r11d, %r11d
movl 0x54(%rsp), %r14d
cmpl $0x0, 0x48(%rsp)
jne 0x353ed
movsbl (%rbx,%r11), %r14d
movl %r14d, (%rcx,%r11,4)
movb 0x24(%rsp), %dl
movb %dl, (%r9,%r11)
movq 0x60(%rsp), %rdx
movl %edx, 0x400(%rcx,%r11,4)
movl %eax, 0x2500(%rcx,%r11,4)
movl 0x34(%rsp), %edx
movl %edx, 0x2900(%rcx,%r11,4)
testq %r12, %r12
js 0x35564
movl %r12d, 0x900(%rcx,%r11,4)
movq 0x18(%rsp), %rdx
movzwl 0x10(%rdx), %r14d
testb $0x10, %r14b
movq 0x40(%rsp), %r14
cmoveq %rsi, %r14
movq %rsi, %rdi
movl $0x0, %edx
cmoveq %rdx, %rdi
movq 0x88(%rsp), %r8
cmoveq %rdx, %r8
movq 0x28(%rsp), %rbp
addq %rbp, %r8
addq %r11, %r8
testq %rbp, %rbp
movq %r14, -0x1000(%r13,%r11,8)
movq %rdi, -0x800(%r13,%r11,8)
cmoveq %rbp, %r8
movq %r8, (%r13,%r11,8)
cmpq %r11, %r10
je 0x3557a
incq %r11
cmpq %r11, %rax
jne 0x353dc
movq 0x10(%rsp), %rcx
addl %r11d, %ecx
movq %rcx, 0x10(%rsp)
movq 0x18(%rsp), %r13
movq 0x78(%rsp), %rbp
movq 0x28(%rsp), %rdx
testq %rdx, %rdx
je 0x354ec
testb $0x10, 0x10(%r13)
jne 0x35531
cltq
movzbl (%r15), %ecx
cmpl $0x2c, %ecx
je 0x354d4
testl %ecx, %ecx
je 0x3553a
cmpl $0x3b, %ecx
jne 0x354d7
jmp 0x3553a
addq %rax, %rdx
incq %r15
jmp 0x354c0
xorl %r12d, %r12d
movq %r13, %rbx
movq 0x18(%rsp), %r13
jmp 0x355b2
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x3550e
testb $0x10, 0x10(%r13)
je 0x3550e
movq $0x0, 0x28(%rsp)
movq %rax, %r15
jmp 0x3554f
incq %r15
movzbl (%r15), %eax
testl %eax, %eax
je 0x3551b
cmpl $0x3b, %eax
jne 0x3550b
movq $0x0, 0x28(%rsp)
jmp 0x3554f
xorl %r12d, %r12d
movq %rbp, %rbx
jmp 0x355b2
imull %eax, %ebp
movslq %ebp, %rax
addq %rax, %rdx
movq %rdx, 0x28(%rsp)
cmpq 0x38(%rsp), %rdx
jbe 0x3554f
leaq 0x66516(%rip), %rdx     # 0x9ba63
jmp 0x35595
movb $0x1, %r12b
cmpb $0x0, (%r15)
leaq 0x66485(%rip), %rdx     # 0x9b9e2
movq %r15, %rbx
jne 0x355b2
jmp 0x35595
leaq 0x664a1(%rip), %rdx     # 0x9ba0c
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x35590
movq 0x10(%rsp), %rax
addl %r11d, %eax
incl %eax
movq %rax, 0x10(%rsp)
leaq 0x664ae(%rip), %rdx     # 0x9ba3e
movq 0x18(%rsp), %r13
movq 0x38(%r13), %rcx
xorl %r12d, %r12d
movl $0x1, %edi
leaq 0x663ae(%rip), %rsi     # 0x9b956
xorl %eax, %eax
callq 0x14378
movq %r15, %rbx
testb %r12b, %r12b
je 0x3504d
movb 0x1(%rbx), %al
incq %rbx
movq %rbx, %rbp
jmp 0x35123
cmpb $0x0, (%rbp)
jne 0x35120
jmp 0x355de
movq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x35606
cmpq 0x38(%rsp), %rax
je 0x35606
movq 0x38(%r13), %rcx
leaq 0x6635c(%rip), %rsi     # 0x9b956
leaq 0x6648f(%rip), %rdx     # 0x9ba90
jmp 0x35041
movq 0x58(%rsp), %rax
movq 0x10(%rsp), %rcx
movl %ecx, 0x2d04(%rax)
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq