// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movq %rdx, 0x48(%rsp)
movq %rsi, 0x50(%rsp)
cmpq $0x0, 0x18(%rsi)
movq %rdi, 0x38(%rsp)
jne 0x160d58
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0x14547c
movq 0x38(%rsp), %rdi
movq $0x0, 0x58(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
cmpq $0x0, (%rdi)
je 0x161047
movl $0x28, %ebx
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r9d, %r9d
xorl %r8d, %r8d
movq 0x10(%rdi), %r14
movl -0x20(%r14,%rbx), %ebp
movl -0x14(%r14,%rbx), %r15d
cmpl %r15d, %ebp
ja 0x160ff6
movb (%r14,%rbx), %al
andb $-0x41, %al
cmpb $0x2, %al
jne 0x160ff6
movq %r8, 0x68(%rsp)
movl %ebp, %edi
movl %r15d, %esi
movq %r13, 0xa0(%rsp)
movq 0x50(%rsp), %r13
movq %r13, %rdx
movq 0x48(%rsp), %rcx
movq %r9, 0x40(%rsp)
callq 0x160ca7
movl %eax, 0x64(%rsp)
movl %r15d, %edi
movl %ebp, %esi
movq %r13, %rdx
movq 0xa0(%rsp), %r13
movq 0x48(%rsp), %rcx
callq 0x160ca7
movq 0x40(%rsp), %r9
movl %eax, %ebp
cmpq %r9, %r13
jne 0x160e52
leaq (,%r13,2), %r15
testq %r13, %r13
movl $0x2, %eax
cmoveq %rax, %r15
movq %r15, 0x78(%rsp)
movq %r15, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
movq 0x68(%rsp), %rdi
callq 0x2370
movq %r15, %r9
movq %rax, %r8
movq %rax, 0x80(%rsp)
xorpd %xmm3, %xmm3
jmp 0x160e5b
xorpd %xmm3, %xmm3
movq 0x68(%rsp), %r8
leaq (%r14,%rbx), %rax
addq $-0x28, %rax
addl 0x64(%rsp), %ebp
leaq (,%r13,2), %rcx
addq %r13, %rcx
shlq $0x4, %rcx
leaq (%r8,%rcx), %r15
movq %r15, 0x58(%rsp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%r8,%rcx)
movups %xmm1, 0x10(%r8,%rcx)
movups %xmm0, (%r8,%rcx)
addq %rbp, %rbp
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
movq 0x50(%rsp), %rdx
movq 0x40(%rdx), %rdx
movl 0x8(%rax), %esi
movl 0x14(%rax), %edi
movq (%rdx,%rdi,8), %rdi
addq (%rdx,%rsi,8), %rdi
movq %rdi, %xmm0
punpckldq 0xd03b6(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xd03be(%rip), %xmm0    # 0x231290
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd 0xd0426(%rip), %xmm0    # 0x231310
subsd %xmm1, %xmm0
mulsd 0x20(%rax), %xmm0
movsd %xmm0, 0x20(%r8,%rcx)
ucomisd %xmm0, %xmm3
ja 0x161008
ucomisd %xmm3, %xmm0
jne 0x160f21
jp 0x160f21
movsd -0x8(%r14,%rbx), %xmm0
mulsd 0xda34d(%rip), %xmm0    # 0x23b268
movsd %xmm0, 0x20(%r15)
ucomisd %xmm3, %xmm0
movq 0x38(%rsp), %rdi
jne 0x160f38
jp 0x160f38
movq %r13, 0x70(%rsp)
jmp 0x160ff6
leaq 0x1(%r13), %r14
cmpq %r9, %r14
jne 0x160f7c
leaq (%r9,%r9), %r15
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %r15
movq %r15, 0x78(%rsp)
movq %r15, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
movq %r8, %rdi
callq 0x2370
movq 0x38(%rsp), %rdi
movq %rax, %r8
movq %rax, 0x80(%rsp)
movq %r15, %r9
addq $0x2, %r13
movq %r13, 0x70(%rsp)
leaq (%r14,%r14,2), %rax
shlq $0x4, %rax
leaq (%r8,%rax), %rcx
movq %rcx, 0x58(%rsp)
movups -0x30(%r8,%rax), %xmm0
movups -0x20(%r8,%rax), %xmm1
movupd -0x10(%r8,%rax), %xmm2
movupd %xmm2, 0x20(%r8,%rax)
movups %xmm1, 0x10(%r8,%rax)
movups %xmm0, (%r8,%rax)
leaq (,%r13,2), %rcx
addq %r13, %rcx
shlq $0x4, %rcx
movl -0x54(%r8,%rcx), %edx
movl %edx, (%r8,%rax)
movl -0x58(%r8,%rcx), %edx
movsd -0x50(%r8,%rcx), %xmm0
movl %edx, 0x14(%r8,%rax)
movsd -0x60(%r8,%rcx), %xmm1
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movupd %xmm0, 0x4(%r8,%rax)
incq %r12
addq $0x30, %rbx
cmpq (%rdi), %r12
jb 0x160d98
jmp 0x161047
movq 0x133fd1(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
movl $0x9, %esi
movl $0x1, %edx
leaq 0xdb7e8(%rip), %rdi     # 0x23c80b
movq %r8, %rbp
movq %r9, 0x40(%rsp)
callq 0x2410
movq 0x40(%rsp), %r9
movq %rbp, %r8
xorpd %xmm3, %xmm3
movsd 0x20(%r15), %xmm0
jmp 0x160f04
movq 0x50(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rbx
movq (%rbx), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x14(%rax), %esi
andl %ebp, %esi
leaq 0x70(%rsp), %r15
movq %r15, %rdi
callq 0xc4edd
movq (%rbx), %rcx
movq 0x8(%rbx), %r8
leaq 0x1351aa(%rip), %rax    # 0x296230
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 %r14d, %r14d
movsd 0xd04f0(%rip), %xmm0    # 0x2315b0
xorl %edi, %edi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1e07b
movq 0x38(%rsp), %rdi
cmpq $0x0, (%rdi)
je 0x16116e
xorl %r15d, %r15d
leaq 0x70(%rsp), %r12
leaq 0x58(%rsp), %r13
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x10(%rdi), %rax
movb 0x28(%rax,%r15), %cl
testb %cl, %cl
js 0x161159
andb $0x3f, %cl
cmpb $0x2, %cl
jne 0x16112b
movl 0x8(%rax,%r15), %esi
movl 0x14(%rax,%r15), %edx
movq %r12, %rdi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0xcba40
cmpq $0x0, 0x58(%rsp)
je 0x161167
movq 0x38(%rsp), %rdi
movq 0x10(%rdi), %rax
leaq (%r14,%r14,2), %rcx
incq %r14
shlq $0x4, %rcx
movups (%rax,%r15), %xmm0
movupd 0x10(%rax,%r15), %xmm1
movupd 0x20(%rax,%r15), %xmm2
movupd %xmm2, 0x20(%rax,%rcx)
movupd %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %rbx
addq $0x30, %r15
cmpq (%rdi), %rbx
jb 0x1610ed
jmp 0x16116e
movq 0x38(%rsp), %rdi
jmp 0x161159
movq %r14, (%rdi)
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
andl 0x14(%rax), %ebp
movl %ebp, %esi
callq 0xc4edd
movq 0x50(%rsp), %r14
movq 0x28(%r14), %rdi
callq 0x2220
xorl %ebx, %ebx
movq 0x40(%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 0x80(%rsp), %rdi
callq 0x2220
movq 0x98(%rsp), %rdi
callq 0x2220
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq