// File: /workspace/github_cpp_makefile_star2_O2/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
movq 0x40(%rsp), %rbp
movl %edi, %esi
imulq $0x18, %rsi, %rcx
addq %rax, %rcx
xorl %eax, %eax
xorl %edx, %edx
movl 0xc(%rcx), %edi
cmpq %rdi, %rdx
jae 0x6b468
movq (%rcx), %rdi
cmpq $0x0, 0x18(%rdi,%rax)
js 0x6b45f
movl (%rdi,%rax), %r8d
movl 0x8(%rdi,%rax), %edi
testl %r8d, %r8d
jne 0x6b433
movl (%r15), %r9d
testl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
je 0x6b429
andl $0x80000000, %r9d       # imm = 0x80000000
movl %r9d, (%r15)
cmpl 0x4(%r15), %edi
jbe 0x6b433
movl %edi, 0x4(%r15)
cmpq %r14, %rdi
jne 0x6b45f
movl (%rbx), %r9d
movl %r9d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpl %r8d, %r10d
jbe 0x6b457
andl $0x80000000, %r9d       # imm = 0x80000000
orl %r8d, %r9d
movl %r9d, (%rbx)
cmpl 0x4(%rbx), %edi
jbe 0x6b45f
movl %edi, 0x4(%rbx)
incq %rdx
addq $0x28, %rax
jmp 0x6b3f3
testq %r12, %r12
je 0x6b4e8
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rdx
callq 0x1948fc
xorl %edx, %edx
movq (%r13), %rsi
xorl %ecx, %ecx
cmpq %rsi, %rdx
jae 0x6b4d1
movq 0x48(%r12), %rdi
movq (%rax,%rdx,8), %r8
movl %r8d, %r9d
shrq $0x20, %r8
imulq $0x58, %r8, %r8
movq 0x30(%rdi,%r8), %rdi
imulq $0x24, %r9, %r8
movb 0x20(%rdi,%r8), %r9b
andb $-0x40, %r9b
negb %r9b
jno 0x6b4cc
addq %r8, %rdi
cmpl $0x0, 0xc(%rdi)
jne 0x6b4cc
movl 0x10(%rdi), %edi
cmpq %r14, %rdi
jne 0x6b4cc
incq %rcx
cmpq %rbp, %rcx
jae 0x6b4d1
incq %rdx
jmp 0x6b483
cmpq %rbp, %rcx
jb 0x6b4e8
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