// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/POA.cpp
// Function: addmatchedSeqToGraph(Graph*, long long, char*, long long, char*, long long, window_list*, window_list_alloc*, long long, long long)
void addmatchedSeqToGraph(Graph* backbone, long long currentNodeID, char* x_string, long long x_length, 
        char* y_string, long long y_length, window_list *cigar_idx, window_list_alloc *cigar_s, long long backbone_start, long long backbone_end)
{
    
    int64_t x_i = 0, y_i = 0, c_i = 0, c_n = cigar_idx->clen;
    uint32_t i, operLen; uint8_t oper; int8_t last_oper = -1;
    // if(currentNodeID == 366 && x_length == 9 && y_length == 9) {
    //     fprintf(stderr, "[M::%s] currentNodeID::%lld, x_length::%lld, c_n::%ld, cidx::%u, cigar_s_n::%lld\n", __func__, 
    //     currentNodeID, x_length, c_n, cigar_idx->cidx, (long long)cigar_s->c.n);
    // }
    
    ///note that node 0 is the start node
    ///0 is match, 1 is mismatch, 2 is up, 3 is left
    ///2 mean y has more bases, while 3 means x has more bases
    for (c_i = 0; c_i < c_n; c_i++) {
        get_cigar_cell(cigar_idx, cigar_s, c_i, &oper, &operLen);

        // if(currentNodeID == 366 && x_length == 9 && y_length == 9) {
        //     fprintf(stderr, "[M::%s] c_i::%ld, oper::%u, operLen::%u, last_oper::%d\n", __func__, c_i, oper, operLen, last_oper);
        // }
        
        if (oper == 0 || oper == 1) { ///match/mismatch
            for (i = 0; i < operLen; i++) {
                ///if the previous node is insertion, this node might be mismatch/match
                add_mismatchEdge_weight(backbone, currentNodeID, y_string[y_i], last_oper);    
                x_i++; y_i++; currentNodeID++;
            }
        } else if (oper == 2) { ///insertion
            ///the begin and end of cigar cannot be 2, so -1 is right here
            ///if (operationLen <= CORRECT_INDEL_LENGTH)
            {
                add_insertionEdge_weight(backbone, currentNodeID, y_string + y_i, operLen);
                backbone->g_nodes.list[currentNodeID].num_insertions++;
            }
            y_i += operLen;
        } else if (oper == 3) {
            ///3 means x has more bases, that means backbone has more bases
            ///like a mismatch (-)
            ///if (operationLen <= CORRECT_INDEL_LENGTH)
            {
                add_deletionEdge_weight(backbone, currentNodeID, operLen);
            }

            
            currentNodeID += operLen;
            x_i += operLen;
        }

        last_oper = oper;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x50(%rsp), %rax
movl 0x1c(%rax), %eax
movq %rax, 0x8(%rsp)
pushq $-0x1
popq %rbp
xorl %r13d, %r13d
xorl %edx, %edx
cmpq 0x8(%rsp), %rdx
je 0x1870b
movq 0x58(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x50(%rsp), %rcx
movl 0x18(%rcx), %ecx
movq %rdx, 0x10(%rsp)
addl %edx, %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, %ecx
shrl $0xe, %ecx
movl %eax, %r12d
andl $0x3fff, %r12d          # imm = 0x3FFF
testw %ax, %ax
js 0x186ba
movl %ecx, 0x4(%rsp)
subl $0x1, %r12d
jb 0x186b4
movsbl (%rbx,%r13), %edx
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %ecx
callq 0x18848
incq %r13
incq %r14
jmp 0x18694
movl 0x4(%rsp), %ebp
jmp 0x186fe
cmpw $0x2, %cx
movl %ecx, %ebp
jne 0x186e9
leaq (%rbx,%r13), %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x18929
movq 0x18(%r15), %rax
movq %r14, %rcx
shlq $0x7, %rcx
incq 0x10(%rax,%rcx)
addq %r12, %r13
jmp 0x186fe
movzwl %r12w, %r12d
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x18a0e
addq %r12, %r14
movq 0x10(%rsp), %rdx
incq %rdx
jmp 0x18655
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq