// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/sam_mods.c
// Function: bam_next_basemod
int bam_next_basemod(const bam1_t *b, hts_base_mod_state *state,
                     hts_base_mod *mods, int n_mods, int *pos) {
    // Look through state->MMcount arrays to see when the next lowest is
    // per base type;
    int next[16], freq[16] = {0}, i;
    memset(next, 0x7f, 16*sizeof(*next));
    const int unchecked = state->flags & HTS_MOD_REPORT_UNCHECKED;
    if (b->core.flag & BAM_FREVERSE) {
        for (i = 0; i < state->nmods; i++) {
            if (unchecked && !state->implicit[i])
                next[seqi_rc[state->canonical[i]]] = 1;
            else if (next[seqi_rc[state->canonical[i]]] > state->MMcount[i])
                next[seqi_rc[state->canonical[i]]] = state->MMcount[i];
        }
    } else {
        for (i = 0; i < state->nmods; i++) {
            if (unchecked && !state->implicit[i])
                next[state->canonical[i]] = 0;
            else if (next[state->canonical[i]] > state->MMcount[i])
                next[state->canonical[i]] = state->MMcount[i];
       }
    }

    // Now step through the sequence counting off base types.
    for (i = state->seq_pos; i < b->core.l_qseq; i++) {
        unsigned char bc = bam_seqi(bam_get_seq(b), i);
        if (next[bc] <= freq[bc] || next[15] <= freq[15])
            break;
        freq[bc]++;
        if (bc != 15) // N
            freq[15]++;
    }
    *pos = state->seq_pos = i;

    if (b->core.flag & BAM_FREVERSE) {
        for (i = 0; i < state->nmods; i++)
            state->MMcount[i] -= freq[seqi_rc[state->canonical[i]]];
    } else {
        for (i = 0; i < state->nmods; i++)
            state->MMcount[i] -= freq[state->canonical[i]];
    }

    if (b->core.l_qseq && state->seq_pos >= b->core.l_qseq) {
        if (!(b->core.flag & BAM_FREVERSE)) {
            // Spots +ve orientation run-overs.
            // The -ve orientation is spotted in bam_parse_basemod2
            int i;
            for (i = 0; i < state->nmods; i++) {
                // Check if any remaining items in MM after hitting the end
                // of the sequence.
                if (state->MMcount[i] < 0x7f000000 ||
                    (*state->MM[i]!=0 && *state->MM[i]!=';')) {
                    hts_log_warning("MM tag refers to bases beyond sequence length");
                    return -1;
                }
            }
        }
        return 0;
    }

    int r = bam_mods_at_next_pos(b, state, mods, n_mods);
    return r > 0 ? r : 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x80, %rsp
movl %ecx, %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps 0x65e67(%rip), %xmm0    # 0x9b940
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movl 0x2d04(%rsi), %ecx
movl 0x2d08(%rsi), %r10d
movzwl 0x10(%rdi), %r9d
testb $0x10, %r9b
jne 0x35b59
testl %ecx, %ecx
jle 0x35bbb
xorl %r11d, %r11d
testb $0x1, %r10b
je 0x35b20
cmpl $0x0, 0x2900(%rsi,%r11,4)
je 0x35b48
movslq 0x400(%rsi,%r11,4), %rbx
movl (%rsp,%rbx,4), %ebp
movl 0x900(%rsi,%r11,4), %r14d
cmpl %r14d, %ebp
cmovll %ebp, %r14d
movl %r14d, (%rsp,%rbx,4)
incq %r11
cmpq %r11, %rcx
jne 0x35b0f
jmp 0x35bbb
movslq 0x400(%rsi,%r11,4), %rbx
movl $0x0, (%rsp,%rbx,4)
jmp 0x35b3e
testl %ecx, %ecx
jle 0x35bbb
xorl %r11d, %r11d
leaq 0x65fb9(%rip), %rbx     # 0x9bb20
testb $0x1, %r10b
je 0x35b78
cmpl $0x0, 0x2900(%rsi,%r11,4)
je 0x35ba5
movslq 0x400(%rsi,%r11,4), %r14
movslq (%rbx,%r14,4), %r14
movl (%rsp,%r14,4), %ebp
movl 0x900(%rsi,%r11,4), %r15d
cmpl %r15d, %ebp
cmovll %ebp, %r15d
movl %r15d, (%rsp,%r14,4)
incq %r11
cmpq %r11, %rcx
jne 0x35b67
jmp 0x35bbb
movslq 0x400(%rsi,%r11,4), %r14
movslq (%rbx,%r14,4), %r14
movl $0x1, (%rsp,%r14,4)
jmp 0x35b9b
movl 0x2d00(%rsi), %r10d
movl 0x18(%rdi), %r11d
cmpl %r11d, %r10d
jge 0x35c3d
movl 0x14(%rdi), %ecx
shll $0x2, %ecx
addq 0x38(%rdi), %rcx
movzwl 0x12(%rdi), %ebx
addq %rcx, %rbx
movl 0x3c(%rsp), %ebp
leal (,%r10,4), %r14d
movl %r10d, %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rbx,%rcx), %r15d
movl %r14d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r15d
andl $0xf, %r15d
leal (,%r15,4), %r12d
movl 0x40(%rsp,%r12), %ecx
cmpl %ecx, (%rsp,%r12)
jle 0x35c3d
cmpl 0x7c(%rsp), %ebp
jle 0x35c3d
incl %ecx
movl %ecx, 0x40(%rsp,%r15,4)
cmpq $0xf, %r15
je 0x35c2e
incl 0x7c(%rsp)
incl %r10d
addl $0x4, %r14d
cmpl %r10d, %r11d
jne 0x35be8
movl %r11d, %r10d
movl %r10d, 0x2d00(%rsi)
movl %r10d, (%r8)
movl 0x2d04(%rsi), %ecx
testb $0x10, %r9b
jne 0x35c7f
testl %ecx, %ecx
jle 0x35cb4
xorl %ecx, %ecx
movslq 0x400(%rsi,%rcx,4), %r8
movl 0x40(%rsp,%r8,4), %r8d
subl %r8d, 0x900(%rsi,%rcx,4)
incq %rcx
movslq 0x2d04(%rsi), %r8
cmpq %r8, %rcx
jl 0x35c59
jmp 0x35cb4
testl %ecx, %ecx
jle 0x35cb4
xorl %ecx, %ecx
leaq 0x65e94(%rip), %r8      # 0x9bb20
movslq 0x400(%rsi,%rcx,4), %r10
movslq (%r8,%r10,4), %r10
movl 0x40(%rsp,%r10,4), %r10d
subl %r10d, 0x900(%rsi,%rcx,4)
incq %rcx
movslq 0x2d04(%rsi), %r10
cmpq %r10, %rcx
jl 0x35c8c
movl 0x18(%rdi), %ecx
testl %ecx, %ecx
je 0x35cc3
cmpl %ecx, 0x2d00(%rsi)
jge 0x35ce1
movl %eax, %ecx
callq 0x35631
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
testb $0x10, %r9b
jne 0x35cd1
movslq 0x2d04(%rsi), %rcx
testq %rcx, %rcx
jle 0x35cd1
xorl %eax, %eax
cmpl $0x7f000000, 0x900(%rsi,%rax,4) # imm = 0x7F000000
jl 0x35d24
movq 0xd00(%rsi,%rax,8), %rdx
movzbl (%rdx), %edx
testl %edx, %edx
je 0x35d18
cmpl $0x3b, %edx
jne 0x35d24
incq %rax
cmpq %rax, %rcx
jne 0x35cf7
xorl %eax, %eax
jmp 0x35cd1
leaq 0x65dd1(%rip), %rsi     # 0x9bafc
leaq 0x65cde(%rip), %rdx     # 0x9ba10
movl $0x3, %edi
xorl %eax, %eax
callq 0x14378
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x35cd1