// File: chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_base_boundary_chain(R_to_U*, ma_hit_t_alloc*, ma_sub_t*, asg_t*, unsigned long*, int, int, ma_utg_t*, ma_utg_t*, unsigned int, unsigned int, long long, long long, long long, long long, unsigned int, kvec_asg_arc_t_offset*, kvec_t_i32_warp*, kvec_t_i32_warp*)
int get_base_boundary_chain(R_to_U* ruIndex, ma_hit_t_alloc* reverse_sources, ma_sub_t *coverage_cut, 
asg_t *read_g, uint64_t* position_index, int max_hang, int min_ovlp, ma_utg_t *xReads, ma_utg_t *yReads,
uint32_t xUid, uint32_t yUid, long long xBegIndex, long long xEndIndex, long long yBegIndex, long long yEndIndex, 
uint32_t rev, kvec_asg_arc_t_offset* u_buffer, kvec_t_i32_warp* tailIndex, kvec_t_i32_warp* prevIndex)
{
    long long k, j, l, offset, m;
    ma_hit_t_alloc *xR = NULL;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL, *st = NULL;
    int32_t r;
    asg_arc_t t;
    uint32_t rId, Hap_uId, is_Unitig, v, w, v_dir, w_dir;
    uint64_t tmp;
    asg_arc_t_offset t_offset;
    u_buffer->a.n = 0;
    for (k = xBegIndex; k <= xEndIndex; k++)
    {
        xR = &(reverse_sources[xReads->a[k]>>33]);
        for (j = 0; j < xR->length; j++)
        {
            h = &(xR->buffer[j]);
            sq = &(coverage_cut[Get_qn(*h)]);
            st = &(coverage_cut[Get_tn(*h)]);
            if(st->del || read_g->seq[Get_tn(*h)].del) continue;

            r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, 
                            asm_opt.max_hang_rate, min_ovlp, &t);
            ///if it is a contained overlap, skip
            if(r < 0) continue;

            rId = t.v>>1;
            if(read_g->seq[rId].del == 1) continue;
            ///there are two cases: 
            ///1. read at primary contigs, get_R_to_U() return its corresponding contig Id  
            ///2. read at alternative contigs, get_R_to_U() return (uint32_t)-1
            get_R_to_U(ruIndex, rId, &Hap_uId, &is_Unitig);
            if(is_Unitig == 0 || Hap_uId == (uint32_t)-1) continue;
            if(Hap_uId != yUid) continue;

            v = xReads->a[k]>>32;
            get_R_to_U(ruIndex, v>>1, &Hap_uId, &is_Unitig);
            if(is_Unitig == 0 || Hap_uId == (uint32_t)-1) continue;
            if(Hap_uId != xUid) continue;
            if((uint32_t)(position_index[v>>1]) != k) continue;

            w = (yReads->a[(uint32_t)(position_index[rId])])>>32;

            v_dir = ((t.ul>>32)==v)?1:0;
            w_dir = (t.v == w)?1:0;
            if(rev == 0 && v_dir != w_dir) continue;
            if(rev == 1 && v_dir == w_dir) continue;
        
            /****************************may have bugs********************************/
            offset = (uint32_t)(position_index[rId]);
            if(offset < yBegIndex || offset > yEndIndex) continue;
            /****************************may have bugs********************************/

            tmp = get_xy_pos(read_g, &t, v, w, xReads->len, yReads->len, position_index, &(t.el));
            if(((tmp>>32) == (uint32_t)-1) || (((uint32_t)tmp) == (uint32_t)-1)) continue;

            t_offset.Off = tmp;
            t_offset.x = t;
            t_offset.weight = 1;
            kv_push(asg_arc_t_offset, u_buffer->a, t_offset);      
        }
    }
    if(u_buffer->a.n == 0) return 0;

    qsort(u_buffer->a.a, u_buffer->a.n, sizeof(asg_arc_t_offset), cmp_hap_alignment_chaining);

    ///print_asg_arc_t_offset(u_buffer->a.a, u_buffer->a.n, "before");
    for (k = 1, l = 0, m = 0; k <= (long long)u_buffer->a.n; ++k)
    {
        if (k == (long long)u_buffer->a.n || u_buffer->a.a[k].Off != u_buffer->a.a[l].Off) 
        {
            u_buffer->a.a[m] = u_buffer->a.a[l];
            for (l += 1; l < k; l++)
            {
                u_buffer->a.a[m].weight += u_buffer->a.a[l].weight;
                if(u_buffer->a.a[l].x.ol > u_buffer->a.a[m].x.ol)
                {
                    u_buffer->a.a[m].x = u_buffer->a.a[l].x;
                }
            }
            l = k;
            m++;
        }
    } 
    u_buffer->a.n = m;

    ///print_asg_arc_t_offset(u_buffer->a.a, u_buffer->a.n, "after");
    quick_LIS(u_buffer->a.a, u_buffer->a.n, tailIndex, prevIndex);

    if(tailIndex->a.n == 0) return 0;
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq 0x70(%rbp), %rax
movq 0x68(%rbp), %rax
movq 0x60(%rbp), %rax
movl 0x58(%rbp), %eax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movl 0x30(%rbp), %eax
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq 0x60(%rbp), %rax
movq $0x0, (%rax)
movq 0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq 0x40(%rbp), %rax
jg 0x175c24
movq -0x18(%rbp), %rax
movq 0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x68(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0x175c11
movq -0x68(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x48(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq -0x70(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x175880
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x70(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x175885
jmp 0x175c00
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x78(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %esi
movq -0x80(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x80(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
movl -0x34(%rbp), %ecx
leaq 0x24ef3d(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl 0x10(%rbp), %r8d
leaq -0xa0(%rbp), %r9
callq 0x172eb0
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jge 0x1758e7
jmp 0x175c00
movl -0x98(%rbp), %eax
shrl %eax
movl %eax, -0xa4(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl -0xa4(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x175913
jmp 0x175c00
movq -0x10(%rbp), %rdi
movl -0xa4(%rbp), %esi
leaq -0xa8(%rbp), %rdx
leaq -0xac(%rbp), %rcx
callq 0xd3d50
cmpl $0x0, -0xac(%rbp)
je 0x175942
cmpl $-0x1, -0xa8(%rbp)
jne 0x175947
jmp 0x175c00
movl -0xa8(%rbp), %eax
cmpl 0x30(%rbp), %eax
je 0x175957
jmp 0x175c00
movq 0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
movl -0xb0(%rbp), %esi
shrl %esi
leaq -0xa8(%rbp), %rdx
leaq -0xac(%rbp), %rcx
callq 0xd3d50
cmpl $0x0, -0xac(%rbp)
je 0x1759a2
cmpl $-0x1, -0xa8(%rbp)
jne 0x1759a7
jmp 0x175c00
movl -0xa8(%rbp), %eax
cmpl 0x28(%rbp), %eax
je 0x1759b7
jmp 0x175c00
movq -0x30(%rbp), %rax
movl -0xb0(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
cmpq -0x40(%rbp), %rax
je 0x1759d6
jmp 0x175c00
movq 0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
movl -0xa4(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0xb4(%rbp)
movq -0xa0(%rbp), %rdx
shrq $0x20, %rdx
movl -0xb0(%rbp), %eax
movl %eax, %esi
xorl %eax, %eax
movl $0x1, %ecx
cmpq %rsi, %rdx
cmovel %ecx, %eax
movl %eax, -0xb8(%rbp)
movl -0x98(%rbp), %edx
movl -0xb4(%rbp), %esi
xorl %eax, %eax
movl $0x1, %ecx
cmpl %esi, %edx
cmovel %ecx, %eax
movl %eax, -0xbc(%rbp)
cmpl $0x0, 0x58(%rbp)
jne 0x175a59
movl -0xb8(%rbp), %eax
cmpl -0xbc(%rbp), %eax
je 0x175a59
jmp 0x175c00
cmpl $0x1, 0x58(%rbp)
jne 0x175a72
movl -0xb8(%rbp), %eax
cmpl -0xbc(%rbp), %eax
jne 0x175a72
jmp 0x175c00
movq -0x30(%rbp), %rax
movl -0xa4(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq 0x48(%rbp), %rax
jl 0x175a9a
movq -0x58(%rbp), %rax
cmpq 0x50(%rbp), %rax
jle 0x175a9f
jmp 0x175c00
movq -0x28(%rbp), %rdi
movl -0xb0(%rbp), %edx
movl -0xb4(%rbp), %ecx
movq 0x18(%rbp), %rax
movl (%rax), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x20(%rbp), %rax
movl (%rax), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq -0x30(%rbp), %r10
leaq -0xa0(%rbp), %rax
addq $0x12, %rax
leaq -0xa0(%rbp), %rsi
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1859f0
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
shrq $0x20, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x175b17
movq -0xc8(%rbp), %rax
cmpl $-0x1, %eax
jne 0x175b1c
jmp 0x175c00
movq -0xc8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq $0x1, -0xd0(%rbp)
movq 0x60(%rbp), %rax
movq (%rax), %rax
movq 0x60(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x175bcd
movq 0x60(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x175b8f
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xf8(%rbp)
jmp 0x175b9d
movl $0x2, %eax
movq %rax, -0xf8(%rbp)
jmp 0x175b9d
movq -0xf8(%rbp), %rcx
movq 0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rax
imulq $0x28, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq -0xf0(%rbp), %rsi
movl $0x28, %edx
callq 0x2180
jmp 0x175c00
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1757ff
jmp 0x175c13
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1757c6
movq 0x60(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x175c3a
movl $0x0, -0x4(%rbp)
jmp 0x175e16
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rax
movq (%rax), %rsi
movl $0x28, %edx
leaq -0x55b5(%rip), %rcx     # 0x1706a0
callq 0x23e0
movq $0x1, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq 0x60(%rbp), %rcx
cmpq (%rcx), %rax
jg 0x175dd3
movq -0x40(%rbp), %rax
movq 0x60(%rbp), %rcx
cmpq (%rcx), %rax
je 0x175cbe
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq 0x60(%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, -0x50(%rbp), %rdx
addq %rdx, %rcx
cmpq 0x18(%rcx), %rax
je 0x175dc0
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rsi
imulq $0x28, -0x50(%rbp), %rax
addq %rax, %rsi
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
imulq $0x28, -0x60(%rbp), %rax
addq %rax, %rdi
movl $0x28, %edx
callq 0x2180
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x175dac
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x50(%rbp), %rcx
addq %rcx, %rax
movq 0x20(%rax), %rcx
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x60(%rbp), %rdx
addq %rdx, %rax
addq 0x20(%rax), %rcx
movq %rcx, 0x20(%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x50(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x60(%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, -0x60(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jle 0x175d99
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rcx
imulq $0x28, -0x50(%rbp), %rax
addq %rax, %rcx
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x60(%rbp), %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x175d9b
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x175cf4
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x175dc2
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x175c72
movq -0x60(%rbp), %rcx
movq 0x60(%rbp), %rax
movq %rcx, (%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rax
movq (%rax), %rax
movl %eax, %esi
movq 0x68(%rbp), %rdx
movq 0x70(%rbp), %rcx
callq 0x1732b0
movq 0x68(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x175e0f
movl $0x0, -0x4(%rbp)
jmp 0x175e16
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)