// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movq 0x70(%rsp), %rax
movl 0x1c(%rax), %eax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x4568f
movq %rsi, %r14
movq 0x8(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x10(%rsp)
movb $-0x1, %al
xorl %edx, %edx
xorl %ebx, %ebx
movb %al, 0x7(%rsp)
movq 0x78(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x70(%rsp), %rcx
movl 0x18(%rcx), %ecx
addl %edx, %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, %ecx
shrl $0xe, %ecx
movl %eax, %r13d
andl $0x3fff, %r13d          # imm = 0x3FFF
testw %ax, %ax
movq %rdx, 0x30(%rsp)
movl %ecx, 0x1c(%rsp)
js 0x455b9
testw %r13w, %r13w
je 0x45678
xorl %r12d, %r12d
movq 0x20(%rsp), %rax
movb (%rax,%rbx), %al
movq 0x10(%rsp), %rcx
movq (%rcx), %rsi
movq %r14, %rcx
shlq $0x7, %rcx
leaq (%rsi,%rcx), %rbp
addq $0x20, %rbp
movq 0x10(%rbp), %rcx
testq %rcx, %rcx
jle 0x45522
movq (%rbp), %rdi
addq $0x18, %rdi
xorl %edx, %edx
movq -0x10(%rdi), %r8
shlq $0x7, %r8
cmpb %al, 0x18(%rsi,%r8)
je 0x45526
incq %rdx
addq $0x38, %rdi
cmpq %rdx, %rcx
jne 0x45502
movq %rcx, %rdx
jmp 0x45534
xorl %edx, %edx
jmp 0x45534
incq -0x8(%rdi)
cmpb $0x2, 0x7(%rsp)
jne 0x45534
incq (%rdi)
cmpq %rcx, %rdx
jne 0x455a2
movsbl %al, %esi
movq 0x10(%rsp), %rdi
callq 0x45147
movq %rax, %r15
movl $0x1, %ecx
movl $0x1, %r8d
movq %rbp, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x44c37
cmpb $0x2, 0x7(%rsp)
jne 0x45577
imulq $0x38, 0x10(%rbp), %rax
movq (%rbp), %rcx
incq -0x20(%rcx,%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %r15, %rcx
shlq $0x7, %rcx
leaq (%rax,%rcx), %rdi
addq $0x20, %rdi
leaq 0x1(%r14), %rdx
movl $0x1, %ecx
movq %r15, %rsi
xorl %r8d, %r8d
callq 0x44c37
incq %rbx
incq %r14
incl %r12d
cmpl %r13d, %r12d
jne 0x454d0
jmp 0x45678
cmpb $0x3, %cl
je 0x455fb
movzbl %cl, %eax
cmpl $0x2, %eax
jne 0x45678
movq 0x20(%rsp), %rax
leaq (%rax,%rbx), %rdx
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rcx
callq 0x4569e
movq 0x18(%r15), %rax
movq %r14, %rcx
shlq $0x7, %rcx
incq 0x10(%rax,%rcx)
addq %r13, %rbx
jmp 0x45678
movzwl %r13w, %r15d
testw %r15w, %r15w
movq 0x8(%rsp), %r13
je 0x45675
xorl %r12d, %r12d
leaq (%r12,%r14), %rsi
leaq (%r12,%r14), %rdx
incq %rdx
movq 0x18(%r13), %rax
movq %rsi, %rcx
shlq $0x7, %rcx
leaq (%rax,%rcx), %rdi
addq $0x40, %rdi
movq 0x10(%rdi), %rax
testq %rax, %rax
jle 0x45654
movq (%rdi), %r8
addq $0x10, %r8
xorl %ecx, %ecx
cmpq %rdx, -0x8(%r8)
je 0x45658
incq %rcx
addq $0x38, %r8
cmpq %rcx, %rax
jne 0x4563d
movq %rax, %rcx
jmp 0x4565b
xorl %ecx, %ecx
jmp 0x4565b
incq (%r8)
cmpq %rax, %rcx
jne 0x4566d
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x44c37
incq %r12
cmpq %r15, %r12
jne 0x4560d
addq %r15, %r14
movq 0x30(%rsp), %rdx
incq %rdx
cmpq 0x28(%rsp), %rdx
movl 0x1c(%rsp), %eax
jne 0x45487
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq