// File: /workspace/github_cpp_makefile_star2_O3/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 0x188e14
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x3cf83
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x188dfc
movl $0x1, %ebp
xorl %ecx, %ecx
xorl %r14d, %r14d
cmpq %rax, %rbp
je 0x188ce9
movq (%rbx), %rdx
imulq $0xa8, %rbp, %rsi
movl 0x10(%rdx,%rsi), %esi
imulq $0xa8, %rcx, %rdi
cmpl 0x10(%rdx,%rdi), %esi
je 0x188dee
movq %rbp, %rdx
subq %rcx, %rdx
cmpq $0x2, %rdx
jb 0x188d8c
cmpq %rcx, %rbp
jbe 0x188deb
movq (%rbx), %rsi
imulq $0xa8, %rcx, %rdx
leaq (%rsi,%rdx), %rdi
addq $0x30, %rdi
movq $-0x1, %rdx
movl $0x3, %r8d
movq $-0x80000000, %r9       # imm = 0x80000000
movl -0x28(%rdi), %r10d
subl -0x2c(%rdi), %r10d
movl (%rdi), %r11d
incl %r10d
shll $0x2, %r11d
leal (%r11,%r11,2), %r15d
movq %r10, %r11
subq %r15, %r11
movzbl -0x4(%rdi), %r15d
cmpq %r15, %r8
jbe 0x188d59
movq %rcx, %rdx
movq %r15, %r8
movq %r11, %r9
jmp 0x188d7b
jne 0x188d7b
cmpq %r9, %r11
jg 0x188d4e
jne 0x188d7b
imulq $0xa8, %rdx, %r12
movl 0x8(%rsi,%r12), %r13d
subl 0x4(%rsi,%r12), %r13d
incl %r13d
cmpl %r13d, %r10d
ja 0x188d4e
incq %rcx
addq $0xa8, %rdi
cmpq %rcx, %rbp
jne 0x188d28
jmp 0x188d8f
movq %rcx, %rdx
cmpq $-0x1, %rdx
je 0x188deb
cmpq %r14, %rdx
je 0x188de8
movq (%rbx), %r15
imulq $0xa8, %rdx, %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
movq 0x10(%rbx), %rax
incq %r14
movq %rbp, %rcx
incq %rbp
cmpq %rax, %rbp
jbe 0x188cc5
jmp 0x188dff
xorl %r14d, %r14d
movq %r14, 0x10(%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq