// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x90(%rbp), %rax
movq 0x88(%rbp), %rax
movq 0x80(%rbp), %rax
movq 0x78(%rbp), %rax
movq 0x70(%rbp), %rax
movq 0x68(%rbp), %rax
movq 0x60(%rbp), %rax
movl 0x58(%rbp), %eax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movl 0x30(%rbp), %eax
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq 0x60(%rbp), %rax
movq $0x0, (%rax)
movq 0x90(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x88(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x80(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x78(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq 0x40(%rbp), %rax
jg 0x175145
movq -0x10(%rbp), %rax
movq 0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0x175132
movq -0x58(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x174daa
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x60(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x174daf
jmp 0x175121
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x68(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %esi
movq -0x70(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x70(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
movl -0x2c(%rbp), %ecx
leaq 0x24fa13(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl 0x10(%rbp), %r8d
leaq -0x90(%rbp), %r9
callq 0x172eb0
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jge 0x174e0b
jmp 0x175121
movl -0x88(%rbp), %eax
shrl %eax
movl %eax, -0x94(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x94(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x174e37
jmp 0x175121
movq -0x8(%rbp), %rdi
movl -0x94(%rbp), %esi
leaq -0x98(%rbp), %rdx
leaq -0x9c(%rbp), %rcx
callq 0xd3d50
cmpl $0x0, -0x9c(%rbp)
je 0x174e66
cmpl $-0x1, -0x98(%rbp)
jne 0x174e6b
jmp 0x175121
movl -0x98(%rbp), %eax
cmpl 0x30(%rbp), %eax
je 0x174e7b
jmp 0x175121
movq 0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
movl -0xa0(%rbp), %esi
shrl %esi
leaq -0x98(%rbp), %rdx
leaq -0x9c(%rbp), %rcx
callq 0xd3d50
cmpl $0x0, -0x9c(%rbp)
je 0x174ec6
cmpl $-0x1, -0x98(%rbp)
jne 0x174ecb
jmp 0x175121
movl -0x98(%rbp), %eax
cmpl 0x28(%rbp), %eax
je 0x174edb
jmp 0x175121
movq -0x28(%rbp), %rax
movl -0xa0(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
cmpq -0x38(%rbp), %rax
je 0x174efa
jmp 0x175121
movq 0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movl -0x94(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0xa4(%rbp)
movq -0x90(%rbp), %rdx
shrq $0x20, %rdx
movl -0xa0(%rbp), %eax
movl %eax, %esi
xorl %eax, %eax
movl $0x1, %ecx
cmpq %rsi, %rdx
cmovel %ecx, %eax
movl %eax, -0xa8(%rbp)
movl -0x88(%rbp), %edx
movl -0xa4(%rbp), %esi
xorl %eax, %eax
movl $0x1, %ecx
cmpl %esi, %edx
cmovel %ecx, %eax
movl %eax, -0xac(%rbp)
cmpl $0x0, 0x58(%rbp)
jne 0x174f7d
movl -0xa8(%rbp), %eax
cmpl -0xac(%rbp), %eax
je 0x174f7d
jmp 0x175121
cmpl $0x1, 0x58(%rbp)
jne 0x174f96
movl -0xa8(%rbp), %eax
cmpl -0xac(%rbp), %eax
jne 0x174f96
jmp 0x175121
movq -0x28(%rbp), %rax
movl -0x94(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq 0x48(%rbp), %rax
jl 0x174fbe
movq -0x48(%rbp), %rax
cmpq 0x50(%rbp), %rax
jle 0x174fc3
jmp 0x175121
movq -0x20(%rbp), %rdi
movl -0xa0(%rbp), %edx
movl -0xa4(%rbp), %ecx
movq 0x18(%rbp), %rax
movl (%rax), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x20(%rbp), %rax
movl (%rax), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq -0x28(%rbp), %r10
leaq -0x90(%rbp), %rax
addq $0x12, %rax
leaq -0x90(%rbp), %rsi
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1859f0
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
shrq $0x20, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x17503b
movq -0xb8(%rbp), %rax
cmpl $-0x1, %eax
jne 0x175040
jmp 0x175121
movq -0xb8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq $0x1, -0xc0(%rbp)
movq 0x60(%rbp), %rax
movq (%rax), %rax
movq 0x60(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1750ee
movq 0x60(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1750b0
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xe8(%rbp)
jmp 0x1750be
movl $0x2, %eax
movq %rax, -0xe8(%rbp)
jmp 0x1750be
movq -0xe8(%rbp), %rcx
movq 0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rax
imulq $0x28, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq -0xe0(%rbp), %rsi
movl $0x28, %edx
callq 0x2180
jmp 0x175121
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x174d29
jmp 0x175134
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x174cf0
movq 0x60(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x175154
jmp 0x1753c0
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rax
movq (%rax), %rsi
movl $0x28, %edx
leaq -0x4acf(%rip), %rcx     # 0x1706a0
callq 0x23e0
movq $0x1, -0x38(%rbp)
movq $0x1, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq 0x60(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x1752bf
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
subq $0x1, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq 0x60(%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, -0x38(%rbp), %rdx
addq %rdx, %rcx
cmpq 0x18(%rcx), %rax
jne 0x175278
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq 0x20(%rax), %rcx
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
addq 0x20(%rax), %rcx
movq %rcx, 0x20(%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x60(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x50(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jle 0x175276
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rcx
imulq $0x28, -0x38(%rbp), %rax
addq %rax, %rcx
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x1752ae
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rsi
imulq $0x28, -0x38(%rbp), %rax
addq %rax, %rsi
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
imulq $0x28, -0x50(%rbp), %rax
addq %rax, %rdi
movl $0x28, %edx
callq 0x2180
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x175184
movq -0x50(%rbp), %rcx
movq 0x60(%rbp), %rax
movq %rcx, (%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rax
movq (%rax), %rax
movl %eax, %esi
movq 0x68(%rbp), %rdx
movq 0x70(%rbp), %rcx
callq 0x1732b0
movq 0x68(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x1752f7
jmp 0x1753c0
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x68(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq (%rcx), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
movl %eax, %eax
movl %eax, %ecx
movq 0x78(%rbp), %rax
movl %ecx, (%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x68(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq (%rcx), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, %eax
movl %eax, %ecx
movq 0x88(%rbp), %rax
movl %ecx, (%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x68(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x68(%rbp), %rdx
movq (%rdx), %rdx
subq $0x1, %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
movl %eax, %eax
movl %eax, %ecx
movq 0x80(%rbp), %rax
movl %ecx, (%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x68(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x68(%rbp), %rdx
movq (%rdx), %rdx
subq $0x1, %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, %eax
movl %eax, %ecx
movq 0x90(%rbp), %rax
movl %ecx, (%rax)
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)