// File: /workspace/github_cpp_makefile_star2_O1/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, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
testl %edi, %edi
jle 0x1f2b1
addq $0x10, %r8
movl %edi, %r12d
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %ecx, %ecx
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movq %r8, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
movq %r12, 0x50(%rsp)
movl (%rsi,%r13,4), %edi
movl %edi, %ebx
andl $0xf, %ebx
movl %edi, %edx
shrl $0x4, %edx
testq %rbx, %rbx
movq %rdx, 0x20(%rsp)
je 0x1f154
movq %r12, %r14
movl %ecx, %r12d
movq %r8, %r15
movq %r8, %rdi
movq %rdx, %rsi
callq 0x75d0
leaq 0x25c0a(%rip), %rcx     # 0x44d30
movb (%rbx,%rcx), %cl
movb %cl, 0xf(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0xf(%rsp), %rsi
callq 0x7340
cmpl $0x2, %ebx
je 0x1f16c
cmpl $0x1, %ebx
jne 0x1f177
movq 0x20(%rsp), %rcx
addl %ecx, 0x10(%rsp)
jmp 0x1f175
movq %r13, 0x38(%rsp)
movl %ebp, 0x2c(%rsp)
xorl %ebp, %ebp
cmpl $0x10, %edi
jae 0x1f18a
xorl %r13d, %r13d
jmp 0x1f232
movq 0x20(%rsp), %rcx
addl %ecx, 0x14(%rsp)
addl %ecx, %ebp
movl %r12d, %ecx
movq %r14, %r12
movq %r15, %r8
movq 0x18(%rsp), %rsi
jmp 0x1f2a1
movslq 0x10(%rsp), %rbx
movslq 0x14(%rsp), %r12
addq 0x40(%rsp), %rbx
addq 0x48(%rsp), %r12
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r14d, %r14d
movb (%rbx,%r15), %al
cmpb (%r12,%r15), %al
jne 0x1f1eb
incl %r14d
testl %r13d, %r13d
je 0x1f1e3
movq 0x30(%rsp), %rdi
movl %r13d, %esi
movl %ecx, %ebp
callq 0x75d0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x24421(%rip), %rsi     # 0x435f8
callq 0x7340
movq 0x20(%rsp), %rdx
movl %ebp, %ecx
movl %r14d, %ebp
xorl %r13d, %r13d
jmp 0x1f223
incl %ecx
incl %r13d
xorl %ebp, %ebp
testl %r14d, %r14d
je 0x1f223
movq 0x30(%rsp), %rdi
movl %r14d, %esi
movl %ecx, %r14d
callq 0x75d0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2126e(%rip), %rsi     # 0x40484
callq 0x7340
movq 0x20(%rsp), %rdx
movl %r14d, %ecx
incq %r15
movl %ebp, %r14d
cmpq %r15, %rdx
jne 0x1f1a7
movl %ecx, %ebx
testl %ebp, %ebp
movq 0x50(%rsp), %r12
je 0x1f25d
movq 0x30(%rsp), %rdi
movq %rdi, %r14
movl %ebp, %esi
callq 0x75d0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x21229(%rip), %rsi     # 0x40484
jmp 0x1f27c
movq 0x30(%rsp), %rdi
movq %rdi, %r14
movl %r13d, %esi
callq 0x75d0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2437c(%rip), %rsi     # 0x435f8
callq 0x7340
movq 0x18(%rsp), %rsi
movl 0x2c(%rsp), %ebp
movq 0x38(%rsp), %r13
movq 0x20(%rsp), %rcx
addl %ecx, 0x10(%rsp)
addl %ecx, 0x14(%rsp)
movl %ebx, %ecx
movq %r14, %r8
incq %r13
cmpq %r12, %r13
jne 0x1f0f3
addl %ecx, %ebp
jmp 0x1f2b3
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq