// File: /workspace/github_cpp_makefile_star2_O3/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), %rsi
movq 0x38(%rdi), %rdx
movl (%r14), %ecx
movl 0x4(%r14), %edi
movl %edi, %eax
subl %ecx, %eax
cmpq %rax, %rdx
cmovaeq %rax, %rdx
testq %rsi, %rsi
je 0x160751
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 %rcx, %r11
cmovbeq %rcx, %r11
subq %r11, %r15
movl $0x0, %r11d
cmovaeq %r15, %r11
addq %r11, %r9
movl %r10d, %r11d
addq $0x10, %r8
incl %r10d
cmpq %r11, %rsi
ja 0x1606fa
movq %r9, %xmm2
punpckldq 0xd0b45(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0xd0b4d(%rip), %xmm2    # 0x231290
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
jmp 0x160755
xorpd %xmm1, %xmm1
testq %rdx, %rdx
je 0x1607a6
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x1607a6
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
ucomisd 0xd0eb4(%rip), %xmm0    # 0x231630
movsd 0x20(%r14), %xmm0
movsd 0x30(%rbx), %xmm2
jae 0x160793
mulsd 0xd0e5f(%rip), %xmm2    # 0x2315f0
jmp 0x16079b
mulsd 0xd0e95(%rip), %xmm2    # 0x231630
ucomisd %xmm0, %xmm2
jb 0x1607a6
orb $-0x80, 0x28(%r14)
cmpb $0x0, 0x28(%r14)
js 0x160985
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 0x18(%rbx), %rdx
cmpq 0x20(%rbx), %rdx
jne 0x16080f
leaq (%rdx,%rdx), %rax
testq %rdx, %rdx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x20(%rbx)
movq 0x28(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x28(%rbx)
movl (%r14), %ecx
movq 0x18(%rbx), %rdx
jmp 0x160813
movq 0x28(%rbx), %rax
addl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x18(%rbx)
movq %rcx, (%rax,%rdx,8)
movq 0x18(%rbx), %rcx
cmpq 0x20(%rbx), %rcx
jne 0x160853
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x20(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x28(%rbx)
movq 0x18(%rbx), %rcx
movl 0x4(%r14), %edx
leal 0x1(,%rdx,2), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x18(%rbx)
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rbx), %rcx
cmpq $0x3, %rcx
jb 0x160890
movq -0x10(%rax,%rcx,8), %rdx
cmpq -0x18(%rax,%rcx,8), %rdx
jae 0x1608a4
leaq (%rax,%rcx,8), %rsi
movq %rax, %rdi
callq 0x1379f9
movq 0x18(%rbx), %rcx
xorl %eax, %eax
movq %rax, 0x8(%rbx)
movq %rax, 0x38(%rbx)
testq %rcx, %rcx
jne 0x1608ae
jmp 0x160985
xorl %eax, %eax
movq %rax, 0x8(%rbx)
movq %rax, 0x38(%rbx)
movq 0x28(%rbx), %rdx
movl $0x1, %ebp
xorl %esi, %esi
movl $0x2, %r14d
xorl %edi, %edi
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r12, %rax
movq (%rdx,%r13,8), %r8
movl %r8d, %r12d
andl $0x1, %r12d
negq %r12
orq $0x1, %r12
addq %rax, %r12
testq %rax, %rax
setle %r9b
testq %r12, %r12
setg %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x160901
shrq %r8
movq %r8, %r15
jmp 0x160977
testq %rax, %rax
setg %al
testq %r12, %r12
setle %r8b
andb %al, %r8b
cmpb $0x1, %r8b
jne 0x160977
movq (%rbx), %rax
cmpq 0x10(%rbx), %rdi
jne 0x16094e
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
cmoveq %r14, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rdi
movq 0x18(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq 0x38(%rbx), %rsi
leaq 0x1(%rdi), %r8
movq %r8, 0x8(%rbx)
shlq $0x4, %rdi
movq %r15, (%rax,%rdi)
movq (%rdx,%r13,8), %r9
shrq %r9
movq %r9, 0x8(%rax,%rdi)
subq %r15, %r9
addq %r9, %rsi
movq %rsi, 0x38(%rbx)
movq %r8, %rdi
movl %ebp, %r13d
incl %ebp
cmpq %r13, %rcx
ja 0x1608ca
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq