// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x1, 0x10(%rax)
jbe 0x2588c9
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1dfd0
movq $0x1, -0x10(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
ja 0x2588bd
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
je 0x2586aa
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x10(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x8(%rbp), %rcx
movq (%rcx), %rcx
imulq $0xa8, -0x18(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
je 0x2588aa
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
subq -0x18(%rbp), %rax
cmpq $0x1, %rax
jbe 0x25880d
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq $-0x80000000, -0x50(%rbp) # imm = 0x80000000
movq $-0x1, -0x30(%rbp)
movq $0x3, -0x38(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x25880b
movq -0x8(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x68(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x68(%rbp), %rax
imull $0xc, 0x30(%rax), %eax
movl %eax, %eax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x68(%rbp), %rax
movzbl 0x2c(%rax), %eax
cmpq -0x38(%rbp), %rax
jae 0x258759
movq $0x1, -0x40(%rbp)
jmp 0x2587d5
movq -0x68(%rbp), %rax
movzbl 0x2c(%rax), %eax
cmpq -0x38(%rbp), %rax
jne 0x2587d3
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x25877b
movq $0x1, -0x40(%rbp)
jmp 0x2587d1
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x2587cf
movq -0x68(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x68(%rbp), %rcx
subl 0x4(%rcx), %eax
movq -0x8(%rbp), %rcx
movq (%rcx), %rcx
imulq $0xa8, -0x30(%rbp), %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
addl $0x1, %ecx
movq -0x8(%rbp), %rdx
movq (%rdx), %rdx
imulq $0xa8, -0x30(%rbp), %rsi
addq %rsi, %rdx
subl 0x4(%rdx), %ecx
cmpl %ecx, %eax
jbe 0x2587cf
movq $0x1, -0x40(%rbp)
jmp 0x2587d1
jmp 0x2587d3
jmp 0x2587d5
cmpq $0x0, -0x40(%rbp)
je 0x2587f8
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rax
movzbl 0x2c(%rax), %eax
movq %rax, -0x38(%rbp)
jmp 0x2587fa
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x2586e4
jmp 0x25880d
cmpq $-0x1, -0x30(%rbp)
je 0x2588a2
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
je 0x258896
movq -0x8(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x30(%rbp), %rax
addq %rax, %rsi
leaq -0x110(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x8(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x28(%rbp), %rax
addq %rax, %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x30(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x8(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x28(%rbp), %rax
addq %rax, %rdi
leaq -0x110(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x2588ac
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x25865a
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)