// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xe8, %rsp
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movl %r9d, -0x3c(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xeae41
movl $0x0, -0xc(%rbp)
jmp 0xeb26e
movq -0x18(%rbp), %rax
cmpl $0x3, 0x10(%rax)
jae 0xeae57
movl $0x0, -0xc(%rbp)
jmp 0xeb26e
movl $0x0, -0x64(%rbp)
movl $0x1, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x68(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
ja 0xeb22a
movl -0x68(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
je 0xeaeb1
leaq 0x2da441(%rip), %rax    # 0x3c52d0
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x68(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0xeb21a
movl -0x68(%rbp), %eax
subl -0x6c(%rbp), %eax
cmpl $0x3, %eax
jb 0xeb214
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x6c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x50(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl -0x68(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xeb212
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xeaf14
jmp 0xeb204
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x54(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x50(%rbp), %eax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xeaf80
movq -0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0xeaf8b
xorl %eax, %eax
movq %rax, -0xc8(%rbp)
jmp 0xeaf8b
movq -0xb0(%rbp), %rcx
movq -0xb8(%rbp), %rsi
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %r8
movl -0x3c(%rbp), %r9d
movl 0x10(%rbp), %ebx
movl -0x48(%rbp), %r11d
xorl $0x1, %r11d
movl -0x4c(%rbp), %r10d
xorl $0x1, %r10d
xorl %eax, %eax
movl %eax, %edx
leaq -0x90(%rbp), %rax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0xea7d0
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
movl -0x3c(%rbp), %r9d
movl 0x10(%rbp), %ebx
movl -0x48(%rbp), %r11d
movl -0x54(%rbp), %r10d
xorl %eax, %eax
movl %eax, %edx
leaq -0xa8(%rbp), %rax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0xea7d0
movzbl -0x7e(%rbp), %eax
cmpl $0x1, %eax
jne 0xeb057
movzbl -0x96(%rbp), %eax
cmpl $0x1, %eax
jne 0xeb057
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0xeb204
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
movl -0x3c(%rbp), %r9d
movl 0x10(%rbp), %r11d
movl -0x4c(%rbp), %r10d
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0xea640
movl %eax, -0x58(%rbp)
cmpl $-0x1, -0x58(%rbp)
jne 0xeb0b9
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0xeb204
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x58(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x54(%rbp)
movl $0xffffffff, -0x70(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x74(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0xeb132
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, %edi
movq -0x28(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0xe85d0
movl -0x60(%rbp), %eax
cmpl -0x70(%rbp), %eax
jae 0xeb125
movl -0x60(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0xeb127
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xeb0e3
movl -0x4c(%rbp), %edi
shrl %edi
movq -0x28(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0xe85d0
movl -0x60(%rbp), %eax
cmpl -0x70(%rbp), %eax
ja 0xeb160
movl -0x60(%rbp), %eax
cmpl -0x70(%rbp), %eax
jne 0xeb181
movl -0x5c(%rbp), %eax
cmpl -0x74(%rbp), %eax
ja 0xeb181
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0xeb204
movl -0x54(%rbp), %edi
shrl %edi
movq -0x28(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0xe85d0
movl -0x60(%rbp), %eax
cmpl -0x70(%rbp), %eax
ja 0xeb1af
movl -0x60(%rbp), %eax
cmpl -0x70(%rbp), %eax
jne 0xeb1cd
movl -0x5c(%rbp), %eax
cmpl -0x74(%rbp), %eax
ja 0xeb1cd
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0xeb204
movl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0xeb202
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq $-0x1, (%rax,%rcx,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xeb1d3
jmp 0xeb204
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xeaeec
jmp 0xeb214
movl -0x68(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0xeb21c
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0xeae6c
cmpl $0x0, -0x64(%rbp)
jne 0xeb239
movl $0x0, -0xc(%rbp)
jmp 0xeb26e
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
movl -0x3c(%rbp), %r9d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xe96c0
movl $0x1, -0xc(%rbp)
movl -0xc(%rbp), %eax
addq $0xe8, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax,%rax)