// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x28(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, 0x20(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0x7b6b0
movq %rdi, %r13
movl 0x10(%rdi), %esi
cmpq $0x3, %rsi
jb 0x7b6b0
movq %r9, %r14
movq %r8, %rbp
xorl %eax, %eax
testl %esi, %esi
movl $0x0, %edi
cmovgl %esi, %edi
incl %edi
pushq $0x1
popq %r8
movl $0x0, 0xc(%rsp)
movq %r9, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
cmpq %rdi, %r8
je 0x7b675
cmpq %rsi, %r8
je 0x7b46d
leaq 0x1d8dde(%rip), %rcx    # 0x254230
movq 0x10(%rcx), %rcx
movq 0x18(%r13), %rdx
movq (%rdx,%r8,8), %rdx
shrq $0x21, %rdx
cmpq $0x0, (%rcx,%rdx,8)
jne 0x7b66d
movl %r8d, %ecx
subl %eax, %ecx
cmpl $0x3, %ecx
jl 0x7b656
incl %eax
movq %r8, 0x58(%rsp)
leaq -0x1(%r8), %r15
movslq %eax, %rdx
movq %rdx, %rbx
cmpq %r15, %rbx
jge 0x7b65b
leal 0x1(%rbx), %edx
movq %r13, %rdi
movl %ebx, %esi
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x28(%rsp), %r9
leaq 0x4(%rsp), %rax
pushq %rax
leaq 0x10(%rsp), %rax
pushq %rax
movl 0xc8(%rsp), %eax
pushq %rax
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq %r14
pushq %rbp
callq 0x7ae8c
addq $0x40, %rsp
cmpl $0x1, %eax
jne 0x7b514
movl 0x4(%rsp), %eax
movl 0x8(%rsp), %r12d
cvtsi2sd %r12d, %xmm0
cvtsi2sd %eax, %xmm1
movapd %xmm1, %xmm2
mulsd 0x168043(%rip), %xmm2   # 0x1e3548
testl %r12d, %r12d
jle 0x7b51c
testl %eax, %eax
jle 0x7b51c
ucomisd %xmm2, %xmm0
jb 0x7b51c
incq %rbx
jmp 0x7b48c
xorpd %xmm2, %xmm2
movsd %xmm2, 0x10(%rsp)
testl %eax, %eax
je 0x7b534
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movl %ebx, %edi
leaq 0x1(%rbx), %rdx
movq %rbx, 0x18(%rsp)
movl %edi, %r14d
decq %rdi
testl %r14d, %r14d
jle 0x7b61d
decl %r14d
movq 0x18(%r13), %rax
movl %edi, %ecx
cmpq $-0x1, (%rax,%rcx,8)
je 0x7b542
movq %rdi, 0x60(%rsp)
movq %r13, %rdi
movl %r14d, %esi
movq 0x20(%rsp), %rcx
movq %r13, %rbp
movq %r15, %r13
movq %rdx, %r15
movq 0x40(%rsp), %r8
movq 0x28(%rsp), %r9
leaq 0x4(%rsp), %rax
pushq %rax
leaq 0x10(%rsp), %rax
pushq %rax
movl 0xc8(%rsp), %eax
pushq %rax
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0x68(%rsp)
pushq 0x68(%rsp)
callq 0x7ae8c
movq 0xa0(%rsp), %rdi
movq %r15, %rdx
movq %r13, %r15
movq %rbp, %r13
addq $0x40, %rsp
testl %eax, %eax
js 0x7b61d
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
je 0x7b61d
movl 0x8(%rsp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x10(%rsp), %xmm0
ja 0x7b60a
setnp %cl
sete %sil
andb %cl, %sil
cmpl %r12d, %eax
setg %cl
testb %cl, %sil
je 0x7b542
movq %rdi, 0x18(%rsp)
movl %eax, %r12d
movsd %xmm0, 0x10(%rsp)
jmp 0x7b53f
movslq 0x18(%rsp), %rax
movq 0x38(%rsp), %r14
movq 0x30(%rsp), %rbp
movq %rax, %rcx
cmpq %rbx, %rcx
jge 0x7b489
leaq 0x1(%rcx), %rax
movq 0x18(%r13), %rsi
cmpq $-0x1, 0x8(%rsi,%rcx,8)
movq %rax, %rcx
je 0x7b62f
orq $-0x1, (%rsi,%rax,8)
incl 0xc(%rsp)
jmp 0x7b62c
movl %r8d, %eax
jmp 0x7b66d
movq 0x58(%rsp), %r8
movl %r8d, %eax
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdi
incq %r8
jmp 0x7b43d
cmpl $0x0, 0xc(%rsp)
je 0x7b6ad
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rbp, %rcx
movq %r14, %r8
movl 0xb0(%rsp), %r9d
pushq 0xc0(%rsp)
movl 0xc0(%rsp), %eax
pushq %rax
callq 0x792d5
popq %rax
popq %rcx
pushq $0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq