// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %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 0xb555e
movq %rdi, %r13
movl 0x10(%rdi), %esi
cmpq $0x3, %rsi
jb 0xb555e
testl %esi, %esi
jle 0xb5559
xorl %r15d, %r15d
movl $0x1, %edi
leaq 0x1d5f51(%rip), %r8     # 0x28b230
xorl %r12d, %r12d
xorl %eax, %eax
movq %rsi, 0x48(%rsp)
cmpq %rsi, %rdi
je 0xb5309
movq 0x10(%r8), %rcx
movq 0x18(%r13), %rdx
movq (%rdx,%rdi,8), %rdx
shrq $0x21, %rdx
cmpq $0x0, (%rcx,%rdx,8)
jne 0xb550e
movl %edi, %ecx
subl %eax, %ecx
cmpl $0x3, %ecx
jl 0xb550c
incl %eax
leaq -0x1(%rdi), %rcx
movslq %eax, %r14
cmpq %r14, %rcx
jle 0xb550c
movq %rdi, 0x50(%rsp)
movl %r15d, 0x14(%rsp)
movq %r14, %rbx
incq %r14
movq %r13, %rdi
movl %ebx, %esi
movl %r14d, %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 0xb8(%rsp), %eax
pushq %rax
movl 0xb8(%rsp), %eax
pushq %rax
pushq 0xb8(%rsp)
pushq 0xb8(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0xb4d20
addq $0x40, %rsp
cmpl $0x1, %eax
jne 0xb54ee
movl 0xc(%rsp), %eax
movl 0x10(%rsp), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movapd %xmm1, %xmm2
mulsd 0x17013b(%rip), %xmm2   # 0x2254f0
testl %ebp, %ebp
jle 0xb53c7
testl %eax, %eax
jle 0xb53c7
ucomisd %xmm2, %xmm0
jae 0xb54ee
testq %rbx, %rbx
jle 0xb54b4
xorpd %xmm2, %xmm2
movsd %xmm2, 0x38(%rsp)
testl %eax, %eax
je 0xb53e8
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movl %ebx, %r15d
decq %r15
movl %ebx, 0x8(%rsp)
movq 0x18(%r13), %rax
movl %r15d, %ecx
cmpq $-0x1, (%rax,%rcx,8)
je 0xb54a2
movq %r13, %rdi
movl %r15d, %esi
movl %r14d, %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 0xb8(%rsp), %eax
pushq %rax
movl 0xb8(%rsp), %eax
pushq %rax
pushq 0xb8(%rsp)
pushq 0xb8(%rsp)
pushq 0x60(%rsp)
pushq 0x60(%rsp)
callq 0xb4d20
addq $0x40, %rsp
testl %eax, %eax
js 0xb54b8
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
je 0xb54b8
movl 0x10(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x38(%rsp), %xmm0
ja 0xb5495
setnp %cl
sete %dl
andb %cl, %dl
cmpl %ebp, %eax
setg %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0xb54a2
movl %r15d, 0x8(%rsp)
movl %eax, %ebp
movsd %xmm0, 0x38(%rsp)
decq %r15
leal 0x2(%r15), %eax
cmpl $0x1, %eax
jg 0xb53f2
jmp 0xb54b8
movl %ebx, 0x8(%rsp)
movslq 0x8(%rsp), %rax
cmpq %rax, %rbx
jle 0xb54e9
movq 0x18(%r13), %rcx
movl 0x14(%rsp), %r15d
cmpq $-0x1, 0x8(%rcx,%rax,8)
je 0xb54df
movq $-0x1, 0x8(%rcx,%rax,8)
incl %r12d
incq %rax
cmpq %rax, %rbx
jne 0xb54cb
jmp 0xb54ee
movl 0x14(%rsp), %r15d
cmpl %r14d, %r15d
jne 0xb5332
movq 0x50(%rsp), %rdi
movl %edi, %eax
movq 0x48(%rsp), %rsi
leaq 0x1d5d26(%rip), %r8     # 0x28b230
jmp 0xb550e
movl %edi, %eax
incq %rdi
incl %r15d
cmpl %esi, %r15d
jne 0xb52e9
testl %r12d, %r12d
je 0xb5559
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movl 0xa0(%rsp), %r9d
pushq 0xb0(%rsp)
movl 0xb0(%rsp), %eax
pushq %rax
callq 0xb2dcb
addq $0x10, %rsp
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq