// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movl 0x38(%rbp), %eax
movl 0x30(%rbp), %eax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq 0x48(%rbp), %rax
movl $0x0, (%rax)
movq 0x40(%rbp), %rax
movl $0x0, (%rax)
cmpl $0x1, -0x14(%rbp)
jae 0xec1f5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xec611
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x14(%rbp), %ecx
cmpq $-0x1, (%rax,%rcx,8)
je 0xec221
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x18(%rbp), %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xec22d
movl $0x0, -0x4(%rbp)
jmp 0xec611
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x18(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x58(%rbp)
movq -0x30(%rbp), %rdi
movq 0x10(%rbp), %rsi
movl 0x30(%rbp), %ecx
movl 0x38(%rbp), %r8d
movl -0x54(%rbp), %r9d
movl -0x58(%rbp), %r10d
xorl %eax, %eax
movl %eax, %edx
leaq -0x50(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xe9560
cmpl $0x0, %eax
jne 0xec32a
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq 0x18(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xec328
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xec2c5
jmp 0xec31a
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl -0x54(%rbp), %ecx
cmpq %rcx, %rax
jne 0xec318
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x58(%rbp), %eax
jne 0xec318
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0xec328
jmp 0xec31a
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xec296
jmp 0xec332
leaq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xec345
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xec611
movl $0x0, -0x60(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
movq 0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jl 0xec60a
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x70(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xec390
jmp 0xec5fc
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x58(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq 0x10(%rbp), %rsi
movl 0x30(%rbp), %ecx
movl 0x38(%rbp), %r8d
movl -0x58(%rbp), %r9d
movl -0x54(%rbp), %r10d
xorl %eax, %eax
movl %eax, %edx
leaq -0x50(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xe9560
cmpl $0x0, %eax
jne 0xec480
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq 0x18(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xec47e
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xec41b
jmp 0xec470
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl -0x58(%rbp), %ecx
cmpq %rcx, %rax
jne 0xec46e
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x54(%rbp), %eax
jne 0xec46e
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0xec47e
jmp 0xec470
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xec3ec
jmp 0xec488
leaq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xec494
jmp 0xec60a
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
addl -0x64(%rbp), %eax
movq -0x20(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x58(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0xec4ce
movl -0x68(%rbp), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0xec4eb
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x58(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl $0x1, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
subl -0x68(%rbp), %eax
movl -0x64(%rbp), %ecx
subl -0x60(%rbp), %ecx
cmpl %ecx, %eax
je 0xec506
jmp 0xec60a
movq 0x48(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movzbl 0x12(%rax), %eax
cmpl $0x1, %eax
jne 0xec52e
movq 0x40(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0xec5fa
movq 0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xec56b
movq 0x20(%rbp), %rdi
movl -0x60(%rbp), %eax
movl %eax, %esi
movl -0x64(%rbp), %eax
subl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movl -0x54(%rbp), %eax
andl $0x1, %eax
movq -0x28(%rbp), %r8
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl %ecx, %r9d
movzbl %al, %ecx
callq 0x9f30
movq 0x28(%rbp), %rdi
movl -0x68(%rbp), %eax
movl %eax, %esi
movl -0x6c(%rbp), %eax
subl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movl -0x58(%rbp), %eax
andl $0x1, %eax
movq -0x28(%rbp), %r8
movl -0x58(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl %ecx, %r9d
movzbl %al, %ecx
callq 0x9f30
movq 0x20(%rbp), %rax
movq (%rax), %rdi
movq 0x20(%rbp), %rax
movq 0x8(%rax), %rsi
movq 0x28(%rbp), %rax
movq (%rax), %rdx
movq 0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x28(%rbp), %rax
movq 0x8(%rax), %r9
subq $0x1, %r9
movq 0x28(%rbp), %rax
movq 0x8(%rax), %rax
subq $0x1, %rax
xorl %r8d, %r8d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1c610
cmpl $0x1, %eax
jne 0xec5f8
movq 0x40(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0xec5fa
jmp 0xec5fc
movl -0x70(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xec36e
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)