// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/misc/bamcheck.c
// Function: count_mismatches_per_cycle
void count_mismatches_per_cycle(stats_t *stats,bam1_t *bam_line) 
{
    int is_fwd = IS_REVERSE(bam_line) ? 0 : 1;
    int icig,iread=0,icycle=0;
    int iref = bam_line->core.pos - stats->rseq_pos;
    int read_len   = bam_line->core.l_qseq;
    uint8_t *read  = bam1_seq(bam_line);
    uint8_t *quals = bam1_qual(bam_line);
    uint64_t *mpc_buf = stats->mpc_buf;
    for (icig=0; icig<bam_line->core.n_cigar; icig++) 
    {
        // Conversion from uint32_t to MIDNSHP
        //  0123456
        //  MIDNSHP
        int cig  = bam1_cigar(bam_line)[icig] & BAM_CIGAR_MASK;
        int ncig = bam1_cigar(bam_line)[icig] >> BAM_CIGAR_SHIFT;
        if ( cig==1 )
        {
            iread  += ncig;
            icycle += ncig;
            continue;
        }
        if ( cig==2 )
        {
            iref += ncig;
            continue;
        }
        if ( cig==4 )
        {
            icycle += ncig;
            // Soft-clips are present in the sequence, but the position of the read marks a start of non-clipped sequence
            //   iref += ncig;
            iread  += ncig;
            continue;
        }
        if ( cig==5 )
        {
            icycle += ncig;
            continue;
        }
        // Ignore H and N CIGARs. The letter are inserted e.g. by TopHat and often require very large
        //  chunk of refseq in memory. Not very frequent and not noticable in the stats.
        if ( cig==3 || cig==5 ) continue;
        if ( cig!=0 )
            error("TODO: cigar %d, %s:%d %s\n", cig,stats->sam->header->target_name[bam_line->core.tid],bam_line->core.pos+1,bam1_qname(bam_line));
       
        if ( ncig+iref > stats->nrseq_buf )
            error("FIXME: %d+%d > %d, %s, %s:%d\n",ncig,iref,stats->nrseq_buf, bam1_qname(bam_line),stats->sam->header->target_name[bam_line->core.tid],bam_line->core.pos+1);

        int im;
        for (im=0; im<ncig; im++)
        {
            uint8_t cread = bam1_seqi(read,iread);
            uint8_t cref  = stats->rseq_buf[iref];

            // ---------------15
            // =ACMGRSVTWYHKDBN
            if ( cread==15 )
            {
                int idx = is_fwd ? icycle : read_len-icycle-1;
                if ( idx>stats->max_len )
                    error("mpc: %d>%d\n",idx,stats->max_len);
                idx = idx*stats->nquals;
                if ( idx>=stats->nquals*stats->nbases )
                    error("FIXME: mpc_buf overflow\n");
                mpc_buf[idx]++;
            }
            else if ( cref && cread && cref!=cread )
            {
                uint8_t qual = quals[iread] + 1;
                if ( qual>=stats->nquals )
                    error("TODO: quality too high %d>=%d (%s %d %s)\n", qual,stats->nquals, stats->sam->header->target_name[bam_line->core.tid],bam_line->core.pos+1,bam1_qname(bam_line));

                int idx = is_fwd ? icycle : read_len-icycle-1;
                if ( idx>stats->max_len )
                    error("mpc: %d>%d\n",idx,stats->max_len);

                idx = idx*stats->nquals + qual;
                if ( idx>=stats->nquals*stats->nbases )
                    error("FIXME: mpc_buf overflow\n");
                mpc_buf[idx]++;
            }

            iref++;
            iread++;
            icycle++;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rsp)
shrq $0x30, %rax
je 0x4cd3
movl 0x10(%rsi), %r9d
movq 0x30(%rsi), %r10
leaq (%r10,%rax,4), %r15
shlq $0x2, %rax
movq 0x8(%rsp), %r11
movl %r11d, %ecx
shrl $0x18, %ecx
addq %rcx, %r15
leal 0x1(%r9), %edx
sarl %edx
movslq %edx, %rbx
addq %rax, %rbx
movq 0x198(%rdi), %rax
movl 0x4(%rsi), %edx
subl 0x18c(%rdi), %edx
movq 0x30(%rsi), %r8
addq %rcx, %r10
addq %rbx, %r10
movq %r10, 0x30(%rsp)
movq %rax, %r10
decl %r9d
movq %r9, 0x38(%rsp)
xorl %r13d, %r13d
leaq 0x14514(%rip), %r14     # 0x19024
movq %r11, %rax
xorl %r11d, %r11d
xorl %r12d, %r12d
movq %rsi, 0x20(%rsp)
movq %r8, 0x28(%rsp)
shrl $0x18, %eax
addq %r8, %rax
movl (%rax,%r13,4), %ebx
movl %ebx, %r9d
andl $0xf, %r9d
cmpl $0x5, %r9d
ja 0x4d69
movl %ebx, %eax
shrl $0x4, %eax
movslq (%r14,%r9,4), %rcx
addq %r14, %rcx
jmpq *%rcx
leal (%rax,%rdx), %r9d
movl 0x190(%rdi), %ecx
cmpl %ecx, %r9d
jg 0x4d37
cmpl $0x10, %ebx
jb 0x4cbc
movl %r9d, 0x14(%rsp)
movl %r11d, 0x1c(%rsp)
movslq %r11d, %rax
movslq %edx, %r8
movl %eax, %ebp
leal (,%rax,4), %r9d
addq 0x30(%rsp), %rax
movq %rax, 0x40(%rsp)
movl %r12d, %r14d
movq 0x38(%rsp), %rax
movl %eax, %esi
movl %r12d, 0x18(%rsp)
subl %r12d, %esi
shrl $0x4, %ebx
xorl %r12d, %r12d
leal (%r12,%rbp), %eax
sarl %eax
cltq
movzbl (%r15,%rax), %edx
movl %r9d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edx
leaq (%r14,%r12), %rax
andl $0xf, %edx
cmpl $0xf, %edx
jne 0x4bf2
btq $0x24, 0x8(%rsp)
cmovbl %esi, %eax
movl 0x90(%rdi), %edx
cmpl %edx, %eax
jg 0x4ce2
movl 0x4(%rdi), %ecx
imull %ecx, %eax
imull 0x8(%rdi), %ecx
cmpl %ecx, %eax
jl 0x4c5d
jmp 0x4cf2
testl %edx, %edx
je 0x4c63
movq 0x180(%rdi), %rcx
addq %r8, %rcx
movb (%r12,%rcx), %cl
testb %cl, %cl
je 0x4c63
movzbl %cl, %ecx
cmpl %ecx, %edx
je 0x4c63
movq %r10, %r11
movq 0x40(%rsp), %rcx
movb (%rcx,%r12), %cl
incb %cl
movzbl %cl, %r10d
movl 0x4(%rdi), %edx
cmpl %r10d, %edx
jle 0x4d00
btq $0x24, 0x8(%rsp)
cmovbl %esi, %eax
movl 0x90(%rdi), %edx
cmpl %edx, %eax
jg 0x4ce2
movl 0x4(%rdi), %ecx
imull %ecx, %eax
addl %r10d, %eax
imull 0x8(%rdi), %ecx
cmpl %ecx, %eax
movq %r11, %r10
jge 0x4cf2
cltq
incq (%r10,%rax,8)
incq %r12
addl $0x4, %r9d
decl %esi
cmpl %r12d, %ebx
jne 0x4ba4
movl 0x1c(%rsp), %r11d
addl %r12d, %r11d
movl 0x18(%rsp), %eax
addl %r12d, %eax
movl %eax, %r12d
movl 0x14(%rsp), %eax
movl %eax, %edx
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %r8
leaq 0x14386(%rip), %r14     # 0x19024
jmp 0x4cbc
addl %eax, %r12d
addl %eax, %r11d
jmp 0x4cbc
addl %eax, %r11d
addl %eax, %r12d
jmp 0x4cbc
addl %edx, %eax
movl %eax, %edx
jmp 0x4cbc
addl %r12d, %eax
movl %eax, %r12d
incq %r13
movq 0x8(%rsi), %rax
movq %rax, %rcx
shrq $0x30, %rcx
cmpq %rcx, %r13
jb 0x4b23
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x145f1(%rip), %rdi     # 0x192da
movl %eax, %esi
xorl %eax, %eax
callq 0x465a
leaq 0x145ed(%rip), %rdi     # 0x192e6
xorl %eax, %eax
callq 0x465a
movq 0x1c8(%rdi), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq 0x20(%rsp), %rsi
movslq (%rsi), %rcx
movq (%rax,%rcx,8), %rcx
movl 0x4(%rsi), %r8d
incl %r8d
movq 0x30(%rsi), %r9
leaq 0x145d2(%rip), %rdi     # 0x192ff
movl %r10d, %esi
xorl %eax, %eax
callq 0x465a
movq 0x30(%rsi), %r8
movq 0x1c8(%rdi), %rdi
movq 0x10(%rdi), %rdi
movq 0x8(%rdi), %r9
movslq (%rsi), %rdi
movq (%r9,%rdi,8), %r9
movl 0x4(%rsi), %esi
incl %esi
movl %esi, (%rsp)
leaq 0x1455c(%rip), %rdi     # 0x192bc
movl %eax, %esi
xorl %eax, %eax
callq 0x465a
movq 0x1c8(%rdi), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movslq (%rsi), %rcx
movq (%rax,%rcx,8), %rdx
movl 0x4(%rsi), %ecx
incl %ecx
leaq 0x14517(%rip), %rdi     # 0x192a2
movl %r9d, %esi
xorl %eax, %eax
callq 0x465a