// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movl %r9d, 0x24(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %r13
movq %rsi, 0x50(%rsp)
movq %rdi, 0x28(%rsp)
movq 0x100(%rsp), %rax
movq 0xd8(%rsp), %rcx
andq $0x0, (%rax)
cmpq 0xe0(%rsp), %rcx
jg 0xcb286
movq 0xb8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
imulq $0x18, %rax, %rbp
addq 0x50(%rsp), %rbp
xorl %r12d, %r12d
xorl %ebx, %ebx
movl 0xc(%rbp), %eax
cmpq %rax, %rbx
jae 0xcb279
movq (%rbp), %rdi
movl 0xc(%rdi,%r12), %edx
imulq $0xc, %rdx, %rcx
movl (%r13,%rcx), %eax
testl %eax, %eax
js 0xcb26d
movq 0x30(%rsp), %rsi
movq 0x20(%rsi), %r14
cmpl $0x0, (%r14,%rdx,8)
js 0xcb26d
movl 0x4(%rdi,%r12), %edx
addq %r12, %rdi
imulq $0xc, %rdx, %rdx
addq %r13, %rdx
addq %r13, %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 0x188726(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x24(%rsp), %ecx
movl 0xb0(%rsp), %r8d
leaq 0x38(%rsp), %r9
callq 0xc99c6
testl %eax, %eax
js 0xcb26d
movl 0x40(%rsp), %r15d
shrl %r15d
cmpl $0x0, (%r14,%r15,8)
js 0xcb26d
movq 0x28(%rsp), %rdi
movl %r15d, %esi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x6c2a2
cmpl $0x0, 0x8(%rsp)
je 0xcb26d
movl 0xc(%rsp), %eax
cmpl $-0x1, %eax
je 0xcb26d
cmpl 0xd0(%rsp), %eax
jne 0xcb26d
movq 0xb8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %r14
movq %r14, 0x58(%rsp)
shrq $0x21, %r14
movq 0x28(%rsp), %rdi
movl %r14d, %esi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x6c2a2
cmpl $0x0, 0x8(%rsp)
je 0xcb26d
movl 0xc(%rsp), %eax
cmpl $-0x1, %eax
je 0xcb26d
cmpl 0xc8(%rsp), %eax
jne 0xcb26d
movl %r14d, %eax
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,8), %eax
cmpq 0x18(%rsp), %rax
jne 0xcb26d
movq 0x58(%rsp), %rdi
shrq $0x20, %rdi
movl 0xf8(%rsp), %eax
cmpl $0x1, %eax
setne %r9b
testl %eax, %eax
setne %sil
movq 0xc0(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x10(%rsp), %rax
movl (%rax,%r15,8), %eax
movq %rdi, %rdx
cmpl %edi, 0x3c(%rsp)
movl 0x4(%rcx,%rax,8), %ecx
sete %dil
cmpl %ecx, 0x40(%rsp)
sete %r8b
xorb %dil, %r8b
orb %r8b, %r9b
notb %r8b
orb %sil, %r8b
andb %r9b, %r8b
testb $0x1, %r8b
je 0xcb26d
cmpq 0xe8(%rsp), %rax
setl %sil
cmpq 0xf0(%rsp), %rax
setg %al
orb %sil, %al
jne 0xcb26d
movq 0xb8(%rsp), %rax
movl (%rax), %r8d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r8d
movq 0xc0(%rsp), %rax
movl (%rax), %r9d
andl %esi, %r9d
movq 0x30(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x4a(%rsp), %rax
pushq %rax
pushq 0x18(%rsp)
callq 0xd3d59
popq %rcx
popq %rdx
movq %rax, %r14
movabsq $-0x100000001, %rax     # imm = 0xFFFFFFFEFFFFFFFF
cmpq %rax, %r14
seta %al
cmpl $-0x1, %r14d
sete %cl
orb %al, %cl
jne 0xcb26d
movq 0x48(%rsp), %rax
movq %rax, 0x70(%rsp)
movups 0x38(%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x100(%rsp), %r15
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0xcb241
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r15)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
imulq $0x28, %rcx, %rcx
movq 0x70(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movaps 0x60(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq %r14, 0x18(%rax,%rcx)
movq $0x1, 0x20(%rax,%rcx)
incq %rbx
addq $0x28, %r12
jmp 0xcafca
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0xcaf95
movq 0x100(%rsp), %rbx
movq (%rbx), %rsi
testq %rsi, %rsi
je 0xcb38b
movq 0x10(%rbx), %rdi
leaq -0x3317(%rip), %rcx     # 0xc7f8e
pushq $0x28
popq %rdx
callq 0x23f0
pushq $0x1
popq %rax
xorl %esi, %esi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
cmpq (%rbx), %rax
jg 0xcb361
movq 0x10(%rbx), %r9
je 0xcb2dc
imulq $0x28, %rax, %rdi
movq 0x18(%r9,%rdi), %rdi
imulq $0x28, %rdx, %r8
cmpq 0x18(%r9,%r8), %rdi
je 0xcb359
imulq $0x28, %rdx, %rdi
imulq $0x28, %rsi, %r8
movq 0x20(%r9,%rdi), %r10
movq %r10, 0x20(%r9,%r8)
movups (%r9,%rdi), %xmm0
movups 0x10(%r9,%rdi), %xmm1
movups %xmm1, 0x10(%r9,%r8)
movups %xmm0, (%r9,%r8)
addq $0x48, %rdi
incq %rdx
cmpq %rax, %rdx
jge 0xcb353
movq 0x10(%rbx), %r9
movq (%r9,%rdi), %r10
addq %r10, 0x20(%r9,%r8)
movl -0x14(%r9,%rdi), %r10d
andl %ecx, %r10d
movl 0xc(%r9,%r8), %r11d
andl %ecx, %r11d
cmpl %r11d, %r10d
jbe 0xcb34d
leaq (%r9,%rdi), %r10
addq $-0x20, %r10
addq %r8, %r9
movq 0x10(%r10), %r11
movq %r11, 0x10(%r9)
movups (%r10), %xmm0
movups %xmm0, (%r9)
addq $0x28, %rdi
jmp 0xcb308
incq %rsi
movq %rax, %rdx
incq %rax
jmp 0xcb2b9
movq %rsi, (%rbx)
movq 0x10(%rbx), %rdi
movq 0x108(%rsp), %rbx
movq %rbx, %rdx
movq 0x110(%rsp), %rcx
callq 0xc9be5
xorl %eax, %eax
cmpq $0x0, (%rbx)
setne %al
jmp 0xcb38d
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq