// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xe8, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x1d0f24
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x40(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x40(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x48(%rbp), %eax
ja 0x1d0f14
movl -0x3c(%rbp), %eax
cmpl -0x48(%rbp), %eax
je 0x1d0c48
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq -0x30(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x14(%rcx), %eax
je 0x1d0f04
movl -0x44(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x1d0ca5
movq -0x30(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movsd 0x20(%rax), %xmm0
movq -0x38(%rbp), %rax
ucomisd 0x20(%rax), %xmm0
jbe 0x1d0c98
movq -0x30(%rbp), %rax
movl -0x4c(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0x1d0c9a
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1d0c60
movq -0x38(%rbp), %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x2, %eax
jne 0x1d0efe
movq -0x38(%rbp), %rax
movb 0x28(%rax), %al
shrb $0x6, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x1d0d43
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
subl %ecx, %eax
subl $0x1, %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x38(%rbp), %rcx
subl 0xc(%rcx), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1d0d64
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %edi
movl -0x5c(%rbp), %eax
movl %eax, %esi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movl -0x60(%rbp), %eax
movl %eax, %ecx
movl -0x64(%rbp), %eax
movl %eax, %r8d
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %r9d
movl (%rax,%r9,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r9d
leaq -0x88(%rbp), %rbx
leaq -0x90(%rbp), %r11
leaq -0x98(%rbp), %r10
leaq -0xa0(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x170800
movq -0x88(%rbp), %rax
movl %eax, -0x58(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x1d0e48
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0xa0(%rbp), %rax
subq $0x1, %rax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x98(%rbp), %rax
subq $0x1, %rax
addq $0x1, %rax
movl %eax, -0x64(%rbp)
jmp 0x1d0e60
movq -0x98(%rbp), %rax
movl %eax, -0x60(%rbp)
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movl %eax, -0x64(%rbp)
movl -0x50(%rbp), %edi
movl -0x58(%rbp), %esi
movl -0x5c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x50(%rbp), %ecx
movl (%rax,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl -0x54(%rbp), %r8d
movl -0x60(%rbp), %r9d
movl -0x64(%rbp), %ebx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %r10d
movl (%rax,%r10,8), %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl -0x68(%rbp), %r10d
leaq -0x80(%rbp), %rax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x1ce710
movl %eax, -0xa4(%rbp)
cmpl $-0x2, -0xa4(%rbp)
jne 0x1d0ee0
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
movzbl (%rax,%rcx), %edx
orl $0x1, %edx
movb %dl, (%rax,%rcx)
jmp 0x1d0efc
cmpl $-0x3, -0xa4(%rbp)
jne 0x1d0efa
movq -0x28(%rbp), %rax
movl -0x50(%rbp), %ecx
movzbl (%rax,%rcx), %edx
orl $0x2, %edx
movb %dl, (%rax,%rcx)
jmp 0x1d0efc
jmp 0x1d0efe
movl -0x3c(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x1d0f06
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1d0c0c
jmp 0x1d0f16
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1d0bb5
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x10(%rbp), %rcx
leaq -0xc8(%rbp), %r8
callq 0x1cf8e0
cmpl $0x0, %eax
je 0x1d0fcb
cmpq $0x0, -0xc8(%rbp)
je 0x1d0f72
jmp 0x1d0f45
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x1d0fc6
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
movzbl (%rax,%rcx), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x1d0fb9
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
movzbl (%rax,%rcx), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1d0fb9
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
movb $0x2, (%rax,%rcx)
jmp 0x1d0fc6
jmp 0x1d0fbb
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1d0f79
jmp 0x1d0f45
movq -0xb8(%rbp), %rdi
callq 0x2210
movq -0x28(%rbp), %rdi
callq 0x2210
addq $0xe8, %rsp
popq %rbx
popq %rbp
retq
nopw (%rax,%rax)