// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rsi, 0x70(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0xb4f57
movq %rdi, %rbp
cmpl $0x3, 0x10(%rdi)
jb 0xb4f57
movl %r9d, %r12d
movq %rdx, %r15
movq 0x18(%rbp), %rax
movl 0x4(%rax), %edx
xorl %ebx, %ebx
movl $0x1, %edi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x0, 0x14(%rsp)
xorl %esi, %esi
movl %r9d, 0x10(%rsp)
movq %r15, 0x68(%rsp)
movq %rbp, 0x60(%rsp)
movq 0x18(%rbp), %rax
movq (%rax,%rdi,8), %r14
cmpq $-0x1, %r14
je 0xb4d1d
movl %ecx, 0x4(%rsp)
movq %rbx, 0x50(%rsp)
shrq $0x20, %r14
leaq 0x1(%rdi), %rcx
movq %rcx, 0x20(%rsp)
movl 0xc(%rax,%rdi,8), %eax
movq %rax, 0x18(%rsp)
movq %rdi, 0x48(%rsp)
leaq -0x1(%rdi), %rax
movl %esi, 0x2c(%rsp)
movl %esi, %ecx
cmpq %rcx, %rax
movl $0x0, %r8d
movq 0x70(%rsp), %rbx
cmoveq %rbx, %r8
movl %r14d, %eax
xorl $0x1, %eax
movq %rdx, 0x30(%rsp)
xorl $0x1, %edx
movq %r15, %rdi
movq 0x38(%rsp), %rbp
movq %rbp, %rsi
movl %r12d, %r13d
movq 0x40(%rsp), %r12
movq %r12, %rcx
movl %r13d, %r9d
leaq 0xa0(%rsp), %r10
pushq %r10
pushq %rdx
pushq %rax
movl 0x108(%rsp), %eax
pushq %rax
callq 0xb4a8d
addq $0x20, %rsp
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rcx
movl %r13d, %r12d
movq %rbx, %r8
movl %r13d, %r9d
leaq 0x88(%rsp), %rax
pushq %rax
pushq 0x20(%rsp)
pushq %r14
movl 0x108(%rsp), %eax
pushq %rax
callq 0xb4a8d
addq $0x20, %rsp
cmpb $0x1, 0xb2(%rsp)
jne 0xb4d2b
cmpb $0x1, 0x9a(%rsp)
jne 0xb4d2b
movq 0x60(%rsp), %rbp
movq 0x18(%rbp), %rax
movq 0x48(%rsp), %r13
movl 0x4(%rax,%r13,8), %edx
movq 0x68(%rsp), %r15
movq 0x50(%rsp), %rbx
jmp 0xb4e80
incq %rdi
movq %rdi, %r14
movl %esi, %r13d
jmp 0xb4e89
subq $0x8, %rsp
movq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq 0x70(%rsp), %r15
movq %r15, %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r8
movl %r12d, %r9d
movq 0x28(%rsp), %r14
pushq %r14
pushq 0x40(%rsp)
movl 0x108(%rsp), %eax
pushq %rax
callq 0xb49b2
addq $0x20, %rsp
movq 0x18(%rbp), %rcx
cmpl $-0x1, %eax
movq 0x50(%rsp), %rbx
je 0xb4de0
movl %eax, 0x5c(%rsp)
movl %eax, %r12d
movq (%rcx,%r12,8), %rax
movq %rax, 0x80(%rsp)
movq 0x48(%rsp), %r13
cmpq %r12, %r13
jae 0xb4df3
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x0, 0x18(%rsp)
movq %r13, %rbx
movq 0x18(%rbp), %rax
movq (%rax,%rbx,8), %rdi
shrq $0x21, %rdi
movq %r15, %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0xb343e
movl 0x8(%rsp), %eax
cmpl %r14d, %eax
jae 0xb4dd6
movl 0xc(%rsp), %ecx
movl %ecx, 0x18(%rsp)
movl %eax, %r14d
incq %rbx
cmpq %rbx, %r12
jne 0xb4da4
jmp 0xb4e01
movq 0x48(%rsp), %r13
movl 0x4(%rcx,%r13,8), %edx
movl 0x4(%rsp), %ecx
jmp 0xb4e89
movl $0x0, 0x18(%rsp)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x50(%rsp), %rbx
leaq (,%rbx,8), %rax
movq %rax, 0x78(%rsp)
movq 0x30(%rsp), %rax
movl %eax, %edi
shrl %edi
movq %r15, %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0xb343e
cmpl %r14d, 0x8(%rsp)
ja 0xb4e72
jne 0xb4e41
movl 0x18(%rsp), %eax
cmpl %eax, 0xc(%rsp)
jbe 0xb4e72
movq 0x80(%rsp), %rdi
shrq $0x21, %rdi
movq %r15, %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0xb343e
cmpl %r14d, 0x8(%rsp)
ja 0xb4e72
jne 0xb4ea4
movl 0x18(%rsp), %eax
cmpl %eax, 0xc(%rsp)
ja 0xb4ea4
movq 0x18(%rbp), %rax
movl 0x4(%rax,%r13,8), %edx
movl 0x10(%rsp), %r12d
movl 0x4(%rsp), %ecx
movq 0x20(%rsp), %r14
movl %r13d, %esi
movl 0x10(%rbp), %eax
decl %eax
incq %rbx
decl %ecx
movq %r14, %rdi
cmpq %rax, %r14
jb 0xb4c1f
jmp 0xb4f17
cmpq %r12, %r13
movq 0x20(%rsp), %r14
jae 0xb4efd
movq 0x18(%rbp), %rax
movq 0x78(%rsp), %rcx
leaq (%rax,%rcx), %rdi
addq $0x8, %rdi
movl 0x5c(%rsp), %r12d
movl %r12d, %eax
subl %ebx, %eax
addl $-0x2, %eax
leaq 0x8(,%rax,8), %rdx
movl $0xff, %esi
callq 0x20e0
movl 0x14(%rsp), %eax
addl %r12d, %eax
movl 0x4(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x14(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, %r13d
movl 0x10(%rsp), %r12d
jmp 0xb4f0d
movl 0x2c(%rsp), %eax
movl %eax, %r13d
movl 0x10(%rsp), %r12d
movl 0x4(%rsp), %ecx
movq 0x30(%rsp), %rdx
jmp 0xb4e89
cmpl $0x0, 0x14(%rsp)
je 0xb4f55
movq %rbp, %rdi
movq 0x70(%rsp), %rsi
movq %r15, %rdx
movq 0x38(%rsp), %rcx
movq 0x40(%rsp), %r8
movl %r12d, %r9d
pushq 0xf8(%rsp)
movl 0xf8(%rsp), %eax
pushq %rax
callq 0xb3483
addq $0x10, %rsp
movl $0x1, %eax
jmp 0xb4f57
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq