// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: merge_u_trans(u_trans_t*, unsigned int, ma_ug_t*)
double merge_u_trans(u_trans_t *a, uint32_t occ, ma_ug_t *ug)
{
    radix_sort_u_trans_qs(a, a+occ);
    uint32_t i, k, ov_q, ov_t, is_found = 1, ts, te;
    double w_q, w_s;
    u_trans_t *p = NULL;

    for (i = 0; i < occ; i++)
    {
        if(a[i].del || a[i].rev == 0) continue;
        ts = a[i].ts; te = a[i].te - 1;
        a[i].ts = ug->g->seq[a[i].tn].len - te - 1;
        a[i].te = ug->g->seq[a[i].tn].len - ts - 1 + 1;
    }


    while (is_found)
    {
        for (i = is_found = 0; i < occ; i++)
        {
            if(a[i].del) continue;
            p = &(a[i]);
            for (k = i+1; k < occ; k++)
            {
                if(a[k].del) continue;
                if(p->qe < a[k].qs) break;

                if(p->qe >= a[k].qs && p->te >= a[k].ts)
                {
                    ov_q = ((MIN(p->qe, a[k].qe) > MAX(p->qs, a[k].qs))?
                                            MIN(p->qe, a[k].qe) - MAX(p->qs, a[k].qs):0);
                    ov_t = ((MIN(p->te, a[k].te) > MAX(p->ts, a[k].ts))?
                                            MIN(p->te, a[k].te) - MAX(p->ts, a[k].ts):0);

                    ov_q = a[k].qe - a[k].qs - ov_q;
                    ov_t = a[k].te - a[k].ts - ov_t;

                    w_q = ((double)ov_q/(double)(a[k].qe - a[k].qs)) * a[k].nw;
                    w_s = ((double)ov_t/(double)(a[k].te - a[k].ts)) * a[k].nw;

                    p->qe = MAX(p->qe, a[k].qe); 
                    p->te = MAX(p->te, a[k].te);
                    p->nw += MIN(w_q, w_s);
                    is_found = 1;
                    a[k].del = 1;
                }
            }
        }
        for (i = k = 0; i < occ; i++)
        {
            if(a[i].del) continue;
            a[k] = a[i];
            k++;
        }
        occ = k;
    }


    for (i = 0; i < occ; i++)
    {
        if(a[i].del) continue;
        p = &(a[i]);
        for (k = i+1; k < occ; k++)
        {
            if(a[k].del) continue;
            ov_q = ((MIN(p->qe, a[k].qe) > MAX(p->qs, a[k].qs))?
                                        MIN(p->qe, a[k].qe) - MAX(p->qs, a[k].qs):0);

            if(ov_q == 0) break;
            if(ov_q == (a[k].qe-a[k].qs))
            {
                a[k].del = 1;
                continue;
            }
            if(ov_q == (p->qe - p->qs))
            {
                p->del = 1;
                continue;
            }
            ov_t = get_offset_adjust(ov_q, a[k].qe-a[k].qs, a[k].te-a[k].ts);
            a[k].qs += ov_q; a[k].ts += ov_t;
            a[k].nw -= ((double)ov_q/(double)(a[k].qe - a[k].qs)) * a[k].nw;
        }
    }
    for (i = k = 0; i < occ; i++)
    {
        if(a[i].del) continue;
        a[k] = a[i];
        k++;
    }
    occ = k;




    radix_sort_u_trans_ts(a, a+occ);
    for (i = 0; i < occ; i++)
    {
        if(a[i].del) continue;
        p = &(a[i]);
        for (k = i+1; k < occ; k++)
        {
            if(a[k].del) continue;
            ov_t = ((MIN(p->te, a[k].te) > MAX(p->ts, a[k].ts))?
                                            MIN(p->te, a[k].te) - MAX(p->ts, a[k].ts):0);

            if(ov_t == 0) break;
            if(ov_t == (a[k].te-a[k].ts))
            {
                a[k].del = 1;
                continue;
            }
            if(ov_t == (p->te - p->ts))
            {
                p->del = 1;
                continue;
            }
            ov_q = get_offset_adjust(ov_t, a[k].te-a[k].ts, a[k].qe-a[k].qs);
            a[k].ts += ov_t; a[k].qs += ov_q; 
            a[k].nw -= ((double)ov_t/(double)(a[k].te - a[k].ts)) * a[k].nw;
        }
    }
    for (i = k = 0, w_q = 0; i < occ; i++)
    {
        if(a[i].del) continue;
        w_q += a[i].nw;
        a[k] = a[i];
        k++;
    }
    occ = k;


    for (i = 0; i < occ; i++)
    {
        if(a[i].del || a[i].rev == 0) continue;
        ts = a[i].ts; te = a[i].te - 1;
        a[i].ts = ug->g->seq[a[i].tn].len - te - 1;
        a[i].te = ug->g->seq[a[i].tn].len - ts - 1 + 1;
    }

    return w_q;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, 0x18(%rsp)
movl %esi, %r15d
movq %rdi, %r14
movl %esi, %ebx
leaq (%rbx,%rbx,2), %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0x9e905
movq %r15, %r10
testl %r15d, %r15d
je 0xcbb7c
shlq $0x4, %rbx
leaq (%rbx,%rbx,2), %rax
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpb $0x40, 0x28(%r14,%rcx)
jl 0xcbb73
movq 0x18(%rsp), %rsi
movq 0x18(%rsi), %rsi
movq 0x20(%rsi), %rsi
movl 0xc(%r14,%rcx), %edi
movl 0x14(%r14,%rcx), %r8d
movl (%rsi,%r8,8), %r9d
andl %edx, %r9d
subl 0x10(%r14,%rcx), %r9d
movl %r9d, 0xc(%r14,%rcx)
movl (%rsi,%r8,8), %esi
andl %edx, %esi
subl %edi, %esi
movl %esi, 0x10(%r14,%rcx)
addq $0x30, %rcx
cmpq %rcx, %rax
jne 0xcbb36
movq %r10, %r8
testl %r8d, %r8d
sete %cl
je 0xcbd79
leaq 0x58(%r14), %rax
movq %rax, 0x30(%rsp)
movq %r14, 0x8(%rsp)
movb %cl, 0x7(%rsp)
movl %r8d, %edx
decl %r8d
movq 0x30(%rsp), %rdi
xorl %eax, %eax
xorl %r9d, %r9d
movq %rdx, 0x20(%rsp)
leaq (%rax,%rax,2), %r10
shlq $0x4, %r10
cmpb $0x0, 0x28(%r14,%r10)
js 0xcbcff
incq %rax
cmpq %rdx, %rax
jae 0xcbd02
movq %rax, 0x28(%rsp)
addq %r14, %r10
movq %r8, 0x10(%rsp)
movl %r8d, %r11d
movq %rdi, %r12
movb (%r12), %bpl
testb %bpl, %bpl
js 0xcbcdc
movl 0x4(%r10), %r13d
movl -0x28(%r12), %ebx
cmpl %ebx, %r13d
jb 0xcbce9
movl 0x10(%r10), %r14d
movl -0x1c(%r12), %eax
cmpl %eax, %r14d
jb 0xcbcdc
movl (%r10), %r15d
cmpl %ebx, %r15d
cmovbel %ebx, %r15d
movl -0x18(%r12), %r9d
cmpl %r9d, %r14d
movl %r9d, %esi
cmovbl %r14d, %esi
movl -0x24(%r12), %ecx
cmovbel %r9d, %r14d
cmpl %ecx, %r13d
movl %ecx, %edx
cmovbl %r13d, %edx
cmovbel %ecx, %r13d
movl 0xc(%r10), %r8d
subl %r15d, %edx
movl $0x0, %r15d
cmovbl %r15d, %edx
cmpl %eax, %r8d
cmovbel %eax, %r8d
subl %r8d, %esi
cmovbl %r15d, %esi
subl %ebx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
subl %edx, %ecx
subl %eax, %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
subl %esi, %r9d
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
divsd %xmm0, %xmm2
movsd -0x8(%r12), %xmm0
xorps %xmm3, %xmm3
cvtsi2sd %r9, %xmm3
mulsd %xmm0, %xmm2
divsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
movl %r13d, 0x4(%r10)
movl %r14d, 0x10(%r10)
movapd %xmm2, %xmm0
cmplesd %xmm3, %xmm0
andpd %xmm0, %xmm2
andnpd %xmm3, %xmm0
orpd %xmm2, %xmm0
addsd 0x20(%r10), %xmm0
movsd %xmm0, 0x20(%r10)
orb $-0x80, %bpl
movb %bpl, (%r12)
movl $0x1, %r9d
addq $0x30, %r12
decl %r11d
jne 0xcbbe5
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rax
jmp 0xcbd02
incq %rax
addq $0x30, %rdi
decl %r8d
cmpq %rdx, %rax
jne 0xcbbb2
testb $0x1, 0x7(%rsp)
je 0xcbd23
xorl %r8d, %r8d
testl %r9d, %r9d
jne 0xcbd6d
jmp 0xcbd79
xorl %r8d, %r8d
movq %r14, %rax
cmpb $0x0, 0x28(%rax)
js 0xcbd5f
movl %r8d, %ecx
leaq (%rcx,%rcx,2), %rcx
shlq $0x4, %rcx
movupd (%rax), %xmm0
movupd 0x10(%rax), %xmm1
movupd 0x20(%rax), %xmm2
movupd %xmm2, 0x20(%r14,%rcx)
movupd %xmm1, 0x10(%r14,%rcx)
movupd %xmm0, (%r14,%rcx)
incl %r8d
addq $0x30, %rax
decq %rdx
jne 0xcbd29
testl %r9d, %r9d
je 0xcbd97
testl %r8d, %r8d
sete %cl
jne 0xcbb99
movq %r14, %rdi
movq %r14, %rsi
callq 0x9ec1e
xorpd %xmm0, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r8d, %r8d
je 0xcbd79
movl %r8d, %eax
leaq 0x58(%r14), %rcx
leal -0x1(%r8), %edx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rdi
shlq $0x4, %rdi
cmpb $0x0, 0x28(%r14,%rdi)
js 0xcbeae
incq %rsi
cmpq %rax, %rsi
jae 0xcbebd
movq %r8, 0x10(%rsp)
addq %r14, %rdi
movl %edx, %r8d
movq %rcx, %r9
movb (%r9), %bl
testb %bl, %bl
js 0xcbe9f
movl (%rdi), %r12d
movl 0x4(%rdi), %r14d
movl -0x28(%r9), %r11d
movl -0x24(%r9), %r10d
cmpl %r10d, %r14d
movl %r10d, %ebp
cmovbl %r14d, %ebp
cmpl %r11d, %r12d
movl %r11d, %r15d
cmoval %r12d, %r15d
subl %r15d, %ebp
jbe 0xcbeb3
movl %r10d, %r13d
subl %r11d, %r13d
cmpl %r13d, %ebp
jne 0xcbe21
orb $-0x80, %bl
movb %bl, (%r9)
jmp 0xcbe9a
subl %r12d, %r14d
cmpl %r14d, %ebp
jne 0xcbe2f
orb $-0x80, 0x28(%rdi)
jmp 0xcbe9a
movl -0x1c(%r9), %ebx
movl %ebp, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
movl -0x18(%r9), %r14d
movl %r13d, %r15d
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
subl %ebx, %r14d
movapd %xmm0, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %r14, %xmm3
divsd %xmm1, %xmm2
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %r14
addl %ebp, %r11d
movl %r11d, -0x28(%r9)
addl %ebx, %r14d
movl %r14d, -0x1c(%r9)
subl %r11d, %r10d
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
movsd -0x8(%r9), %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm2
movsd %xmm2, -0x8(%r9)
movq 0x8(%rsp), %r14
addq $0x30, %r9
decl %r8d
jne 0xcbdd7
jmp 0xcbeb8
incq %rsi
jmp 0xcbebd
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r8
addq $0x30, %rcx
decl %edx
cmpq %rax, %rsi
jne 0xcbda9
testl %r8d, %r8d
je 0xcbd79
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpb $0x0, 0x28(%r14,%rcx)
js 0xcbf1e
leaq (%r14,%rcx), %rdx
movl %r15d, %esi
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movupd (%rdx), %xmm0
movupd 0x10(%rdx), %xmm1
movupd 0x20(%rdx), %xmm2
movupd %xmm2, 0x20(%r14,%rsi)
movupd %xmm1, 0x10(%r14,%rsi)
movupd %xmm0, (%r14,%rsi)
incl %r15d
addq $0x30, %rcx
cmpq %rcx, %rax
jne 0xcbee2
movl %r15d, %r12d
leaq (%r12,%r12,2), %rsi
shlq $0x4, %rsi
addq %r14, %rsi
movq %r14, %rdi
callq 0x9ec1e
xorpd %xmm0, %xmm0
testl %r15d, %r15d
je 0xcbd88
leaq 0x58(%r14), %rax
leal -0x1(%r15), %ecx
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rsi
shlq $0x4, %rsi
movq 0x8(%rsp), %rdi
cmpb $0x0, 0x28(%rdi,%rsi)
js 0xcc053
incq %rdx
cmpq %r12, %rdx
jae 0xcc056
addq 0x8(%rsp), %rsi
movl %ecx, %edi
movq %rax, %r8
movb (%r8), %bl
testb %bl, %bl
js 0xcc045
movl 0xc(%rsi), %r14d
movl 0x10(%rsi), %ebp
movl -0x1c(%r8), %r10d
movl -0x18(%r8), %r9d
cmpl %r9d, %ebp
movl %r9d, %r11d
cmovbl %ebp, %r11d
cmpl %r10d, %r14d
movl %r10d, %r13d
cmoval %r14d, %r13d
subl %r13d, %r11d
jbe 0xcc056
movl %r9d, %r13d
subl %r10d, %r13d
cmpl %r13d, %r11d
jne 0xcbfcc
orb $-0x80, %bl
movb %bl, (%r8)
jmp 0xcc045
subl %r14d, %ebp
cmpl %ebp, %r11d
jne 0xcbfda
orb $-0x80, 0x28(%rsi)
jmp 0xcc045
movl -0x28(%r8), %ebx
movl %r11d, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
movl -0x24(%r8), %r14d
movl %r13d, %r13d
xorps %xmm2, %xmm2
cvtsi2sd %r13, %xmm2
subl %ebx, %r14d
movapd %xmm1, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %r14, %xmm4
divsd %xmm2, %xmm3
mulsd %xmm3, %xmm4
cvttsd2si %xmm4, %r14
addl %r11d, %r10d
movl %r10d, -0x1c(%r8)
addl %ebx, %r14d
movl %r14d, -0x28(%r8)
subl %r10d, %r9d
xorps %xmm2, %xmm2
cvtsi2sd %r9, %xmm2
movsd -0x8(%r8), %xmm3
divsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm3
movsd %xmm3, -0x8(%r8)
addq $0x30, %r8
decl %edi
jne 0xcbf82
jmp 0xcc056
incq %rdx
addq $0x30, %rax
decl %ecx
cmpq %r12, %rdx
jne 0xcbf54
testl %r15d, %r15d
je 0xcbd88
movq 0x8(%rsp), %rax
addq $0x28, %rax
shlq $0x4, %r12
leaq (%r12,%r12,2), %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movq 0x8(%rsp), %r9
cmpb $0x0, (%rax,%rdx)
js 0xcc0d0
leaq (%rax,%rdx), %rdi
addq $-0x28, %rdi
addsd 0x20(%r9,%rdx), %xmm0
movl %esi, %r8d
leaq (%r8,%r8,2), %r8
shlq $0x4, %r8
movupd (%rdi), %xmm1
movupd 0x10(%rdi), %xmm2
movupd 0x20(%rdi), %xmm3
movupd %xmm3, 0x20(%r9,%r8)
movupd %xmm2, 0x10(%r9,%r8)
movupd %xmm1, (%r9,%r8)
incl %esi
addq $0x30, %rdx
cmpq %rdx, %rcx
jne 0xcc08c
testl %esi, %esi
je 0xcbd88
movl %esi, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r10
cmpb $0x40, 0x28(%r10,%rcx)
jl 0xcc134
movq 0x18(%rsp), %rsi
movq 0x18(%rsi), %rsi
movq 0x20(%rsi), %rsi
movl 0xc(%r10,%rcx), %edi
movl 0x14(%r10,%rcx), %r8d
movl (%rsi,%r8,8), %r9d
andl %edx, %r9d
subl 0x10(%r10,%rcx), %r9d
movl %r9d, 0xc(%r10,%rcx)
movl (%rsi,%r8,8), %esi
andl %edx, %esi
subl %edi, %esi
movl %esi, 0x10(%r10,%rcx)
addq $0x30, %rcx
cmpq %rcx, %rax
jne 0xcc0f7
jmp 0xcbd88