// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, 0x34(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x40(%rsp)
movq 0xf0(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq $0x0, (%rax)
cmpq 0xd0(%rsp), %rcx
jg 0x109afa
movq %rdx, %r12
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x38(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0x109ae0
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax,8), %rbx
xorl %ebp, %ebp
xorl %r15d, %r15d
movq (%rbx), %rdi
movl 0xc(%rdi,%rbp), %edx
leaq (%rdx,%rdx,2), %rcx
movl (%r12,%rcx,4), %eax
testl %eax, %eax
js 0x109a77
movq 0x28(%rsp), %rsi
movq 0x20(%rsi), %r14
cmpl $0x0, (%r14,%rdx,8)
js 0x109a77
movl 0x4(%rdi,%rbp), %edx
addq %rbp, %rdi
leaq (%rdx,%rdx,2), %rdx
leaq (%r12,%rdx,4), %rdx
leaq (%r12,%rcx,4), %rcx
movl 0x4(%rdx), %esi
movl (%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x180fbf(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0x34(%rsp), %ecx
movl 0xa0(%rsp), %r8d
leaq 0x48(%rsp), %r9
callq 0x107fb9
testl %eax, %eax
js 0x109a77
movl 0x50(%rsp), %eax
movl %eax, 0x18(%rsp)
movl %eax, %r13d
shrl %r13d
cmpl $0x0, (%r14,%r13,8)
js 0x109a77
movq 0x40(%rsp), %rdi
movl %r13d, %esi
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
callq 0xa306f
cmpl $0x0, (%rsp)
je 0x109a77
movl 0x4(%rsp), %eax
cmpl $-0x1, %eax
je 0x109a77
cmpl 0xc0(%rsp), %eax
jne 0x109a77
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %r14
movq %r14, 0x8(%rsp)
shrq $0x21, %r14
movq 0x40(%rsp), %rdi
movl %r14d, %esi
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
callq 0xa306f
cmpl $0x0, (%rsp)
je 0x109a77
movl 0x4(%rsp), %eax
cmpl $-0x1, %eax
je 0x109a77
cmpl 0xb8(%rsp), %eax
jne 0x109a77
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %r9
movl %r9d, %eax
cmpq 0x20(%rsp), %rax
jne 0x109a77
movq 0x8(%rsp), %rdi
shrq $0x20, %rdi
movl 0xe8(%rsp), %eax
cmpl $0x1, %eax
setne %dl
testl %eax, %eax
setne %sil
movq 0xb0(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rsp), %rcx
movl (%rcx,%r13,8), %ecx
movq (%rax,%rcx,8), %r10
movq %r10, %r13
shrq $0x20, %r13
movq 0x48(%rsp), %rax
shrq $0x20, %rax
movq %rdi, %r11
cmpq %rdi, %rax
sete %dil
cmpl %r13d, 0x18(%rsp)
sete %r8b
xorb %dil, %r8b
orb %r8b, %dl
notb %r8b
orb %sil, %r8b
andb %dl, %r8b
testb $0x1, %r8b
je 0x109a77
cmpq 0xd8(%rsp), %rcx
setl %dl
cmpq 0xe0(%rsp), %rcx
setg %cl
orb %dl, %cl
jne 0x109a77
movq 0xa8(%rsp), %rcx
movl (%rcx), %edi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %edi
movq 0xb0(%rsp), %rcx
movl (%rcx), %esi
andl %edx, %esi
cmpl %r11d, %eax
jne 0x10994e
shrq $0x20, %r9
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%r14,8), %eax
andl %edx, %eax
addl %eax, %r9d
decl %r9d
xorl %r14d, %r14d
jmp 0x109961
xorl %r11d, %eax
cmpl $0x1, %eax
jne 0x109a8c
shrq $0x20, %r9
movb $0x1, %r14b
movl 0x18(%rsp), %eax
cmpl %r13d, %eax
jne 0x109986
shrq $0x21, %r10
movq 0x10(%rsp), %rax
movl 0x4(%rax,%r10,8), %eax
movl 0x54(%rsp), %ecx
andl %edx, %ecx
addl %ecx, %eax
decl %eax
xorl %ecx, %ecx
jmp 0x1099b9
xorl %r13d, %eax
cmpl $0x1, %eax
jne 0x109ab9
shrq $0x21, %r10
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%r10,8), %eax
andl %edx, %eax
movl 0x54(%rsp), %ecx
andl %edx, %ecx
movq 0x10(%rsp), %rdx
addl 0x4(%rdx,%r10,8), %eax
subl %ecx, %eax
movb $0x1, %cl
movl %ecx, %edx
xorb %r14b, %dl
movb %dl, 0x5a(%rsp)
cmpb %cl, %r14b
je 0x1099d4
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl %esi, %eax
jae 0x1099d6
notl %eax
addl %esi, %eax
movl %eax, %ecx
cmpl %edi, %r9d
movl %r9d, %r13d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovael %eax, %r13d
cmpl %esi, %ecx
movl %ecx, %r14d
cmovael %eax, %r14d
cmpl %edi, %r9d
jae 0x109a77
cmpl %esi, %ecx
jae 0x109a77
movq 0xf0(%rsp), %rsi
movq 0x8(%rsi), %rax
cmpq %rax, (%rsi)
jne 0x109a3d
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsi)
movq 0x10(%rsi), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x2370
movq 0xf0(%rsp), %rsi
movq %rax, 0x10(%rsi)
shlq $0x20, %r13
orq %r13, %r14
movq (%rsi), %rax
movq 0x10(%rsi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rsi)
leaq (%rax,%rax,4), %rax
movq 0x58(%rsp), %rdx
movq %rdx, 0x10(%rcx,%rax,8)
movups 0x48(%rsp), %xmm0
movups %xmm0, (%rcx,%rax,8)
movq %r14, 0x18(%rcx,%rax,8)
movq $0x1, 0x20(%rcx,%rax,8)
incq %r15
movl 0xc(%rbx), %eax
addq $0x28, %rbp
cmpq %rax, %r15
jb 0x10973f
jmp 0x109ae0
movl %esi, %r14d
movl %edi, 0x8(%rsp)
movq %r10, 0x60(%rsp)
callq 0x139eb
movq 0x60(%rsp), %r10
movl 0x8(%rsp), %edi
movl %r14d, %esi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
jmp 0x109949
movq %r9, 0x18(%rsp)
movl %esi, %r13d
movl %edi, 0x8(%rsp)
callq 0x13a0d
movl 0x8(%rsp), %edi
movl %r13d, %esi
movq 0x18(%rsp), %r9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x109982
movq 0x20(%rsp), %rcx
leaq 0x1(%rcx), %rax
cmpq 0xd0(%rsp), %rcx
movq %rax, %rcx
jne 0x109704
movq 0xf0(%rsp), %r15
movq (%r15), %rsi
testq %rsi, %rsi
je 0x109c0e
movq 0x100(%rsp), %r14
movq 0xf8(%rsp), %rbx
movq 0x10(%r15), %rdi
leaq -0x3c54(%rip), %rcx     # 0x105ed5
movl $0x28, %edx
callq 0x23f0
movq (%r15), %rdi
testq %rdi, %rdi
jle 0x109c12
movl $0x1, %eax
xorl %edx, %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
cmpq %rdi, %rax
je 0x109b6e
movq 0x10(%r15), %rdi
leaq (%rax,%rax,4), %r8
movq 0x18(%rdi,%r8,8), %r8
leaq (%rdx,%rdx,4), %r9
cmpq 0x18(%rdi,%r9,8), %r8
je 0x109bf9
movq 0x10(%r15), %r9
leaq (%rdx,%rdx,4), %r8
leaq (%rsi,%rsi,4), %rdi
movq 0x20(%r9,%r8,8), %r10
movq %r10, 0x20(%r9,%rdi,8)
movups (%r9,%r8,8), %xmm0
movups 0x10(%r9,%r8,8), %xmm1
movups %xmm1, 0x10(%r9,%rdi,8)
movups %xmm0, (%r9,%rdi,8)
incq %rdx
cmpq %rax, %rdx
jge 0x109bf3
leaq 0x48(,%r8,8), %r8
movq 0x10(%r15), %r9
movq (%r9,%r8), %r10
addq %r10, 0x20(%r9,%rdi,8)
movl -0x14(%r9,%r8), %r10d
andl %ecx, %r10d
movl 0xc(%r9,%rdi,8), %r11d
andl %ecx, %r11d
cmpl %r11d, %r10d
jbe 0x109be7
leaq (%r9,%rdi,8), %r10
addq %r8, %r9
addq $-0x20, %r9
movq 0x10(%r9), %r11
movq %r11, 0x10(%r10)
movups (%r9), %xmm0
movups %xmm0, (%r10)
incq %rdx
addq $0x28, %r8
cmpq %rax, %rdx
jl 0x109baa
incq %rsi
movq %rax, %rdx
leaq 0x1(%rax), %r8
movq (%r15), %rdi
cmpq %rdi, %rax
movq %r8, %rax
jl 0x109b4d
jmp 0x109c14
xorl %eax, %eax
jmp 0x109c2f
xorl %esi, %esi
movq %rsi, (%r15)
movq 0x10(%r15), %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x108131
xorl %eax, %eax
cmpq $0x0, (%rbx)
setne %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq