// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x70(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0xb539b
movq %rdi, %r14
movl 0x10(%rdi), %ecx
cmpl $0x3, %ecx
jb 0xb539b
movl %r9d, %ebp
movl $0x1, %edi
xorl %r10d, %r10d
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
leaq 0x1e126f(%rip), %r9     # 0x296230
movl $0x0, 0x20(%rsp)
movl %ebp, 0x24(%rsp)
movq %r14, 0x68(%rsp)
cmpl %ecx, %edi
je 0xb4ff3
movq 0x10(%r9), %rax
movq 0x18(%r14), %rdx
movl %edi, %esi
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
cmpq $0x0, (%rax,%rdx,8)
jne 0xb534c
movl %edi, %eax
subl %r10d, %eax
cmpl $0x3, %eax
jb 0xb532c
leal -0x1(%rdi), %edx
leal 0x1(%r10), %eax
cmpl %edx, %eax
jae 0xb532c
movq %rdi, 0x88(%rsp)
movq 0x18(%r14), %rcx
movl %r10d, %edx
movl 0x4(%rcx,%rdx,8), %r13d
movl %eax, %ebx
movl %r10d, %edx
notl %edx
movl %r8d, 0x5c(%rsp)
subl %r10d, %r8d
movl %r8d, 0x64(%rsp)
movq %r10, %rax
movq %rbx, 0x98(%rsp)
movq %rbx, %rsi
xorl %ebx, %ebx
movl %eax, %edi
movq %r10, 0xa0(%rsp)
movq 0x18(%r14), %rcx
movq (%rcx,%rsi,8), %r12
cmpq $-0x1, %r12
je 0xb5157
movq %rsi, 0x18(%rsp)
movq %rbx, 0x80(%rsp)
movl %edx, 0xc(%rsp)
shrq $0x20, %r12
leal 0x2(%rax), %edx
movq %rdx, 0x38(%rsp)
movl 0x4(%rcx,%rdx,8), %ecx
movq %rcx, 0x50(%rsp)
movl %edi, 0x34(%rsp)
cmpl %eax, %edi
movl $0x0, %r8d
movq 0x70(%rsp), %r14
cmoveq %r14, %r8
movl %r12d, %eax
xorl $0x1, %eax
movq %r13, 0x78(%rsp)
movl %r13d, %edx
xorl $0x1, %edx
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq 0x40(%rsp), %r15
movq %r15, %rsi
movl %ebp, %ebx
movq 0x48(%rsp), %rbp
movq %rbp, %rcx
movl %ebx, %r9d
leaq 0xc0(%rsp), %r10
pushq %r10
pushq %rdx
pushq %rax
movl 0x128(%rsp), %eax
pushq %rax
callq 0xb4a8d
addq $0x20, %rsp
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rcx
movl %ebx, %ebp
movq %r14, %r8
movl %ebx, %r9d
leaq 0xa8(%rsp), %rax
pushq %rax
pushq 0x58(%rsp)
pushq %r12
movl 0x128(%rsp), %eax
pushq %rax
callq 0xb4a8d
addq $0x20, %rsp
cmpb $0x1, 0xd2(%rsp)
jne 0xb515e
cmpb $0x1, 0xba(%rsp)
jne 0xb515e
movq 0x68(%rsp), %r14
movq 0x18(%r14), %rax
movq 0x18(%rsp), %rsi
movl 0x4(%rax,%rsi,8), %r13d
movl %esi, %eax
movl %esi, %edi
movl 0xc(%rsp), %edx
movq 0x80(%rsp), %rbx
jmp 0xb52b7
movl %esi, %eax
jmp 0xb52b7
subq $0x8, %rsp
movq 0x70(%rsp), %r14
movq %r14, %rdi
movq 0x30(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movl %ebp, %r9d
pushq 0x40(%rsp)
pushq 0x88(%rsp)
movl 0x128(%rsp), %eax
pushq %rax
callq 0xb49b2
addq $0x20, %rsp
movq 0x18(%r14), %rcx
cmpl $-0x1, %eax
movq 0x80(%rsp), %rbx
je 0xb5204
movl %eax, 0x38(%rsp)
movl %eax, %r15d
movq (%rcx,%r15,8), %rax
movq %rax, 0x50(%rsp)
movq 0x18(%rsp), %r13
cmpq %r15, %r13
jae 0xb5217
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movq 0x18(%r14), %rax
movq (%rax,%r13,8), %rdi
shrq $0x21, %rdi
movq 0x28(%rsp), %rsi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0xb343e
movl 0x10(%rsp), %eax
cmpl %ebp, %eax
jae 0xb51fa
movl 0x14(%rsp), %r12d
movl %eax, %ebp
incq %r13
cmpq %r15, %r13
jb 0xb51cb
jmp 0xb521f
movq 0x18(%rsp), %rsi
movl 0x4(%rcx,%rsi,8), %r13d
movl %esi, %eax
movl %esi, %edi
jmp 0xb52b3
xorl %r12d, %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x98(%rsp), %rax
addq %rbx, %rax
shlq $0x3, %rax
movq %rax, 0x90(%rsp)
movq 0xa0(%rsp), %rax
addl %ebx, %eax
movl %eax, 0x60(%rsp)
movq 0x78(%rsp), %r13
movl %r13d, %edi
shrl %edi
movq 0x28(%rsp), %rsi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0xb343e
cmpl %ebp, 0x10(%rsp)
ja 0xb529d
jne 0xb5271
cmpl %r12d, 0x14(%rsp)
jbe 0xb529d
movq 0x50(%rsp), %rdi
shrq $0x21, %rdi
movq 0x28(%rsp), %rsi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0xb343e
cmpl %ebp, 0x10(%rsp)
ja 0xb529d
jne 0xb52cb
cmpl %r12d, 0x14(%rsp)
ja 0xb52cb
movq 0x18(%r14), %rax
movq 0x18(%rsp), %rsi
movl 0x4(%rax,%rsi,8), %r13d
movl %esi, %eax
movl %esi, %edi
movl 0x24(%rsp), %ebp
movl 0xc(%rsp), %edx
incq %rsi
incq %rbx
decl %edx
cmpl %ebx, 0x64(%rsp)
jne 0xb5052
jmp 0xb5331
movq 0x18(%rsp), %rsi
cmpq %r15, %rsi
movl 0x24(%rsp), %ebp
jae 0xb5320
movq 0x90(%rsp), %rdi
addq 0x18(%r14), %rdi
movl 0x38(%rsp), %r15d
movl %r15d, %eax
subl 0x60(%rsp), %eax
addl $-0x2, %eax
leaq 0x8(,%rax,8), %rdx
movl $0xff, %esi
callq 0x20e0
movq 0x18(%rsp), %rsi
movl 0x20(%rsp), %eax
addl %r15d, %eax
movl 0xc(%rsp), %edx
addl %edx, %eax
movl %eax, 0x20(%rsp)
movl %esi, %eax
jmp 0xb5326
movl %esi, %eax
movl 0xc(%rsp), %edx
movl 0x34(%rsp), %edi
jmp 0xb52b7
movl %edi, %r10d
jmp 0xb534c
movl 0x10(%r14), %ecx
movq 0x88(%rsp), %rdi
movl %edi, %r10d
movl 0x5c(%rsp), %r8d
leaq 0x1e0ee4(%rip), %r9     # 0x296230
incl %edi
incl %r8d
cmpl %ecx, %edi
jbe 0xb4fd2
cmpl $0x0, 0x20(%rsp)
je 0xb5399
movq %r14, %rdi
movq 0x70(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r8
movl %ebp, %r9d
pushq 0x118(%rsp)
movl 0x118(%rsp), %eax
pushq %rax
callq 0xb3ed1
addq $0x10, %rsp
movl $0x1, %eax
jmp 0xb539b
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq