// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: update_containment_distance(asg_t*, kv_u_trans_t*, hc_links*)
void update_containment_distance(asg_t *sg, kv_u_trans_t *ta, hc_links* link)
{
    uint8_t *uc_idx = NULL;
    CALLOC(uc_idx, sg->n_seq);

    u_trans_t *a = NULL, *p = NULL;
    uint32_t i, k, st, n, m;
    uint32_t qn, tn, qs, qe, ts, te, rev;
    asg_arc_t t;
    long long r_qs, r_qe, r_ts, r_te;
    int r;

    for (k = 0; k < ta->idx.n; k++)
    {
        a = u_trans_a(*ta, k);
        n = u_trans_n(*ta, k);
        for (st = 0, i = 1; i <= n; ++i)
        {
            if (i == n || a[i].tn != a[st].tn)
            {
                for (m = st, p = &(a[st]); m < i; m++)
                {
                    if(a[m].nw > p->nw) p = &(a[m]);
                }

                if(p->f == RC_2)///dis-connected
                {
                    rev = p->rev;
                    qn = p->qn; 
                    qs = p->qs; 
                    qe = p->qe - 1;
                    if(rev)
                    {
                        tn = p->tn;
                        ts = sg->seq[tn].len - (p->te - 1) - 1;
                        te = sg->seq[tn].len - p->ts - 1;
                    }
                    else
                    {
                        tn = p->tn; 
                        ts = p->ts; 
                        te = p->te - 1;
                    }

                    classify_hap_overlap(qs, qe, sg->seq[qn].len, ts, te, sg->seq[tn].len, 
                                                                            &r_qs, &r_qe, &r_ts, &r_te);
                    
                    qs = r_qs; qe = r_qe + 1;
                    if(rev)
                    {
                        ts = sg->seq[tn].len - r_te - 1;
                        te = sg->seq[tn].len - r_ts - 1 + 1;
                    }
                    else
                    {
                        ts = r_ts; te = r_te + 1;
                    }

                    r = get_trans_ug_arch(qn, qs, qe, sg->seq[qn].len, 
                                        tn, ts, te, sg->seq[tn].len, rev, &t);
                    if(r == MA_HT_QCONT) uc_idx[qn] |= 1;
                    else if(r == MA_HT_TCONT) uc_idx[qn] |= 2;

                    // if(r < 0) print_u_trans_t(p);
                }
                st = i;
            }
        }
    }

    kvec_t_u64_warp buf; kv_init(buf.a);
    while(up_contain(ta, link, uc_idx, sg, &buf))
    {
        if(buf.a.n != 0) continue;
        for (k = 0; k < ta->idx.n; k++)
        {
            if((uc_idx[k]&1) && (uc_idx[k]&2))
            {
                uc_idx[k] = 2;
                break;
            }
        }
    }

    kv_destroy(buf.a);
    free(uc_idx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, 0x30(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movl 0x14(%rdi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rbx, %r10
movq %rax, (%rsp)
cmpq $0x0, 0x18(%r14)
movq %rbx, 0x8(%rsp)
je 0x13e89a
xorl %ecx, %ecx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq %r14, 0x10(%rsp)
movq 0x28(%r14), %rax
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %r9
testl %r9d, %r9d
je 0x13e884
movq %r9, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %r12
shlq $0x4, %r12
movq 0x10(%rsp), %rax
addq 0x10(%rax), %r12
xorl %edx, %edx
movl $0x1, %r14d
movq %r9, 0x18(%rsp)
movl %r14d, %ecx
movl %edx, %eax
cmpl %r9d, %r14d
je 0x13e6fc
leaq (%rcx,%rcx,2), %rsi
shlq $0x4, %rsi
movl 0x14(%r12,%rsi), %esi
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
cmpl 0x14(%r12,%rdi), %esi
je 0x13e878
leaq (%rax,%rax,2), %rsi
shlq $0x4, %rsi
addq %r12, %rsi
cmpl %r14d, %edx
jae 0x13e73a
leaq (%rax,%rax,2), %rdx
shlq $0x4, %rdx
addq %r12, %rdx
subq %rax, %rcx
movsd 0x20(%rdx), %xmm0
ucomisd 0x20(%rsi), %xmm0
movq %rdx, %rax
ja 0x13e72c
movq %rsi, %rax
addq $0x30, %rdx
movq %rax, %rsi
decq %rcx
jne 0x13e71a
jmp 0x13e73d
movq %rsi, %rax
movzbl 0x28(%rax), %r15d
movl %r15d, %ecx
andb $0x3f, %cl
movl %r14d, %edx
cmpb $0x2, %cl
jne 0x13e878
shrl $0x6, %r15d
movl 0x14(%rax), %ebp
andl $0x1, %r15d
je 0x13e77a
movq 0x20(%r10), %rcx
movl (%rcx,%rbp,8), %ecx
andl %r8d, %ecx
movl 0xc(%rax), %r8d
notl %r8d
addl %ecx, %r8d
subl 0x10(%rax), %ecx
jmp 0x13e784
movl 0xc(%rax), %ecx
movl 0x10(%rax), %r8d
decl %r8d
movl 0x8(%rax), %r13d
movl (%rax), %edi
movl 0x4(%rax), %eax
decl %eax
movl %eax, %esi
movq 0x20(%r10), %rax
movl (%rax,%r13,8), %edx
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andq %r11, %rdx
movl %ecx, %ecx
movl %r8d, %r8d
movl (%rax,%rbp,8), %r9d
andq %r11, %r9
leaq 0x20(%rsp), %rax
pushq %rax
leaq 0x30(%rsp), %rax
pushq %rax
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x60(%rsp), %rax
pushq %rax
movq %r10, %rbx
callq 0x105f44
addq $0x20, %rsp
testb %r15b, %r15b
je 0x13e7f8
movq 0x20(%rbx), %rax
movl (%rax,%rbp,8), %eax
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %eax
movl 0x20(%rsp), %r9d
notl %r9d
addl %eax, %r9d
subl 0x28(%rsp), %eax
jmp 0x13e809
movl 0x28(%rsp), %r9d
movl 0x20(%rsp), %eax
incl %eax
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl 0x40(%rsp), %edx
incl %edx
movl 0x48(%rsp), %esi
movq 0x20(%rbx), %rdi
movl (%rdi,%r13,8), %ecx
andl %r8d, %ecx
movl (%rdi,%rbp,8), %r10d
andl %r8d, %r10d
movl %r13d, %edi
movl %ebp, %r8d
leaq 0x70(%rsp), %r11
pushq %r11
pushq %r15
pushq %r10
pushq %rax
callq 0x13d427
addq $0x20, %rsp
cmpl $-0x2, %eax
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
je 0x13e861
movl %r14d, %edx
cmpl $-0x3, %eax
movq 0x8(%rsp), %r10
movq 0x18(%rsp), %r9
jne 0x13e878
movb $0x2, %al
jmp 0x13e86d
movb $0x1, %al
movq 0x8(%rsp), %r10
movq 0x18(%rsp), %r9
movq (%rsp), %rcx
orb %al, (%rcx,%r13)
movl %r14d, %edx
incl %r14d
cmpl %r9d, %r14d
jbe 0x13e6d2
movq 0x38(%rsp), %rcx
incl %ecx
movq 0x10(%rsp), %r14
cmpq %rcx, 0x18(%r14)
ja 0x13e697
xorpd %xmm0, %xmm0
leaq 0x50(%rsp), %r8
movapd %xmm0, (%r8)
movq $0x0, 0x10(%r8)
movq %r14, %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
movq (%rsp), %rdx
movq %r10, %rcx
callq 0x13daab
testl %eax, %eax
je 0x13e92b
leaq 0x50(%rsp), %r13
cmpq $0x0, 0x50(%rsp)
movq 0x8(%rsp), %rcx
je 0x13e8f5
movq %r14, %rdi
movq %rbx, %rsi
movq (%rsp), %rdx
movq %r13, %r8
callq 0x13daab
testl %eax, %eax
jne 0x13e8d0
jmp 0x13e92b
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x13e8dd
movl $0x1, %edi
xorl %edx, %edx
movq (%rsp), %rsi
movb (%rsi,%rdx), %sil
notb %sil
testb $0x3, %sil
je 0x13e921
movl %edi, %edx
incl %edi
cmpq %rdx, %rax
ja 0x13e905
jmp 0x13e8dd
movq (%rsp), %rax
movb $0x2, (%rax,%rdx)
jmp 0x13e8dd
movq 0x60(%rsp), %rdi
callq 0x2220
movq (%rsp), %rdi
callq 0x2220
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq