// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
testl %edi, %edi
jle 0x1ef82
movq %rsi, %r12
addq $0x10, %r8
movl %edi, %esi
xorl %ecx, %ecx
xorl %eax, %eax
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movq %r8, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
movl (%r12,%rcx,4), %edx
movl %edx, %ebx
andl $0xf, %ebx
movl %edx, %r14d
shrl $0x4, %r14d
testq %rbx, %rbx
je 0x1ee55
movl %eax, %r15d
movq %rcx, %rbp
movq %r8, %r13
movq %r8, %rdi
movl %r14d, %esi
callq 0x75d0
leaq 0x25f08(%rip), %rcx     # 0x44d30
movb (%rbx,%rcx), %cl
movb %cl, 0xb(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0xb(%rsp), %rsi
callq 0x7340
cmpl $0x2, %ebx
je 0x1ee72
cmpl $0x1, %ebx
movl %r15d, %eax
jne 0x1ee7d
addl %r14d, 0xc(%rsp)
jmp 0x1ee7a
movq %rcx, 0x48(%rsp)
xorl %r15d, %r15d
cmpl $0x10, %edx
movl %eax, 0x24(%rsp)
jae 0x1ee8d
leaq 0x2478b(%rip), %r13     # 0x435f8
jmp 0x1ef37
addl %r14d, 0x10(%rsp)
movl %r15d, %eax
addl %r14d, %eax
movq %rbp, %rcx
movq %r13, %r8
movq 0x18(%rsp), %rsi
jmp 0x1ef70
movslq 0xc(%rsp), %rax
movslq 0x10(%rsp), %r12
addq 0x38(%rsp), %rax
movq %rax, 0x50(%rsp)
addq 0x40(%rsp), %r12
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %eax, %eax
movq 0x50(%rsp), %rcx
movb (%rcx,%rbp), %cl
cmpb (%r12,%rbp), %cl
jne 0x1eed6
incl %eax
movl %r15d, %esi
leaq 0x24731(%rip), %r13     # 0x435f8
movl %eax, %ebx
testl %r15d, %r15d
movl $0x0, %r15d
jne 0x1eef1
jmp 0x1ef0d
incl 0x14(%rsp)
incl %r15d
xorl %ebx, %ebx
movl %eax, %esi
leaq 0x21599(%rip), %r13     # 0x40481
testl %eax, %eax
movl $0x0, %eax
je 0x1ef0d
movq 0x28(%rsp), %rdi
callq 0x75d0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x7340
movl %ebx, %eax
incq %rbp
cmpq %rbp, %r14
jne 0x1eead
testl %eax, %eax
cmovnel %eax, %r15d
leaq 0x2155f(%rip), %r13     # 0x40481
leaq 0x246cf(%rip), %rax     # 0x435f8
cmoveq %rax, %r13
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r12
movq %r8, %rdi
movl %r15d, %esi
movq %r8, %rbx
callq 0x75d0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x7340
movq %rbx, %r8
addl %r14d, 0xc(%rsp)
addl %r14d, 0x10(%rsp)
movq 0x18(%rsp), %rsi
movl 0x24(%rsp), %eax
movq 0x48(%rsp), %rcx
incq %rcx
cmpq %rsi, %rcx
jne 0x1edf8
addl 0x14(%rsp), %eax
jmp 0x1ef84
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop