// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %r8, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x70(%rsp), %rax
movl 0x1c(%rax), %eax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x44cb9
movq %rsi, %r14
movq 0x18(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x8(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
xorl %ebx, %ebx
movl %eax, 0x4(%rsp)
movq 0x78(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x70(%rsp), %rcx
movl 0x18(%rcx), %ecx
addl %ebp, %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, %edx
shrl $0xe, %edx
movl %eax, %r12d
andl $0x3fff, %r12d          # imm = 0x3FFF
testw %ax, %ax
movq %rbp, 0x30(%rsp)
movl %edx, 0x14(%rsp)
js 0x44bf2
testw %r12w, %r12w
je 0x44ca2
xorl %r13d, %r13d
movq 0x20(%rsp), %rax
movb (%rax,%rbx), %al
movq 0x8(%rsp), %rdx
movq (%rdx), %rsi
movq %r14, %rdx
shlq $0x7, %rdx
leaq (%rsi,%rdx), %rbp
addq $0x20, %rbp
movq 0x10(%rbp), %r9
testq %r9, %r9
jle 0x44b57
movq (%rbp), %rdi
addq $0x18, %rdi
xorl %edx, %edx
movq -0x10(%rdi), %r8
shlq $0x7, %r8
cmpb %al, 0x18(%rsi,%r8)
je 0x44b5b
incq %rdx
addq $0x38, %rdi
cmpq %rdx, %r9
jne 0x44b3a
jmp 0x44b6e
xorl %edx, %edx
jmp 0x44b69
incq -0x8(%rdi)
cmpl $0x2, 0x4(%rsp)
jne 0x44b69
incq (%rdi)
cmpq %r9, %rdx
jne 0x44bdb
movsbl %al, %esi
movq 0x8(%rsp), %rdi
callq 0x44796
movq %rax, %r15
movl $0x1, %ecx
movl $0x1, %r8d
movq %rbp, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x442e2
cmpl $0x2, 0x4(%rsp)
jne 0x44bac
imulq $0x38, 0x10(%rbp), %rax
movq (%rbp), %rcx
incq -0x20(%rcx,%rax)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %r15, %rcx
shlq $0x7, %rcx
leaq (%rax,%rcx), %rdi
addq $0x20, %rdi
incq %r14
movl $0x1, %ecx
movq %r15, %rsi
movq %r14, %rdx
xorl %r8d, %r8d
callq 0x442e2
jmp 0x44bde
incq %r14
incq %rbx
incl %r13d
cmpl %r12d, %r13d
jne 0x44b08
jmp 0x44ca2
cmpw $0x2, %dx
jne 0x44c29
movq 0x20(%rsp), %rax
leaq (%rax,%rbx), %rdx
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x44cc8
movq 0x18(%r15), %rax
movq %r14, %rcx
shlq $0x7, %rcx
incq 0x10(%rax,%rcx)
addq %r12, %rbx
jmp 0x44ca2
movzwl %r12w, %r15d
testw %r15w, %r15w
je 0x44c9f
xorl %r13d, %r13d
leaq (%r14,%r13), %rsi
leaq (%r14,%r13), %rdx
incq %rdx
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rsi, %rcx
shlq $0x7, %rcx
leaq (%rax,%rcx), %rdi
addq $0x40, %rdi
movq 0x10(%rdi), %rax
testq %rax, %rax
jle 0x44c7e
movq (%rdi), %r8
addq $0x10, %r8
xorl %ecx, %ecx
cmpq %rdx, -0x8(%r8)
je 0x44c82
incq %rcx
addq $0x38, %r8
cmpq %rcx, %rax
jne 0x44c6a
jmp 0x44c8a
xorl %ecx, %ecx
jmp 0x44c85
incq (%r8)
cmpq %rax, %rcx
jne 0x44c97
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x442e2
incq %r13
cmpq %r15, %r13
jne 0x44c36
addq %r15, %r14
movq 0x30(%rsp), %rbp
incq %rbp
cmpq 0x28(%rsp), %rbp
movl 0x14(%rsp), %eax
jne 0x44abf
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq