// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: polish_unitig_advance_scaf(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_scaf(ma_utg_t* in, asg_t* rg, All_reads *RNF, ma_hit_t_alloc* src, ma_sub_t *cov, 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(in->m == 0) return 0;
    if(in->n < 3) return 0;
    // uint32_t i, skip = 0;
    int match_v, total_v, max_i, match_max, k, z, l, in_n = in->n, i, skip = 0;
    double match_rate, match_rate_max;
        

    for (z = 1, l = 0; z <= in_n; z++) {
        if(z == in_n || IS_SCAF_READ(R_INF, (in->a[z])>>33) ) {
            if(z - l >= 3) {
                ///we should be able to handle i = z-1
                for (i = l + 1; i < z - 1; i++) {    
                    ///in practice, in->a[i] and in->a[i+1] must be available
                    ///in->a[index] might be unavailable only if index < i
                    if(get_consensus_rate(in, i, i+1, rg, RNF, src, cov, 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 in[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(in->a[k] == (uint64_t)-1) continue;
                        ///in->a[k] might be unavailable, while in->a[i+1] must be available
                        ///return -1 means there is no overlap from k to i+1
                        if(get_consensus_rate(in, k, i+1, rg, RNF, src, cov, 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(in->a[k] == (uint64_t)-1) continue;
                        in->a[k] = (uint64_t)-1;
                        skip++;
                    }
                }
            }
            l = z;
        }
    }

    if(skip == 0) return 1;

    reduce_ma_utg_t_scaf(in, rg, src, cov, 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 $0xc8, %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 0xec9e4
movl $0x0, -0x2c(%rbp)
jmp 0xecd89
movq -0x38(%rbp), %rax
cmpl $0x3, 0x10(%rax)
jae 0xec9fa
movl $0x0, -0x2c(%rbp)
jmp 0xecd89
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x80(%rbp)
movl $0x0, -0x88(%rbp)
movl $0x1, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x80(%rbp), %eax
jg 0xecd42
movl -0x78(%rbp), %eax
cmpl -0x80(%rbp), %eax
je 0xeca5a
leaq 0x2d8899(%rip), %rax    # 0x3c52d0
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x18(%rcx), %rcx
movslq -0x78(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0xecd32
movl -0x78(%rbp), %eax
subl -0x7c(%rbp), %eax
cmpl $0x3, %eax
jl 0xecd2c
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl -0x78(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xecd2a
movq -0x38(%rbp), %rdi
movl -0x84(%rbp), %esi
movl -0x84(%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 -0x64(%rbp), %r10
leaq -0x68(%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 0xecafc
jmp 0xecd16
cmpl $0x0, -0x68(%rbp)
jne 0xecb0f
xorps %xmm0, %xmm0
movsd %xmm0, -0xa0(%rbp)
jmp 0xecb25
cvtsi2sdl -0x64(%rbp), %xmm0
cvtsi2sdl -0x68(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
cvtsi2sdl -0x64(%rbp), %xmm0
cvtsi2sdl -0x68(%rbp), %xmm1
movsd 0x295729(%rip), %xmm2   # 0x382270
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0xecb62
cmpl $0x0, -0x68(%rbp)
jle 0xecb62
cmpl $0x0, -0x64(%rbp)
jle 0xecb62
jmp 0xecd16
movl -0x84(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x70(%rbp)
movsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movl -0x84(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jl 0xeccbd
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x74(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xecbaf
jmp 0xeccaf
movq -0x38(%rbp), %rdi
movl -0x74(%rbp), %esi
movl -0x84(%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 -0x64(%rbp), %r10
leaq -0x68(%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 0xecc1f
jmp 0xeccbd
cmpl $0x0, -0x68(%rbp)
jne 0xecc2a
jmp 0xeccbd
cmpl $0x0, -0x68(%rbp)
jne 0xecc3d
xorps %xmm0, %xmm0
movsd %xmm0, -0xa8(%rbp)
jmp 0xecc53
cvtsi2sdl -0x64(%rbp), %xmm0
cvtsi2sdl -0x68(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x90(%rbp), %xmm0
ucomisd -0x98(%rbp), %xmm0
ja 0xecc91
movsd -0x90(%rbp), %xmm0
ucomisd -0x98(%rbp), %xmm0
jne 0xeccad
jp 0xeccad
movl -0x64(%rbp), %eax
cmpl -0x70(%rbp), %eax
jle 0xeccad
movl -0x74(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x70(%rbp)
movsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
jmp 0xeccaf
movl -0x74(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xecb8d
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x84(%rbp), %eax
jg 0xecd14
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x74(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xecce6
jmp 0xecd09
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x74(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xeccc6
jmp 0xecd16
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xeca75
jmp 0xecd2c
movl -0x78(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0xecd34
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0xeca1c
cmpl $0x0, -0x88(%rbp)
jne 0xecd54
movl $0x1, -0x2c(%rbp)
jmp 0xecd89
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 0xe96c0
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop