// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: collect_sides(unsigned int, ma_hit_t_alloc*, all_ul_t*, unsigned long, ma_sub_t*, ma_sub_t*, unsigned long)
void collect_sides(uint32_t rid, ma_hit_t_alloc* pafs, all_ul_t *x, uint64_t rLen, ma_sub_t* max_left, ma_sub_t* max_right, uint64_t ul_thres)
{
    long long j;
    uint32_t qs, qe;
    ma_hit_t_alloc *paf = (&pafs[rid]);
    for (j = 0; j < paf->length; j++)
    {
        if(paf->buffer[j].del) continue;

        qs = Get_qs(paf->buffer[j]);
        qe = Get_qe(paf->buffer[j]);


        ///overlaps from left side
        if(qs == 0)
        {
            if(qs < max_left->s) max_left->s = qs;
            if(qe > max_left->e) max_left->e = qe;
        }

        ///overlaps from right side
        if(qe == rLen)
        {
            if(qs < max_right->s) max_right->s = qs;
            if(qe > max_right->e) max_right->e = qe;
        }

        ///note: if (qs == 0 && qe == rLen)
        ///this overlap would be added to both b_left and b_right
        ///that is what we want
    }
    
    if(x) {
        uint64_t *a = NULL, a_n, k;
        uc_block_t *p = NULL; uint64_t cc = 0;
        a = get_hifi2ul_list(x, rid, &a_n);
        for (k = 0; k < a_n; k++) {
            p = &(x->a[a[k]>>32].bb.a[(uint32_t)(a[k])]);
            if(p->base||(!p->el)) continue;
            qs = p->ts; qe = p->te;///note here is ts && te, instead of qs && qe
            ///for UL, we only use overlaps which cover the whole HiFi read
            if(qs == 0 && qe == rLen){
                cc++;
                if(cc >= ul_thres) break; 
            }
        }
        if(cc >= ul_thres) {
            max_left->s = 0; max_left->e = rLen;
            max_right->s = 0; max_right->e = rLen;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r9, %rbx
movq %r8, %r15
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %rax
movl %edi, %esi
leaq (%rsi,%rsi,2), %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0xa26d8
leaq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq (%rax), %rdi
cmpq $0x0, 0x18(%rdi,%rcx)
js 0xa26c9
movl (%rdi,%rcx), %r8d
movl 0x8(%rdi,%rcx), %edi
testl %r8d, %r8d
jne 0xa2696
movl (%r15), %r9d
testl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
je 0xa268c
andl $0x80000000, %r9d       # imm = 0x80000000
movl %r9d, (%r15)
cmpl 0x4(%r15), %edi
jbe 0xa2696
movl %edi, 0x4(%r15)
cmpq %r14, %rdi
jne 0xa26c9
movl (%rbx), %r9d
movl %r9d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpl %r8d, %r10d
jbe 0xa26c1
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl $0x80000000, %r9d       # imm = 0x80000000
orl %r8d, %r9d
movl %r9d, (%rbx)
cmpl 0x4(%rbx), %edi
jbe 0xa26c9
movl %edi, 0x4(%rbx)
incq %rdx
movl 0xc(%rax), %edi
addq $0x28, %rcx
cmpq %rdi, %rdx
jb 0xa265e
testq %r12, %r12
je 0xa276b
movq 0x40(%rsp), %rbp
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rdx
callq 0x1d79fc
movq (%r13), %rcx
testq %rcx, %rcx
je 0xa2752
movq 0x48(%r12), %rsi
xorl %edx, %edx
xorl %edi, %edi
movq (%rax,%rdi,8), %r8
movl %r8d, %r9d
shrq $0x20, %r8
imulq $0x58, %r8, %r8
movq 0x30(%rsi,%r8), %r8
leaq (%r9,%r9,8), %r9
movb 0x20(%r8,%r9,4), %r10b
andb $-0x40, %r10b
negb %r10b
jno 0xa2748
leaq (%r8,%r9,4), %r8
cmpl $0x0, 0xc(%r8)
jne 0xa2748
movl 0x10(%r8), %r8d
cmpq %r14, %r8
jne 0xa2748
incq %rdx
cmpq %rbp, %rdx
jae 0xa2754
incq %rdi
cmpq %rdi, %rcx
jne 0xa2706
jmp 0xa2754
xorl %edx, %edx
cmpq %rbp, %rdx
jb 0xa276b
movl $0x80000000, %eax       # imm = 0x80000000
andl %eax, (%r15)
movl %r14d, 0x4(%r15)
andl %eax, (%rbx)
movl %r14d, 0x4(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq