// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: polish_unitig_advance(ma_utg_t*, asg_t*, All_reads*, ma_hit_t_alloc*, ma_sub_t*, kvec_asg_arc_t_warp*, UC_Read*, UC_Read*, int, int, kvec_asg_arc_t_warp*)
uint32_t polish_unitig_advance(ma_utg_t* collection, 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, kvec_asg_arc_t_warp* newE)
{
    if(collection->m == 0) return 0;
    if(collection->n < 3) return 0;
    uint32_t i, skip = 0;
    int match_v, total_v, max_i, match_max, k;
    double match_rate, match_rate_max;
        
    ///we should be able to handle i = collection->n-1
    for (i = 1; i < collection->n-1; i++)
    {
        ///in practice, collection->a[i] and collection->a[i+1] must be available
        ///collection->a[index] might be unavailable only if index < i
        if(get_consensus_rate(collection, i, i+1, read_g, RNF, sources, coverage_cut, 
        edge, r_read, q_read, max_hang, min_ovlp, &match_v, &total_v) != 1)
        {
            continue;
        }

        match_rate = (total_v == 0)? 0:((double)(match_v)/(double)(total_v));
        ///most reads support collection[i], so it is right
        if(match_v >= total_v * 0.5 && total_v > 0 && match_v > 0) continue;

        max_i = i; match_max = match_v; match_rate_max = match_rate;
        for (k = i - 1; k >= 0; k--)
        {
            if(collection->a[k] == (uint64_t)-1) continue;
            ///collection->a[k] might be unavailable, while collection->a[i+1] must be available
            ///return -1 means there is no overlap from k to i+1
            if(get_consensus_rate(collection, k, i+1, read_g, RNF, sources, coverage_cut, 
            edge, r_read, q_read, max_hang, min_ovlp, &match_v, &total_v) < 0)
            {
                break;
            }

            ///no read support k to i+1
            if(total_v == 0) break;

            match_rate = (total_v == 0)? 0:((double)(match_v)/(double)(total_v));
            if(match_rate > match_rate_max || (match_rate == match_rate_max && match_v > match_max))
            {
                max_i = k; match_max = match_v; match_rate_max = match_rate;
            }
        }
        
        ///set [max_i+1, i] to be unavailable
        for (k = max_i+1; k <= (int)i; k++)
        {
            if(collection->a[k] == (uint64_t)-1) continue;
            collection->a[k] = (uint64_t)-1;
            skip++;
        }
    }

    if(skip == 0) return 1;

    reduce_ma_utg_t(collection, read_g, sources, coverage_cut, edge, max_hang, min_ovlp, newE);

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0x7b3c1
movq %rdi, %r13
movl 0x10(%rdi), %ecx
cmpl $0x3, %ecx
jb 0x7b3c1
movq %rdx, %rbp
xorl %r15d, %r15d
pushq $0x1
popq %r14
decl %ecx
cmpl %ecx, %r14d
jae 0x7b384
leal 0x1(%r14), %ebx
movq %r13, %rdi
movl %r14d, %esi
movl %ebx, %edx
movq 0x18(%rsp), %rcx
movq %rbp, %r8
movq 0x20(%rsp), %r9
leaq 0x8(%rsp), %rax
pushq %rax
leaq 0x14(%rsp), %rax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0x7ae8c
addq $0x40, %rsp
cmpl $0x1, %eax
jne 0x7b25f
movl 0x8(%rsp), %eax
movl 0xc(%rsp), %r12d
cvtsi2sd %r12d, %xmm0
cvtsi2sd %eax, %xmm1
movapd %xmm1, %xmm2
mulsd 0x1682f8(%rip), %xmm2   # 0x1e3548
testl %r12d, %r12d
jle 0x7b26b
testl %eax, %eax
jle 0x7b26b
ucomisd %xmm2, %xmm0
jb 0x7b26b
movl 0x10(%r13), %ecx
movl %ebx, %r14d
jmp 0x7b1ce
movl %r15d, 0x3c(%rsp)
xorpd %xmm2, %xmm2
movsd %xmm2, 0x10(%rsp)
testl %eax, %eax
je 0x7b288
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movl %r14d, %eax
movq %rax, 0x40(%rsp)
leal -0x1(%rax), %r15d
testl %r15d, %r15d
js 0x7b34e
movq 0x18(%r13), %rax
movl %r15d, %ecx
cmpq $-0x1, (%rax,%rcx,8)
je 0x7b335
movq %r13, %rdi
movl %r15d, %esi
movl %ebx, %edx
movq 0x18(%rsp), %rcx
movq %rbp, %r8
movq 0x20(%rsp), %r9
leaq 0x8(%rsp), %rax
pushq %rax
leaq 0x14(%rsp), %rax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0x7ae8c
addq $0x40, %rsp
testl %eax, %eax
js 0x7b34e
movl 0x8(%rsp), %ecx
testl %ecx, %ecx
je 0x7b34e
movl 0xc(%rsp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x10(%rsp), %xmm0
ja 0x7b33d
setnp %cl
sete %dl
andb %cl, %dl
cmpl %r12d, %eax
setg %cl
testb %cl, %dl
jne 0x7b33d
decl %r15d
jmp 0x7b294
movl %eax, %r12d
movl %r15d, %eax
movsd %xmm0, 0x10(%rsp)
jmp 0x7b28b
movslq 0x40(%rsp), %rcx
movslq %r14d, %rax
movl 0x3c(%rsp), %r15d
movq %rcx, %rdx
cmpq %rax, %rdx
jge 0x7b25f
leaq 0x1(%rdx), %rcx
movq 0x18(%r13), %rsi
cmpq $-0x1, 0x8(%rsi,%rdx,8)
movq %rcx, %rdx
je 0x7b35e
orq $-0x1, (%rsi,%rcx,8)
incl %r15d
jmp 0x7b35b
testl %r15d, %r15d
je 0x7b3be
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movl 0x90(%rsp), %r9d
pushq 0xa0(%rsp)
movl 0xa0(%rsp), %eax
pushq %rax
callq 0x7886e
popq %rax
popq %rcx
pushq $0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq