// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_base_boundary_advance_back(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*, unsigned int*, unsigned int*, unsigned int*, unsigned int*)
void get_base_boundary_advance_back(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,
uint32_t* xBeg, uint32_t* xEnd, uint32_t* yBeg, uint32_t* yEnd)
{
    long long k, j, 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;
    (*xBeg) = (*xEnd) = (*yBeg) = (*yEnd) = (uint32_t)-1;
    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;

    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, m = 1; k < (long long)u_buffer->a.n; k++)
    {
        if(u_buffer->a.a[m-1].Off == u_buffer->a.a[k].Off)
        {
            u_buffer->a.a[m-1].weight += u_buffer->a.a[k].weight;
            if(u_buffer->a.a[k].x.ol > u_buffer->a.a[m-1].x.ol)
            {
                u_buffer->a.a[m-1].x = u_buffer->a.a[k].x;
            }
            continue;
        }
        u_buffer->a.a[m] = u_buffer->a.a[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;

    (*xBeg) = Get_xOff(u_buffer->a.a[tailIndex->a.a[0]].Off);
    (*yBeg) = Get_yOff(u_buffer->a.a[tailIndex->a.a[0]].Off);
    (*xEnd) = Get_xOff(u_buffer->a.a[tailIndex->a.a[tailIndex->a.n-1]].Off);
    (*yEnd) = Get_yOff(u_buffer->a.a[tailIndex->a.a[tailIndex->a.n-1]].Off);
}

// 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 %rdx, %r12
movq %rsi, 0x38(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rdx
movq 0x110(%rsp), %rcx
movq 0x108(%rsp), %rsi
movq 0xf0(%rsp), %rdi
movq 0xc8(%rsp), %r8
movq $0x0, (%rdi)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, (%rax)
movl %edi, (%rdx)
movl %edi, (%rcx)
movl %edi, (%rsi)
cmpq 0xd0(%rsp), %r8
jg 0x109345
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rax
movq %r8, 0x20(%rsp)
movq (%rax,%r8,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x38(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0x10932e
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 0x1092c5
movq 0x28(%rsp), %rsi
movq 0x20(%rsi), %r14
cmpl $0x0, (%r14,%rdx,8)
js 0x1092c5
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 0x181771(%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 0x1092c5
movl 0x50(%rsp), %eax
movl %eax, 0x18(%rsp)
movl %eax, %r13d
shrl %r13d
cmpl $0x0, (%r14,%r13,8)
js 0x1092c5
movq 0x40(%rsp), %rdi
movl %r13d, %esi
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
callq 0xa306f
cmpl $0x0, (%rsp)
je 0x1092c5
movl 0x4(%rsp), %eax
cmpl $-0x1, %eax
je 0x1092c5
cmpl 0xc0(%rsp), %eax
jne 0x1092c5
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 0x1092c5
movl 0x4(%rsp), %eax
cmpl $-0x1, %eax
je 0x1092c5
cmpl 0xb8(%rsp), %eax
jne 0x1092c5
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %r9
movl %r9d, %eax
cmpq 0x20(%rsp), %rax
jne 0x1092c5
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 0x1092c5
cmpq 0xd8(%rsp), %rcx
setl %dl
cmpq 0xe0(%rsp), %rcx
setg %cl
orb %dl, %cl
jne 0x1092c5
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 0x10919c
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 0x1091af
xorl %r11d, %eax
cmpl $0x1, %eax
jne 0x1092da
shrq $0x20, %r9
movb $0x1, %r14b
movl 0x18(%rsp), %eax
cmpl %r13d, %eax
jne 0x1091d4
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 0x109207
xorl %r13d, %eax
cmpl $0x1, %eax
jne 0x109307
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 0x109222
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl %esi, %eax
jae 0x109224
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 0x1092c5
cmpl %esi, %ecx
jae 0x1092c5
movq 0xf0(%rsp), %rsi
movq 0x8(%rsi), %rax
cmpq %rax, (%rsi)
jne 0x10928b
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 0x108f8d
jmp 0x10932e
movl %esi, %r14d
movl %edi, 0x8(%rsp)
movq %r10, 0x60(%rsp)
callq 0x139a7
movq 0x60(%rsp), %r10
movl 0x8(%rsp), %edi
movl %r14d, %esi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
jmp 0x109197
movq %r9, 0x18(%rsp)
movl %esi, %r13d
movl %edi, 0x8(%rsp)
callq 0x139c9
movl 0x8(%rsp), %edi
movl %r13d, %esi
movq 0x18(%rsp), %r9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1091d0
movq 0x20(%rsp), %rcx
leaq 0x1(%rcx), %r8
cmpq 0xd0(%rsp), %rcx
jne 0x108f52
movq 0xf0(%rsp), %r12
movq (%r12), %rsi
testq %rsi, %rsi
je 0x10949b
movq 0x100(%rsp), %r14
movq 0xf8(%rsp), %rbx
movq 0x10(%r12), %rdi
leaq -0x34a1(%rip), %rcx     # 0x105ed5
movl $0x28, %r15d
movl $0x28, %edx
callq 0x23f0
movl $0x1, %esi
cmpq $0x2, (%r12)
jl 0x10941e
movl $0x1, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x10(%r12), %rdx
leaq (%rsi,%rsi,4), %rdi
movq -0x10(%rdx,%rdi,8), %r9
leaq (%rdx,%r15), %r8
cmpq 0x18(%rdx,%r15), %r9
jne 0x1093f3
leaq (%rdx,%rdi,8), %r9
movq 0x20(%rdx,%r15), %r10
addq %r10, -0x8(%r9)
movl 0xc(%rdx,%r15), %r10d
andl %ecx, %r10d
movl -0x1c(%r9), %r9d
andl %ecx, %r9d
cmpl %r9d, %r10d
jbe 0x109411
leaq (%rdx,%rdi,8), %rdx
addq $-0x28, %rdx
movq 0x10(%r8), %rdi
movq %rdi, 0x10(%rdx)
movups (%r8), %xmm0
movups %xmm0, (%rdx)
jmp 0x109411
movq 0x20(%r8), %r9
movq %r9, 0x20(%rdx,%rdi,8)
movups (%r8), %xmm0
movups 0x10(%r8), %xmm1
movups %xmm1, 0x10(%rdx,%rdi,8)
movups %xmm0, (%rdx,%rdi,8)
incq %rsi
incq %rax
addq $0x28, %r15
cmpq (%r12), %rax
jl 0x1093a0
movq %rsi, (%r12)
movq 0x10(%r12), %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x108131
movq (%rbx), %rax
testq %rax, %rax
je 0x10949b
movq 0x10(%r12), %rcx
movq 0x10(%rbx), %rdx
movslq (%rdx), %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0x1c(%rcx,%rsi,8), %esi
movq 0x108(%rsp), %rdi
movl %esi, (%rdi)
movslq (%rdx), %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0x18(%rcx,%rsi,8), %esi
movq 0x118(%rsp), %rdi
movl %esi, (%rdi)
movslq -0x4(%rdx,%rax,4), %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0x1c(%rcx,%rsi,8), %esi
movq 0x110(%rsp), %rdi
movl %esi, (%rdi)
movslq -0x4(%rdx,%rax,4), %rax
leaq (%rax,%rax,4), %rax
movl 0x18(%rcx,%rax,8), %eax
movq 0x120(%rsp), %rcx
movl %eax, (%rcx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq