// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq 0x8(%rsi), %rax
movq %rax, 0x10(%rsp)
shrq $0x30, %rax
je 0x4f8f
movq %rsi, %r10
movl 0x10(%rsi), %esi
movq 0x30(%r10), %r8
leaq (%r8,%rax,4), %r15
shlq $0x2, %rax
movq 0x10(%rsp), %r9
movl %r9d, %r11d
shrl $0x18, %r11d
addq %r11, %r15
leal 0x1(%rsi), %ecx
sarl %ecx
movslq %ecx, %rcx
addq %rax, %rcx
movq 0x198(%rdi), %rbx
movl 0x4(%r10), %eax
movl %eax, 0xc(%rsp)
movl %eax, %edx
subl 0x18c(%rdi), %edx
addq %r8, %r11
addq %rcx, %r11
movq %r11, 0x38(%rsp)
movq %rbx, %r11
decl %esi
movq %rsi, 0x40(%rsp)
xorl %ebp, %ebp
leaq 0x15264(%rip), %rbx     # 0x1a024
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r8, 0x30(%rsp)
movq %r10, 0x28(%rsp)
movl %r9d, %eax
shrl $0x18, %eax
addq %r8, %rax
movl (%rax,%rbp,4), %r14d
movl %r14d, %esi
andl $0xf, %esi
cmpl $0x5, %esi
ja 0x5032
movl %r14d, %eax
shrl $0x4, %eax
movslq (%rbx,%rsi,4), %rcx
addq %rbx, %rcx
jmpq *%rcx
leal (%rax,%rdx), %esi
movl 0x190(%rdi), %ecx
movl %esi, 0x24(%rsp)
cmpl %ecx, %esi
jg 0x5003
cmpl $0x10, %r14d
jb 0x4f7c
movl %r12d, 0x20(%rsp)
movslq %r12d, %rax
movslq %edx, %rcx
movq %rcx, 0x50(%rsp)
movl %eax, %r8d
leal (,%rax,4), %r12d
addq 0x38(%rsp), %rax
movq %rax, 0x48(%rsp)
movl %r13d, %r9d
movq 0x40(%rsp), %rax
movl %r13d, %ecx
movl %eax, %r13d
movl %ecx, 0x1c(%rsp)
subl %ecx, %r13d
shrl $0x4, %r14d
xorl %ebx, %ebx
movq 0x10(%rsp), %r10
leal (%r8,%rbx), %eax
sarl %eax
cltq
movzbl (%r15,%rax), %edx
movl %r12d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edx
leaq (%r9,%rbx), %rax
andl $0xf, %edx
cmpl $0xf, %edx
jne 0x4eac
btq $0x24, %r10
cmovbl %r13d, %eax
movl 0x90(%rdi), %edx
cmpl %edx, %eax
jg 0x4fac
movl 0x4(%rdi), %ecx
imull %ecx, %eax
imull 0x8(%rdi), %ecx
cmpl %ecx, %eax
jl 0x4f1a
jmp 0x4f9e
testl %edx, %edx
je 0x4f20
movq 0x180(%rdi), %rcx
addq 0x50(%rsp), %rcx
movb (%rbx,%rcx), %cl
testb %cl, %cl
je 0x4f20
movzbl %cl, %ecx
cmpl %ecx, %edx
je 0x4f20
movq %r11, %rsi
movq 0x48(%rsp), %rcx
movb (%rcx,%rbx), %cl
incb %cl
movzbl %cl, %r11d
movl 0x4(%rdi), %edx
cmpl %r11d, %edx
jle 0x4fb7
btq $0x24, %r10
cmovbl %r13d, %eax
movl 0x90(%rdi), %r10d
cmpl %r10d, %eax
jg 0x4ff0
imull %edx, %eax
addl %r11d, %eax
imull 0x8(%rdi), %edx
cmpl %edx, %eax
movq 0x10(%rsp), %r10
movq %rsi, %r11
jge 0x4f9e
cltq
incq (%r11,%rax,8)
incq %rbx
addl $0x4, %r12d
decl %r13d
cmpl %ebx, %r14d
jne 0x4e5f
movl 0x20(%rsp), %r12d
addl %ebx, %r12d
movq 0x28(%rsp), %r10
movq 0x8(%r10), %r9
movl 0x1c(%rsp), %r13d
addl %ebx, %r13d
movl 0x24(%rsp), %eax
movl %eax, %edx
movq 0x30(%rsp), %r8
leaq 0x150c6(%rip), %rbx     # 0x1a024
jmp 0x4f7c
addl %eax, %r13d
addl %eax, %r12d
jmp 0x4f7c
addl %eax, %r12d
addl %eax, %r13d
jmp 0x4f7c
addl %edx, %eax
movl %eax, %edx
jmp 0x4f7c
addl %r13d, %eax
movl %eax, %r13d
incq %rbp
movq %r9, %rax
shrq $0x30, %rax
cmpq %rax, %rbp
jb 0x4dd0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x153f1(%rip), %rdi     # 0x1a396
xorl %eax, %eax
callq 0x4926
leaq 0x153d7(%rip), %rdi     # 0x1a38a
movl %eax, %esi
jmp 0x4ffc
movq 0x1c8(%rdi), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq 0x28(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movl 0xc(%rsp), %r8d
incl %r8d
leaq 0x153ce(%rip), %rdi     # 0x1a3af
movl %r11d, %esi
movq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0x4926
leaq 0x15393(%rip), %rdi     # 0x1a38a
movl %eax, %esi
movl %r10d, %edx
xorl %eax, %eax
callq 0x4926
movq 0x1c8(%rdi), %rsi
movq 0x10(%rsi), %rsi
movq 0x8(%rsi), %rsi
movslq (%r10), %rdi
movq (%rsi,%rdi,8), %r9
movl 0xc(%rsp), %esi
incl %esi
movl %esi, (%rsp)
leaq 0x15343(%rip), %rdi     # 0x1a36c
movl %eax, %esi
xorl %eax, %eax
callq 0x4926
movq 0x1c8(%rdi), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movslq (%r10), %rdx
movq (%rax,%rdx,8), %rdx
movl 0xc(%rsp), %ecx
incl %ecx
leaq 0x152fd(%rip), %rdi     # 0x1a352
xorl %eax, %eax
callq 0x4926