// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movl %r9d, %r14d
movq %rcx, 0x78(%rsp)
movq %rdx, %r12
movq %rdi, 0x88(%rsp)
movq 0x150(%rsp), %rax
movq 0x148(%rsp), %rdx
movq 0x140(%rsp), %rcx
movq 0x138(%rsp), %r9
movq 0x120(%rsp), %rdi
movq 0xf8(%rsp), %r10
movq $0x0, (%rdi)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, (%rax)
movl %edi, (%rdx)
movl %edi, (%rcx)
movl %edi, (%r9)
cmpq 0x100(%rsp), %r10
jg 0x10e38c
movq %r8, 0x68(%rsp)
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl %eax, 0x34(%rsp)
movq %rsi, 0x90(%rsp)
movl %r14d, 0x14(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq %r10, 0x70(%rsp)
movq (%rax,%r10,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
cmpl $0x0, 0xc(%rsi,%rax,8)
je 0x10e20e
leaq (%rsi,%rax,8), %r8
movl $0x21, %ebx
xorl %r15d, %r15d
movq %r8, 0x8(%rsp)
movq (%r8), %rcx
movl -0x15(%rcx,%rbx), %r13d
leaq (,%r13,2), %rsi
addq %r13, %rsi
movl (%r12,%rsi,4), %edx
testl %edx, %edx
js 0x10dd3b
movq 0x78(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%r13,8)
js 0x10dd3b
leaq (%r12,%rsi,4), %rsi
movl 0x4(%rsi), %esi
subl %edx, %esi
movq -0x9(%rcx,%rbx), %rdx
movq %rdx, 0x28(%rsp)
testl %edx, %edx
movq %rax, 0x18(%rsp)
js 0x10dc71
movl -0x11(%rcx,%rbx), %r10d
movl -0xd(%rcx,%rbx), %r8d
subl %r8d, %esi
movl %esi, %edi
movl %r10d, %esi
jmp 0x10dc81
movl -0x11(%rcx,%rbx), %r10d
movl -0xd(%rcx,%rbx), %r8d
subl %r8d, %esi
movl %r10d, %edi
movq -0x21(%rcx,%rbx), %r11
movq %r11, %r9
shrq $0x20, %r9
leaq (%r9,%r9,2), %r9
movq %r12, %rdx
leaq (%r12,%r9,4), %r9
movl 0x4(%r9), %eax
movl (%r9), %r9d
andl %ebp, %r9d
subl %r9d, %eax
cmpl %r11d, %esi
movq %r11, 0x48(%rsp)
movl %r11d, %ebp
cmovll %esi, %ebp
movl -0x19(%rcx,%rbx), %r9d
movl %eax, %r12d
subl %r9d, %r12d
cmpl %edi, %r12d
movl %edi, %r11d
cmovll %r12d, %r11d
cmpl %r14d, %ebp
jg 0x10dd2e
cmpl %r14d, %r11d
jg 0x10dd2e
movq %rax, 0x60(%rsp)
movl %r9d, %r14d
subl 0x48(%rsp), %r14d
cvtsi2ss %r14, %xmm1
addl %ebp, %r14d
addl %r11d, %r14d
cvtsi2ss %r14, %xmm2
leaq 0x187a62(%rip), %rax    # 0x295758
movss 0x158(%rax), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0x10dd29
subl %r10d, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
addl %ebp, %r11d
addl %r8d, %r11d
xorps %xmm2, %xmm2
cvtsi2ss %r11, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x10dd54
movl 0x14(%rsp), %r14d
movq %rdx, %r12
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r8
incq %r15
movl 0xc(%r8), %eax
addq $0x28, %rbx
cmpq %rax, %r15
jb 0x10dc0e
jmp 0x10e20e
cmpl %edi, %r12d
setl %r8b
setg %bpl
movq 0x48(%rsp), %rax
cmpl %esi, %eax
setg %r12b
setl %r10b
orb %bpl, %r12b
cmpb $0x1, %r12b
jne 0x10dd29
orb %r8b, %r10b
movq %rdx, %r12
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
je 0x10e00f
movl 0xd0(%rsp), %edx
cmpl %edx, %r14d
jl 0x10e00f
cmpl %edx, %r11d
movl 0x14(%rsp), %r14d
jl 0x10dd36
movq 0x28(%rsp), %r10
testl %r10d, %r10d
setns %r11b
addl %r9d, %edi
movq %rax, %rdx
movq 0x60(%rsp), %rax
movl %eax, %r8d
subl %edi, %r8d
subl %esi, %edx
setle %dil
setg %r9b
cmovlel %r8d, %edx
shrl $0x10, %r10d
andl $0x4000, %r10d          # imm = 0x4000
movl 0x34(%rsp), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
orl %r10d, %eax
movzbl (%rcx,%rbx), %ecx
shll $0xf, %ecx
orl %eax, %ecx
testl %edx, %edx
movl %ecx, 0x34(%rsp)
js 0x10dd36
movl %r13d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax,%rsi,8)
js 0x10dd36
movb %r9b, 0x10(%rsp)
movb %dil, 0x18(%rsp)
movb %r11b, 0x28(%rsp)
movq %rdx, 0x58(%rsp)
movq 0x88(%rsp), %rdi
movq %rsi, 0x40(%rsp)
leaq 0x3c(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0xa3c62
cmpl $0x0, 0x38(%rsp)
je 0x10dd36
movl 0x3c(%rsp), %eax
cmpl $-0x1, %eax
movq 0x8(%rsp), %r8
je 0x10dd3b
cmpl 0xf0(%rsp), %eax
jne 0x10dd3b
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x70(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
movq %rax, %rsi
shrq $0x21, %rsi
movq 0x88(%rsp), %rdi
movq %rsi, 0x80(%rsp)
leaq 0x3c(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0xa3c62
cmpl $0x0, 0x38(%rsp)
je 0x10dd36
movl 0x3c(%rsp), %eax
cmpl $-0x1, %eax
movq 0x8(%rsp), %r8
je 0x10dd3b
cmpl 0xe8(%rsp), %eax
jne 0x10dd3b
movq 0x68(%rsp), %rdi
movq 0x80(%rsp), %rax
movq (%rdi,%rax,8), %rax
movq %rax, 0x50(%rsp)
movl %eax, %eax
cmpq 0x70(%rsp), %rax
jne 0x10dd3b
movb 0x28(%rsp), %al
xorb 0x10(%rsp), %al
movzbl %al, %eax
xorl %ecx, %ecx
movb 0x18(%rsp), %dl
movb %dl, %cl
movq 0x48(%rsp), %r9
shrq $0x1f, %r9
andl $-0x2, %r9d
orq %rcx, %r9
leal (%rax,%r13,2), %r10d
movq 0x20(%rsp), %r11
shrq $0x20, %r11
movl 0x118(%rsp), %eax
cmpl $0x1, %eax
setne %cl
testl %eax, %eax
setne %dl
movq 0xe0(%rsp), %rax
movq 0x18(%rax), %rsi
movq 0x40(%rsp), %rax
movl (%rdi,%rax,8), %eax
movq (%rsi,%rax,8), %r13
movq %r13, 0x18(%rsp)
shrq $0x20, %r13
movq %r9, 0x48(%rsp)
movq %r11, 0x20(%rsp)
cmpl %r11d, %r9d
sete %sil
movl %r10d, 0x28(%rsp)
cmpl %r13d, %r10d
sete %dil
xorb %sil, %dil
orb %dil, %cl
notb %dil
orb %dl, %dil
andb %cl, %dil
testb $0x1, %dil
je 0x10dd3b
cmpq 0x108(%rsp), %rax
setl %cl
cmpq 0x110(%rsp), %rax
setg %al
orb %cl, %al
jne 0x10dd3b
movq 0xd8(%rsp), %rax
movl (%rax), %r9d
andl %ebp, %r9d
movq 0xe0(%rsp), %rax
movl (%rax), %edx
andl %ebp, %edx
movq 0x48(%rsp), %rax
movq 0x20(%rsp), %r10
cmpl %r10d, %eax
jne 0x10e019
movq 0x50(%rsp), %rcx
shrq $0x20, %rcx
movq 0x78(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x80(%rsp), %rsi
movl (%rax,%rsi,8), %eax
andl %ebp, %eax
addl %eax, %ecx
decl %ecx
movq %rcx, 0x50(%rsp)
movl $0x0, 0x40(%rsp)
movq 0x58(%rsp), %rsi
jmp 0x10e037
movl 0x14(%rsp), %r14d
jmp 0x10dd36
xorl %eax, %r10d
cmpl $0x1, %r10d
movq 0x58(%rsp), %rsi
jne 0x10e1ab
shrq $0x20, 0x50(%rsp)
movb $0x1, %al
movl %eax, 0x40(%rsp)
movq 0x60(%rsp), %rax
subl %esi, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %r13d, 0x28(%rsp)
movq %rax, 0x60(%rsp)
jne 0x10e073
movq 0x18(%rsp), %rcx
shrq $0x21, %rcx
movl %edx, %r13d
movq %rax, %rdx
movq 0x68(%rsp), %rax
movl 0x4(%rax,%rcx,8), %eax
addl %edx, %eax
decl %eax
movl %r13d, %edx
xorl %r11d, %r11d
jmp 0x10e0ad
xorl 0x28(%rsp), %r13d
cmpl $0x1, %r13d
jne 0x10e1e3
movq 0x18(%rsp), %r13
shrq $0x21, %r13
movq 0x68(%rsp), %rax
movl 0x4(%rax,%r13,8), %eax
movq 0x78(%rsp), %rcx
movq 0x20(%rcx), %rcx
movl (%rcx,%r13,8), %ecx
andl %ebp, %ecx
subl 0x60(%rsp), %eax
addl %ecx, %eax
movb $0x1, %r11b
cmpb %r11b, 0x40(%rsp)
je 0x10e0c1
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl %edx, %eax
jae 0x10e0c3
notl %eax
addl %edx, %eax
movl %eax, %ecx
movq 0x50(%rsp), %r10
cmpl %r9d, %r10d
movl %r10d, %r13d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovael %eax, %r13d
cmpl %edx, %ecx
movl %ecx, %edi
cmovael %eax, %edi
movq %rdi, 0x18(%rsp)
cmpl %r9d, %r10d
jae 0x10dd3b
cmpl %edx, %ecx
jae 0x10dd3b
movq 0x120(%rsp), %r14
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x10e148
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r14)
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rsi
movq %rax, %rdi
movl %r11d, 0x10(%rsp)
callq 0x2370
movl 0x10(%rsp), %r11d
movq 0x58(%rsp), %rsi
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r14)
movq (%r14), %rcx
movq 0x18(%rsp), %r9
movq 0x48(%rsp), %rdx
shlq $0x20, %rdx
orq %rdx, %rsi
xorb 0x40(%rsp), %r11b
shlq $0x20, %r13
orq %r13, %r9
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
leaq (%rcx,%rcx,4), %rcx
movq %rsi, (%rax,%rcx,8)
movl 0x28(%rsp), %edx
movl %edx, 0x8(%rax,%rcx,8)
movq 0x60(%rsp), %rdx
movl %edx, 0xc(%rax,%rcx,8)
movl 0x34(%rsp), %edx
movw %dx, 0x10(%rax,%rcx,8)
movb %r11b, 0x12(%rax,%rcx,8)
movq %r9, 0x18(%rax,%rcx,8)
movq $0x1, 0x20(%rax,%rcx,8)
movl 0x14(%rsp), %r14d
jmp 0x10dd3b
movl %r9d, 0x10(%rsp)
movl %edx, 0x20(%rsp)
callq 0x132fd
movl 0x20(%rsp), %edx
movl 0x10(%rsp), %r9d
movq 0x58(%rsp), %rsi
movq 0x8(%rsp), %r8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x50(%rsp)
movl $0x0, 0x40(%rsp)
jmp 0x10e037
movl %r9d, 0x10(%rsp)
movl %edx, 0x20(%rsp)
callq 0x1331f
movl 0x20(%rsp), %edx
movl 0x10(%rsp), %r9d
movq 0x58(%rsp), %rsi
movq 0x8(%rsp), %r8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x10e06e
movq 0x70(%rsp), %rcx
leaq 0x1(%rcx), %r10
cmpq 0x100(%rsp), %rcx
movq 0x90(%rsp), %rsi
jne 0x10dbd5
movq 0x120(%rsp), %rbp
movq (%rbp), %rsi
testq %rsi, %rsi
movq 0x150(%rsp), %r14
movq 0x140(%rsp), %r15
movq 0x148(%rsp), %r12
movq 0x138(%rsp), %r13
je 0x10e38c
movq 0x10(%rbp), %rdi
leaq -0x3bf9(%rip), %rcx     # 0x10a674
movl $0x28, %ebx
movl $0x28, %edx
callq 0x23f0
movl $0x1, %esi
cmpq $0x2, (%rbp)
jl 0x10e316
movl $0x1, %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x10(%rbp), %rdi
leaq (%rsi,%rsi,4), %r8
leaq (%rdi,%r8,8), %r9
movq -0x10(%r9), %r10
leaq (%rdi,%rbx), %rdx
cmpq 0x18(%rdi,%rbx), %r10
jne 0x10e2e6
movq 0x20(%rdi,%rbx), %r10
addq %r10, -0x8(%r9)
movl 0xc(%rdi,%rbx), %r10d
andl %ecx, %r10d
movl -0x1c(%r9), %r9d
andl %ecx, %r9d
cmpl %r9d, %r10d
jbe 0x10e301
leaq (%rdi,%r8,8), %rdi
addq $-0x28, %rdi
movq 0x10(%rdx), %r8
movq %r8, 0x10(%rdi)
movups (%rdx), %xmm0
movups %xmm0, (%rdi)
jmp 0x10e301
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 %rax
addq $0x28, %rbx
movq 0x120(%rsp), %rbp
cmpq (%rbp), %rax
jl 0x10e296
movq %rsi, (%rbp)
movq 0x10(%rbp), %rdi
movq 0x128(%rsp), %rbx
movq %rbx, %rdx
movq 0x130(%rsp), %rcx
callq 0x10ca61
movq (%rbx), %rax
testq %rax, %rax
je 0x10e38c
movq 0x10(%rbp), %rcx
movq 0x128(%rsp), %rdx
movq 0x10(%rdx), %rdx
movslq (%rdx), %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0x1c(%rcx,%rsi,8), %esi
movl %esi, (%r13)
movslq (%rdx), %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0x18(%rcx,%rsi,8), %esi
movl %esi, (%r12)
movslq -0x4(%rdx,%rax,4), %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0x1c(%rcx,%rsi,8), %esi
movl %esi, (%r15)
movslq -0x4(%rdx,%rax,4), %rax
leaq (%rax,%rax,4), %rax
movl 0x18(%rcx,%rax,8), %eax
movl %eax, (%r14)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq