// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: polish_unitig_scaf(ma_utg_t*, asg_t*, ma_hit_t_alloc*, ma_sub_t*, kvec_asg_arc_t_warp*, int, int, kvec_asg_arc_t_warp*)
uint32_t polish_unitig_scaf(ma_utg_t* in, asg_t* rg, ma_hit_t_alloc* src, ma_sub_t *cov, kvec_asg_arc_t_warp* edge, 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, k, v, pre, pre_i, afte, afte_i, exactLen, inexactLen, skip = 0, z, l;
    uint32_t min_inexactLen, max_exactLen;
    asg_arc_t pE, aE;

    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) {
                pre = (uint64_t)(in->a[l])>>32; pre_i = l; afte_i = (uint32_t)-1;
                for (i = l + 1; i < z - 1; i++) {
                    if(in->a[i] == (uint64_t)-1) continue;
                    ///v and after must be available
                    v = (uint64_t)(in->a[i])>>32;
                    afte = (uint64_t)(in->a[i+1])>>32;

                    get_specific_edge(src, cov, NULL, edge, pre_i == i-1? rg:NULL, max_hang, min_ovlp, v^1, pre^1, &pE);
                    get_specific_edge(src, cov, NULL, edge, rg, max_hang, min_ovlp, v, afte, &aE);
        
                    if(pE.el == 1 && aE.el == 1) {
                        pre = (uint64_t)(in->a[i])>>32; pre_i = i;
                        continue;
                    } 

                    ///pre must be a good read, we need to find a good after
                    ///update a new afte
                    afte_i = detect_exact_ovec(in, rg, src, cov, edge, max_hang, min_ovlp, pre, i+1);
                    if(afte_i == (uint32_t)-1) {
                        pre = (uint64_t)(in->a[i])>>32; pre_i = i;
                        continue;
                    } 
                    afte = (uint64_t)(in->a[afte_i])>>32;


                    min_inexactLen = (uint32_t)-1;max_exactLen = 0;
                    for (k = i; k < afte_i; k++) {
                        get_overlapLen((uint64_t)(in->a[k])>>33, src, &exactLen, &inexactLen);
                        if(inexactLen < min_inexactLen) {
                            min_inexactLen = inexactLen;
                            max_exactLen = exactLen;
                        }
                    }

                    get_overlapLen(pre>>1, src, &exactLen, &inexactLen);
                    if((inexactLen > min_inexactLen) || (inexactLen == min_inexactLen && exactLen <= max_exactLen)) {
                        pre = (uint64_t)(in->a[i])>>32; pre_i = i;
                        continue;
                    } 

                    get_overlapLen(afte>>1, src, &exactLen, &inexactLen);
                    if((inexactLen > min_inexactLen) || (inexactLen == min_inexactLen && exactLen <= max_exactLen)) {
                        pre = (uint64_t)(in->a[i])>>32; pre_i = i;
                        continue;
                    } 

                    for (k = i; k < afte_i; k++) {
                        in->a[k] = (uint64_t)-1;
                        skip++;
                    }
                }
            }
            l = z;
        }
    }

    if(skip == 0) return 0;
    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 $0xa8, %rsp
movl %r9d, 0x4(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, 0x58(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0x7a523
movq %rdi, %r15
movl 0x10(%rdi), %ecx
cmpl $0x3, %ecx
jb 0x7a523
pushq $0x1
popq %r8
movq $0x0, 0x20(%rsp)
xorl %eax, %eax
movq %r15, 0x68(%rsp)
cmpl %ecx, %r8d
ja 0x7a4e3
je 0x7a274
leaq 0x1d9fda(%rip), %rdx    # 0x254230
movq 0x10(%rdx), %rdx
movq 0x18(%r15), %rsi
movl %r8d, %edi
movq (%rsi,%rdi,8), %rsi
shrq $0x21, %rsi
cmpq $0x0, (%rdx,%rsi,8)
jne 0x7a4db
movl %r8d, %edx
subl %eax, %edx
cmpl $0x3, %edx
jb 0x7a4d8
movq 0x18(%r15), %rcx
movl %eax, %edx
movl 0x4(%rcx,%rdx,8), %edx
leal -0x1(%r8), %ecx
movl %ecx, 0x54(%rsp)
leal 0x1(%rax), %r12d
movl %eax, %esi
movq %r8, 0x18(%rsp)
leal 0x1(%rax), %edi
cmpl 0x54(%rsp), %edi
jae 0x7a4d4
movq 0x18(%r15), %rcx
movl %edi, %r9d
movq (%rcx,%r9,8), %r14
cmpq $-0x1, %r14
je 0x7a48a
movq %r9, 0x60(%rsp)
movl %edi, 0x8(%rsp)
movq %r12, 0x48(%rsp)
shrq $0x20, %r14
leal 0x2(%rax), %edi
movq %rdi, 0x70(%rsp)
movl 0x4(%rcx,%rdi,8), %ecx
movq %rcx, 0x40(%rsp)
movl %esi, 0x50(%rsp)
cmpl %eax, %esi
movl $0x0, %r8d
movq 0x58(%rsp), %r15
cmoveq %r15, %r8
movl %r14d, %eax
xorl $0x1, %eax
movq %rdx, 0x38(%rsp)
xorl $0x1, %edx
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq 0x30(%rsp), %rbx
movq %rbx, %rcx
movl 0x4(%rsp), %r13d
movl %r13d, %r9d
leaq 0x90(%rsp), %r10
pushq %r10
pushq %rdx
pushq %rax
movl 0xf8(%rsp), %eax
pushq %rax
callq 0x79db6
addq $0x20, %rsp
movq %rbp, %rdi
leaq 0xc(%rsp), %rbp
movq %r12, %rsi
movq %rbx, %rcx
movq %r15, %r8
movq 0x68(%rsp), %r15
movl %r13d, %r9d
leaq 0x78(%rsp), %rax
pushq %rax
pushq 0x48(%rsp)
pushq %r14
movl 0xf8(%rsp), %eax
pushq %rax
callq 0x79db6
addq $0x20, %rsp
cmpb $0x1, 0xa2(%rsp)
jne 0x7a392
cmpb $0x1, 0x8a(%rsp)
je 0x7a46d
subq $0x8, %rsp
movq %r15, %rdi
movq 0x18(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r8
movl 0xc(%rsp), %r9d
pushq 0x78(%rsp)
pushq 0x48(%rsp)
movl 0xf8(%rsp), %eax
pushq %rax
callq 0x79cf8
addq $0x20, %rsp
movq 0x18(%r15), %rcx
cmpl $-0x1, %eax
movq 0x48(%rsp), %r12
je 0x7a494
movl %eax, %r13d
movq (%rcx,%r13,8), %rax
movq %rax, 0x40(%rsp)
xorl %ebx, %ebx
pushq $-0x1
popq %r14
cmpq %r13, %r12
jae 0x7a41f
movq 0x18(%r15), %rax
movq (%rax,%r12,8), %rdi
shrq $0x21, %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rsp, %rcx
callq 0x7882c
movl (%rsp), %eax
cmpl %r14d, %eax
jae 0x7a41a
movl 0xc(%rsp), %ebx
movl %eax, %r14d
incq %r12
jmp 0x7a3ea
movq 0x38(%rsp), %rax
movl %eax, %edi
shrl %edi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rsp, %rcx
callq 0x7882c
cmpl %r14d, (%rsp)
ja 0x7a46d
jne 0x7a446
cmpl %ebx, 0xc(%rsp)
jbe 0x7a46d
movq 0x40(%rsp), %rdi
shrq $0x21, %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %rsp, %rcx
callq 0x7882c
cmpl %r14d, (%rsp)
ja 0x7a46d
jne 0x7a49b
cmpl %ebx, 0xc(%rsp)
ja 0x7a49b
movq 0x18(%r15), %rcx
movq 0x18(%rsp), %r8
movq 0x48(%rsp), %r12
movl 0x8(%rsp), %edi
movq 0x60(%rsp), %rax
movl 0x4(%rcx,%rax,8), %edx
movl %edi, %esi
incq %r12
movl %edi, %eax
jmp 0x7a29f
movq 0x18(%rsp), %r8
jmp 0x7a47b
movq 0x48(%rsp), %r12
movq %r12, %rax
movq 0x18(%rsp), %r8
movq 0x38(%rsp), %rdx
movl 0x50(%rsp), %esi
movl 0x8(%rsp), %edi
cmpq %r13, %rax
jae 0x7a48a
movq 0x18(%r15), %rcx
orq $-0x1, (%rcx,%rax,8)
movq 0x20(%rsp), %rcx
incl %ecx
movq %rcx, 0x20(%rsp)
incq %rax
jmp 0x7a4b5
movl 0x10(%r15), %ecx
movl %r8d, %eax
incl %r8d
jmp 0x7a244
cmpl $0x0, 0x20(%rsp)
je 0x7a521
movq %r15, %rdi
movq 0x58(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movl 0x4(%rsp), %r9d
pushq 0xe8(%rsp)
movl 0xe8(%rsp), %eax
pushq %rax
callq 0x792d5
popq %rax
popq %rcx
pushq $0x1
popq %rax
jmp 0x7a523
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq