// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movq %rdx, %r14
movl %esi, %ebp
movl %esi, %eax
imulq $0x30, %rax, %rbx
leaq (%rdi,%rbx), %rsi
movq %rdi, 0x8(%rsp)
callq 0x677ed
xorl %eax, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r9
cmpq %rax, %rbx
je 0x8fffe
movq 0x8(%rsp), %rdx
cmpb $0x40, 0x28(%rdx,%rax)
jl 0x8fff8
movq 0x18(%r14), %rdx
movq 0x20(%rdx), %rdx
movl 0xc(%r9,%rax), %esi
movl 0x14(%r9,%rax), %edi
movl (%rdx,%rdi,8), %r8d
andl %ecx, %r8d
subl 0x10(%r9,%rax), %r8d
movl %r8d, 0xc(%r9,%rax)
movl (%rdx,%rdi,8), %edx
andl %ecx, %edx
subl %esi, %edx
movl %edx, 0x10(%r9,%rax)
addq $0x30, %rax
jmp 0x8ffb8
movq %r14, 0x18(%rsp)
movq 0x8(%rsp), %rbx
leaq 0x58(%rbx), %r14
pushq $0x1
popq %r10
movq %r14, 0x20(%rsp)
movl %ebp, %ecx
testl %r10d, %r10d
je 0x9019d
xorl %edi, %edi
xorl %r10d, %r10d
cmpq %rcx, %rdi
je 0x9015a
imulq $0x30, %rdi, %r8
cmpb $0x0, 0x28(%rbx,%r8)
js 0x90152
addq %rbx, %r8
movq %rdi, %r9
imulq $0x30, %r9, %r11
incq %r9
addq %r14, %r11
cmpq %rcx, %r9
jae 0x90152
movb (%r11), %bpl
testb %bpl, %bpl
js 0x9007d
movl 0x4(%r8), %r15d
movl -0x28(%r11), %edx
cmpl %edx, %r15d
jb 0x90152
movl 0x10(%r8), %r12d
movl -0x1c(%r11), %r13d
cmpl %r13d, %r12d
jae 0x90086
incq %r9
addq $0x30, %r11
jmp 0x9004e
movl (%r8), %r10d
cmpl %edx, %r10d
cmovbel %edx, %r10d
movl -0x18(%r11), %eax
movq %rax, 0x10(%rsp)
cmpl %eax, %r12d
movl %eax, %ebx
cmovbl %r12d, %ebx
movl -0x24(%r11), %esi
cmovbel %eax, %r12d
cmpl %esi, %r15d
movl %esi, %r14d
cmovbl %r15d, %r14d
cmovbel %esi, %r15d
movl 0xc(%r8), %eax
subl %r10d, %r14d
movl $0x0, %r10d
cmovbl %r10d, %r14d
cmpl %r13d, %eax
cmovbel %r13d, %eax
subl %eax, %ebx
cmovbl %r10d, %ebx
subl %edx, %esi
cvtsi2sd %rsi, %xmm0
subl %r14d, %esi
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %rax
subl %r13d, %eax
cvtsi2sd %rax, %xmm1
subl %ebx, %eax
movq 0x8(%rsp), %rbx
cvtsi2sd %rsi, %xmm2
divsd %xmm0, %xmm2
movsd -0x8(%r11), %xmm0
cvtsi2sd %rax, %xmm3
mulsd %xmm0, %xmm2
divsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
movl %r15d, 0x4(%r8)
movl %r12d, 0x10(%r8)
movapd %xmm2, %xmm0
cmplesd %xmm3, %xmm0
andpd %xmm0, %xmm2
andnpd %xmm3, %xmm0
orpd %xmm2, %xmm0
addsd 0x20(%r8), %xmm0
movsd %xmm0, 0x20(%r8)
orb $-0x80, %bpl
movb %bpl, (%r11)
pushq $0x1
popq %r10
jmp 0x90044
incq %rdi
jmp 0x90025
imulq $0x30, %rcx, %rcx
xorl %edx, %edx
xorl %ebp, %ebp
cmpq %rdx, %rcx
je 0x90015
cmpb $0x0, 0x28(%rbx,%rdx)
js 0x90197
leaq (%rbx,%rdx), %rax
movl %ebp, %esi
imulq $0x30, %rsi, %rsi
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rbx,%rsi)
movups %xmm1, 0x10(%rbx,%rsi)
movups %xmm0, (%rbx,%rsi)
incl %ebp
addq $0x30, %rdx
jmp 0x90162
pushq $0x1
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rsi
je 0x902ac
imulq $0x30, %rsi, %rdi
cmpb $0x0, 0x28(%rbx,%rdi)
js 0x90298
addq 0x8(%rsp), %rdi
movq %r14, %r8
movq %rdx, %r9
cmpq %rcx, %r9
jae 0x90298
movb (%r8), %bpl
testb %bpl, %bpl
js 0x9028c
movl (%rdi), %r15d
movl 0x4(%rdi), %r12d
movl -0x28(%r8), %r11d
movl -0x24(%r8), %r10d
cmpl %r10d, %r12d
movl %r10d, %ebx
cmovbl %r12d, %ebx
cmpl %r11d, %r15d
movl %r11d, %eax
cmoval %r15d, %eax
subl %eax, %ebx
jbe 0x90298
movl %r10d, %r13d
subl %r11d, %r13d
cmpl %r13d, %ebx
jne 0x90219
orb $-0x80, %bpl
movb %bpl, (%r8)
jmp 0x9028c
subl %r15d, %r12d
cmpl %r12d, %ebx
jne 0x90227
orb $-0x80, 0x28(%rdi)
jmp 0x9028c
movl -0x1c(%r8), %eax
movq %r14, %r12
movl %ebx, %r14d
cvtsi2sd %r14, %xmm0
movl -0x18(%r8), %r14d
movl %r13d, %r15d
cvtsi2sd %r15, %xmm1
subl %eax, %r14d
movapd %xmm0, %xmm2
cvtsi2sd %r14, %xmm3
divsd %xmm1, %xmm2
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %r14
addl %ebx, %r11d
movl %r11d, -0x28(%r8)
addl %eax, %r14d
movl %r14d, -0x1c(%r8)
movq %r12, %r14
subl %r11d, %r10d
cvtsi2sd %r10, %xmm1
movsd -0x8(%r8), %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm2
movsd %xmm2, -0x8(%r8)
incq %r9
addq $0x30, %r8
jmp 0x901c5
incq %rsi
incq %rdx
addq $0x30, %r14
movq 0x8(%rsp), %rbx
jmp 0x901a2
imulq $0x30, %rcx, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl %edx, %r12d
cmpq %rcx, %rax
je 0x902f2
cmpb $0x0, 0x28(%rbx,%rcx)
js 0x902ec
leaq (%rbx,%rcx), %rsi
imulq $0x30, %r12, %rdi
movupd (%rsi), %xmm0
movupd 0x10(%rsi), %xmm1
movupd 0x20(%rsi), %xmm2
movupd %xmm2, 0x20(%rbx,%rdi)
movupd %xmm1, 0x10(%rbx,%rdi)
movupd %xmm0, (%rbx,%rdi)
incl %edx
addq $0x30, %rcx
jmp 0x902b4
imulq $0x30, %r12, %rax
movq %rax, 0x10(%rsp)
leaq (%rbx,%rax), %rsi
movq %rbx, %rdi
callq 0x67af5
leaq 0x58(%rbx), %rax
pushq $0x1
popq %rcx
xorl %edx, %edx
cmpq %r12, %rdx
je 0x9040e
imulq $0x30, %rdx, %rsi
cmpb $0x0, 0x28(%rbx,%rsi)
js 0x903fa
addq 0x8(%rsp), %rsi
movq %rax, %rdi
movq %rcx, %r8
cmpq %r12, %r8
jae 0x903fa
movb (%rdi), %bl
testb %bl, %bl
js 0x903ee
movl 0xc(%rsi), %ebp
movl 0x10(%rsi), %r13d
movl -0x1c(%rdi), %r10d
movl -0x18(%rdi), %r9d
cmpl %r9d, %r13d
movl %r9d, %r11d
cmovbl %r13d, %r11d
cmpl %r10d, %ebp
movl %r10d, %r14d
cmoval %ebp, %r14d
subl %r14d, %r11d
jbe 0x903fa
movl %r9d, %r15d
subl %r10d, %r15d
cmpl %r15d, %r11d
jne 0x90384
orb $-0x80, %bl
movb %bl, (%rdi)
jmp 0x903ee
subl %ebp, %r13d
cmpl %r13d, %r11d
jne 0x90392
orb $-0x80, 0x28(%rsi)
jmp 0x903ee
movl -0x28(%rdi), %ebx
movl %r11d, %r14d
cvtsi2sd %r14, %xmm0
movl -0x24(%rdi), %r14d
movl %r15d, %r15d
cvtsi2sd %r15, %xmm1
subl %ebx, %r14d
movapd %xmm0, %xmm2
cvtsi2sd %r14, %xmm3
divsd %xmm1, %xmm2
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %r14
addl %r11d, %r10d
movl %r10d, -0x1c(%rdi)
addl %ebx, %r14d
movl %r14d, -0x28(%rdi)
subl %r10d, %r9d
cvtsi2sd %r9, %xmm1
movsd -0x8(%rdi), %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm2
movsd %xmm2, -0x8(%rdi)
incq %r8
addq $0x30, %rdi
jmp 0x90333
incq %rdx
incq %rcx
addq $0x30, %rax
movq 0x8(%rsp), %rbx
jmp 0x90310
leaq 0x28(%rbx), %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r8
movl %edx, %esi
cmpq %rcx, %r8
je 0x90468
cmpb $0x0, (%rax,%rcx)
js 0x90462
leaq (%rax,%rcx), %rdi
addq $-0x28, %rdi
addsd 0x20(%rbx,%rcx), %xmm0
imulq $0x30, %rsi, %rsi
movupd (%rdi), %xmm1
movupd 0x10(%rdi), %xmm2
movups 0x20(%rdi), %xmm3
movups %xmm3, 0x20(%rbx,%rsi)
movupd %xmm2, 0x10(%rbx,%rsi)
movupd %xmm1, (%rbx,%rsi)
incl %edx
addq $0x30, %rcx
jmp 0x90424
imulq $0x30, %rsi, %rax
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rcx, %rax
je 0x904b3
cmpb $0x40, 0x28(%rbx,%rcx)
jl 0x904ad
movq 0x18(%r10), %rsi
movq 0x20(%rsi), %rsi
movl 0xc(%rbx,%rcx), %edi
movl 0x14(%rbx,%rcx), %r8d
movl (%rsi,%r8,8), %r9d
andl %edx, %r9d
subl 0x10(%rbx,%rcx), %r9d
movl %r9d, 0xc(%rbx,%rcx)
movl (%rsi,%r8,8), %esi
andl %edx, %esi
subl %edi, %esi
movl %esi, 0x10(%rbx,%rcx)
addq $0x30, %rcx
jmp 0x90473
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq