// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq 0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x50(%rbp)
movb $-0x1, -0x5a(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x2c677
movq 0x10(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq -0x59(%rbp), %rcx
leaq -0x58(%rbp), %r8
callq 0x2c960
movzbl -0x59(%rbp), %eax
cmpl $0x0, %eax
je 0x2c57a
movzbl -0x59(%rbp), %eax
cmpl $0x1, %eax
jne 0x2c5dc
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x2c5d7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movb (%rax,%rcx), %al
movsbl -0x5a(%rbp), %ecx
movsbl %al, %edx
callq 0x2c9c0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2c581
jmp 0x2c660
movzbl -0x59(%rbp), %eax
cmpl $0x2, %eax
jne 0x2c62b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
addq -0x40(%rbp), %rdx
movl -0x58(%rbp), %eax
movl %eax, %ecx
callq 0x2cb40
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movl -0x58(%rbp), %eax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x2c65e
movzbl -0x59(%rbp), %eax
cmpl $0x3, %eax
jne 0x2c65c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x58(%rbp), %eax
movl %eax, %edx
callq 0x2cc80
movl -0x58(%rbp), %eax
addq -0x10(%rbp), %rax
movq %rax, -0x10(%rbp)
movl -0x58(%rbp), %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x2c65e
jmp 0x2c660
movb -0x59(%rbp), %al
movb %al, -0x5a(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2c53f
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)