// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/mapper.cpp
// Function: recoverCIGAR4(int, unsigned int*, unsigned char const*, unsigned char const*, std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char>>&)
int recoverCIGAR4(int n_cigar, uint32_t *BamCigar, const uint8_t * query, const uint8_t * rseq, stringstream & sNM){
    int i, k, x, y, n_mm = 0, n_gap = 0;
    for (k = 0, x = y = 0; k < n_cigar; ++k) {
        int op  = BamCigar[k]&0xf;
        int len = BamCigar[k]>>4;
        if (op == 0) { // match
            int matchLen = 0;
            int mmLen = 0;
            for(i = 0; i < len; i++){
                if(query[x + i] != rseq[y + i]){
                    ++n_mm; ++mmLen;
                    if(matchLen){
                        sNM << matchLen << "=";
                        matchLen = 0;
                    }
                }
                else{
                    ++matchLen;
                    if(mmLen){
                        sNM << mmLen << "X";
                        mmLen = 0;
                    }
                }
            }
            if(matchLen)
                sNM << matchLen << "=";
            else
                sNM << mmLen << "X";
            x += len; y += len;
        } 
        else{
            sNM << len << "MIDSH"[op];
            if (op == 1) x += len, n_gap += len;
            else if (op == 2) y += len, n_gap += len;
        }
    }
    return n_mm + n_gap;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movl %edi, %r14d
addq $0x10, %r8
movq %r8, 0x8(%rsp)
xorl %r15d, %r15d
testl %edi, %edi
cmovlel %r15d, %r14d
movq %rsi, %r12
xorl %r13d, %r13d
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
movq %r14, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
cmpq %r14, %r15
je 0x192dc
movl (%r12,%r15,4), %ecx
movl %ecx, %ebx
andl $0xf, %ebx
shrl $0x4, %ecx
testq %rbx, %rbx
movq %rcx, 0x10(%rsp)
je 0x191ea
movq 0x8(%rsp), %rdi
movl %ecx, %esi
callq 0x75c0
leaq 0x1db31(%rip), %rcx     # 0x36cf0
movsbl (%rbx,%rcx), %esi
movq %rax, %rdi
callq 0x7330
cmpl $0x2, %ebx
je 0x192ca
cmpl $0x1, %ebx
jne 0x192d4
movq 0x10(%rsp), %rax
addl %eax, %r13d
jmp 0x192d2
movl %ebp, 0x1c(%rsp)
movq %r15, 0x40(%rsp)
movl %r13d, %eax
movslq (%rsp), %r13
movl %eax, 0x18(%rsp)
movslq %eax, %rbx
addq 0x30(%rsp), %rbx
addq 0x38(%rsp), %r13
xorl %r12d, %r12d
xorl %esi, %esi
xorl %r15d, %r15d
cmpq %r12, %rcx
je 0x19277
movb (%rbx,%r12), %al
cmpb (%r13,%r12), %al
jne 0x19240
movl %esi, %r14d
incl %r14d
movl %r15d, %esi
leaq 0x1c445(%rip), %rbp     # 0x35678
testl %r15d, %r15d
movl $0x0, %r15d
jne 0x19255
jmp 0x1926f
incl 0x4(%rsp)
incl %r15d
xorl %r14d, %r14d
leaq 0x192a2(%rip), %rbp     # 0x324f3
testl %esi, %esi
je 0x1926f
movq 0x8(%rsp), %rdi
callq 0x75c0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x72b0
movq 0x10(%rsp), %rcx
incq %r12
movl %r14d, %esi
jmp 0x19213
testl %esi, %esi
cmovnel %esi, %r15d
leaq 0x1926f(%rip), %rbp     # 0x324f3
leaq 0x1c3ed(%rip), %rax     # 0x35678
cmoveq %rax, %rbp
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movq %rcx, %rbx
callq 0x75c0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x72b0
movl 0x18(%rsp), %r13d
addl %ebx, %r13d
addl %ebx, (%rsp)
movq 0x28(%rsp), %r14
movq 0x40(%rsp), %r15
movq 0x20(%rsp), %r12
movl 0x1c(%rsp), %ebp
jmp 0x192d4
movq 0x10(%rsp), %rax
addl %eax, (%rsp)
addl %eax, %ebp
incq %r15
jmp 0x1918d
addl 0x4(%rsp), %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop