// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_consensus_rate(ma_utg_t*, unsigned int, unsigned int, asg_t*, All_reads*, ma_hit_t_alloc*, ma_sub_t*, kvec_asg_arc_t_warp*, UC_Read*, UC_Read*, int, int, int*, int*)
int get_consensus_rate(ma_utg_t* collection, uint32_t cur_i, uint32_t next_i, 
asg_t* read_g, All_reads *RNF, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, 
kvec_asg_arc_t_warp* edge, UC_Read* r_read, UC_Read* q_read, int max_hang, int min_ovlp,
int* r_match, int* r_total)
{
    (*r_match) = (*r_total) = 0;
    if(cur_i < 1) return -1;
    asg_arc_t *t = NULL, i_t;
    uint32_t v, w, k, v_beg, v_end, w_beg, w_end;
    int j;
    if(collection->a[cur_i] == (uint64_t)-1 || collection->a[next_i] == (uint64_t)-1) return 0;

    v = (uint64_t)(collection->a[cur_i])>>32;
    w = (uint64_t)(collection->a[next_i])>>32;

    if(get_edge_from_source(sources, coverage_cut, NULL, max_hang, min_ovlp, v, w, &i_t) == 0)
    {
        for (k = 0; k < edge->a.n; k++)
        {
            if(edge->a.a[k].del) continue;
            if((edge->a.a[k].ul>>32) == v && edge->a.a[k].v == w)
            {
                t = &(edge->a.a[k]);
                break;
            }
        }
    }
    else
    {
        t = &i_t;
    }

    if(t == NULL) return -1;
    v_beg = 0; v_end = asg_arc_len(*t) - 1; r_read->length = 0;
    ///cur_i must >= 1
    for (j = cur_i-1; j >= 0; j--)
    {
        if(collection->a[j] == (uint64_t)-1) continue;

        w = (uint64_t)(collection->a[j])>>32;
        t = NULL;

        if(get_edge_from_source(sources, coverage_cut, NULL, max_hang, min_ovlp, w, v, &i_t) == 0)
        {
            for (k = 0; k < edge->a.n; k++)
            {
                if(edge->a.a[k].del) continue;
                if((edge->a.a[k].ul>>32) == w && edge->a.a[k].v == v)
                {
                    t = &(edge->a.a[k]);
                    break;
                }
            }
        }
        else
        {
            t = &i_t;
        }

        if(t == NULL) break;
        
        w_beg = asg_arc_len(*t); 
        w_end = MIN(((int)(w_beg + v_end)), ((int)(read_g->seq[w>>1].len-1)));
        // if(t->el == 1)
        // {
        //     if(r_read->length == 0) recover_UC_sub_Read(r_read, v_beg, v_end - v_beg + 1, v&1, RNF, v>>1);
        //     ///for debug: check if v[v_beg, v_end] == w[w_beg, w_end]
        //     recover_UC_sub_Read(q_read, w_beg, w_end - w_beg +1, w&1, RNF, w>>1);
        //     ///q_read->length<=r_read->length
        //     if(if_exact_match(r_read->seq, r_read->length, q_read->seq, q_read->length, 
        //     0, q_read->length-1, 0, q_read->length-1) == 0)
        //     {
        //         fprintf(stderr, "ERROR: cur_i: %u, j: %d, r_len: %lld, q_len: %lld\n", cur_i, j, r_read->length, q_read->length);
        //     }
        // }
        
        //filter overlaps which cannot cover the whole [v_beg, v_end]
        if(w_end - w_beg != v_end - v_beg) break;

        (*r_total)++;

        if(t->el == 1)
        {
            (*r_match)++;
        }
        else
        {
            if(r_read->length == 0) recover_UC_sub_Read(r_read, v_beg, v_end - v_beg + 1, v&1, RNF, v>>1);
            recover_UC_sub_Read(q_read, w_beg, w_end - w_beg +1, w&1, RNF, w>>1);
            ///q_read->length<=r_read->length
            if(if_exact_match(r_read->seq, r_read->length, q_read->seq, q_read->length, 
            0, q_read->length-1, 0, q_read->length-1) == 1)
            {
                (*r_match)++;
            }
        }
    }


    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r12
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rcx
andl $0x0, (%rax)
andl $0x0, (%rcx)
testl %esi, %esi
je 0x7b121
movq %r9, %r15
movq 0x18(%rdi), %r9
movl %esi, %ebp
movq (%r9,%rbp,8), %r13
xorl %eax, %eax
cmpq $-0x1, %r13
je 0x7b128
movl %edx, %ecx
movq (%r9,%rcx,8), %rbx
cmpq $-0x1, %rbx
je 0x7b128
movq %rdi, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movl 0xb8(%rsp), %r8d
movl 0xb0(%rsp), %ecx
movq 0x98(%rsp), %r14
movq 0x90(%rsp), %rsi
movq %r13, 0x20(%rsp)
shrq $0x20, %r13
shrq $0x20, %rbx
leaq 0x40(%rsp), %rax
movq %r15, %rdi
movl %r13d, %r9d
pushq %rax
pushq %rbx
callq 0x79204
popq %rcx
popq %rdx
testl %eax, %eax
je 0x7b137
movl 0x40(%rsp), %r14d
movq 0x8(%rsp), %rdi
leaq 0x40(%rsp), %r10
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq 0xa0(%rsp), %rax
andq $0x0, 0x8(%rax)
movl %r14d, %eax
movq %rax, 0x18(%rsp)
decl %r14d
movl %r13d, %eax
andb $0x1, %al
movb %al, 0x7(%rsp)
shrq $0x21, 0x20(%rsp)
movq %r14, 0x28(%rsp)
decq %rbp
testl %ebp, %ebp
js 0x7b125
movq 0x18(%rdi), %rax
movl %ebp, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq (%rax,%rcx,8), %rbx
cmpq $-0x1, %rbx
je 0x7af7b
movq %rbx, %r15
shrq $0x20, %r15
movq 0x38(%rsp), %rdi
movq 0x90(%rsp), %rsi
movl 0xb0(%rsp), %ecx
movl 0xb8(%rsp), %r8d
movl %r15d, %r9d
pushq %r10
movq %r13, %r14
pushq %r13
movq %r10, %r13
callq 0x79204
popq %rcx
popq %rdx
testl %eax, %eax
je 0x7b0ce
movl 0x40(%rsp), %r12d
movq %r13, %rax
movq 0x8(%rsp), %rdi
movq %r13, %r10
movq %r14, %r13
movq 0x28(%rsp), %r14
leal (%r14,%r12), %ecx
movq 0x30(%rsp), %rdx
movq 0x20(%rdx), %rdx
shrq $0x21, %rbx
movl (%rdx,%rbx,8), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
leal -0x1(%rdx), %esi
cmpl %edx, %ecx
cmovll %ecx, %esi
subl %r12d, %esi
cmpl %r14d, %esi
jne 0x7b125
movq 0xc8(%rsp), %rcx
incl (%rcx)
cmpb $0x1, 0x12(%rax)
movq 0xa0(%rsp), %rax
je 0x7b0bf
cmpq $0x0, 0x8(%rax)
movq 0x10(%rsp), %r8
jne 0x7b072
movzbl 0x7(%rsp), %ecx
movq 0xa0(%rsp), %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %r9
callq 0x6f2f
movq 0x10(%rsp), %r8
movl %r12d, %esi
andl $0x1, %r15d
movq 0xa8(%rsp), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rdx
movl %r15d, %ecx
movq %rbx, %r9
callq 0x6f2f
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
movq (%r12), %rsi
movq 0x8(%r12), %rdx
callq 0x21a0
testl %eax, %eax
movq 0x8(%rsp), %rdi
leaq 0x40(%rsp), %r10
jne 0x7af7b
movq 0xc0(%rsp), %rax
incl (%rax)
jmp 0x7af7b
movq 0x98(%rsp), %r8
movq (%r8), %rcx
xorl %edx, %edx
movq 0x8(%rsp), %rdi
movq %r13, %r10
movq %r14, %r13
movq 0x28(%rsp), %r14
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x7b125
movq 0x10(%r8), %rax
imulq $0x18, %rdx, %rsi
cmpl $0x0, 0xc(%rax,%rsi)
js 0x7b11d
addq %rsi, %rax
movq (%rax), %r12
movq %r12, %rsi
shrq $0x20, %rsi
cmpl %r15d, %esi
jne 0x7b11d
cmpl %r13d, 0x8(%rax)
je 0x7aff3
incl %edx
jmp 0x7b0eb
pushq $-0x1
jmp 0x7b127
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rax
xorl %ecx, %ecx
pushq $-0x1
popq %r9
movq 0x8(%rsp), %rdi
movq %r14, %r8
leaq 0x40(%rsp), %r10
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x7b182
movq 0x10(%r8), %rdx
imulq $0x18, %rcx, %rsi
cmpl $0x0, 0xc(%rdx,%rsi)
js 0x7b17e
addq %rsi, %rdx
movq (%rdx), %r14
movq %r14, %rsi
shrq $0x20, %rsi
cmpl %r13d, %esi
jne 0x7b17e
cmpl %ebx, 0x8(%rdx)
je 0x7af45
incl %ecx
jmp 0x7b14d
movl %r9d, %eax
jmp 0x7b128