// File: /workspace/github_cpp_makefile_star2_O3/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, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, 0x10(%rsp)
movl 0x14(%rdi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x8(%rsp)
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x144496
xorl %edx, %edx
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %r14, 0x30(%rsp)
movq 0x28(%r14), %rcx
movq (%rcx,%rdx,8), %rbx
testl %ebx, %ebx
je 0x14448b
movq %rdx, 0x38(%rsp)
movq %rbx, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %r12
shlq $0x4, %r12
addq 0x10(%r14), %r12
xorl %edx, %edx
movl $0x1, %r14d
movl %r14d, %ecx
cmpl %ebx, %r14d
jne 0x1442d4
movl %edx, %eax
jmp 0x1442f6
leaq (%rcx,%rcx,2), %rax
shlq $0x4, %rax
movl 0x14(%r12,%rax), %esi
movl %edx, %eax
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
cmpl 0x14(%r12,%rdi), %esi
je 0x144471
leaq (%rax,%rax,2), %rsi
shlq $0x4, %rsi
leaq (%r12,%rsi), %rdi
cmpl %r14d, %edx
jae 0x14432d
addq %r12, %rsi
subq %rax, %rcx
movsd 0x20(%rsi), %xmm0
ucomisd 0x20(%rdi), %xmm0
movq %rsi, %rax
ja 0x14431f
movq %rdi, %rax
addq $0x30, %rsi
movq %rax, %rdi
decq %rcx
jne 0x14430d
jmp 0x144330
movq %rdi, %rax
movzbl 0x28(%rax), %r15d
movl %r15d, %ecx
andb $0x3f, %cl
movl %r14d, %edx
cmpb $0x2, %cl
jne 0x144471
shrl $0x6, %r15d
movl 0x14(%rax), %r13d
andl $0x1, %r15d
je 0x144377
movq 0x10(%rsp), %rcx
movq 0x20(%rcx), %rdx
movl (%rdx,%r13,8), %r9d
andl %r10d, %r9d
movl %r9d, %ecx
subl 0x10(%rax), %ecx
movl 0xc(%rax), %r8d
notl %r8d
addl %r9d, %r8d
jmp 0x144391
movl 0xc(%rax), %ecx
movl 0x10(%rax), %r8d
decl %r8d
movq 0x10(%rsp), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%r13,8), %r9d
andl %r10d, %r9d
movl 0x8(%rax), %ebp
movl (%rax), %edi
movl 0x4(%rax), %eax
decl %eax
movl %eax, %esi
movl (%rdx,%rbp,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
movl %r8d, %r8d
movl %r9d, %r9d
leaq 0x18(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x60(%rsp), %rax
pushq %rax
callq 0x10a6e3
addq $0x20, %rsp
testb %r15b, %r15b
je 0x1443fd
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rcx
movl (%rcx,%r13,8), %eax
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %eax
movl 0x18(%rsp), %r9d
notl %r9d
addl %eax, %r9d
movl %eax, %r10d
subl 0x20(%rsp), %r10d
jmp 0x14441e
movl 0x20(%rsp), %r9d
movl 0x18(%rsp), %r10d
incl %r10d
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rcx
movl (%rcx,%r13,8), %eax
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %eax
movl 0x40(%rsp), %edx
incl %edx
movl 0x48(%rsp), %esi
movl (%rcx,%rbp,8), %ecx
andl %edi, %ecx
movl %ebp, %edi
movl %r13d, %r8d
leaq 0x70(%rsp), %r11
pushq %r11
pushq %r15
pushq %rax
pushq %r10
callq 0x14327e
addq $0x20, %rsp
cmpl $-0x2, %eax
je 0x14445e
movl %r14d, %edx
cmpl $-0x3, %eax
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jne 0x144471
movb $0x2, %al
jmp 0x144466
movb $0x1, %al
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rcx
orb %al, (%rcx,%rbp)
movl %r14d, %edx
incl %r14d
cmpl %ebx, %r14d
jbe 0x1442c8
movq 0x30(%rsp), %r14
movq 0x18(%r14), %rax
movq 0x38(%rsp), %rdx
incl %edx
cmpq %rdx, %rax
ja 0x144298
xorpd %xmm0, %xmm0
leaq 0x50(%rsp), %r8
movapd %xmm0, (%r8)
movq $0x0, 0x10(%r8)
movq %r14, %rdi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0x143913
testl %eax, %eax
je 0x14452d
leaq 0x50(%rsp), %r13
cmpq $0x0, 0x50(%rsp)
je 0x1444f5
movq %r14, %rdi
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %r13, %r8
callq 0x143913
testl %eax, %eax
jne 0x1444cf
jmp 0x14452d
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x1444d7
movl $0x1, %ecx
xorl %edx, %edx
movq 0x8(%rsp), %rsi
movb (%rsi,%rdx), %sil
notb %sil
testb $0x3, %sil
je 0x144522
movl %ecx, %edx
incl %ecx
cmpq %rdx, %rax
ja 0x144505
jmp 0x1444d7
movq 0x8(%rsp), %rax
movb $0x2, (%rax,%rdx)
jmp 0x1444d7
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq