// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/mapper.cpp
// Function: alignment_t::setFieldsKsw(int, unsigned int*, unsigned char const*, unsigned char const*, int, int)
void alignment_t::setFieldsKsw(int n_cigar, uint32_t *BamCigar, const uint8_t * query, const uint8_t * rseq, int beginGap, int endGap){
    int i, k, x, y, n_mm = 0, n_gap = 0;
    stringstream sNM;
    stringstream sCig;
    if(beginGap){
        sCig << beginGap << "S";
        sNM << beginGap << "S";
    }    
    for (k = 0, x = y = 0; k < n_cigar; ++k) {
        int op  = BamCigar[k]&0xf;
        int len = BamCigar[k]>>4;
        sCig << len << "MIDSH"[op];
        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;
        }
    }
    if(endGap){
        sCig << endGap << "S";
        sNM << endGap << "S";
    }  
    editDist = n_mm + n_gap;
    cigar = sCig.str();
    NMtag = sNM.str();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x378, %rsp            # imm = 0x378
movl %r9d, %ebx
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x28(%rsp)
movl %esi, %r14d
movq %rdi, %r15
leaq 0x68(%rsp), %rdi
callq 0x71e0
leaq 0x1f0(%rsp), %rdi
callq 0x71e0
testl %ebx, %ebx
je 0x1936f
leaq 0x200(%rsp), %rdi
movl %ebx, %esi
callq 0x75c0
leaq 0x1e215(%rip), %rsi     # 0x37561
movq %rax, %rdi
callq 0x72b0
leaq 0x78(%rsp), %rdi
movl %ebx, %esi
callq 0x75c0
leaq 0x1e1fa(%rip), %rsi     # 0x37561
movq %rax, %rdi
callq 0x72b0
xorl %ecx, %ecx
testl %r14d, %r14d
cmovlel %ecx, %r14d
movq %r14, 0x30(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, (%rsp)
movq %r15, 0x10(%rsp)
cmpq 0x30(%rsp), %rcx
je 0x19519
movq 0x28(%rsp), %rax
movq %rcx, 0x38(%rsp)
movl (%rax,%rcx,4), %r14d
movl %r14d, %ebx
shrl $0x4, %ebx
leaq 0x200(%rsp), %rdi
movl %ebx, %esi
callq 0x75c0
andl $0xf, %r14d
leaq 0x1d916(%rip), %rcx     # 0x36cf0
movsbl (%r14,%rcx), %r12d
movq %rax, %rdi
movl %r12d, %esi
callq 0x7330
testl %r14d, %r14d
je 0x19423
leaq 0x78(%rsp), %rdi
movl %ebx, %esi
callq 0x75c0
movq %rax, %rdi
movl %r12d, %esi
callq 0x7330
cmpl $0x2, %r14d
je 0x194da
cmpl $0x1, %r14d
jne 0x1950c
addl %ebx, 0x8(%rsp)
jmp 0x194de
movslq 0x4(%rsp), %rax
movslq 0x8(%rsp), %r15
addq 0x18(%rsp), %r15
addq 0x20(%rsp), %rax
movq %rax, 0x40(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r12d, %r12d
cmpq %r14, %rbx
je 0x194bc
movb (%r15,%r14), %al
movq 0x40(%rsp), %rcx
cmpb (%rcx,%r14), %al
jne 0x19483
incl %ebp
testl %r12d, %r12d
je 0x194ae
leaq 0x78(%rsp), %rdi
movl %r12d, %esi
callq 0x75c0
movq %rax, %rdi
leaq 0x1c202(%rip), %rsi     # 0x35678
callq 0x72b0
movl %ebp, %r13d
xorl %r12d, %r12d
jmp 0x194b4
incl 0xc(%rsp)
incl %r12d
xorl %r13d, %r13d
testl %ebp, %ebp
je 0x194b4
leaq 0x78(%rsp), %rdi
movl %ebp, %esi
callq 0x75c0
movq %rax, %rdi
leaq 0x1904c(%rip), %rsi     # 0x324f3
callq 0x72b0
jmp 0x194b4
movl %ebp, %r13d
xorl %r12d, %r12d
incq %r14
movl %r13d, %ebp
jmp 0x19444
testl %ebp, %ebp
je 0x194e3
leaq 0x78(%rsp), %rdi
movl %ebp, %esi
callq 0x75c0
movq 0x10(%rsp), %r15
leaq 0x1901b(%rip), %rsi     # 0x324f3
jmp 0x194fc
addl %ebx, 0x4(%rsp)
addl %ebx, (%rsp)
jmp 0x1950c
leaq 0x78(%rsp), %rdi
movl %r12d, %esi
callq 0x75c0
movq 0x10(%rsp), %r15
leaq 0x1c17c(%rip), %rsi     # 0x35678
movq %rax, %rdi
callq 0x72b0
addl %ebx, 0x8(%rsp)
addl %ebx, 0x4(%rsp)
movq 0x38(%rsp), %rcx
incq %rcx
jmp 0x193a1
movl 0x3b0(%rsp), %ebx
testl %ebx, %ebx
je 0x1955d
leaq 0x200(%rsp), %rdi
movl %ebx, %esi
callq 0x75c0
leaq 0x1e027(%rip), %rsi     # 0x37561
movq %rax, %rdi
callq 0x72b0
leaq 0x78(%rsp), %rdi
movl %ebx, %esi
callq 0x75c0
leaq 0x1e00c(%rip), %rsi     # 0x37561
movq %rax, %rdi
callq 0x72b0
movl (%rsp), %eax
addl 0xc(%rsp), %eax
movl %eax, 0x6c(%r15)
leaq 0x208(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x7540
leaq 0x20(%r15), %rdi
leaq 0x48(%rsp), %rbx
movq %rbx, %rsi
callq 0x73a0
movq %rbx, %rdi
callq 0x76a8
leaq 0x80(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x7540
addq $0x78, %r15
leaq 0x48(%rsp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x73a0
movq %rbx, %rdi
callq 0x76a8
leaq 0x1f0(%rsp), %rdi
callq 0x7210
leaq 0x68(%rsp), %rdi
callq 0x7210
addq $0x378, %rsp            # imm = 0x378
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x195f9
jmp 0x195f9
movq %rax, %rbx
jmp 0x19609
jmp 0x195f9
jmp 0x195f9
jmp 0x195f9
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
callq 0x7210
leaq 0x68(%rsp), %rdi
callq 0x7210
movq %rbx, %rdi
callq 0x75e0
nop