// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_base_boundary(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, unsigned int*, unsigned int*)
void get_base_boundary(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, uint32_t* x_off, uint32_t* y_off)
{
    long long k, j, offset;
    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, is_found = 0, oLen = 0;
    uint64_t tmp;
    (*x_off) = (*y_off) = (uint32_t)-1;
    if(dir == 1)
    {
        for (k = xEndIndex; k >= xBegIndex; k--)
        {
            xR = &(reverse_sources[xReads->a[k]>>33]);
            is_found = 0; oLen = 0;
            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;

                ///if(is_found == 0 || ((uint32_t)(tmp>>32) > (*x_off) && ((uint32_t)tmp) > (*y_off)))
                if(is_found == 0 || t.ol > oLen)
                {
                    (*x_off) = tmp>>32;
                    (*y_off) = (uint32_t)tmp;
                    oLen = t.ol;
                }

                is_found = 1; 
            }
            if(is_found) return;
        }   
    }
    else
    {
        for (k = xBegIndex; k <= xEndIndex; k++)
        {
            xR = &(reverse_sources[xReads->a[k]>>33]);
            is_found = 0; oLen = 0;
            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;

                ///if(is_found == 0 || ((uint32_t)(tmp>>32) < (*x_off) && ((uint32_t)tmp) < (*y_off)))
                if(is_found == 0 || t.ol > oLen)
                {
                    (*x_off) = tmp>>32;
                    (*y_off) = (uint32_t)tmp;
                    oLen = t.ol;
                }  

                is_found = 1;         
            }
            if(is_found) return;
        }
    }


    (*x_off) = (*y_off) = (uint32_t)-1;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, 0x2c(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, 0x60(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x100(%rsp), %rcx
movq 0xf8(%rsp), %rax
movq 0xd0(%rsp), %rdx
orl $-0x1, (%rcx)
movq 0xc8(%rsp), %rcx
orl $-0x1, (%rax)
movl 0xe8(%rsp), %eax
cmpl $0x1, %eax
movq %r13, 0x8(%rsp)
jne 0xc96d7
cmpq %rcx, %rdx
jl 0xc99a1
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rdx, 0x20(%rsp)
movq (%rax,%rdx,8), %rax
shrq $0x21, %rax
imulq $0x18, %rax, %rbx
addq 0x60(%rsp), %rbx
xorl %r15d, %r15d
xorl %r14d, %r14d
movl $0x0, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0xc(%rbx), %eax
cmpq %rax, %r14
jae 0xc96b7
movq (%rbx), %rdi
movl 0xc(%rdi,%r15), %edx
imulq $0xc, %rdx, %rcx
movl (%r12,%rcx), %eax
testl %eax, %eax
js 0xc96ab
movq 0x20(%r13), %rbp
cmpl $0x0, (%rbp,%rdx,8)
js 0xc96ab
movl 0x4(%rdi,%r15), %edx
addq %r15, %rdi
imulq $0xc, %rdx, %rdx
addq %r12, %rdx
addq %r12, %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 0x18a2aa(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x2c(%rsp), %ecx
movl 0xa0(%rsp), %r8d
leaq 0x40(%rsp), %r9
callq 0xc99c6
testl %eax, %eax
js 0xc96ab
movl 0x48(%rsp), %r13d
shrl %r13d
cmpl $0x0, (%rbp,%r13,8)
js 0xc96a6
movq 0x38(%rsp), %rdi
movl %r13d, %esi
leaq 0x18(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x6c2a2
cmpl $0x0, 0x14(%rsp)
je 0xc96a6
movl 0x18(%rsp), %eax
cmpl $-0x1, %eax
je 0xc96a6
cmpl 0xc0(%rsp), %eax
jne 0xc96a6
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rbp
movq %rbp, 0x58(%rsp)
shrq $0x21, %rbp
movq 0x38(%rsp), %rdi
movl %ebp, %esi
leaq 0x18(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x6c2a2
cmpl $0x0, 0x14(%rsp)
je 0xc96a6
movl 0x18(%rsp), %eax
cmpl $-0x1, %eax
je 0xc96a6
cmpl 0xb8(%rsp), %eax
jne 0xc96a6
movl %ebp, %eax
movq 0x30(%rsp), %rdi
movl (%rdi,%rax,8), %eax
cmpq 0x20(%rsp), %rax
jne 0xc96a6
movq 0x58(%rsp), %rdx
shrq $0x20, %rdx
movl 0xf0(%rsp), %eax
cmpl $0x1, %eax
setne %r9b
testl %eax, %eax
setne %sil
movq 0xb0(%rsp), %rax
movq 0x18(%rax), %rcx
movl (%rdi,%r13,8), %eax
cmpl %edx, 0x44(%rsp)
movl 0x4(%rcx,%rax,8), %ecx
sete %dil
cmpl %ecx, 0x48(%rsp)
sete %r8b
xorb %dil, %r8b
orb %r8b, %r9b
notb %r8b
orb %sil, %r8b
andb %r9b, %r8b
testb $0x1, %r8b
je 0xc96a6
cmpq 0xd8(%rsp), %rax
setl %sil
cmpq 0xe0(%rsp), %rax
setg %al
orb %sil, %al
movq 0x8(%rsp), %r13
jne 0xc96ab
movq 0xa8(%rsp), %rax
movl (%rax), %r8d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
andl %ebp, %r8d
movq 0xb0(%rsp), %rax
movl (%rax), %r9d
andl %ebp, %r9d
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x52(%rsp), %rax
pushq %rax
pushq 0x38(%rsp)
callq 0xd3d59
addq $0x10, %rsp
movq %rax, %rcx
shrq $0x20, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
sete %dl
cmpl $-0x1, %eax
sete %sil
orb %dl, %sil
jne 0xc96ab
movl 0x4c(%rsp), %edx
andl %ebp, %edx
cmpl $0x0, 0x10(%rsp)
je 0xc9685
pushq $0x1
popq %rsi
movl %esi, 0x10(%rsp)
cmpl 0x1c(%rsp), %edx
jbe 0xc96ab
movq 0xf8(%rsp), %rsi
movl %ecx, (%rsi)
movq 0x100(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
movl %edx, 0x1c(%rsp)
jmp 0xc96ab
movq 0x8(%rsp), %r13
incq %r14
addq $0x28, %r15
jmp 0xc944d
movq 0x20(%rsp), %rdx
decq %rdx
cmpl $0x0, 0x10(%rsp)
movq 0xc8(%rsp), %rcx
je 0xc940c
jmp 0xc99b7
cmpq %rdx, %rcx
jg 0xc99a1
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
imulq $0x18, %rax, %rbx
addq 0x60(%rsp), %rbx
xorl %r15d, %r15d
xorl %ebp, %ebp
movl $0x0, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0xc(%rbx), %eax
cmpq %rax, %rbp
jae 0xc9984
movq (%rbx), %rdi
movl 0xc(%rdi,%r15), %edx
imulq $0xc, %rdx, %rcx
movl (%r12,%rcx), %eax
testl %eax, %eax
js 0xc9978
movq 0x20(%r13), %r14
cmpl $0x0, (%r14,%rdx,8)
js 0xc9978
movl 0x4(%rdi,%r15), %edx
addq %r15, %rdi
imulq $0xc, %rdx, %rdx
addq %r12, %rdx
addq %r12, %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 0x189fe0(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x2c(%rsp), %ecx
movl 0xa0(%rsp), %r8d
leaq 0x40(%rsp), %r9
callq 0xc99c6
testl %eax, %eax
js 0xc9978
movl 0x48(%rsp), %r13d
shrl %r13d
cmpl $0x0, (%r14,%r13,8)
js 0xc9973
movq 0x38(%rsp), %rdi
movl %r13d, %esi
leaq 0x18(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x6c2a2
cmpl $0x0, 0x14(%rsp)
je 0xc9973
movl 0x18(%rsp), %eax
cmpl $-0x1, %eax
je 0xc9973
cmpl 0xc0(%rsp), %eax
jne 0xc9973
movq 0xa8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %r14
movq %r14, 0x58(%rsp)
shrq $0x21, %r14
movq 0x38(%rsp), %rdi
movl %r14d, %esi
leaq 0x18(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x6c2a2
cmpl $0x0, 0x14(%rsp)
je 0xc9973
movl 0x18(%rsp), %eax
cmpl $-0x1, %eax
je 0xc9973
cmpl 0xb8(%rsp), %eax
jne 0xc9973
movl %r14d, %eax
movq 0x30(%rsp), %rdi
movl (%rdi,%rax,8), %eax
cmpq 0x20(%rsp), %rax
jne 0xc9973
movq 0x58(%rsp), %rdx
shrq $0x20, %rdx
movl 0xf0(%rsp), %eax
cmpl $0x1, %eax
setne %r9b
testl %eax, %eax
setne %sil
movq 0xb0(%rsp), %rax
movq 0x18(%rax), %rcx
movl (%rdi,%r13,8), %eax
cmpl %edx, 0x44(%rsp)
movl 0x4(%rcx,%rax,8), %ecx
sete %dil
cmpl %ecx, 0x48(%rsp)
sete %r8b
xorb %dil, %r8b
orb %r8b, %r9b
notb %r8b
orb %sil, %r8b
andb %r9b, %r8b
testb $0x1, %r8b
je 0xc9973
cmpq 0xd8(%rsp), %rax
setl %sil
cmpq 0xe0(%rsp), %rax
setg %al
orb %sil, %al
movq 0x8(%rsp), %r13
jne 0xc9978
movq 0xa8(%rsp), %rax
movl (%rax), %r8d
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andl %r14d, %r8d
movq 0xb0(%rsp), %rax
movl (%rax), %r9d
andl %r14d, %r9d
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x52(%rsp), %rax
pushq %rax
pushq 0x38(%rsp)
callq 0xd3d59
addq $0x10, %rsp
movq %rax, %rcx
shrq $0x20, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
sete %dl
cmpl $-0x1, %eax
sete %sil
orb %dl, %sil
jne 0xc9978
movl 0x4c(%rsp), %edx
andl %r14d, %edx
cmpl $0x0, 0x10(%rsp)
je 0xc9952
pushq $0x1
popq %rsi
movl %esi, 0x10(%rsp)
cmpl 0x1c(%rsp), %edx
jbe 0xc9978
movq 0xf8(%rsp), %rsi
movl %ecx, (%rsi)
movq 0x100(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
movl %edx, 0x1c(%rsp)
jmp 0xc9978
movq 0x8(%rsp), %r13
incq %rbp
addq $0x28, %r15
jmp 0xc9717
movq 0x20(%rsp), %rcx
incq %rcx
cmpl $0x0, 0x10(%rsp)
movq 0xd0(%rsp), %rdx
je 0xc96d7
jmp 0xc99b7
movq 0x100(%rsp), %rax
orl $-0x1, (%rax)
movq 0xf8(%rsp), %rax
orl $-0x1, (%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq