// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: insert_dip_chain(kv_f_chain*, u_trans_t*, double)
void insert_dip_chain(kv_f_chain *x, u_trans_t *p, double LenRate)
{
    uint32_t i;
    uint64_t ovlp = 0, tLen = MIN(x->tov, p->qe-p->qs);
    int64_t dp, old_dp, start = 0;
    f_chain_t *t = NULL;
    double r;
    for (i = 0; i < x->n; i++)
    {
        ovlp += ((MIN(x->a[i].e, p->qe) > MAX(x->a[i].s, p->qs))?
                                        (MIN(x->a[i].e, p->qe) - MAX(x->a[i].s, p->qs)):0);
    }
    if(tLen > 0 && ovlp > tLen*LenRate)
    {
        r = ((double)ovlp)/((double)tLen);
        if(r >= 0.75)
        {
            if(p->nw <= x->tw*0.75) p->del = 1;
        } 
        else
        {
            if(p->nw <= x->tw*0.5) p->del = 1;
        }
    }

    if(!p->del)
    {
        x->tw += p->nw - (((double)ovlp)/((double)(p->qe-p->qs)))*p->nw;   

        kv_push(uint64_t, x->b, p->qs<<1);
        kv_push(uint64_t, x->b, p->qe<<1|1);
        if(x->b.n > 2 && x->b.a[x->b.n-2] < x->b.a[x->b.n-3])
        {
            radix_sort_hc64(x->b.a, x->b.a + x->b.n);
        }
        x->n = 0; x->tov = 0;
        for (i = 0, dp = 0, start = 0; i < x->b.n; ++i) 
        {
            old_dp = dp;
            ///if a[j] is qe
            if (x->b.a[i]&1) 
            {
                --dp;
            }
            else
            {
                ++dp;
            } 


            if (old_dp < 1 && dp >= 1) ///old_dp < dp, b.a[j] is qs
            { 
                ///case 2, a[j] is qs
                start = x->b.a[i]>>1;
            } 
            else if (old_dp >= 1 && dp < 1) ///old_dp > min_dp, b.a[j] is qe
            {
                kv_pushp(f_chain_t, *x, &t);
                t->s = start; t->e = x->b.a[i]>>1;
                x->tov += t->e - t->s;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rdi), %rdx
movq 0x38(%rdi), %rcx
movl (%rsi), %esi
movl 0x4(%r14), %edi
movl %edi, %eax
subl %esi, %eax
cmpq %rax, %rcx
cmovaeq %rax, %rcx
testq %rdx, %rdx
je 0x159de9
movq (%rbx), %r8
addq $0x8, %r8
movl $0x1, %r10d
xorl %r9d, %r9d
movq -0x8(%r8), %r11
movq (%r8), %r15
cmpq %rdi, %r15
cmovaeq %rdi, %r15
cmpq %rsi, %r11
cmovbeq %rsi, %r11
subq %r11, %r15
movl $0x0, %r11d
cmovaeq %r15, %r11
addq %r11, %r9
movl %r10d, %r11d
addq $0x10, %r8
incl %r10d
cmpq %r11, %rdx
ja 0x159d92
movq %r9, %xmm2
punpckldq 0xcb46d(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0xcb475(%rip), %xmm2    # 0x225250
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
jmp 0x159ded
xorpd %xmm1, %xmm1
testq %rcx, %rcx
je 0x159e3e
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x159e3e
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
ucomisd 0xcb71c(%rip), %xmm0    # 0x225530
movsd 0x20(%r14), %xmm0
movsd 0x30(%rbx), %xmm2
jae 0x159e2b
mulsd 0xcb6c7(%rip), %xmm2    # 0x2254f0
jmp 0x159e33
mulsd 0xcb6fd(%rip), %xmm2    # 0x225530
ucomisd %xmm0, %xmm2
jb 0x159e3e
orb $-0x80, 0x28(%r14)
cmpb $0x0, 0x28(%r14)
js 0x159ffa
movsd 0x20(%r14), %xmm0
movl %eax, %eax
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
divsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
subsd %xmm1, %xmm0
addsd 0x30(%rbx), %xmm0
movsd %xmm0, 0x30(%rbx)
movq 0x20(%rbx), %rax
cmpq %rax, 0x18(%rbx)
jne 0x159e9e
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x20(%rbx)
movq 0x28(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x28(%rbx)
movl (%r14), %eax
addl %eax, %eax
movq 0x18(%rbx), %rcx
movq 0x28(%rbx), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x18(%rbx)
movq %rax, (%rdi,%rcx,8)
movq 0x20(%rbx), %rax
cmpq %rax, 0x18(%rbx)
jne 0x159ee2
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x20(%rbx)
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x28(%rbx)
movl 0x4(%r14), %eax
leal 0x1(,%rax,2), %eax
movq 0x18(%rbx), %rcx
movq 0x28(%rbx), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x18(%rbx)
movq %rax, (%rdi,%rcx,8)
movq 0x18(%rbx), %rax
cmpq $0x3, %rax
jb 0x159f20
movq -0x10(%rdi,%rax,8), %rcx
cmpq -0x18(%rdi,%rax,8), %rcx
jae 0x159f20
leaq (%rdi,%rax,8), %rsi
callq 0x13180d
xorl %eax, %eax
movq %rax, 0x8(%rbx)
movq %rax, 0x38(%rbx)
cmpq %rax, 0x18(%rbx)
je 0x159ffa
movl $0x1, %ebp
xorl %r13d, %r13d
movl $0x2, %r14d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r12, %rax
movq 0x28(%rbx), %rcx
movq (%rcx,%r13,8), %rcx
movl %ecx, %r12d
andl $0x1, %r12d
negq %r12
orq $0x1, %r12
addq %rax, %r12
testq %rax, %rax
setle %dl
testq %r12, %r12
setg %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x159f82
shrq %rcx
movq %rcx, %r15
jmp 0x159feb
testq %rax, %rax
setg %al
testq %r12, %r12
setle %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x159feb
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x159fbd
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r14, %rsi
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
shlq $0x4, %rcx
movq %r15, (%rax,%rcx)
movq 0x28(%rbx), %rdx
movq (%rdx,%r13,8), %rdx
shrq %rdx
movq %rdx, 0x8(%rax,%rcx)
subq %r15, %rdx
addq %rdx, 0x38(%rbx)
movl %ebp, %r13d
incl %ebp
cmpq %r13, 0x18(%rbx)
ja 0x159f48
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq