// File: /workspace/github_cpp_makefile_star2_O1/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 %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rsi, 0x58(%rsp)
xorl %eax, %eax
cmpl $0x0, 0xc(%rdi)
je 0xb3e80
cmpl $0x3, 0x10(%rdi)
jb 0xb3e80
movl 0x10(%rdi), %eax
addl $-0x3, %eax
cmpl $-0x3, %eax
ja 0xb3e7e
movq %rdx, %r12
movl %r9d, 0x2c(%rsp)
movq 0x18(%rdi), %rax
movl 0x4(%rax), %r14d
xorl %r13d, %r13d
movl $0x1, %r15d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movl $0x0, 0x1c(%rsp)
xorl %edx, %edx
movq %r12, 0x60(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x18(%rdi), %rax
movq (%rax,%r15,8), %rbp
cmpq $-0x1, %rbp
je 0xb3ddd
movq %r10, 0x20(%rsp)
movl %ecx, 0xc(%rsp)
shrq $0x20, %rbp
movl 0xc(%rax,%r15,8), %eax
movq %rax, 0x50(%rsp)
movq %r15, 0x30(%rsp)
leaq -0x1(%r15), %rax
movl %edx, 0x28(%rsp)
movl %edx, %ecx
cmpq %rcx, %rax
movl $0x0, %r8d
cmoveq 0x58(%rsp), %r8
movl %ebp, %eax
xorl $0x1, %eax
movq %r14, 0x48(%rsp)
movl %r14d, %edx
xorl $0x1, %edx
movq %r12, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
movq 0x40(%rsp), %r15
movq %r15, %rcx
movl 0x2c(%rsp), %r14d
movl %r14d, %r9d
leaq 0x80(%rsp), %r10
pushq %r10
pushq %rdx
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
callq 0xb3953
addq $0x20, %rsp
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rcx
movq 0x58(%rsp), %r8
movl %r14d, %r9d
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0x58(%rsp)
pushq %rbp
movl 0xe8(%rsp), %eax
pushq %rax
callq 0xb3953
addq $0x20, %rsp
cmpb $0x1, 0x92(%rsp)
jne 0xb3be5
cmpb $0x1, 0x7a(%rsp)
jne 0xb3be5
movq 0x10(%rsp), %rdi
movq 0x18(%rdi), %rax
movq 0x30(%rsp), %r15
movl 0x4(%rax,%r15,8), %r14d
movl %r15d, %edx
movq 0x60(%rsp), %r12
movl 0xc(%rsp), %ecx
movq 0x20(%rsp), %r10
jmp 0xb3ddd
movq 0x30(%rsp), %r15
leaq 0x1(%r15), %rax
subq $0x8, %rsp
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x68(%rsp), %r12
movq %r12, %rdx
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r8
movl %r14d, %r9d
pushq %rax
pushq 0x58(%rsp)
movl 0xe8(%rsp), %eax
pushq %rax
callq 0xb386e
addq $0x20, %rsp
movq 0x18(%rbx), %rbx
cmpl $-0x1, %eax
je 0xb3d9f
movl %eax, 0x50(%rsp)
movl %eax, %eax
xorl %edi, %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x0, %edx
cmpq %rax, %r15
jae 0xb3cc7
xorl %edx, %edx
movq 0x30(%rsp), %r8
movq (%rbx,%r8,8), %r9
shrq $0x21, %r9
leaq (%r9,%r9,2), %r11
movl 0xc(%r12,%r11,8), %r14d
movl $0x0, %r9d
movl $0x0, %r10d
testq %r14, %r14
je 0xb3cb4
leaq (%r12,%r11,8), %r9
movq (%r9), %r11
shlq $0x3, %r14
leaq (%r14,%r14,4), %r14
xorl %ebp, %ebp
xorl %r9d, %r9d
xorl %r10d, %r10d
movl 0x8(%r11,%rbp), %r15d
subl (%r11,%rbp), %r15d
cmpb $0x1, 0x20(%r11,%rbp)
movl $0x0, %ecx
cmovel %r15d, %ecx
cmovel %r13d, %r15d
addl %ecx, %r9d
addl %r15d, %r10d
addq $0x28, %rbp
cmpq %rbp, %r14
jne 0xb3c89
cmpl %esi, %r10d
cmovbl %r10d, %esi
cmovbl %r9d, %edx
incq %r8
cmpq %rax, %r8
jne 0xb3c50
movq 0x48(%rsp), %rcx
shrl %ecx
leaq (%rcx,%rcx,2), %r8
movl 0xc(%r12,%r8,8), %r10d
movl $0x0, %r9d
testq %r10, %r10
je 0xb3d22
leaq (%r12,%r8,8), %rcx
movq (%rcx), %r8
shlq $0x3, %r10
leaq (%r10,%r10,4), %r10
xorl %r11d, %r11d
xorl %edi, %edi
xorl %r9d, %r9d
movl 0x8(%r8,%r11), %ecx
subl (%r8,%r11), %ecx
cmpb $0x1, 0x20(%r8,%r11)
movl $0x0, %ebp
cmovel %ecx, %ebp
cmovel %r13d, %ecx
addl %ebp, %edi
addl %ecx, %r9d
addq $0x28, %r11
cmpq %r11, %r10
jne 0xb3cf9
movq 0x20(%rsp), %r10
leaq (,%r10,8), %r8
cmpl %esi, %r9d
movq 0x30(%rsp), %r15
ja 0xb3dcc
movq (%rbx,%rax,8), %r9
jne 0xb3d4b
cmpl %edx, %edi
jbe 0xb3dcc
shrq $0x21, %r9
leaq (%r9,%r9,2), %r9
movl 0xc(%r12,%r9,8), %edi
testq %rdi, %rdi
je 0xb3db2
leaq (%r12,%r9,8), %rcx
movq (%rcx), %r9
shlq $0x3, %rdi
leaq (%rdi,%rdi,4), %r10
xorl %r14d, %r14d
xorl %edi, %edi
xorl %r11d, %r11d
movl 0x8(%r9,%r14), %ecx
subl (%r9,%r14), %ecx
cmpb $0x1, 0x20(%r9,%r14)
movl $0x0, %ebp
cmovel %ecx, %ebp
cmovel %r13d, %ecx
addl %ebp, %edi
addl %ecx, %r11d
addq $0x28, %r14
cmpq %r14, %r10
jne 0xb3d74
jmp 0xb3db7
movl 0x4(%rbx,%r15,8), %r14d
movl %r15d, %edx
movl 0xc(%rsp), %ecx
movq 0x20(%rsp), %r10
jmp 0xb3dd8
xorl %edi, %edi
xorl %r11d, %r11d
cmpl %esi, %r11d
movq 0x48(%rsp), %r14
movq 0x20(%rsp), %r10
ja 0xb3dcc
jne 0xb3df5
cmpl %edx, %edi
ja 0xb3df5
movl 0x4(%rbx,%r15,8), %r14d
movl %r15d, %edx
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %rdi
incq %r15
movl 0x10(%rdi), %eax
decl %eax
incq %r10
decl %ecx
cmpq %rax, %r15
jb 0xb3aef
jmp 0xb3e41
cmpq %rax, %r15
jae 0xb3e37
leaq (%rbx,%r8), %rdi
addq $0x8, %rdi
movl 0x50(%rsp), %ebx
movl %ebx, %eax
subl %r10d, %eax
addl $-0x2, %eax
leaq 0x8(,%rax,8), %rdx
movl $0xff, %esi
callq 0x20e0
movq 0x20(%rsp), %r10
movl 0x1c(%rsp), %eax
addl %ebx, %eax
movl 0xc(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x1c(%rsp)
jmp 0xb3e3b
movl 0xc(%rsp), %ecx
movl 0x28(%rsp), %edx
jmp 0xb3dd8
cmpl $0x0, 0x1c(%rsp)
je 0xb3e7e
movq 0x58(%rsp), %rsi
movq %r12, %rdx
movq 0x38(%rsp), %rcx
movq 0x40(%rsp), %r8
movl 0x2c(%rsp), %r9d
pushq 0xd8(%rsp)
movl 0xd8(%rsp), %eax
pushq %rax
callq 0xb2398
addq $0x10, %rsp
movl $0x1, %eax
jmp 0xb3e80
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq