// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %rdx, 0x48(%rsp)
movq %rsi, %rdx
cmpq $0x0, 0x18(%rsi)
movq %rsi, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
jne 0x11affd
movq %rdx, %rdi
movq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0xfdcd5
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
andq $0x0, 0x50(%rsp)
leaq 0x18(%rdx), %rax
movq %rax, 0xa0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
pushq $0x28
popq %rbx
xorl %r12d, %r12d
xorl %r9d, %r9d
xorl %r13d, %r13d
xorl %r8d, %r8d
cmpq (%rdi), %r8
jae 0x11b2c6
movq 0x10(%rdi), %r14
movl -0x20(%r14,%rbx), %ebp
movl -0x14(%r14,%rbx), %r15d
cmpl %r15d, %ebp
ja 0x11b284
movb (%r14,%rbx), %al
andb $-0x41, %al
cmpb $0x2, %al
jne 0x11b284
movq %r8, 0x68(%rsp)
movq %r12, 0x60(%rsp)
movl %ebp, %edi
movl %r15d, %esi
movq %r13, 0xb0(%rsp)
movq 0x48(%rsp), %r13
movq %r13, %rcx
movq %rdx, %r12
movq %r9, 0xa8(%rsp)
callq 0x11af52
movl %eax, 0x5c(%rsp)
movl %r15d, %edi
movl %ebp, %esi
movq %r12, %rdx
movq %r13, %rcx
movq 0xb0(%rsp), %r13
callq 0x11af52
movq 0xa8(%rsp), %r9
movl %eax, %ebp
cmpq %r9, %r13
jne 0x11b0f7
leaq (%r9,%r9), %r15
testq %r9, %r9
pushq $0x2
popq %rax
cmoveq %rax, %r15
movq %r15, 0x78(%rsp)
imulq $0x30, %r15, %rsi
movq 0x60(%rsp), %rdi
callq 0x2370
movq %rax, %r12
movq %rax, 0x80(%rsp)
movq %r15, %r9
xorpd %xmm3, %xmm3
jmp 0x11b100
xorpd %xmm3, %xmm3
movq 0x60(%rsp), %r12
leaq (%r14,%rbx), %rax
addq $-0x28, %rax
addl 0x5c(%rsp), %ebp
imulq $0x30, %r13, %rcx
leaq (%r12,%rcx), %r15
movq %r15, 0x50(%rsp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%r12,%rcx)
movups %xmm1, 0x10(%r12,%rcx)
movups %xmm0, (%r12,%rcx)
addq %rbp, %rbp
cvtsi2sd %rbp, %xmm1
movq 0x40(%rsp), %rdx
movq 0x40(%rdx), %r8
movl 0x8(%rax), %esi
movl 0x14(%rax), %edi
movq (%r8,%rdi,8), %rdi
addq (%r8,%rsi,8), %rdi
movq %rdi, %xmm0
punpckldq 0xc810f(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xc8117(%rip), %xmm0    # 0x1e3280
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd 0xc811f(%rip), %xmm0    # 0x1e32a0
subsd %xmm1, %xmm0
mulsd 0x20(%rax), %xmm0
movsd %xmm0, 0x20(%r12,%rcx)
ucomisd %xmm0, %xmm3
ja 0x11b290
ucomisd %xmm3, %xmm0
jne 0x11b1b8
jp 0x11b1b8
movsd -0x8(%r14,%rbx), %xmm0
mulsd 0xd1fa6(%rip), %xmm0    # 0x1ed158
movsd %xmm0, 0x20(%r15)
ucomisd %xmm3, %xmm0
movq 0x38(%rsp), %rdi
movq 0x68(%rsp), %r8
jne 0x11b1d4
jp 0x11b1d4
movq %r13, 0x70(%rsp)
jmp 0x11b284
leaq 0x1(%r13), %r14
cmpq %r9, %r14
jne 0x11b219
leaq (%r9,%r9), %r15
testq %r9, %r9
pushq $0x2
popq %rax
cmoveq %rax, %r15
movq %r15, 0x78(%rsp)
imulq $0x30, %r15, %rsi
movq %r12, %rdi
callq 0x2370
movq 0x68(%rsp), %r8
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq %rax, %r12
movq %rax, 0x80(%rsp)
movq %r15, %r9
addq $0x2, %r13
movq %r13, 0x70(%rsp)
imulq $0x30, %r14, %rax
leaq (%r12,%rax), %rcx
movq %rcx, 0x50(%rsp)
movups -0x30(%r12,%rax), %xmm0
movups -0x20(%r12,%rax), %xmm1
movupd -0x10(%r12,%rax), %xmm2
movupd %xmm2, 0x20(%r12,%rax)
movups %xmm1, 0x10(%r12,%rax)
movups %xmm0, (%r12,%rax)
imulq $0x30, %r13, %rcx
movl -0x54(%r12,%rcx), %esi
movl %esi, (%r12,%rax)
movl -0x58(%r12,%rcx), %esi
movsd -0x50(%r12,%rcx), %xmm0
movl %esi, 0x14(%r12,%rax)
movsd -0x60(%r12,%rcx), %xmm1
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movupd %xmm0, 0x4(%r12,%rax)
incq %r8
addq $0x30, %rbx
jmp 0x11b03a
movq 0x137d49(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xd345a(%rip), %rdi     # 0x1ee6fb
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
movq %r9, %rbp
callq 0x2410
movq %rbp, %r9
xorpd %xmm3, %xmm3
movq 0x40(%rsp), %rdx
movsd 0x20(%r15), %xmm0
jmp 0x11b19b
movq 0x48(%rsp), %rbx
movq (%rbx), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
leaq 0x70(%rsp), %r12
movq %r12, %rdi
callq 0x88a31
movq (%rbx), %rcx
movq 0x8(%rbx), %r8
leaq 0x138f3b(%rip), %rax    # 0x254230
movq 0x28(%rax), %r9
pushq $0x1
popq %rax
movl %eax, 0x28(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl %eax, (%rsp)
andl $0x0, 0x30(%rsp)
andq $0x0, 0x8(%rsp)
xorl %ebx, %ebx
movsd 0xc81da(%rip), %xmm0    # 0x1e3500
xorl %edi, %edi
xorl %esi, %esi
movq %r12, %rdx
callq 0x12b808
movq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %r13
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq (%rdi), %r15
jae 0x11b3bb
movq 0x10(%rdi), %rax
movb 0x28(%rax,%rbx), %cl
testb %cl, %cl
js 0x11b3ab
andb $0x3f, %cl
cmpb $0x2, %cl
jne 0x11b382
movl 0x8(%rax,%rbx), %esi
movl 0x14(%rax,%rbx), %edx
movq %r12, %rdi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x8feef
cmpq $0x0, 0x50(%rsp)
je 0x11b3b4
movq 0x38(%rsp), %rdi
movq 0x10(%rdi), %rax
imulq $0x30, %r14, %rcx
incq %r14
movupd (%rax,%rbx), %xmm0
movupd 0x10(%rax,%rbx), %xmm1
movupd 0x20(%rax,%rbx), %xmm2
movupd %xmm2, 0x20(%rax,%rcx)
movupd %xmm1, 0x10(%rax,%rcx)
movupd %xmm0, (%rax,%rcx)
incq %r15
addq $0x30, %rbx
jmp 0x11b342
movq 0x38(%rsp), %rdi
jmp 0x11b3ab
movq %r14, (%rdi)
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%rax), %esi
callq 0x88a31
movq 0x40(%rsp), %rbx
movq 0x28(%rbx), %rdi
callq 0x2220
xorpd %xmm0, %xmm0
movq 0xa0(%rsp), %rax
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x40(%rbx), %rdi
callq 0x2220
xorpd %xmm0, %xmm0
movupd %xmm0, 0x30(%rbx)
andq $0x0, 0x40(%rbx)
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x98(%rsp), %rdi
callq 0x2220
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq