// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x208063
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1d2bc0
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x208418
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x30, -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x38(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jbe 0x2080e5
jmp 0x208407
movq -0x38(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x2, %eax
jne 0x208104
movq -0x38(%rbp), %rax
movb 0x28(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x208109
jmp 0x208407
movq -0x38(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x38(%rbp), %rax
movl 0x14(%rax), %esi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x207f50
movl %eax, -0x74(%rbp)
movq -0x38(%rbp), %rax
movl 0x14(%rax), %edi
movq -0x38(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x207f50
movl %eax, %ecx
movl -0x74(%rbp), %eax
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x30(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x208192
cmpq $0x0, -0x68(%rbp)
je 0x20816d
movq -0x68(%rbp), %rax
shlq %rax
movq %rax, -0x80(%rbp)
jmp 0x208178
movl $0x2, %eax
movq %rax, -0x80(%rbp)
jmp 0x208178
movq -0x80(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
imulq $0x30, -0x68(%rbp), %rsi
callq 0x2360
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x70(%rbp)
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq -0x38(%rbp), %rdx
movsd 0x20(%rdx), %xmm0
movq -0x30(%rbp), %rax
addq %rax, %rax
movq %rax, %xmm2
movaps 0x179e26(%rip), %xmm4   # 0x382010
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
movapd 0x179e2a(%rip), %xmm3   # 0x382020
subpd %xmm3, %xmm2
movaps %xmm2, %xmm1
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
addsd %xmm1, %xmm2
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rcx
movl 0x8(%rdx), %eax
movl 0x14(%rdx), %edx
movq (%rcx,%rax,8), %rax
movq (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq %rax, %xmm1
punpckldq %xmm4, %xmm1    # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
subpd %xmm3, %xmm1
movaps %xmm1, %xmm3
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm3, %xmm1
divsd %xmm1, %xmm2
movsd 0x179dbe(%rip), %xmm1   # 0x382000
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movq -0x40(%rbp), %rax
xorps %xmm0, %xmm0
ucomisd 0x20(%rax), %xmm0
jbe 0x208279
movq 0x1bbd78(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1851fa(%rip), %rsi    # 0x38d46c
movb $0x0, %al
callq 0x22a0
movq -0x40(%rbp), %rax
movsd 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2082a7
jp 0x2082a7
movq -0x38(%rbp), %rax
movsd 0x183c0f(%rip), %xmm0   # 0x38bea8
mulsd 0x20(%rax), %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movq -0x40(%rbp), %rax
movsd 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2082cc
jp 0x2082cc
movq -0x70(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x208405
jmp 0x2082ce
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x20831a
cmpq $0x0, -0x68(%rbp)
je 0x2082ef
movq -0x68(%rbp), %rax
shlq %rax
movq %rax, -0x88(%rbp)
jmp 0x2082fd
movl $0x2, %eax
movq %rax, -0x88(%rbp)
jmp 0x2082fd
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
imulq $0x30, -0x68(%rbp), %rsi
callq 0x2360
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x70(%rbp)
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rax
subq $0x2, %rax
imulq $0x30, %rax, %rax
addq %rax, %rsi
movq -0x40(%rbp), %rdi
movl $0x30, %edx
callq 0x2180
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x2, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x2, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x2, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x2, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x2, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
subq $0x2, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x10(%rax)
jmp 0x208407
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x2080ab
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leaq -0x70(%rbp), %rdi
callq 0x104540
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %r8
leaq 0x1bce85(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %r9
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdx
movsd 0x179dd1(%rip), %xmm0   # 0x382230
xorl %eax, %eax
movq %rsi, %rdi
movl $0x1, (%rsp)
movq $0x0, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x1, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
callq 0x227770
movq $0x0, -0x28(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x20856d
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x30, -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movb 0x28(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x2084e8
jmp 0x20855c
movq -0x38(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x2, %eax
jne 0x208524
movq -0x38(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x38(%rbp), %rax
movl 0x14(%rax), %edx
leaq -0x70(%rbp), %rdi
leaq -0x40(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x110ee0
cmpq $0x0, -0x40(%rbp)
jne 0x208522
jmp 0x20855c
jmp 0x208524
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rsi
imulq $0x30, -0x20(%rbp), %rax
addq %rax, %rsi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rbp)
imulq $0x30, %rax, %rax
addq %rax, %rdi
movl $0x30, %edx
callq 0x2180
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x2084b3
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0x104540
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x2210
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x60(%rbp), %rdi
callq 0x2210
movq -0x48(%rbp), %rdi
callq 0x2210
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)