// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, 0x30(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, 0x50(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0x7a1d9
movq %rdi, %rbp
movl 0x10(%rdi), %ecx
cmpl $0x3, %ecx
jb 0x7a1d9
movq %r8, %r13
movl %r9d, 0x1c(%rsp)
movq 0x18(%rbp), %rax
movl 0x4(%rax), %r15d
xorl %edx, %edx
pushq $0x1
popq %rsi
movl $0x0, 0xc(%rsp)
movq %r8, 0x20(%rsp)
movq %rbp, 0x48(%rsp)
decl %ecx
cmpq %rcx, %rsi
jae 0x7a19b
movq 0x18(%rbp), %rax
movq (%rax,%rsi,8), %rbx
cmpq $-0x1, %rbx
je 0x7a032
shrq $0x20, %rbx
leaq 0x1(%rsi), %rcx
movq %rcx, 0x28(%rsp)
movl 0xc(%rax,%rsi,8), %eax
movq %rax, 0x38(%rsp)
movq %rsi, 0x40(%rsp)
leaq -0x1(%rsi), %rax
movq %rdx, 0x60(%rsp)
movl %edx, %ecx
cmpq %rcx, %rax
movl $0x0, %r8d
movq 0x50(%rsp), %r14
cmoveq %r14, %r8
movl %ebx, %eax
xorl $0x1, %eax
movq %r15, 0x58(%rsp)
movl %r15d, %edx
xorl $0x1, %edx
movq %r13, %r12
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq 0x30(%rsp), %rbp
movq %rbp, %rsi
movq %r12, %rcx
movl 0x1c(%rsp), %r15d
movl %r15d, %r9d
leaq 0x80(%rsp), %r10
pushq %r10
pushq %rdx
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
callq 0x79db6
addq $0x20, %rsp
movq %r13, %rdi
movq %r12, %r13
movq %rbp, %rsi
movq %r12, %rcx
movq %r14, %r8
movl %r15d, %r9d
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0x40(%rsp)
pushq %rbx
movl 0xe8(%rsp), %eax
pushq %rax
callq 0x79db6
addq $0x20, %rsp
cmpb $0x1, 0x92(%rsp)
jne 0x7a040
cmpb $0x1, 0x7a(%rsp)
jne 0x7a040
movq 0x48(%rsp), %rbp
movq 0x18(%rbp), %rax
movq 0x40(%rsp), %r12
movl 0x4(%rax,%r12,8), %r15d
jmp 0x7a112
incq %rsi
movq %rsi, %rbx
movl %edx, %r12d
jmp 0x7a18d
subq $0x8, %rsp
movq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq 0x18(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %r13, %r8
movl %r15d, %r9d
movq 0x30(%rsp), %rbx
pushq %rbx
pushq 0x68(%rsp)
movl 0xe8(%rsp), %eax
pushq %rax
callq 0x79cf8
addq $0x20, %rsp
movq 0x18(%rbp), %rcx
cmpl $-0x1, %eax
je 0x7a119
movl %eax, %r14d
movq (%rcx,%r14,8), %rax
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
movq 0x40(%rsp), %r12
movq %r12, %r15
pushq $-0x1
popq %rbx
cmpq %r14, %r15
jae 0x7a0d7
movq 0x18(%rbp), %rax
movq (%rax,%r15,8), %rdi
shrq $0x21, %rdi
movq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0x7882c
movl 0x4(%rsp), %eax
cmpl %ebx, %eax
jae 0x7a0d2
movl 0x8(%rsp), %r13d
movl %eax, %ebx
incq %r15
jmp 0x7a09e
movq 0x58(%rsp), %r15
movl %r15d, %edi
shrl %edi
movq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0x7882c
cmpl %ebx, 0x4(%rsp)
ja 0x7a104
jne 0x7a125
cmpl %r13d, 0x8(%rsp)
ja 0x7a125
movq 0x18(%rbp), %rax
movl 0x4(%rax,%r12,8), %r15d
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbx
jmp 0x7a18d
movq 0x40(%rsp), %r12
movl 0x4(%rcx,%r12,8), %r15d
jmp 0x7a18d
movq 0x38(%rsp), %rdi
shrq $0x21, %rdi
movq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0x7882c
cmpl %ebx, 0x4(%rsp)
movq 0x28(%rsp), %rbx
ja 0x7a156
jne 0x7a166
cmpl %r13d, 0x8(%rsp)
ja 0x7a166
movq 0x18(%rbp), %rax
movl 0x4(%rax,%r12,8), %r15d
movq 0x20(%rsp), %r13
jmp 0x7a18d
movq 0x20(%rsp), %r13
movl 0xc(%rsp), %ecx
cmpq %r14, %r12
jae 0x7a184
movq 0x18(%rbp), %rax
orq $-0x1, (%rax,%r12,8)
incl %ecx
incq %r12
jmp 0x7a16f
movl %ecx, 0xc(%rsp)
movq 0x60(%rsp), %r12
movl %r12d, %edx
movl 0x10(%rbp), %ecx
movq %rbx, %rsi
jmp 0x79f3b
cmpl $0x0, 0xc(%rsp)
je 0x7a1d7
movq %rbp, %rdi
movq 0x50(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %r13, %r8
movl 0x1c(%rsp), %r9d
pushq 0xd8(%rsp)
movl 0xd8(%rsp), %eax
pushq %rax
callq 0x7886e
popq %rax
popq %rcx
pushq $0x1
popq %rax
jmp 0x7a1d9
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq