// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rcx, %r13
movq 0xd8(%rsp), %rax
movq 0xd0(%rsp), %rcx
xorl %r14d, %r14d
movl %r14d, (%rax)
movl %r14d, (%rcx)
testl %esi, %esi
je 0xb4ff0
movl %esi, %ebp
movq 0x18(%rdi), %rax
movl %esi, %ecx
movq (%rax,%rcx,8), %rcx
movq %rcx, 0x10(%rsp)
cmpq $-0x1, %rcx
je 0xb4ff6
movl %edx, %ecx
movq (%rax,%rcx,8), %rbx
cmpq $-0x1, %rbx
je 0xb4ff6
movq %r9, %r15
movq %r8, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movl 0xc8(%rsp), %r8d
movl 0xc0(%rsp), %ecx
movq 0xa0(%rsp), %rsi
movq 0x10(%rsp), %r12
shrq $0x20, %r12
shrq $0x20, %rbx
leaq 0x50(%rsp), %r14
movq %r9, %rdi
movl %r12d, %r9d
pushq %r14
pushq %rbx
callq 0xb2ced
movq %r12, %r10
addq $0x10, %rsp
testl %eax, %eax
jne 0xb4e0e
movq 0xa8(%rsp), %rcx
movq (%rcx), %rax
testq %rax, %rax
je 0xb4e0b
movq 0x10(%rcx), %rcx
movl $0x1, %edx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rsi
cmpl $0x0, 0xc(%rcx,%rsi,8)
js 0xb4e02
leaq (%rcx,%rsi,8), %r14
cmpl %r10d, 0x4(%r14)
jne 0xb4e02
cmpl %ebx, 0x8(%r14)
je 0xb4e0e
movl %edx, %esi
incl %edx
cmpq %rsi, %rax
ja 0xb4de7
xorl %r14d, %r14d
testq %r14, %r14
je 0xb4ff0
movq 0xb0(%rsp), %rax
movq (%r14), %rcx
movq $0x0, 0x8(%rax)
decl %ebp
movl $0x1, %r14d
js 0xb4ff6
movq %r13, 0x40(%rsp)
movq %r15, 0x48(%rsp)
movl %ecx, %eax
movq %rax, 0x30(%rsp)
decl %ecx
movq %rcx, 0x38(%rsp)
movl %r10d, %eax
andb $0x1, %al
movb %al, 0xf(%rsp)
shrq $0x21, 0x10(%rsp)
movl %ebp, %r12d
movq 0x18(%rsp), %rcx
movq %r10, 0x28(%rsp)
movq 0x18(%rcx), %rax
movq (%rax,%r12,8), %r15
cmpq $-0x1, %r15
je 0xb4fdd
movq %r15, %rbp
shrq $0x20, %rbp
movq 0x48(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movl 0xc0(%rsp), %ecx
movl 0xc8(%rsp), %r8d
movl %ebp, %r9d
leaq 0x50(%rsp), %rbx
pushq %rbx
pushq %r10
callq 0xb2ced
movq 0x38(%rsp), %r10
addq $0x10, %rsp
movq %rbx, %rcx
testl %eax, %eax
jne 0xb4f01
movq 0xa8(%rsp), %rcx
movq (%rcx), %rax
testq %rax, %rax
je 0xb4eff
movq 0x10(%rcx), %rdx
movl $0x1, %esi
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
cmpl $0x0, 0xc(%rdx,%rcx,8)
js 0xb4ef6
leaq (%rdx,%rcx,8), %rcx
cmpl %ebp, 0x4(%rcx)
jne 0xb4ef6
cmpl %r10d, 0x8(%rcx)
je 0xb4f01
movl %esi, %ecx
incl %esi
cmpq %rcx, %rax
ja 0xb4edc
xorl %ecx, %ecx
testq %rcx, %rcx
je 0xb4ff6
movq (%rcx), %r13
movq 0x38(%rsp), %rsi
leal (%rsi,%r13), %eax
movq 0x40(%rsp), %rdx
movq 0x20(%rdx), %rdx
shrq $0x21, %r15
movl (%rdx,%r15,8), %edx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %edx
leal -0x1(%rdx), %ebx
cmpl %edx, %eax
cmovll %eax, %ebx
subl %r13d, %ebx
cmpl %esi, %ebx
jne 0xb4ff6
movq 0xd8(%rsp), %rax
incl (%rax)
cmpb $0x1, 0x12(%rcx)
movq 0x18(%rsp), %rcx
je 0xb4fd3
movq 0xb0(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xb4f88
movzbl 0xf(%rsp), %ecx
movq 0xb0(%rsp), %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %r9
callq 0x33a96
movl %r13d, %esi
incl %ebx
andl $0x1, %ebp
movq 0xb8(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rdx
movl %ebp, %ecx
movq 0x20(%rsp), %r8
movq %r15, %r9
callq 0x33a96
movq 0xb0(%rsp), %rax
movq (%rax), %rdi
movq (%r13), %rsi
movq 0x8(%r13), %rdx
callq 0x21a0
testl %eax, %eax
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %r10
jne 0xb4fdd
movq 0xd0(%rsp), %rax
incl (%rax)
leaq -0x1(%r12), %rax
testq %r12, %r12
movq %rax, %r12
jg 0xb4e6c
jmp 0xb4ff6
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq