// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rcx
xorl %ebp, %ebp
movl %ebp, (%rax)
movl %ebp, (%rcx)
testl %esi, %esi
je 0xb5ffc
movl %esi, %ebx
movq 0x18(%rdi), %rax
movl %esi, %ecx
movq (%rax,%rcx,8), %rcx
movq %rcx, 0x18(%rsp)
cmpq $-0x1, %rcx
je 0xb6001
movl %edx, %ecx
movq (%rax,%rcx,8), %r15
cmpq $-0x1, %r15
je 0xb6001
movq %r9, %r12
movq %r8, %r13
movq %rdi, 0x10(%rsp)
movl 0xb8(%rsp), %r8d
movl 0xb0(%rsp), %ecx
movq 0x98(%rsp), %rbp
movq 0x90(%rsp), %rsi
movq 0x18(%rsp), %r9
shrq $0x20, %r9
shrq $0x20, %r15
leaq 0x40(%rsp), %rax
movq %r12, %rdi
movq %r9, 0x8(%rsp)
pushq %rax
pushq %r15
callq 0xb3df3
addq $0x10, %rsp
testl %eax, %eax
je 0xb6012
movq %r14, 0x30(%rsp)
movl 0x40(%rsp), %r14d
movq 0x8(%rsp), %r10
movq 0xa0(%rsp), %rax
movq $0x0, 0x8(%rax)
decl %ebx
movl $0x1, %ebp
js 0xb6001
movq %r13, 0x28(%rsp)
movq %r12, 0x38(%rsp)
movl %r14d, %eax
movq %rax, 0x20(%rsp)
decl %r14d
movl %r10d, %eax
andb $0x1, %al
movb %al, 0x7(%rsp)
shrq $0x21, 0x18(%rsp)
movl %ebx, %r12d
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rax
movq (%rax,%r12,8), %rbx
cmpq $-0x1, %rbx
je 0xb5f91
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
leaq 0x40(%rsp), %r13
pushq %r13
pushq %r10
callq 0xb3df3
addq $0x10, %rsp
testl %eax, %eax
je 0xb5fa4
movq %r13, %rcx
movl 0x40(%rsp), %r13d
movq 0x8(%rsp), %r10
leal (%r14,%r13), %eax
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, %eax
cmovll %eax, %esi
subl %r13d, %esi
cmpl %r14d, %esi
jne 0xb6001
movq 0xc8(%rsp), %rax
incl (%rax)
cmpb $0x1, 0x12(%rcx)
movq 0x10(%rsp), %rcx
je 0xb5f87
movq 0xa0(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xb5f3a
movzbl 0x7(%rsp), %ecx
movq 0xa0(%rsp), %rdi
xorl %esi, %esi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %r8
movq 0x18(%rsp), %r9
callq 0x33191
movl %r13d, %esi
andl $0x1, %r15d
movq 0xa8(%rsp), %r13
movq %r13, %rdi
movq 0x20(%rsp), %rdx
movl %r15d, %ecx
movq 0x28(%rsp), %r8
movq %rbx, %r9
callq 0x33191
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
movq (%r13), %rsi
movq 0x8(%r13), %rdx
callq 0x21a0
testl %eax, %eax
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r10
jne 0xb5f91
movq 0xc0(%rsp), %rax
incl (%rax)
leaq -0x1(%r12), %rax
testq %r12, %r12
movq %rax, %r12
jg 0xb5e65
jmp 0xb6001
movq 0x98(%rsp), %rcx
movq (%rcx), %rax
testq %rax, %rax
je 0xb6001
movq 0x10(%rcx), %rdx
movl $0x1, %esi
xorl %ecx, %ecx
movq 0x8(%rsp), %r10
leaq (%rcx,%rcx,2), %rcx
cmpl $0x0, 0xc(%rdx,%rcx,8)
js 0xb5fec
leaq (%rdx,%rcx,8), %rcx
movq (%rcx), %r13
movq %r13, %rdi
shrq $0x20, %rdi
cmpl %r15d, %edi
jne 0xb5fec
cmpl %r10d, 0x8(%rcx)
je 0xb5ec4
movl %esi, %ecx
incl %esi
cmpq %rcx, %rax
ja 0xb5fc4
movl $0x1, %ebp
jmp 0xb6001
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbp), %rax
movq %rbp, %rcx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0xb6001
movq %r14, 0x30(%rsp)
movq 0x10(%rcx), %rcx
movl $0x1, %edx
xorl %esi, %esi
movq 0x8(%rsp), %r10
leaq (%rsi,%rsi,2), %rsi
cmpl $0x0, 0xc(%rcx,%rsi,8)
js 0xb6060
leaq (%rcx,%rsi,8), %rsi
movq (%rsi), %r14
movq %r14, %rdi
shrq $0x20, %rdi
cmpl %r10d, %edi
jne 0xb6060
cmpl %r15d, 0x8(%rsi)
je 0xb5e1c
movl %edx, %esi
incl %edx
cmpq %rsi, %rax
ja 0xb6038
jmp 0xb6001