// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_base_boundary_chain(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*)
int get_base_boundary_chain(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)
{
    long long k, j, l, 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;
    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 0;

    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, l = 0, m = 0; k <= (long long)u_buffer->a.n; ++k)
    {
        if (k == (long long)u_buffer->a.n || u_buffer->a.a[k].Off != u_buffer->a.a[l].Off) 
        {
            u_buffer->a.a[m] = u_buffer->a.a[l];
            for (l += 1; l < k; l++)
            {
                u_buffer->a.a[m].weight += u_buffer->a.a[l].weight;
                if(u_buffer->a.a[l].x.ol > u_buffer->a.a[m].x.ol)
                {
                    u_buffer->a.a[m].x = u_buffer->a.a[l].x;
                }
            }
            l = 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 0;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, 0x78(%rsp)
movq %rdi, 0x88(%rsp)
movq 0x120(%rsp), %rax
movq 0xf8(%rsp), %rcx
movq $0x0, (%rax)
cmpq 0x100(%rsp), %rcx
jg 0x10ed68
movl %r9d, %r15d
movq %rdx, %r12
movq %r8, 0x68(%rsp)
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl %eax, 0x34(%rsp)
movq %rsi, 0x90(%rsp)
movl %r9d, 0x14(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x70(%rsp)
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
cmpl $0x0, 0xc(%rsi,%rax,8)
je 0x10ec46
leaq (%rsi,%rax,8), %r8
movl $0x21, %ebx
xorl %r14d, %r14d
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 0x10e773
movq 0x78(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%r13,8)
js 0x10e773
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 0x10e6a9
movl -0x11(%rcx,%rbx), %r10d
movl -0xd(%rcx,%rbx), %r8d
subl %r8d, %esi
movl %esi, %edi
movl %r10d, %esi
jmp 0x10e6b9
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 %r15d, %ebp
jg 0x10e766
cmpl %r15d, %r11d
jg 0x10e766
movq %rax, 0x60(%rsp)
movl %r9d, %r15d
subl 0x48(%rsp), %r15d
cvtsi2ss %r15, %xmm1
addl %ebp, %r15d
addl %r11d, %r15d
cvtsi2ss %r15, %xmm2
leaq 0x18702a(%rip), %rax    # 0x295758
movss 0x158(%rax), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0x10e761
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 0x10e78c
movl 0x14(%rsp), %r15d
movq %rdx, %r12
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r8
incq %r14
movl 0xc(%r8), %eax
addq $0x28, %rbx
cmpq %rax, %r14
jb 0x10e646
jmp 0x10ec46
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 0x10e761
orb %r8b, %r10b
movq %rdx, %r12
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
je 0x10ea47
movl 0xd0(%rsp), %edx
cmpl %edx, %r15d
jl 0x10ea47
cmpl %edx, %r11d
movl 0x14(%rsp), %r15d
jl 0x10e76e
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 0x10e76e
movl %r13d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax,%rsi,8)
js 0x10e76e
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 0x10e76e
movl 0x3c(%rsp), %eax
cmpl $-0x1, %eax
movq 0x8(%rsp), %r8
je 0x10e773
cmpl 0xf0(%rsp), %eax
jne 0x10e773
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 0x10e76e
movl 0x3c(%rsp), %eax
cmpl $-0x1, %eax
movq 0x8(%rsp), %r8
je 0x10e773
cmpl 0xe8(%rsp), %eax
jne 0x10e773
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 0x10e773
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 0x10e773
cmpq 0x108(%rsp), %rax
setl %cl
cmpq 0x110(%rsp), %rax
setg %al
orb %cl, %al
jne 0x10e773
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 0x10ea51
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 0x10ea6f
movl 0x14(%rsp), %r15d
jmp 0x10e76e
xorl %eax, %r10d
cmpl $0x1, %r10d
movq 0x58(%rsp), %rsi
jne 0x10ebe3
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 0x10eaab
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 0x10eae5
xorl 0x28(%rsp), %r13d
cmpl $0x1, %r13d
jne 0x10ec1b
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 0x10eaf9
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl %edx, %eax
jae 0x10eafb
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 0x10e773
cmpl %edx, %ecx
jae 0x10e773
movq 0x120(%rsp), %r15
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x10eb80
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r15)
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(%r15)
movq (%r15), %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, (%r15)
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), %r15d
jmp 0x10e773
movl %r9d, 0x10(%rsp)
movl %edx, 0x20(%rsp)
callq 0x13341
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 0x10ea6f
movl %r9d, 0x10(%rsp)
movl %edx, 0x20(%rsp)
callq 0x13363
movl 0x20(%rsp), %edx
movl 0x10(%rsp), %r9d
movq 0x58(%rsp), %rsi
movq 0x8(%rsp), %r8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x10eaa6
movq 0x70(%rsp), %rcx
leaq 0x1(%rcx), %rax
cmpq 0x100(%rsp), %rcx
movq %rax, %rcx
movq 0x90(%rsp), %rsi
jne 0x10e60d
movq 0x120(%rsp), %rbx
movq (%rbx), %rsi
testq %rsi, %rsi
je 0x10ed68
movq 0x10(%rbx), %rdi
leaq -0x4613(%rip), %rcx     # 0x10a674
movl $0x28, %edx
callq 0x23f0
movq (%rbx), %rdi
testq %rdi, %rdi
jle 0x10ed6c
movl $0x1, %eax
xorl %edx, %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
movq 0x10(%rbx), %r8
cmpq %rdi, %rax
je 0x10eccc
leaq (%rax,%rax,4), %r9
movq 0x18(%r8,%r9,8), %r9
leaq (%rdx,%rdx,4), %r10
cmpq 0x18(%r8,%r10,8), %r9
je 0x10ed56
leaq (%rdx,%rdx,4), %r9
leaq (%rsi,%rsi,4), %rdi
movq 0x20(%r8,%r9,8), %r10
movq %r10, 0x20(%r8,%rdi,8)
movups (%r8,%r9,8), %xmm0
movups 0x10(%r8,%r9,8), %xmm1
movups %xmm1, 0x10(%r8,%rdi,8)
movups %xmm0, (%r8,%rdi,8)
incq %rdx
cmpq %rax, %rdx
jge 0x10ed4d
leaq 0x48(,%r9,8), %r8
movq 0x10(%rbx), %r9
movq (%r9,%r8), %r10
addq %r10, 0x20(%r9,%rdi,8)
movl -0x14(%r9,%r8), %r10d
andl %ecx, %r10d
movl 0xc(%r9,%rdi,8), %r11d
andl %ecx, %r11d
cmpl %r11d, %r10d
jbe 0x10ed41
leaq (%r9,%rdi,8), %r10
addq %r8, %r9
addq $-0x20, %r9
movq 0x10(%r9), %r11
movq %r11, 0x10(%r10)
movups (%r9), %xmm0
movups %xmm0, (%r10)
incq %rdx
addq $0x28, %r8
cmpq %rax, %rdx
jl 0x10ed04
incq %rsi
movq (%rbx), %rdi
movq %rax, %rdx
leaq 0x1(%rax), %r8
cmpq %rdi, %rax
movq %r8, %rax
jl 0x10ecab
jmp 0x10ed6e
xorl %eax, %eax
jmp 0x10ed96
xorl %esi, %esi
movq %rsi, (%rbx)
movq 0x10(%rbx), %rdi
movq 0x128(%rsp), %rbx
movq %rbx, %rdx
movq 0x130(%rsp), %rcx
callq 0x10ca61
xorl %eax, %eax
cmpq $0x0, (%rbx)
setne %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq