// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: optimize_u_trans(kv_u_trans_t*, kvec_pe_hit*, ha_ug_index*)
void optimize_u_trans(kv_u_trans_t *ovlp, kvec_pe_hit* hits, ha_ug_index* idx)
{
    if(hits->idx.n == 0) idx_hc_links(hits, idx, NULL);
    uint64_t i, m, occ;
    u_trans_t *x = NULL, *p = NULL;
    kv_u_trans_t k_trans; 
    kv_init(k_trans); kv_init(k_trans.idx);
    for (i = 0; i < ovlp->n; i++){
        x = &(ovlp->a[i]);
        if(x->qn > x->tn) continue;
        if(x->f != RC_2 || x->del) continue;///no need to adjust RC_0/RC_1
        occ = get_oe_occ(x->qn, x->tn, hits, idx) + get_oe_occ(x->tn, x->qn, hits, idx);///how many UL bridging qn and tn
        kv_pushp(u_trans_t, k_trans, &p);
        (*p) = (*x); p->nw = (x->nw*(1-(((double)(occ<<1))/((double)(hits->occ.a[x->qn]+hits->occ.a[x->tn])))));
        if(p->nw < 0) fprintf(stderr, "ERROR-nw\n");
        if(p->nw == 0) p->nw = x->nw*0.005;
        if(p->nw == 0) {
            k_trans.n--;
        } else {
            kv_pushp(u_trans_t, k_trans, &p);
            (*p) = k_trans.a[k_trans.n-2];
            p->qn = k_trans.a[k_trans.n-2].tn; p->qs = k_trans.a[k_trans.n-2].ts; p->qe = k_trans.a[k_trans.n-2].te;
            p->tn = k_trans.a[k_trans.n-2].qn; p->ts = k_trans.a[k_trans.n-2].qs; p->te = k_trans.a[k_trans.n-2].qe;
        }
    }
    kt_u_trans_t_idx(&k_trans, idx->ug->g->n_seq);
    mc_solve(NULL, NULL, &k_trans, idx->ug, idx->read_g, 0.8, R_INF.trio_flag, 1, NULL, 1, NULL, NULL, 1, 0);
    for (i = m = 0; i < ovlp->n; i++){
        x = &(ovlp->a[i]);
        if(x->del) continue;
        if(x->f == RC_2){
            get_u_trans_spec(&k_trans, x->qn, x->tn, &p, NULL);
            if(!p) continue;
        }
        ovlp->a[m++] = ovlp->a[i];
    }
    ovlp->n = m;
    kt_u_trans_t_idx(ovlp, idx->ug->g->n_seq);
    free(hits->idx.a); hits->idx.a = NULL; hits->idx.n = hits->idx.m = 0;
    free(hits->occ.a); hits->occ.a = NULL; hits->occ.n = hits->occ.m = 0;
    kv_destroy(k_trans); kv_destroy(k_trans.idx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, 0x38(%rsp)
movq %rdi, %r14
movq %rsi, 0x40(%rsp)
cmpq $0x0, 0x18(%rsi)
jne 0x15a3bc
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x13f81d
movq $0x0, 0x48(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
cmpq $0x0, (%r14)
je 0x15a66d
movl $0x28, %r15d
xorl %ebx, %ebx
movq %r14, 0x90(%rsp)
movq 0x10(%r14), %rbp
movl -0x20(%rbp,%r15), %r12d
movl -0x14(%rbp,%r15), %r13d
cmpl %r13d, %r12d
ja 0x15a64d
movb (%rbp,%r15), %al
andb $-0x41, %al
cmpb $0x2, %al
jne 0x15a64d
movl %r12d, %edi
movl %r13d, %esi
movq 0x40(%rsp), %r14
movq %r14, %rdx
movq 0x38(%rsp), %rcx
callq 0x15a312
movl %eax, 0x88(%rsp)
movl %r13d, %edi
movl %r12d, %esi
movq %r14, %rdx
movq 0x38(%rsp), %rcx
callq 0x15a312
movl %eax, %r12d
movq 0x58(%rsp), %rax
cmpq %rax, 0x50(%rsp)
jne 0x15a488
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x58(%rsp)
movq 0x60(%rsp), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x60(%rsp)
leaq (%r15,%rbp), %rax
addq $-0x28, %rax
addl 0x88(%rsp), %r12d
movq 0x50(%rsp), %rdx
movq 0x60(%rsp), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x50(%rsp)
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
leaq (%rcx,%rdx), %rsi
movq %rsi, 0x48(%rsp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rcx,%rdx)
movups %xmm1, 0x10(%rcx,%rdx)
movups %xmm0, (%rcx,%rdx)
addq %r12, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movq 0x40(%rsp), %rsi
movq 0x40(%rsi), %rsi
movl 0x8(%rax), %edi
movl 0x14(%rax), %r8d
movq (%rsi,%r8,8), %r8
addq (%rsi,%rdi,8), %r8
movq %r8, %xmm1
punpckldq 0xcad3b(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xcad43(%rip), %xmm1    # 0x225250
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movsd 0xcad4b(%rip), %xmm1    # 0x225270
subsd %xmm0, %xmm1
mulsd 0x20(%rax), %xmm1
movsd %xmm1, 0x20(%rcx,%rdx)
xorpd %xmm2, %xmm2
ucomisd %xmm1, %xmm2
ja 0x15a65f
movq 0x48(%rsp), %rax
movsd 0x20(%rax), %xmm0
ucomisd %xmm2, %xmm0
movq 0x90(%rsp), %r14
jne 0x15a570
jp 0x15a570
movsd -0x8(%rbp,%r15), %xmm0
mulsd 0xd4aad(%rip), %xmm0    # 0x22f018
movsd %xmm0, 0x20(%rax)
movsd 0x20(%rax), %xmm0
ucomisd %xmm2, %xmm0
movq 0x50(%rsp), %rax
jne 0x15a58f
jp 0x15a58f
decq %rax
movq %rax, 0x50(%rsp)
jmp 0x15a64d
movq 0x58(%rsp), %rcx
cmpq %rcx, %rax
jne 0x15a5c5
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rdi
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x60(%rsp)
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x50(%rsp)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
leaq (%rcx,%rax), %rdx
movq %rdx, 0x48(%rsp)
movupd -0x30(%rcx,%rax), %xmm0
movupd -0x20(%rcx,%rax), %xmm1
movupd -0x10(%rcx,%rax), %xmm2
movupd %xmm2, 0x20(%rcx,%rax)
movupd %xmm1, 0x10(%rcx,%rax)
movupd %xmm0, (%rcx,%rax)
movq 0x50(%rsp), %rdx
movq 0x60(%rsp), %rsi
leaq (%rdx,%rdx,2), %rdx
shlq $0x4, %rdx
movl -0x4c(%rsi,%rdx), %edi
movl %edi, 0x8(%rcx,%rax)
movl -0x54(%rsi,%rdx), %edi
movl %edi, (%rcx,%rax)
movl -0x50(%rsi,%rdx), %edi
movl %edi, 0x4(%rcx,%rax)
movl -0x58(%rsi,%rdx), %edi
movl %edi, 0x14(%rcx,%rax)
movl -0x60(%rsi,%rdx), %edi
movl %edi, 0xc(%rcx,%rax)
movl -0x5c(%rsi,%rdx), %edx
movl %edx, 0x10(%rcx,%rax)
incq %rbx
addq $0x30, %r15
cmpq (%r14), %rbx
jb 0x15a3f5
jmp 0x15a66d
callq 0x1db41
xorpd %xmm2, %xmm2
jmp 0x15a542
movq 0x40(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x88(%rsp)
movq 0x38(%rsp), %rbx
movq (%rbx), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl 0x14(%rax), %esi
andl %ecx, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
callq 0xc405a
movq (%rbx), %rcx
movq 0x8(%rbx), %r8
leaq 0x130b81(%rip), %rax    # 0x28b230
movq 0x28(%rax), %r9
movl $0x1, %eax
movl %eax, 0x28(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl %eax, (%rsp)
movl $0x0, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
movsd 0xcadc7(%rip), %xmm0    # 0x2254b0
xorl %edi, %edi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1edea
cmpq $0x0, (%r14)
je 0x15a779
xorl %ebx, %ebx
leaq 0x50(%rsp), %r12
leaq 0x48(%rsp), %r13
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movb 0x28(%rax,%rbx), %cl
testb %cl, %cl
js 0x15a76d
andb $0x3f, %cl
cmpb $0x2, %cl
jne 0x15a73e
movl 0x8(%rax,%rbx), %esi
movl 0x14(%rax,%rbx), %edx
movq %r12, %rdi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0xca79c
cmpq $0x0, 0x48(%rsp)
je 0x15a76d
movq 0x10(%r14), %rax
leaq (%r15,%r15,2), %rcx
incq %r15
shlq $0x4, %rcx
movups (%rax,%rbx), %xmm0
movupd 0x10(%rax,%rbx), %xmm1
movupd 0x20(%rax,%rbx), %xmm2
movupd %xmm2, 0x20(%rax,%rcx)
movupd %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %rbp
addq $0x30, %rbx
cmpq (%r14), %rbp
jb 0x15a70c
movq %r15, (%r14)
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
movq %r14, %rdi
callq 0xc405a
movq 0x40(%rsp), %r14
movq 0x28(%r14), %rdi
callq 0x2220
xorl %ebx, %ebx
movq 0x88(%rsp), %rax
movq %rbx, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x40(%r14), %rdi
callq 0x2220
movq %rbx, 0x40(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r14)
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq