// File: /workspace/github_cpp_makefile_star2_O1/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 %rdx, 0x40(%rsp)
movq %rsi, 0x18(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0xb5276
movq %rdi, %r13
cmpl $0x3, 0x10(%rdi)
jb 0xb5276
movl 0x10(%r13), %eax
addl $-0x3, %eax
cmpl $-0x3, %eax
ja 0xb5271
movl $0x1, %ebx
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %ebx, %r14d
leal 0x1(%r14), %ebx
movq %r13, %rdi
movl %r14d, %esi
movl %ebx, %edx
movq 0x18(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x20(%rsp), %r9
leaq 0xc(%rsp), %rax
pushq %rax
leaq 0x18(%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 0xb4d20
addq $0x40, %rsp
cmpl $0x1, %eax
jne 0xb5224
movl 0xc(%rsp), %eax
movl 0x10(%rsp), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movapd %xmm1, %xmm2
mulsd 0x170409(%rip), %xmm2   # 0x2254f0
testl %ebp, %ebp
jle 0xb50f9
testl %eax, %eax
jle 0xb50f9
ucomisd %xmm2, %xmm0
jae 0xb5224
movl %r15d, 0x14(%rsp)
movl %r14d, %ecx
decl %ecx
movl %r14d, 0x8(%rsp)
js 0xb51e9
xorpd %xmm2, %xmm2
movsd %xmm2, 0x38(%rsp)
testl %eax, %eax
je 0xb5126
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movq %r12, %r15
movl %r14d, 0x8(%rsp)
movq 0x18(%r13), %rax
cmpq $-0x1, (%rax,%r15,8)
je 0xb51da
movq %r13, %rdi
movl %r15d, %esi
movl %ebx, %edx
movq 0x18(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x20(%rsp), %r9
leaq 0xc(%rsp), %rax
pushq %rax
leaq 0x18(%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 0xb4d20
addq $0x40, %rsp
testl %eax, %eax
js 0xb51e9
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
je 0xb51e9
movl 0x10(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x38(%rsp), %xmm0
ja 0xb51cd
setnp %cl
sete %dl
andb %cl, %dl
cmpl %ebp, %eax
setg %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0xb51da
movl %r15d, 0x8(%rsp)
movl %eax, %ebp
movsd %xmm0, 0x38(%rsp)
decq %r15
leal 0x1(%r15), %eax
testl %eax, %eax
jg 0xb512e
movl 0x8(%rsp), %edx
cmpl %r14d, %edx
jge 0xb521f
movslq %r14d, %rax
movq 0x18(%r13), %rcx
movslq %edx, %rdx
movl 0x14(%rsp), %r15d
cmpq $-0x1, 0x8(%rcx,%rdx,8)
je 0xb5215
movq $-0x1, 0x8(%rcx,%rdx,8)
incl %r15d
incq %rdx
cmpq %rdx, %rax
jne 0xb5201
jmp 0xb5224
movl 0x14(%rsp), %r15d
movl 0x10(%r13), %eax
decl %eax
incq %r12
cmpl %eax, %ebx
jb 0xb5063
testl %r15d, %r15d
je 0xb5271
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 0xb2398
addq $0x10, %rsp
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq