// File: /workspace/github_c_makefile_star2_O3/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 0x64286(%rip), %xmm0    # 0x98950
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 0x3474a
testl %ecx, %ecx
jle 0x347ac
xorl %r11d, %r11d
testb $0x1, %r10b
je 0x34711
cmpl $0x0, 0x2900(%rsi,%r11,4)
je 0x34739
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 0x34700
jmp 0x347ac
movslq 0x400(%rsi,%r11,4), %rbx
movl $0x0, (%rsp,%rbx,4)
jmp 0x3472f
testl %ecx, %ecx
jle 0x347ac
xorl %r11d, %r11d
leaq 0x643d8(%rip), %rbx     # 0x98b30
testb $0x1, %r10b
je 0x34769
cmpl $0x0, 0x2900(%rsi,%r11,4)
je 0x34796
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 0x34758
jmp 0x347ac
movslq 0x400(%rsi,%r11,4), %r14
movslq (%rbx,%r14,4), %r14
movl $0x1, (%rsp,%r14,4)
jmp 0x3478c
movl 0x2d00(%rsi), %r10d
movl 0x18(%rdi), %r11d
cmpl %r11d, %r10d
jge 0x3482e
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 0x3482e
cmpl 0x7c(%rsp), %ebp
jle 0x3482e
incl %ecx
movl %ecx, 0x40(%rsp,%r15,4)
cmpq $0xf, %r15
je 0x3481f
incl 0x7c(%rsp)
incl %r10d
addl $0x4, %r14d
cmpl %r10d, %r11d
jne 0x347d9
movl %r11d, %r10d
movl %r10d, 0x2d00(%rsi)
movl %r10d, (%r8)
movl 0x2d04(%rsi), %ecx
testb $0x10, %r9b
jne 0x34870
testl %ecx, %ecx
jle 0x348a5
xorl %r8d, %r8d
movslq 0x400(%rsi,%r8,4), %rcx
movl 0x40(%rsp,%rcx,4), %ecx
subl %ecx, 0x900(%rsi,%r8,4)
incq %r8
movslq 0x2d04(%rsi), %rcx
cmpq %rcx, %r8
jl 0x3484b
jmp 0x348a5
testl %ecx, %ecx
jle 0x348a5
xorl %r8d, %r8d
leaq 0x642b2(%rip), %r10     # 0x98b30
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
movslq 0x2d04(%rsi), %rcx
cmpq %rcx, %r8
jl 0x3487e
movl 0x18(%rdi), %r8d
testl %r8d, %r8d
je 0x348b7
cmpl %r8d, 0x2d00(%rsi)
jge 0x348c7
movl %eax, %ecx
callq 0x34290
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
jmp 0x3492a
testb $0x10, %r9b
sete %al
testl %ecx, %ecx
setg %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x34907
movl %ecx, %eax
xorl %ecx, %ecx
cmpl $0x7f000000, 0x900(%rsi,%rcx,4) # imm = 0x7F000000
jl 0x3490b
movq 0xd00(%rsi,%rcx,8), %rdx
movzbl (%rdx), %edx
testl %edx, %edx
je 0x348ff
cmpl $0x3b, %edx
jne 0x3490b
incq %rcx
cmpq %rcx, %rax
jne 0x348de
xorl %eax, %eax
jmp 0x3492a
leaq 0x641fa(%rip), %rsi     # 0x98b0c
leaq 0x64107(%rip), %rdx     # 0x98a20
movl $0x3, %edi
xorl %eax, %eax
callq 0x13de5
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq