// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xec674
movl $0x0, -0x2c(%rbp)
jmp 0xec974
movq -0x38(%rbp), %rax
cmpl $0x3, 0x10(%rax)
jae 0xec68a
movl $0x0, -0x2c(%rbp)
jmp 0xec974
movl $0x0, -0x68(%rbp)
movl $0x1, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xec930
movq -0x38(%rbp), %rdi
movl -0x64(%rbp), %esi
movl -0x64(%rbp), %edx
addl $0x1, %edx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %r9
movq -0x58(%rbp), %r13
movq -0x60(%rbp), %r12
movq 0x10(%rbp), %r15
movq 0x18(%rbp), %r14
movl 0x20(%rbp), %ebx
movl 0x28(%rbp), %r11d
leaq -0x6c(%rbp), %r10
leaq -0x70(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movl %ebx, 0x20(%rsp)
movl %r11d, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0xec190
cmpl $0x1, %eax
je 0xec71a
jmp 0xec922
cmpl $0x0, -0x70(%rbp)
jne 0xec72d
xorps %xmm0, %xmm0
movsd %xmm0, -0x98(%rbp)
jmp 0xec743
cvtsi2sdl -0x6c(%rbp), %xmm0
cvtsi2sdl -0x70(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
cvtsi2sdl -0x6c(%rbp), %xmm0
cvtsi2sdl -0x70(%rbp), %xmm1
movsd 0x295b0b(%rip), %xmm2   # 0x382270
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0xec780
cmpl $0x0, -0x70(%rbp)
jle 0xec780
cmpl $0x0, -0x6c(%rbp)
jle 0xec780
jmp 0xec922
movl -0x64(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x78(%rbp)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0x64(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jl 0xec8d2
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x7c(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xec7c7
jmp 0xec8c4
movq -0x38(%rbp), %rdi
movl -0x7c(%rbp), %esi
movl -0x64(%rbp), %edx
addl $0x1, %edx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %r9
movq -0x58(%rbp), %r13
movq -0x60(%rbp), %r12
movq 0x10(%rbp), %r15
movq 0x18(%rbp), %r14
movl 0x20(%rbp), %ebx
movl 0x28(%rbp), %r11d
leaq -0x6c(%rbp), %r10
leaq -0x70(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movl %ebx, 0x20(%rsp)
movl %r11d, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0xec190
cmpl $0x0, %eax
jge 0xec834
jmp 0xec8d2
cmpl $0x0, -0x70(%rbp)
jne 0xec83f
jmp 0xec8d2
cmpl $0x0, -0x70(%rbp)
jne 0xec852
xorps %xmm0, %xmm0
movsd %xmm0, -0xa0(%rbp)
jmp 0xec868
cvtsi2sdl -0x6c(%rbp), %xmm0
cvtsi2sdl -0x70(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
ucomisd -0x90(%rbp), %xmm0
ja 0xec8a6
movsd -0x88(%rbp), %xmm0
ucomisd -0x90(%rbp), %xmm0
jne 0xec8c2
jp 0xec8c2
movl -0x6c(%rbp), %eax
cmpl -0x78(%rbp), %eax
jle 0xec8c2
movl -0x7c(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x78(%rbp)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
jmp 0xec8c4
movl -0x7c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xec7a5
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x64(%rbp), %eax
jg 0xec920
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x7c(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xec8f8
jmp 0xec915
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x7c(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xec8db
jmp 0xec922
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xec698
cmpl $0x0, -0x68(%rbp)
jne 0xec93f
movl $0x1, -0x2c(%rbp)
jmp 0xec974
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %r8
movl 0x20(%rbp), %r9d
movl 0x28(%rbp), %r10d
movq 0x30(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xe8680
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)