// File: /workspace/github_c_makefile_star2_O2/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 %rbx
subq $0x88, %rsp
movl %ecx, %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps 0x4fd56(%rip), %xmm0    # 0x7a8b0
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movl 0x2d04(%rsi), %r11d
movl 0x2d08(%rsi), %ecx
movzwl 0x10(%rdi), %r9d
xorl %r10d, %r10d
testl %r11d, %r11d
cmovgl %r11d, %r10d
xorl %r11d, %r11d
testb $0x10, %r9b
jne 0x2abdc
cmpq %r11, %r10
je 0x2ac36
testb $0x1, %cl
je 0x2abab
cmpl $0x0, 0x2900(%rsi,%r11,4)
je 0x2abce
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
jmp 0x2ab92
movslq 0x400(%rsi,%r11,4), %rbx
andl $0x0, (%rsp,%rbx,4)
jmp 0x2abc9
leaq 0x4fead(%rip), %rbx     # 0x7aa90
cmpq %r11, %r10
je 0x2ac36
testb $0x1, %cl
je 0x2abf8
cmpl $0x0, 0x2900(%rsi,%r11,4)
je 0x2ac20
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
jmp 0x2abe3
movslq 0x400(%rsi,%r11,4), %r14
movslq (%rbx,%r14,4), %r14
movl $0x1, (%rsp,%r14,4)
jmp 0x2ac1b
movl 0x2d00(%rsi), %r10d
movl 0x18(%rdi), %r11d
movl 0x3c(%rsp), %ebx
leal (,%r10,4), %ebp
cmpl %r11d, %r10d
jge 0x2acb1
movl 0x14(%rdi), %ecx
shll $0x2, %ecx
addq 0x38(%rdi), %rcx
movzwl 0x12(%rdi), %r14d
addq %rcx, %r14
movl %r10d, %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rcx,%r14), %r14d
movl %ebp, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r14d
andl $0xf, %r14d
leal (,%r14,4), %r15d
movl 0x40(%rsp,%r15), %ecx
cmpl %ecx, (%rsp,%r15)
jle 0x2acb1
cmpl 0x7c(%rsp), %ebx
jle 0x2acb1
incl %ecx
movl %ecx, 0x40(%rsp,%r14,4)
cmpq $0xf, %r14
je 0x2aca9
incl 0x7c(%rsp)
incl %r10d
addl $0x4, %ebp
jmp 0x2ac4d
movl %r10d, 0x2d00(%rsi)
movl %r10d, (%r8)
xorl %r8d, %r8d
testb $0x10, %r9b
jne 0x2ace9
movslq 0x2d04(%rsi), %rcx
cmpq %rcx, %r8
jge 0x2ad19
movslq 0x400(%rsi,%r8,4), %rcx
movl 0x40(%rsp,%rcx,4), %ecx
subl %ecx, 0x900(%rsi,%r8,4)
incq %r8
jmp 0x2acc4
leaq 0x4fda0(%rip), %r10     # 0x7aa90
movslq 0x2d04(%rsi), %rcx
cmpq %rcx, %r8
jge 0x2ad19
movslq 0x400(%rsi,%r8,4), %rcx
movslq (%r10,%rcx,4), %rcx
movl 0x40(%rsp,%rcx,4), %ecx
subl %ecx, 0x900(%rsi,%r8,4)
incq %r8
jmp 0x2acf0
movl 0x18(%rdi), %r8d
testl %r8d, %r8d
je 0x2ad2b
cmpl %r8d, 0x2d00(%rsi)
jge 0x2ad3b
movl %eax, %ecx
callq 0x2a79d
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
jmp 0x2ad92
xorl %eax, %eax
testb $0x10, %r9b
jne 0x2ad92
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x2ad92
cmpl $0x7f000000, 0x900(%rsi,%rdx,4) # imm = 0x7F000000
jl 0x2ad77
movq 0xd00(%rsi,%rdx,8), %rdi
movzbl (%rdi), %edi
testl %edi, %edi
je 0x2ad72
cmpl $0x3b, %edi
jne 0x2ad77
incq %rdx
jmp 0x2ad4c
leaq 0x4fcee(%rip), %rsi     # 0x7aa6c
leaq 0x4fbfb(%rip), %rdx     # 0x7a980
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x110c6
pushq $-0x1
popq %rax
addq $0x88, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq