// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq 0x10(%rbp), %rax
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl -0x4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0xd2066
movq -0x48(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
je 0xd1f92
jmp 0xd2055
movq -0x48(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0xd2006
movl -0x3c(%rbp), %eax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0xd1fee
movl -0x3c(%rbp), %edx
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, (%rax)
movl -0x40(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0xd2004
movl -0x40(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xd2006
movl -0x40(%rbp), %eax
cmpq -0x20(%rbp), %rax
jne 0xd2053
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0xd203b
movl -0x3c(%rbp), %edx
movq -0x30(%rbp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, (%rax)
movl -0x40(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0xd2051
movl -0x40(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xd2053
jmp 0xd2055
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xd1f5d
cmpq $0x0, -0x18(%rbp)
je 0xd21a9
movq $0x0, -0x50(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x18(%rbp), %rdi
movl -0x4(%rbp), %eax
movl %eax, %esi
leaq -0x58(%rbp), %rdx
callq 0x2f2900
movq %rax, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0xd2161
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x50(%rbp), %rcx
movq -0x60(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rax
movq -0x50(%rbp), %rcx
movq -0x60(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
jne 0xd210f
movq -0x68(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
jne 0xd2111
jmp 0xd2150
movq -0x68(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x68(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0xd214e
movl -0x40(%rbp), %eax
cmpq -0x20(%rbp), %rax
jne 0xd214e
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq 0x10(%rbp), %rax
jb 0xd214c
jmp 0xd2161
jmp 0xd214e
jmp 0xd2150
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xd20a7
movq -0x70(%rbp), %rax
cmpq 0x10(%rbp), %rax
jb 0xd21a7
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $0x80000000, %ecx       # imm = 0x80000000
orl $0x0, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x30(%rbp), %rax
movl (%rax), %ecx
andl $0x80000000, %ecx       # imm = 0x80000000
orl $0x0, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xd21a9
addq $0x70, %rsp
popq %rbp
retq
nop