// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/ecovlp.cpp
// Function: dedup_chains(overlap_region_alloc*)
void dedup_chains(overlap_region_alloc* ol)
{
    uint64_t k, l, s, m, mm_k, mm_m, sf; int64_t sc, mm_sc, plus, minus; overlap_region *z, t; 
    if(ol->length > 1) {///for duplicated chains
        overlap_region_sort_y_id(ol->list, ol->length);
        for (k = 1, l = m = 0; k <= ol->length; k++) {
            if(k == ol->length || ol->list[k].y_id != ol->list[l].y_id) {
                // fprintf(stderr, "\n[M::%s::tid->%u] n->%lu\n", __func__, ol->list[l].y_id, k - l);
                mm_k = l;
                if(k - l > 1) {
                    for (s = l, mm_sc = INT32_MIN, mm_k = ((uint64_t)-1), mm_m = 3; s < k; s++) {
                        z = &(ol->list[s]);
                        plus = z->x_pos_e + 1 - z->x_pos_s; minus = (z->non_homopolymer_errors) * 12;
                        sc = plus - minus; 
                        
                        sf = 0;
                        if(z->is_match < mm_m) {
                            sf = 1;
                        } else if(z->is_match == mm_m) {
                            if(sc > mm_sc) {
                                sf = 1;
                            } else if((sc == mm_sc) && ((z->x_pos_e+1-z->x_pos_s) > (ol->list[mm_k].x_pos_e+1-ol->list[mm_k].x_pos_s))) {
                                sf = 1;
                            }
                        }
                        if(sf) {
                            mm_sc = sc; mm_k = s; mm_m = z->is_match;
                        }
                        
                        // fprintf(stderr, "[M::%s::%c] q::[%u, %u), t::[%u, %u), sc::%ld, err::%u, mm::%u, s::%lu\n", __func__, "+-"[z->y_pos_strand], z->x_pos_s, z->x_pos_e + 1, z->y_pos_s, z->y_pos_e + 1, sc, z->non_homopolymer_errors, z->is_match, s);
                    }
                }
                // fprintf(stderr, "[M::%s::tid->%u] mm_k::%lu\n", __func__, ol->list[l].y_id, mm_k);
                if(mm_k != ((uint64_t)-1)) {
                    if(mm_k != m) {
                        t = ol->list[mm_k];
                        ol->list[mm_k] = ol->list[m];
                        ol->list[m] = t;
                    }
                    m++;
                }
                l = k;
            }
        }
        ol->length = m;
    }
}

// Assembly:
movq 0x10(%rdi), %rsi
cmpq $0x2, %rsi
jb 0x1818b8
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x3daa2
movq 0x10(%rbx), %rcx
testq %rcx, %rcx
je 0x1818a0
movl $0x1, %ebp
xorl %eax, %eax
xorl %r14d, %r14d
cmpq %rcx, %rbp
je 0x181787
movq (%rbx), %rcx
imulq $0xa8, %rbp, %rdx
movl 0x10(%rcx,%rdx), %edx
imulq $0xa8, %rax, %rsi
cmpl 0x10(%rcx,%rsi), %edx
je 0x18188e
movq %rbp, %rcx
subq %rax, %rcx
cmpq $0x2, %rcx
jb 0x181830
movq $-0x1, %rcx
cmpq %rax, %rbp
jbe 0x181833
movq (%rbx), %rdx
imulq $0xa8, %rax, %rcx
leaq (%rdx,%rcx), %rsi
addq $0x30, %rsi
movq $-0x1, %rcx
movl $0x3, %edi
movq $-0x80000000, %r8       # imm = 0x80000000
movl -0x28(%rsi), %r9d
subl -0x2c(%rsi), %r9d
movl (%rsi), %r10d
incl %r9d
shll $0x2, %r10d
leal (%r10,%r10,2), %r11d
movq %r9, %r10
subq %r11, %r10
movzbl -0x4(%rsi), %r11d
cmpq %r11, %rdi
jbe 0x1817fd
movq %rax, %rcx
movq %r11, %rdi
movq %r10, %r8
jmp 0x18181f
jne 0x18181f
cmpq %r8, %r10
jg 0x1817f2
jne 0x18181f
imulq $0xa8, %rcx, %r15
movl 0x8(%rdx,%r15), %r12d
subl 0x4(%rdx,%r15), %r12d
incl %r12d
cmpl %r12d, %r9d
ja 0x1817f2
incq %rax
addq $0xa8, %rsi
cmpq %rax, %rbp
jne 0x1817cc
jmp 0x181833
movq %rax, %rcx
cmpq $-0x1, %rcx
je 0x18188b
cmpq %r14, %rcx
je 0x181888
movq (%rbx), %r15
imulq $0xa8, %rcx, %r12
addq %r15, %r12
movl $0xa8, %edx
movq %rsp, %rdi
movq %r12, %rsi
callq 0x2160
imulq $0xa8, %r14, %r13
addq %r13, %r15
movl $0xa8, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2160
addq (%rbx), %r13
movl $0xa8, %edx
movq %r13, %rdi
movq %rsp, %rsi
callq 0x2160
incq %r14
movq %rbp, %rax
incq %rbp
movq 0x10(%rbx), %rcx
cmpq %rcx, %rbp
jbe 0x181763
jmp 0x1818a3
xorl %r14d, %r14d
movq %r14, 0x10(%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq