// File: chhylp123[P]hifiasm/Correct.cpp
// Function: fusion_chain_ovlp(overlap_region*, k_mer_hit*, long, ul_ov_t*, long, unsigned long, long, long)
int64_t fusion_chain_ovlp(overlap_region *z, k_mer_hit *ch_a, int64_t ch_n, ul_ov_t *ov, int64_t on, uint64_t wl, int64_t ql, int64_t tl)
{
    int64_t i, srt, ch_i, m, os, oe, ovlp; ul_ov_t *p;
    for (i = ch_i = 0, srt = 1; i < on; i++) {
        // fprintf(stderr, "[M::%s::i->%ld] ovq::[%u, %u)\n", __func__, i, ov->qs, ov->qe);
        ov[i].sec = 6;///do not know the aln type
        ch_i = adjust_base_coordinates(z, ch_a, ch_n, &(ov[i]), wl, ql, tl, ch_i);
        if(i > 0 && ov[i].qs < ov[i-1].qe) srt = 0;
    }
    if(on <= 1) return on;

    if(!srt) radix_sort_uov_srt_qs(ov, ov+on);
    for (i = m = 1; i < on; i++) {
        p = &(ov[m-1]);
        os = MAX(p->qs, ov[i].qs); 
        oe = MIN(p->qe, ov[i].qe);
        ovlp = oe - os;
        if(ovlp >= 0) {//merge
            p->qe = MAX(p->qe, ov[i].qe);
            p->te = MAX(p->te, ov[i].te);
            p->tn = MAX(p->tn, ov[i].tn);
        } else {//new
            ov[m++] = ov[i]; 
        }
    }

    on = m;
    return on;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x1, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x63bff
movq -0x28(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl $0x6, %ecx
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
imulq $0x1c, -0x40(%rbp), %rax
addq %rax, %rcx
movq -0x38(%rbp), %r8
movq 0x10(%rbp), %r9
movq 0x18(%rbp), %r10
movq -0x50(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x63860
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x40(%rbp)
jle 0x63bec
movq -0x28(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rdx
subq $0x1, %rdx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jae 0x63bec
movq $0x0, -0x48(%rbp)
jmp 0x63bee
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x63b53
cmpq $0x1, -0x30(%rbp)
jg 0x63c13
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x63e3b
cmpq $0x0, -0x48(%rbp)
jne 0x63c2f
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rsi
imulq $0x1c, -0x30(%rbp), %rax
addq %rax, %rsi
callq 0x2f8f0
movq $0x1, -0x58(%rbp)
movq $0x1, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x63e2b
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
subq $0x1, %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x28(%rbp), %rcx
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jb 0x63c88
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x7c(%rbp)
jmp 0x63c9a
movq -0x28(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x60(%rbp)
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x28(%rbp), %rcx
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
ja 0x63cc7
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x80(%rbp)
jmp 0x63cd9
movq -0x28(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
subq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jl 0x63dd6
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x28(%rbp), %rcx
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jb 0x63d20
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x84(%rbp)
jmp 0x63d35
movq -0x28(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x78(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x28(%rbp), %rcx
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x14(%rcx), %eax
jb 0x63d69
movq -0x78(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x88(%rbp)
jmp 0x63d7e
movq -0x28(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, -0x88(%rbp)
movl -0x88(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x78(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x28(%rbp), %rcx
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl 0xc(%rcx), %eax
jb 0x63db2
movq -0x78(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x8c(%rbp)
jmp 0x63dc7
movq -0x28(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0x63e18
movq -0x28(%rbp), %rcx
imulq $0x1c, -0x40(%rbp), %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x58(%rbp)
imulq $0x1c, %rdx, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movl 0x18(%rcx), %ecx
movl %ecx, 0x18(%rax)
jmp 0x63e1a
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x63c3f
movq -0x58(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)