// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_node_boundary_advance(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, unsigned int, kvec_asg_arc_t_offset*, kvec_t_i32_warp*, kvec_t_i32_warp*, asg_arc_t*, asg_arc_t*)
void get_node_boundary_advance(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 dir, uint32_t rev, kvec_asg_arc_t_offset* u_buffer, 
kvec_t_i32_warp* tailIndex, kvec_t_i32_warp* prevIndex, asg_arc_t* reture_t_f, asg_arc_t* reture_t_r)
{
    long long k, j, offset, m;
    ma_hit_t_alloc *xR = NULL;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL, *st = NULL;
    int r, index;
    asg_arc_t t_f, t_r;
    uint32_t rId, Hap_uId, is_Unitig, v, w, v_dir, w_dir;
    uint64_t tmp;
    asg_arc_t_offset t_offset;
    reture_t_f->del = reture_t_r->del = 1;
    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_f);
            ///if it is a contained overlap, skip
            if(r < 0) continue;

            rId = t_f.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_f.ul>>32)==v)?1:0;
            w_dir = (t_f.v == w)?1:0;
            if(rev == 0 && v_dir != w_dir) continue;
            if(rev == 1 && v_dir == w_dir) continue;
            if(dir == v_dir) continue;

            /****************************may have bugs********************************/
            offset = (uint32_t)(position_index[rId]);
            if(offset < yBegIndex || offset > yEndIndex) continue;
            /****************************may have bugs********************************/
        
            /************************get reverse edge*************************/
            index = get_specific_overlap(&(reverse_sources[Get_tn(*h)]), Get_tn(*h), Get_qn(*h));
            if(index == -1) continue;
            h = &(reverse_sources[Get_tn(*h)].buffer[index]);
            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_r);
            if(r < 0) continue;
            /************************get reverse edge*************************/

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

            t_offset.Off = tmp;
            t_offset.x = t_f;
            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);

    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;

    quick_LIS(u_buffer->a.a, u_buffer->a.n, tailIndex, prevIndex);

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

    if(dir == 0)
    {
        for (k = 0; k < (long long)tailIndex->a.n; k++)
        {
            v = u_buffer->a.a[tailIndex->a.a[k]].x.v>>1;
            w = u_buffer->a.a[tailIndex->a.a[k]].x.ul>>33;
            index = get_specific_overlap(&(reverse_sources[v]), v, w);
            if(index == -1) continue;
            h = &(reverse_sources[v].buffer[index]);
            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_r);
            if(r < 0) continue;

            (*reture_t_f) = u_buffer->a.a[tailIndex->a.a[k]].x;
            (*reture_t_r) = t_r;
            return;
        }
    }
    else
    {
        for (k = tailIndex->a.n-1; k >= 0; k--)
        {
            v = u_buffer->a.a[tailIndex->a.a[k]].x.v>>1;
            w = u_buffer->a.a[tailIndex->a.a[k]].x.ul>>33;
            index = get_specific_overlap(&(reverse_sources[v]), v, w);
            if(index == -1) continue;
            h = &(reverse_sources[v].buffer[index]);
            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_r);
            if(r < 0) continue;

            (*reture_t_f) = u_buffer->a.a[tailIndex->a.a[k]].x;
            (*reture_t_r) = t_r;
            return;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %r9d, 0x14(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x148(%rsp), %rax
movq 0x140(%rsp), %rcx
movq 0x128(%rsp), %r15
orb $-0x80, 0xf(%rax)
orb $-0x80, 0xf(%rcx)
andq $0x0, (%r15)
movq 0xf8(%rsp), %rcx
movq %rdx, 0x8(%rsp)
cmpq 0x100(%rsp), %rcx
jg 0xcf78e
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
imulq $0x18, %rax, %r12
addq 0x20(%rsp), %r12
xorl %r14d, %r14d
xorl %ebp, %ebp
movl 0xc(%r12), %eax
cmpq %rax, %rbp
jae 0xcf779
movq (%r12), %r15
movl 0xc(%r15,%r14), %edi
imulq $0xc, %rdi, %rcx
movl (%rdx,%rcx), %eax
testl %eax, %eax
js 0xcf574
movq 0x28(%rsp), %rsi
movq 0x20(%rsi), %r13
cmpl $0x0, (%r13,%rdi,8)
js 0xcf574
leaq (%r15,%r14), %rdi
movl 0x4(%r15,%r14), %esi
imulq $0xc, %rsi, %r8
addq %rdx, %r8
addq %rdx, %rcx
movl 0x4(%r8), %esi
movl (%r8), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x184316(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x14(%rsp), %ecx
movl 0xd0(%rsp), %r8d
leaq 0x48(%rsp), %r9
callq 0xc99c6
movq 0x8(%rsp), %rdx
testl %eax, %eax
js 0xcf574
movl 0x50(%rsp), %ebx
shrl %ebx
cmpl $0x0, (%r13,%rbx,8)
js 0xcf574
movq 0x40(%rsp), %rdi
movl %ebx, %esi
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x6c2a2
movq 0x8(%rsp), %rdx
cmpl $0x0, 0x18(%rsp)
je 0xcf574
movl 0x1c(%rsp), %eax
cmpl $-0x1, %eax
je 0xcf574
cmpl 0xf0(%rsp), %eax
jne 0xcf574
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %r13
movq %r13, 0x60(%rsp)
shrq $0x21, %r13
movq 0x40(%rsp), %rdi
movl %r13d, %esi
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x6c2a2
movq 0x8(%rsp), %rdx
cmpl $0x0, 0x18(%rsp)
je 0xcf574
movl 0x1c(%rsp), %eax
cmpl $-0x1, %eax
je 0xcf574
cmpl 0xe8(%rsp), %eax
jne 0xcf574
movl %r13d, %eax
movq 0x30(%rsp), %rcx
movl (%rcx,%rax,8), %eax
cmpq 0x38(%rsp), %rax
jne 0xcf574
movq 0x60(%rsp), %rsi
shrq $0x20, %rsi
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x30(%rsp), %rax
movl (%rax,%rbx,8), %eax
movl 0x4(%rcx,%rax,8), %r13d
movq 0x48(%rsp), %rcx
shrq $0x20, %rcx
movq %rsi, %rbx
cmpq %rsi, %rcx
sete %sil
cmpl %r13d, 0x50(%rsp)
sete %r8b
xorb %sil, %r8b
cmpl $0x0, 0x120(%rsp)
jne 0xcf580
testb %r8b, %r8b
je 0xcf580
incq %rbp
addq $0x28, %r14
jmp 0xcf3d6
xorl %esi, %esi
cmpl %ebx, %ecx
sete %cl
cmpl $0x1, 0x120(%rsp)
setne %dil
orb %r8b, %dil
testb $0x1, %dil
je 0xcf574
movb %cl, %sil
cmpl 0x118(%rsp), %esi
je 0xcf574
cmpq 0x108(%rsp), %rax
setl %cl
cmpq 0x110(%rsp), %rax
setg %al
orb %cl, %al
jne 0xcf574
movl 0x4(%r15,%r14), %edx
movl 0xc(%r15,%r14), %esi
imulq $0x18, %rsi, %rdi
addq 0x20(%rsp), %rdi
callq 0x696ab
movq 0x8(%rsp), %rdx
cmpl $-0x1, %eax
je 0xcf574
movl 0xc(%r15,%r14), %ecx
imulq $0x18, %rcx, %rcx
movq 0x20(%rsp), %rsi
movq (%rsi,%rcx), %r9
cltq
imulq $0x28, %rax, %rsi
movl 0xc(%r9,%rsi), %edi
imulq $0xc, %rdi, %rcx
movl (%rdx,%rcx), %eax
testl %eax, %eax
js 0xcf574
movq 0x28(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rdi,8)
js 0xcf574
leaq (%r9,%rsi), %rdi
movl 0x4(%r9,%rsi), %esi
imulq $0xc, %rsi, %r8
addq %rdx, %r8
addq %rdx, %rcx
movl 0x4(%r8), %esi
movl (%r8), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x184103(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x14(%rsp), %ecx
movl 0xd0(%rsp), %r8d
leaq 0x68(%rsp), %r9
callq 0xc99c6
movq 0x8(%rsp), %rdx
testl %eax, %eax
js 0xcf574
movq 0xd8(%rsp), %rax
movl (%rax), %r8d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r8d
movq 0xe0(%rsp), %rax
movl (%rax), %r9d
andl %ecx, %r9d
movq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movl %ebx, %edx
movl %r13d, %ecx
leaq 0x5a(%rsp), %rax
pushq %rax
pushq 0x38(%rsp)
callq 0xd3d59
movq 0x18(%rsp), %rdx
addq $0x10, %rsp
movq %rax, %rbx
movabsq $-0x100000001, %rax     # imm = 0xFFFFFFFEFFFFFFFF
cmpq %rax, %rbx
seta %al
cmpl $-0x1, %ebx
sete %cl
orb %al, %cl
jne 0xcf574
movq 0x58(%rsp), %rax
movq %rax, 0x90(%rsp)
movups 0x48(%rsp), %xmm0
movaps %xmm0, 0x80(%rsp)
movq 0x128(%rsp), %r15
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0xcf742
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 0x8(%rsp), %rdx
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
imulq $0x28, %rcx, %rcx
movq 0x90(%rsp), %rsi
movq %rsi, 0x10(%rax,%rcx)
movaps 0x80(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq %rbx, 0x18(%rax,%rcx)
movq $0x1, 0x20(%rax,%rcx)
jmp 0xcf574
movq 0x38(%rsp), %rcx
incq %rcx
movq 0x128(%rsp), %r15
jmp 0xcf3a1
movq (%r15), %rsi
testq %rsi, %rsi
je 0xcfa70
movq 0x10(%r15), %rdi
leaq -0x7817(%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 (%r15), %rcx
jge 0xcf834
movq 0x10(%r15), %rdi
imulq $0x28, %rsi, %r8
leaq (%rdi,%r8), %r9
movq -0x10(%rdi,%r8), %r10
leaq (%rdi,%rbx), %rdx
cmpq 0x18(%rdi,%rbx), %r10
jne 0xcf810
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 0xcf82b
addq %r8, %rdi
addq $-0x28, %rdi
movq 0x10(%rdx), %r8
movq %r8, 0x10(%rdi)
movups (%rdx), %xmm0
movups %xmm0, (%rdi)
jmp 0xcf82b
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 0xcf7bb
movq %rsi, (%r15)
movq 0x10(%r15), %rdi
movq 0x130(%rsp), %rbx
movq %rbx, %rdx
movq 0x138(%rsp), %rcx
callq 0xc9be5
movq (%rbx), %rax
testq %rax, %rax
je 0xcfa70
cmpl $0x0, 0x118(%rsp)
je 0xcf948
decq %rax
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq 0x68(%rsp), %rbx
movq %rax, %r14
testq %r14, %r14
js 0xcfa70
movq 0x10(%r15), %rax
movq 0x130(%rsp), %rcx
movq 0x10(%rcx), %rcx
movslq (%rcx,%r14,4), %rcx
imulq $0x28, %rcx, %rcx
movl 0x8(%rax,%rcx), %esi
shrl %esi
movq (%rax,%rcx), %rdx
shrq $0x21, %rdx
imulq $0x18, %rsi, %r12
addq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x696ab
cmpl $-0x1, %eax
je 0xcf940
movq (%r12), %rdx
cltq
imulq $0x28, %rax, %rsi
movl 0xc(%rdx,%rsi), %edi
imulq $0xc, %rdi, %rcx
movq 0x8(%rsp), %rax
movl (%rax,%rcx), %eax
testl %eax, %eax
js 0xcf940
movq 0x28(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rdi,8)
js 0xcf940
leaq (%rdx,%rsi), %rdi
movl 0x4(%rdx,%rsi), %edx
imulq $0xc, %rdx, %rdx
movq 0x8(%rsp), %rsi
addq %rsi, %rdx
addq %rsi, %rcx
movl 0x4(%rdx), %esi
movl (%rdx), %edx
andl %ebp, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x183e3c(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x14(%rsp), %ecx
movl 0xd0(%rsp), %r8d
movq %rbx, %r9
callq 0xc99c6
testl %eax, %eax
jns 0xcfa27
decq %r14
jmp 0xcf87d
xorl %r14d, %r14d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq 0x68(%rsp), %rbx
cmpq %rax, %r14
jge 0xcfa70
movq 0x10(%r15), %rax
movq 0x130(%rsp), %rcx
movq 0x10(%rcx), %rcx
movslq (%rcx,%r14,4), %rcx
imulq $0x28, %rcx, %rcx
movl 0x8(%rax,%rcx), %esi
shrl %esi
movq (%rax,%rcx), %rdx
shrq $0x21, %rdx
imulq $0x18, %rsi, %r12
addq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x696ab
cmpl $-0x1, %eax
je 0xcfa14
movq (%r12), %rdx
cltq
imulq $0x28, %rax, %rsi
movl 0xc(%rdx,%rsi), %edi
imulq $0xc, %rdi, %rcx
movq 0x8(%rsp), %rax
movl (%rax,%rcx), %eax
testl %eax, %eax
js 0xcfa14
movq 0x28(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rdi,8)
js 0xcfa14
leaq (%rdx,%rsi), %rdi
movl 0x4(%rdx,%rsi), %edx
imulq $0xc, %rdx, %rdx
movq 0x8(%rsp), %rsi
addq %rsi, %rdx
addq %rsi, %rcx
movl 0x4(%rdx), %esi
movl (%rdx), %edx
andl %ebp, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x183d64(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x14(%rsp), %ecx
movl 0xd0(%rsp), %r8d
movq %rbx, %r9
callq 0xc99c6
testl %eax, %eax
jns 0xcfa27
incq %r14
movq 0x130(%rsp), %rax
movq (%rax), %rax
jmp 0xcf955
movq 0x10(%r15), %rax
movq 0x130(%rsp), %rcx
movq 0x10(%rcx), %rcx
movslq (%rcx,%r14,4), %rcx
imulq $0x28, %rcx, %rcx
movq 0x10(%rax,%rcx), %rdx
movq 0x140(%rsp), %rsi
movq %rdx, 0x10(%rsi)
movups (%rax,%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x78(%rsp), %rax
movq 0x148(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups 0x68(%rsp), %xmm0
movups %xmm0, (%rcx)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq