// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movl %r9d, 0xc(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rsi, 0x58(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0xb4370
movq %rdi, %r15
cmpl $0x3, 0x10(%rdi)
jb 0xb4370
movl 0x10(%r15), %eax
testl %eax, %eax
je 0xb436e
movq %rdx, %rbx
movl $0x1, %esi
xorl %r14d, %r14d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq 0x1d733d(%rip), %r8     # 0x28b230
xorl %r9d, %r9d
movl $0x0, 0x8(%rsp)
movq %rdx, 0x68(%rsp)
movq %r15, 0x10(%rsp)
cmpl %eax, %esi
je 0xb3f29
movq 0x10(%r8), %rax
movq 0x18(%r15), %rcx
movl %esi, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0xb431e
movl %esi, %eax
subl %r9d, %eax
cmpl $0x3, %eax
jb 0xb4306
leal -0x1(%rsi), %ecx
leal 0x1(%r9), %eax
cmpl %ecx, %eax
jae 0xb4306
movq %rsi, 0x70(%rsp)
movq 0x18(%r15), %rcx
movl %r9d, %edx
movl 0x4(%rcx,%rdx,8), %esi
movl %eax, %ecx
movl %r9d, %edx
notl %edx
movl %edi, 0x60(%rsp)
subl %r9d, %edi
movl %edi, 0x64(%rsp)
xorl %ebp, %ebp
movq %rcx, 0x78(%rsp)
movq %rcx, %r13
movl %r9d, %edi
movq %r9, 0x80(%rsp)
movl %r9d, %eax
movq 0x18(%r15), %rcx
movq (%rcx,%r13,8), %r12
cmpq $-0x1, %r12
je 0xb4296
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movl %edx, 0x4(%rsp)
shrq $0x20, %r12
leal 0x2(%rax), %edx
movq %rdx, 0x30(%rsp)
movl 0x4(%rcx,%rdx,8), %ecx
movq %rcx, 0x50(%rsp)
movl %edi, 0x2c(%rsp)
cmpl %eax, %edi
movl $0x0, %r8d
cmoveq 0x58(%rsp), %r8
movl %r12d, %eax
xorl $0x1, %eax
movq %rsi, 0x38(%rsp)
movl %esi, %edx
xorl $0x1, %edx
movq %rbx, %rdi
movq 0x40(%rsp), %r15
movq %r15, %rsi
movq 0x48(%rsp), %rbp
movq %rbp, %rcx
movl 0xc(%rsp), %r13d
movl %r13d, %r9d
leaq 0xa0(%rsp), %r10
pushq %r10
pushq %rdx
pushq %rax
movl 0x108(%rsp), %eax
pushq %rax
callq 0xb3953
addq $0x20, %rsp
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rcx
movq 0x58(%rsp), %r8
movl %r13d, %r9d
leaq 0x88(%rsp), %rax
pushq %rax
pushq 0x58(%rsp)
pushq %r12
movl 0x108(%rsp), %eax
pushq %rax
callq 0xb3953
addq $0x20, %rsp
cmpb $0x1, 0xb2(%rsp)
jne 0xb4074
cmpb $0x1, 0x9a(%rsp)
jne 0xb4074
movq 0x10(%rsp), %r15
movq 0x18(%r15), %rax
movq 0x20(%rsp), %r13
movl 0x4(%rax,%r13,8), %esi
movl %r13d, %edi
movq 0x68(%rsp), %rbx
jmp 0xb428d
subq $0x8, %rsp
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq 0x70(%rsp), %rbx
movq %rbx, %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movl 0x14(%rsp), %r9d
pushq 0x38(%rsp)
pushq 0x48(%rsp)
movl 0x108(%rsp), %eax
pushq %rax
callq 0xb386e
addq $0x20, %rsp
movq 0x18(%r15), %r12
cmpl $-0x1, %eax
movq 0x20(%rsp), %r13
je 0xb4204
movl %eax, 0x50(%rsp)
movl %eax, %edi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x0, %ecx
cmpq %rdi, %r13
jae 0xb4161
xorl %ecx, %ecx
movq 0x20(%rsp), %r8
movq %r12, %r13
movq (%r12,%r8,8), %r9
shrq $0x21, %r9
leaq (%r9,%r9,2), %r11
movl 0xc(%rbx,%r11,8), %r15d
movl $0x0, %r9d
movl $0x0, %r10d
testq %r15, %r15
je 0xb414b
leaq (%rbx,%r11,8), %r9
movq (%r9), %r11
shlq $0x3, %r15
leaq (%r15,%r15,4), %r15
xorl %r12d, %r12d
xorl %r9d, %r9d
xorl %r10d, %r10d
movl 0x8(%r11,%r12), %ebp
subl (%r11,%r12), %ebp
cmpb $0x1, 0x20(%r11,%r12)
movl $0x0, %eax
cmovel %ebp, %eax
cmovel %r14d, %ebp
addl %eax, %r9d
addl %ebp, %r10d
addq $0x28, %r12
cmpq %r12, %r15
jne 0xb4121
cmpl %edx, %r10d
cmovbl %r10d, %edx
cmovbl %r9d, %ecx
incq %r8
cmpq %rdi, %r8
movq %r13, %r12
jb 0xb40e4
movq 0x38(%rsp), %rax
shrl %eax
leaq (%rax,%rax,2), %r8
movl 0xc(%rbx,%r8,8), %r9d
movl $0x0, %r10d
testq %r9, %r9
je 0xb41bc
leaq (%rbx,%r8,8), %rax
movq (%rax), %r8
shlq $0x3, %r9
leaq (%r9,%r9,4), %r9
xorl %r11d, %r11d
xorl %esi, %esi
xorl %r10d, %r10d
movl 0x8(%r8,%r11), %eax
subl (%r8,%r11), %eax
cmpb $0x1, 0x20(%r8,%r11)
movl $0x0, %ebp
cmovel %eax, %ebp
cmovel %r14d, %eax
addl %ebp, %esi
addl %eax, %r10d
addq $0x28, %r11
cmpq %r11, %r9
jne 0xb4193
movq 0x78(%rsp), %rax
movq 0x18(%rsp), %rbp
leaq (%rax,%rbp), %r8
shlq $0x3, %r8
movq 0x80(%rsp), %rax
leal (%rax,%rbp), %r9d
cmpl %edx, %r10d
ja 0xb41e9
movq (%r12,%rdi,8), %r10
jne 0xb420e
cmpl %ecx, %esi
ja 0xb420e
movq 0x20(%rsp), %r13
movl 0x4(%r12,%r13,8), %esi
movl %r13d, %edi
movq 0x10(%rsp), %r15
movl 0x4(%rsp), %edx
jmp 0xb4296
movl 0x4(%r12,%r13,8), %esi
movl %r13d, %edi
jmp 0xb428d
movq %r12, 0x30(%rsp)
shrq $0x21, %r10
leaq (%r10,%r10,2), %r10
movl 0xc(%rbx,%r10,8), %esi
testq %rsi, %rsi
movq 0x20(%rsp), %r13
je 0xb426d
leaq (%rbx,%r10,8), %rax
movq (%rax), %r10
shlq $0x3, %rsi
leaq (%rsi,%rsi,4), %r11
xorl %r15d, %r15d
xorl %esi, %esi
xorl %ebp, %ebp
movl 0x8(%r10,%r15), %eax
subl (%r10,%r15), %eax
cmpb $0x1, 0x20(%r10,%r15)
movl $0x0, %r12d
cmovel %eax, %r12d
cmovel %r14d, %eax
addl %r12d, %esi
addl %eax, %ebp
addq $0x28, %r15
cmpq %r15, %r11
jne 0xb4240
jmp 0xb4271
xorl %esi, %esi
xorl %ebp, %ebp
cmpl %edx, %ebp
ja 0xb427b
jne 0xb42ad
cmpl %ecx, %esi
ja 0xb42ad
movq 0x30(%rsp), %rax
movl 0x4(%rax,%r13,8), %esi
movl %r13d, %edi
movq 0x10(%rsp), %r15
movl 0x4(%rsp), %edx
movq 0x18(%rsp), %rbp
movl %r13d, %eax
incq %r13
incq %rbp
decl %edx
cmpl %ebp, 0x64(%rsp)
jne 0xb3f80
jmp 0xb430b
cmpq %rdi, %r13
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rbp
movq 0x30(%rsp), %rdi
jae 0xb42f7
addq %r8, %rdi
movl 0x50(%rsp), %r12d
movl %r12d, %eax
subl %r9d, %eax
addl $-0x2, %eax
leaq 0x8(,%rax,8), %rdx
movl $0xff, %esi
callq 0x20e0
movl 0x8(%rsp), %eax
addl %r12d, %eax
movl 0x4(%rsp), %edx
addl %edx, %eax
movl %eax, 0x8(%rsp)
jmp 0xb42fb
movl 0x4(%rsp), %edx
movq 0x38(%rsp), %rsi
movl 0x2c(%rsp), %edi
jmp 0xb4296
movl %esi, %r9d
jmp 0xb431e
movq 0x70(%rsp), %rsi
movl %esi, %r9d
movl 0x60(%rsp), %edi
leaq 0x1d6f12(%rip), %r8     # 0x28b230
incl %esi
movl 0x10(%r15), %eax
incl %edi
cmpl %eax, %esi
jbe 0xb3f08
cmpl $0x0, 0x8(%rsp)
je 0xb436e
movq %r15, %rdi
movq 0x58(%rsp), %rsi
movq %rbx, %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r8
movl 0xc(%rsp), %r9d
pushq 0xf8(%rsp)
movl 0xf8(%rsp), %eax
pushq %rax
callq 0xb2dcb
addq $0x10, %rsp
movl $0x1, %eax
jmp 0xb4370
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq