// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: polish_unitig(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(ma_utg_t* collection, asg_t* read_g, ma_hit_t_alloc* sources, 
ma_sub_t *coverage_cut, kvec_asg_arc_t_warp* edge, int max_hang, int min_ovlp, 
kvec_asg_arc_t_warp* newE)
{
    if(collection->m == 0) return 0;
    if(collection->n < 3) return 0;
    uint32_t i, k, v, pre, pre_i, afte, afte_i, exactLen, inexactLen, skip = 0;
    uint32_t min_inexactLen, max_exactLen;
    asg_arc_t pE, aE;

    pre = (uint64_t)(collection->a[0])>>32; pre_i = 0; afte_i = (uint32_t)-1;
    for (i = 1; i < collection->n - 1; i++) {
        if(collection->a[i] == (uint64_t)-1) continue;
        ///v and after must be available
        v = (uint64_t)(collection->a[i])>>32;
        afte = (uint64_t)(collection->a[i+1])>>32;

        get_specific_edge(sources, coverage_cut, NULL, edge, pre_i == i-1? read_g:NULL, max_hang, min_ovlp, 
        v^1, pre^1, &pE);
        get_specific_edge(sources, coverage_cut, NULL, edge, read_g, max_hang, min_ovlp, 
        v, afte, &aE);
        
        if(pE.el == 1 && aE.el == 1)
        {
            pre = (uint64_t)(collection->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(collection, read_g, sources, coverage_cut, edge, 
        max_hang, min_ovlp, pre, i+1);
        if(afte_i == (uint32_t)-1)
        {
            pre = (uint64_t)(collection->a[i])>>32; pre_i = i;
            continue;
        } 
        afte = (uint64_t)(collection->a[afte_i])>>32;


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

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

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

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

    if(skip == 0) return 0;
    reduce_ma_utg_t(collection, read_g, sources, coverage_cut, edge, max_hang, min_ovlp, newE);

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xd8, %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 0xeaa41
movl $0x0, -0xc(%rbp)
jmp 0xeadf3
movq -0x18(%rbp), %rax
cmpl $0x3, 0x10(%rax)
jae 0xeaa57
movl $0x0, -0xc(%rbp)
jmp 0xeadf3
movl $0x0, -0x64(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x18(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xeadaf
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
cmpq $-0x1, (%rax,%rcx,8)
jne 0xeaab1
jmp 0xeada1
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, -0xb8(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0x50(%rbp), %eax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xeab1d
movq -0x20(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0xeab28
xorl %eax, %eax
movq %rax, -0xc0(%rbp)
jmp 0xeab28
movq -0xa8(%rbp), %rcx
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdi
movq -0xc0(%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 -0x88(%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 -0xa0(%rbp), %rax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0xea7d0
movzbl -0x76(%rbp), %eax
cmpl $0x1, %eax
jne 0xeabf4
movzbl -0x8e(%rbp), %eax
cmpl $0x1, %eax
jne 0xeabf4
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 0xeada1
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 0xeac56
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 0xeada1
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, -0x68(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x6c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0xeaccf
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 -0x68(%rbp), %eax
jae 0xeacc2
movl -0x60(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0xeacc4
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xeac80
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 -0x68(%rbp), %eax
ja 0xeacfd
movl -0x60(%rbp), %eax
cmpl -0x68(%rbp), %eax
jne 0xead1e
movl -0x5c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
ja 0xead1e
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 0xeada1
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 -0x68(%rbp), %eax
ja 0xead4c
movl -0x60(%rbp), %eax
cmpl -0x68(%rbp), %eax
jne 0xead6a
movl -0x5c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
ja 0xead6a
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 0xeada1
movl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0xead9f
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 0xead70
jmp 0xeada1
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xeaa85
cmpl $0x0, -0x64(%rbp)
jne 0xeadbe
movl $0x0, -0xc(%rbp)
jmp 0xeadf3
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 0xe8680
movl $0x1, -0xc(%rbp)
movl -0xc(%rbp), %eax
addq $0xd8, %rsp
popq %rbx
popq %rbp
retq