// File: /workspace/github_cpp_makefile_star2_O2/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 $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 0x130(%rsp), %rax
movq 0x128(%rsp), %rdx
movq 0x120(%rsp), %rcx
movq 0x118(%rsp), %rsi
movq 0x100(%rsp), %rdi
andq $0x0, (%rdi)
orl $-0x1, (%rax)
orl $-0x1, (%rdx)
orl $-0x1, (%rcx)
orl $-0x1, (%rsi)
movq 0xd8(%rsp), %rcx
cmpq 0xe0(%rsp), %rcx
jg 0xcac0e
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 0xcac01
movq (%rbp), %rdi
movl 0xc(%rdi,%r12), %edx
imulq $0xc, %rdx, %rcx
movl (%r13,%rcx), %eax
testl %eax, %eax
js 0xcabf5
movq 0x30(%rsp), %rsi
movq 0x20(%rsi), %r14
cmpl $0x0, (%r14,%rdx,8)
js 0xcabf5
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 0x188d9e(%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 0xcabf5
movl 0x40(%rsp), %r15d
shrl %r15d
cmpl $0x0, (%r14,%r15,8)
js 0xcabf5
movq 0x28(%rsp), %rdi
movl %r15d, %esi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x6c2a2
cmpl $0x0, 0x8(%rsp)
je 0xcabf5
movl 0xc(%rsp), %eax
cmpl $-0x1, %eax
je 0xcabf5
cmpl 0xd0(%rsp), %eax
jne 0xcabf5
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 0xcabf5
movl 0xc(%rsp), %eax
cmpl $-0x1, %eax
je 0xcabf5
cmpl 0xc8(%rsp), %eax
jne 0xcabf5
movl %r14d, %eax
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,8), %eax
cmpq 0x18(%rsp), %rax
jne 0xcabf5
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 0xcabf5
cmpq 0xe8(%rsp), %rax
setl %sil
cmpq 0xf0(%rsp), %rax
setg %al
orb %sil, %al
jne 0xcabf5
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 0xcabf5
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 0xcabc9
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 0xca952
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0xca91d
movq 0x100(%rsp), %r14
movq (%r14), %rsi
testq %rsi, %rsi
movq 0x108(%rsp), %r15
je 0xcad43
movq 0x10(%r14), %rdi
leaq -0x2ca7(%rip), %rcx     # 0xc7f8e
pushq $0x28
popq %rbx
movq %rbx, %rdx
callq 0x23f0
pushq $0x1
popq %rsi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rsi, %rcx
cmpq (%r14), %rcx
jge 0xcacc4
movq 0x10(%r14), %rdi
imulq $0x28, %rsi, %r8
leaq (%rdi,%r8), %r9
movq -0x10(%rdi,%r8), %r10
leaq (%rdi,%rbx), %rdx
cmpq 0x18(%rdi,%rbx), %r10
jne 0xcaca0
movq 0x20(%rdi,%rbx), %r10
addq %r10, -0x8(%r9)
movl 0xc(%rdi,%rbx), %r10d
andl %eax, %r10d
movl -0x1c(%r9), %r9d
andl %eax, %r9d
cmpl %r9d, %r10d
jbe 0xcacbb
addq %r8, %rdi
addq $-0x28, %rdi
movq 0x10(%rdx), %r8
movq %r8, 0x10(%rdi)
movups (%rdx), %xmm0
movups %xmm0, (%rdi)
jmp 0xcacbb
movq 0x20(%rdx), %rdi
movq %rdi, 0x20(%r9)
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups %xmm1, 0x10(%r9)
movups %xmm0, (%r9)
incq %rsi
incq %rcx
addq $0x28, %rbx
jmp 0xcac4b
movq %rsi, (%r14)
movq 0x10(%r14), %rdi
movq %r15, %rdx
movq 0x110(%rsp), %rcx
callq 0xc9be5
movq (%r15), %rax
testq %rax, %rax
je 0xcad43
movq 0x10(%r14), %rcx
movq 0x10(%r15), %rdx
movslq (%rdx), %rsi
imulq $0x28, %rsi, %rsi
movl 0x1c(%rcx,%rsi), %esi
movq 0x118(%rsp), %rdi
movl %esi, (%rdi)
movslq (%rdx), %rsi
imulq $0x28, %rsi, %rsi
movl 0x18(%rcx,%rsi), %esi
movq 0x128(%rsp), %rdi
movl %esi, (%rdi)
movslq -0x4(%rdx,%rax,4), %rsi
imulq $0x28, %rsi, %rsi
movl 0x1c(%rcx,%rsi), %esi
movq 0x120(%rsp), %rdi
movl %esi, (%rdi)
movslq -0x4(%rdx,%rax,4), %rax
imulq $0x28, %rax, %rax
movl 0x18(%rcx,%rax), %eax
movq 0x130(%rsp), %rcx
movl %eax, (%rcx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq