// File: /workspace/github_cpp_makefile_star2_O2/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 0x14460d
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x1188f
pushq $0x1
popq %r14
xorl %ebp, %ebp
xorl %eax, %eax
cmpq 0x10(%rbx), %r14
ja 0x1445f8
je 0x14450a
movq (%rbx), %rcx
imulq $0xa8, %r14, %rdx
movl 0x10(%rcx,%rdx), %edx
imulq $0xa8, %rax, %rsi
cmpl 0x10(%rcx,%rsi), %edx
je 0x1445f0
movq %r14, %rcx
subq %rax, %rcx
cmpq $0x2, %rcx
jb 0x144595
imulq $0xa8, %rax, %rcx
addq $0x30, %rcx
movq $-0x80000000, %rsi      # imm = 0x80000000
pushq $-0x1
popq %rdx
pushq $0x3
popq %rdi
cmpq %r14, %rax
jae 0x144592
movq (%rbx), %r10
movl -0x28(%r10,%rcx), %r9d
subl -0x2c(%r10,%rcx), %r9d
incl %r9d
imull $0xc, (%r10,%rcx), %r11d
movq %r9, %r8
subq %r11, %r8
movzbl -0x4(%r10,%rcx), %r11d
cmpq %r11, %rdi
jbe 0x144564
movq %rax, %rdx
movq %r11, %rdi
movq %r8, %rsi
jmp 0x144586
jne 0x144586
cmpq %rsi, %r8
jg 0x144559
jne 0x144586
imulq $0xa8, %rdx, %r15
movl 0x8(%r10,%r15), %r12d
subl 0x4(%r10,%r15), %r12d
incl %r12d
cmpl %r12d, %r9d
ja 0x144559
incq %rax
addq $0xa8, %rcx
jmp 0x14452e
movq %rdx, %rax
cmpq $-0x1, %rax
je 0x1445ed
cmpq %rbp, %rax
je 0x1445ea
movq (%rbx), %r15
imulq $0xa8, %rax, %r12
addq %r15, %r12
movl $0xa8, %edx
movq %rsp, %rdi
movq %r12, %rsi
callq 0x2160
imulq $0xa8, %rbp, %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 %rbp
movq %r14, %rax
incq %r14
jmp 0x1444df
movq %rbp, 0x10(%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq