// File: 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:
subq $0x418, %rsp            # imm = 0x418
movl 0x420(%rsp), %eax
movq %rdi, 0x410(%rsp)
movl %esi, 0x40c(%rsp)
movq %rdx, 0x400(%rsp)
movq %rcx, 0x3f8(%rsp)
movq %r8, 0x3f0(%rsp)
movl %r9d, 0x3ec(%rsp)
movq 0x410(%rsp), %rax
movq %rax, 0x58(%rsp)
movl $0x0, 0x3d8(%rsp)
movl $0x0, 0x3d4(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x8260
leaq 0xc0(%rsp), %rdi
callq 0x8260
jmp 0x3a39c
cmpl $0x0, 0x3ec(%rsp)
je 0x3a447
leaq 0xd0(%rsp), %rdi
movl 0x3ec(%rsp), %esi
callq 0x8710
movq %rax, 0x50(%rsp)
jmp 0x3a3c5
movq 0x50(%rsp), %rdi
leaq 0x44030(%rip), %rsi     # 0x7e401
callq 0x8360
jmp 0x3a3d8
leaq 0x258(%rsp), %rdi
movl 0x3ec(%rsp), %esi
callq 0x8710
movq %rax, 0x48(%rsp)
jmp 0x3a3f3
movq 0x48(%rsp), %rdi
leaq 0x44002(%rip), %rsi     # 0x7e401
callq 0x8360
jmp 0x3a406
jmp 0x3a447
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x3a8ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x8290
jmp 0x3a8ac
movl $0x0, 0x3e4(%rsp)
movl $0x0, 0x3dc(%rsp)
movl $0x0, 0x3e0(%rsp)
movl 0x3e4(%rsp), %eax
cmpl 0x40c(%rsp), %eax
jge 0x3a79f
movq 0x400(%rsp), %rax
movslq 0x3e4(%rsp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, 0xb0(%rsp)
movq 0x400(%rsp), %rax
movslq 0x3e4(%rsp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, 0xac(%rsp)
leaq 0xd0(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x8710
movq %rax, 0x40(%rsp)
jmp 0x3a4d1
movq 0x40(%rsp), %rdi
movslq 0xb0(%rsp), %rax
leaq 0x436ab(%rip), %rcx     # 0x7db90
movsbl (%rax,%rcx), %esi
callq 0x8440
jmp 0x3a4f0
cmpl $0x0, 0xb0(%rsp)
jne 0x3a6df
movl $0x0, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl $0x0, 0x3e8(%rsp)
movl 0x3e8(%rsp), %eax
cmpl 0xac(%rsp), %eax
jge 0x3a646
movq 0x3f8(%rsp), %rax
movl 0x3e0(%rsp), %ecx
addl 0x3e8(%rsp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq 0x3f0(%rsp), %rcx
movl 0x3dc(%rsp), %edx
addl 0x3e8(%rsp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x3a5d8
movl 0x3d8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3d8(%rsp)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
cmpl $0x0, 0xa8(%rsp)
je 0x3a5d6
leaq 0x258(%rsp), %rdi
movl 0xa8(%rsp), %esi
callq 0x8710
movq %rax, 0x38(%rsp)
jmp 0x3a5b8
movq 0x38(%rsp), %rdi
leaq 0x3ef05(%rip), %rsi     # 0x794c9
callq 0x8360
jmp 0x3a5cb
movl $0x0, 0xa8(%rsp)
jmp 0x3a62e
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
cmpl $0x0, 0xa4(%rsp)
je 0x3a62c
leaq 0x258(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x8710
movq %rax, 0x30(%rsp)
jmp 0x3a60e
movq 0x30(%rsp), %rdi
leaq 0x4201e(%rip), %rsi     # 0x7c638
callq 0x8360
jmp 0x3a621
movl $0x0, 0xa4(%rsp)
jmp 0x3a62e
jmp 0x3a630
movl 0x3e8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3e8(%rsp)
jmp 0x3a51f
cmpl $0x0, 0xa8(%rsp)
je 0x3a680
leaq 0x258(%rsp), %rdi
movl 0xa8(%rsp), %esi
callq 0x8710
movq %rax, 0x28(%rsp)
jmp 0x3a66b
movq 0x28(%rsp), %rdi
leaq 0x3ee52(%rip), %rsi     # 0x794c9
callq 0x8360
jmp 0x3a67e
jmp 0x3a6b0
leaq 0x258(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x8710
movq %rax, 0x20(%rsp)
jmp 0x3a69b
movq 0x20(%rsp), %rdi
leaq 0x41f91(%rip), %rsi     # 0x7c638
callq 0x8360
jmp 0x3a6ae
jmp 0x3a6b0
movl 0xac(%rsp), %eax
addl 0x3e0(%rsp), %eax
movl %eax, 0x3e0(%rsp)
movl 0xac(%rsp), %eax
addl 0x3dc(%rsp), %eax
movl %eax, 0x3dc(%rsp)
jmp 0x3a787
leaq 0x258(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x8710
movq %rax, 0x18(%rsp)
jmp 0x3a6fa
movq 0x18(%rsp), %rdi
movslq 0xb0(%rsp), %rax
leaq 0x43482(%rip), %rcx     # 0x7db90
movsbl (%rax,%rcx), %esi
callq 0x8440
jmp 0x3a719
cmpl $0x1, 0xb0(%rsp)
jne 0x3a74f
movl 0xac(%rsp), %eax
addl 0x3e0(%rsp), %eax
movl %eax, 0x3e0(%rsp)
movl 0xac(%rsp), %eax
addl 0x3d4(%rsp), %eax
movl %eax, 0x3d4(%rsp)
jmp 0x3a785
cmpl $0x2, 0xb0(%rsp)
jne 0x3a783
movl 0xac(%rsp), %eax
addl 0x3dc(%rsp), %eax
movl %eax, 0x3dc(%rsp)
movl 0xac(%rsp), %eax
addl 0x3d4(%rsp), %eax
movl %eax, 0x3d4(%rsp)
jmp 0x3a785
jmp 0x3a787
jmp 0x3a789
movl 0x3e4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3e4(%rsp)
jmp 0x3a468
cmpl $0x0, 0x420(%rsp)
je 0x3a807
leaq 0xd0(%rsp), %rdi
movl 0x420(%rsp), %esi
callq 0x8710
movq %rax, 0x10(%rsp)
jmp 0x3a7c4
movq 0x10(%rsp), %rdi
leaq 0x43c31(%rip), %rsi     # 0x7e401
callq 0x8360
jmp 0x3a7d7
leaq 0x258(%rsp), %rdi
movl 0x420(%rsp), %esi
callq 0x8710
movq %rax, 0x8(%rsp)
jmp 0x3a7f2
movq 0x8(%rsp), %rdi
leaq 0x43c03(%rip), %rsi     # 0x7e401
callq 0x8360
jmp 0x3a805
jmp 0x3a807
movq 0x58(%rsp), %rax
movl 0x3d8(%rsp), %ecx
movl 0x3d4(%rsp), %edx
addl %edx, %ecx
movl %ecx, 0x6c(%rax)
leaq 0x80(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x8350
jmp 0x3a836
movq 0x58(%rsp), %rdi
addq $0x20, %rdi
leaq 0x80(%rsp), %rsi
movq %rsi, (%rsp)
callq 0x84b0
movq (%rsp), %rdi
callq 0x8858
leaq 0x60(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x8350
jmp 0x3a86d
movq 0x58(%rsp), %rdi
addq $0x78, %rdi
leaq 0x60(%rsp), %rsi
callq 0x84b0
leaq 0x60(%rsp), %rdi
callq 0x8858
leaq 0xc0(%rsp), %rdi
callq 0x8290
leaq 0x248(%rsp), %rdi
callq 0x8290
addq $0x418, %rsp            # imm = 0x418
retq
leaq 0x248(%rsp), %rdi
callq 0x8290
movq 0xb8(%rsp), %rdi
callq 0x8730
nopw %cs:(%rax,%rax)