// File: /workspace/github_cpp_makefile_star2_O3/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, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x10(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0xb62c8
movq %rdi, %r13
cmpl $0x3, 0x10(%rdi)
jb 0xb62c8
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 0x10(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x18(%rsp), %r9
movq %rsp, %rax
pushq %rax
leaq 0xc(%rsp), %rax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
pushq 0x58(%rsp)
pushq 0x58(%rsp)
callq 0xb5d5b
addq $0x40, %rsp
cmpl $0x1, %eax
jne 0xb6276
movl (%rsp), %eax
movl 0x4(%rsp), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movapd %xmm1, %xmm2
mulsd 0x17b4b9(%rip), %xmm2   # 0x2315f0
testl %ebp, %ebp
jle 0xb6149
testl %eax, %eax
jle 0xb6149
ucomisd %xmm2, %xmm0
jae 0xb6276
movl %r14d, %ecx
decl %ecx
js 0xb6276
movl %r15d, 0x8(%rsp)
xorpd %xmm2, %xmm2
movsd %xmm2, 0x30(%rsp)
testl %eax, %eax
je 0xb6171
divsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movslq %r14d, %rax
movq %rax, 0x40(%rsp)
movq %r12, %r15
movl %r14d, 0xc(%rsp)
movq 0x18(%r13), %rax
cmpq $-0x1, (%rax,%r15,8)
je 0xb622a
movq %r13, %rdi
movl %r15d, %esi
movl %ebx, %edx
movq 0x10(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x18(%rsp), %r9
movq %rsp, %rax
pushq %rax
leaq 0xc(%rsp), %rax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
pushq 0x58(%rsp)
pushq 0x58(%rsp)
callq 0xb5d5b
addq $0x40, %rsp
testl %eax, %eax
js 0xb6239
movl (%rsp), %ecx
testl %ecx, %ecx
je 0xb6239
movl 0x4(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x30(%rsp), %xmm0
ja 0xb621d
setnp %cl
sete %dl
andb %cl, %dl
cmpl %ebp, %eax
setg %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0xb622a
movl %r15d, 0xc(%rsp)
movl %eax, %ebp
movsd %xmm0, 0x30(%rsp)
decq %r15
leal 0x1(%r15), %eax
testl %eax, %eax
jg 0xb6181
movl 0xc(%rsp), %ecx
cmpl %r14d, %ecx
jge 0xb6271
movq 0x18(%r13), %rax
movslq %ecx, %rcx
movl 0x8(%rsp), %r15d
movq 0x40(%rsp), %rdx
cmpq $-0x1, 0x8(%rax,%rcx,8)
je 0xb6267
movq $-0x1, 0x8(%rax,%rcx,8)
incl %r15d
incq %rcx
cmpq %rcx, %rdx
jne 0xb6253
jmp 0xb6276
movl 0x8(%rsp), %r15d
movl 0x10(%r13), %eax
decl %eax
incq %r12
cmpl %eax, %ebx
jb 0xb60b6
testl %r15d, %r15d
je 0xb62c3
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r8
movl 0x90(%rsp), %r9d
pushq 0xa0(%rsp)
movl 0xa0(%rsp), %eax
pushq %rax
callq 0xb3483
addq $0x10, %rsp
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq