// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movl %esi, %ebp
movq %rdi, %r14
movl %esi, %ebx
leaq (%rbx,%rbx,2), %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
callq 0x9e1d9
testl %ebp, %ebp
je 0xca8c9
shlq $0x4, %rbx
leaq (%rbx,%rbx,2), %rax
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpb $0x40, 0x28(%r14,%rcx)
jl 0xca8c0
movq 0x10(%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 0xca883
leaq 0x58(%r14), %rax
movq %rax, 0x20(%rsp)
movq %r14, (%rsp)
movl %ebp, %eax
testl %ebp, %ebp
je 0xcaa54
movl %eax, %ecx
movq %rcx, 0x18(%rsp)
movq %rax, 0x28(%rsp)
leal -0x1(%rax), %r11d
xorl %r8d, %r8d
movq 0x20(%rsp), %r15
xorl %r12d, %r12d
leaq (%r8,%r8,2), %r10
shlq $0x4, %r10
cmpb $0x0, 0x28(%r14,%r10)
movl %r11d, 0xc(%rsp)
movq %r15, 0x30(%rsp)
js 0xcaa23
leaq 0x1(%r8), %rax
cmpq 0x18(%rsp), %rax
jae 0xcaa23
addq %r14, %r10
movb (%r15), %bpl
testb %bpl, %bpl
js 0xcaa16
movl 0x4(%r10), %r13d
movl -0x28(%r15), %r14d
cmpl %r14d, %r13d
jb 0xcaa23
movl 0x10(%r10), %ebx
movl -0x1c(%r15), %eax
cmpl %eax, %ebx
jb 0xcaa16
movl (%r10), %esi
cmpl %r14d, %esi
cmovbel %r14d, %esi
movl -0x18(%r15), %r12d
cmpl %r12d, %ebx
movl %r12d, %edx
cmovbl %ebx, %edx
movl -0x24(%r15), %ecx
cmovbel %r12d, %ebx
cmpl %ecx, %r13d
movl %ecx, %edi
cmovbl %r13d, %edi
cmovbel %ecx, %r13d
movl 0xc(%r10), %r9d
subl %esi, %edi
movl $0x0, %esi
cmovbl %esi, %edi
cmpl %eax, %r9d
cmovbel %eax, %r9d
subl %r9d, %edx
cmovbl %esi, %edx
subl %r14d, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
subl %edi, %ecx
subl %eax, %r12d
xorps %xmm1, %xmm1
cvtsi2sd %r12, %xmm1
subl %edx, %r12d
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
divsd %xmm0, %xmm2
movsd -0x8(%r15), %xmm0
xorps %xmm3, %xmm3
cvtsi2sd %r12, %xmm3
mulsd %xmm0, %xmm2
divsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
movl %r13d, 0x4(%r10)
movl %ebx, 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, (%r15)
movl $0x1, %r12d
addq $0x30, %r15
decl %r11d
jne 0xca92b
incq %r8
movq 0x30(%rsp), %r15
addq $0x30, %r15
movl 0xc(%rsp), %r11d
decl %r11d
cmpq 0x18(%rsp), %r8
movq (%rsp), %r14
jne 0xca8fb
testl %r12d, %r12d
sete %sil
movq 0x28(%rsp), %rax
jmp 0xcaa57
movb $0x1, %sil
xorl %ebp, %ebp
testl %eax, %eax
je 0xcaaae
movl %eax, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
xorl %edx, %edx
xorl %ebp, %ebp
cmpb $0x0, 0x28(%r14,%rdx)
js 0xcaaa5
leaq (%r14,%rdx), %rcx
movl %ebp, %edi
leaq (%rdi,%rdi,2), %rdi
shlq $0x4, %rdi
movupd (%rcx), %xmm0
movupd 0x10(%rcx), %xmm1
movupd 0x20(%rcx), %xmm2
movupd %xmm2, 0x20(%r14,%rdi)
movupd %xmm1, 0x10(%r14,%rdi)
movupd %xmm0, (%r14,%rdi)
incl %ebp
addq $0x30, %rdx
cmpq %rdx, %rax
jne 0xcaa6b
testb %sil, %sil
je 0xca8d6
xorl %ebx, %ebx
movl $0x0, %r12d
testl %ebp, %ebp
je 0xcac3d
movl %ebp, %eax
leaq 0x58(%r14), %rcx
decl %ebp
xorl %edx, %edx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rdi
shlq $0x4, %rdi
cmpb $0x0, 0x28(%r14,%rdi)
js 0xcabd3
leaq 0x1(%rsi), %r8
cmpq %rax, %r8
jae 0xcabd3
addq (%rsp), %rdi
movl %ebp, %r8d
movq %rcx, %r9
movb (%r9), %r14b
testb %r14b, %r14b
js 0xcabc6
movl (%rdi), %r12d
movl 0x4(%rdi), %r13d
movl -0x28(%r9), %r11d
movl -0x24(%r9), %r10d
cmpl %r10d, %r13d
movl %r10d, %ebx
cmovbl %r13d, %ebx
cmpl %r11d, %r12d
movl %r11d, %r15d
cmoval %r12d, %r15d
subl %r15d, %ebx
cmovbl %edx, %ebx
jbe 0xcabd3
movl %r10d, %r15d
subl %r11d, %r15d
cmpl %r15d, %ebx
jne 0xcab4d
orb $-0x80, %r14b
movb %r14b, (%r9)
jmp 0xcabc6
subl %r12d, %r13d
cmpl %r13d, %ebx
jne 0xcab5b
orb $-0x80, 0x28(%rdi)
jmp 0xcabc6
movl -0x1c(%r9), %r14d
movl %ebx, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movl -0x18(%r9), %r12d
movl %r15d, %r15d
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
subl %r14d, %r12d
movapd %xmm0, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %r12, %xmm3
divsd %xmm1, %xmm2
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %r15
addl %ebx, %r11d
movl %r11d, -0x28(%r9)
addl %r14d, %r15d
movl %r15d, -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)
addq $0x30, %r9
decl %r8d
jne 0xcaafe
incq %rsi
addq $0x30, %rcx
decl %ebp
cmpq %rax, %rsi
movq (%rsp), %r14
jne 0xcaad3
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpb $0x0, 0x28(%r14,%rcx)
js 0xcac34
leaq (%r14,%rcx), %rdx
movl %r12d, %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 %r12d
addq $0x30, %rcx
cmpq %rcx, %rax
jne 0xcabf8
movl %r12d, %r13d
leaq (,%r13,2), %rsi
addq %r13, %rsi
shlq $0x4, %rsi
addq %r14, %rsi
movq %r14, %rdi
callq 0x9e4f5
testl %r12d, %r12d
je 0xcadec
leaq 0x58(%r14), %rax
decl %r12d
xorl %ecx, %ecx
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rsi
shlq $0x4, %rsi
cmpb $0x0, 0x28(%r14,%rsi)
js 0xcad6c
leaq 0x1(%rdx), %rdi
cmpq %r13, %rdi
jae 0xcad6c
addq (%rsp), %rsi
movl %r12d, %edi
movq %rax, %r8
movb (%r8), %bl
testb %bl, %bl
js 0xcad60
movl 0xc(%rsi), %ebp
movl 0x10(%rsi), %r14d
movl -0x1c(%r8), %r10d
movl -0x18(%r8), %r9d
cmpl %r9d, %r14d
movl %r9d, %r11d
cmovbl %r14d, %r11d
cmpl %r10d, %ebp
movl %r10d, %r15d
cmoval %ebp, %r15d
subl %r15d, %r11d
cmovbl %ecx, %r11d
jbe 0xcad6c
movl %r9d, %r15d
subl %r10d, %r15d
cmpl %r15d, %r11d
jne 0xcace7
orb $-0x80, %bl
movb %bl, (%r8)
jmp 0xcad60
subl %ebp, %r14d
cmpl %r14d, %r11d
jne 0xcacf5
orb $-0x80, 0x28(%rsi)
jmp 0xcad60
movl -0x28(%r8), %ebx
movl %r11d, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
movl -0x24(%r8), %r14d
movl %r15d, %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 %r11d, %r10d
movl %r10d, -0x1c(%r8)
addl %ebx, %r14d
movl %r14d, -0x28(%r8)
subl %r10d, %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
movsd -0x8(%r8), %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
subsd %xmm0, %xmm2
movsd %xmm2, -0x8(%r8)
addq $0x30, %r8
decl %edi
jne 0xcac99
incq %rdx
addq $0x30, %rax
decl %r12d
cmpq %r13, %rdx
movq (%rsp), %r14
jne 0xcac6e
leaq 0x28(%r14), %rax
shlq $0x4, %r13
leaq (,%r13,2), %rcx
addq %r13, %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
cmpb $0x0, (%rax,%rdx)
js 0xcade1
leaq (%rax,%rdx), %rsi
addq $-0x28, %rsi
addsd 0x20(%r14,%rdx), %xmm0
movl %ebx, %edi
leaq (%rdi,%rdi,2), %rdi
shlq $0x4, %rdi
movupd (%rsi), %xmm1
movupd 0x10(%rsi), %xmm2
movupd 0x20(%rsi), %xmm3
movupd %xmm3, 0x20(%r14,%rdi)
movupd %xmm2, 0x10(%r14,%rdi)
movupd %xmm1, (%r14,%rdi)
incl %ebx
addq $0x30, %rdx
cmpq %rdx, %rcx
jne 0xcad9e
jmp 0xcadf0
xorpd %xmm0, %xmm0
testl %ebx, %ebx
je 0xcae4b
movl %ebx, %eax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpb $0x40, 0x28(%r14,%rcx)
jl 0xcae42
movq 0x10(%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 0xcae05
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq