// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movl 0x14(%rdi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq %r14, 0x28(%rsp)
movl %ecx, %ecx
cmpq %rcx, 0x18(%r14)
jbe 0xfcd6f
movq 0x28(%r14), %rax
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %rbx
movq %rbx, %rax
shrq $0x20, %rax
imulq $0x30, %rax, %r12
addq 0x10(%r14), %r12
xorl %esi, %esi
pushq $0x1
popq %r14
cmpl %ebx, %r14d
ja 0xfcd5e
movl %esi, %ecx
movl %r14d, %edx
je 0xfcbe7
imulq $0x30, %rdx, %rax
movl 0x14(%r12,%rax), %eax
imulq $0x30, %rcx, %rdi
cmpl 0x14(%r12,%rdi), %eax
je 0xfcd56
imulq $0x30, %rcx, %rsi
leaq (%r12,%rsi), %rax
addq %r12, %rsi
cmpq %rdx, %rcx
jae 0xfcc15
movsd 0x20(%rsi), %xmm0
ucomisd 0x20(%rax), %xmm0
movq %rsi, %rdi
ja 0xfcc09
movq %rax, %rdi
incq %rcx
addq $0x30, %rsi
movq %rdi, %rax
jmp 0xfcbf2
movzbl 0x28(%rax), %r15d
movl %r15d, %ecx
andb $0x3f, %cl
movl %r14d, %esi
cmpb $0x2, %cl
jne 0xfcd56
shrl $0x6, %r15d
movl 0x14(%rax), %r13d
andl $0x1, %r15d
je 0xfcc5c
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 0xfcc76
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 0xc7ff2
addq $0x20, %rsp
testb %r15b, %r15b
je 0xfcce2
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 0xfcd03
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 0xfbbfa
addq $0x20, %rsp
cmpl $-0x2, %eax
je 0xfcd43
movl %r14d, %esi
cmpl $-0x3, %eax
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jne 0xfcd56
movb $0x2, %al
jmp 0xfcd4b
movb $0x1, %al
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rcx
orb %al, (%rcx,%rbp)
movl %r14d, %esi
incl %r14d
jmp 0xfcbbf
movq 0x38(%rsp), %rcx
incl %ecx
movq 0x28(%rsp), %r14
jmp 0xfcb91
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r13
movaps %xmm0, (%r13)
andq $0x0, 0x10(%r13)
movq %r14, %rdi
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %r13, %r8
callq 0xfc26b
testl %eax, %eax
je 0xfcdd4
cmpq $0x0, 0x50(%rsp)
jne 0xfcd81
movq 0x18(%r14), %rax
xorl %edx, %edx
movl %edx, %ecx
cmpq %rcx, %rax
jbe 0xfcd81
movq 0x8(%rsp), %rdx
movb (%rdx,%rcx), %sil
notb %sil
leal 0x1(%rcx), %edx
testb $0x3, %sil
jne 0xfcdad
movq 0x8(%rsp), %rax
movb $0x2, (%rax,%rcx)
jmp 0xfcd81
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