// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: query_reverse_sources(asg_t*, ma_hit_t_alloc*, R_to_U*, unsigned int, unsigned int, kvec_t_u64_warp*)
void query_reverse_sources(asg_t *read_g, ma_hit_t_alloc* reverse_sources,
R_to_U* ruIndex, uint32_t qn, uint32_t self_offset, kvec_t_u64_warp* u_vecs)
{
    uint32_t i, rId, is_Unitig, cId;
    uint64_t mode;
    ///means the reads coming from different haplotype have already been purged
    if(read_g->seq[qn].c == HAP_LABLE)
    {
        cId = (uint32_t)-1;
        mode = self_offset; mode = mode << 33; mode = mode|cId; mode = mode | (uint64_t)(0x100000000);
        kv_push(uint64_t, u_vecs->a, mode);
        return;
    }


    for (i = 0; i < reverse_sources[qn].length; i++)
    {
        rId = Get_tn(reverse_sources[qn].buffer[i]);
        ///there are three 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
        ///3. read has bee deleted, get_R_to_U() return the id of read that contains it 
        if(read_g->seq[rId].del == 1)
        {
            ///get the id of read that contains it 
            get_R_to_U(ruIndex, rId, &rId, &is_Unitig);
            if(rId == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[rId].del == 1) continue;
        }

        ///here rId is the id of the read coming from the different haplotype
        ///cId is the id of the corresponding contig (note here is the contig, instead of untig)
        get_R_to_U(ruIndex, rId, &cId, &is_Unitig);
        if(is_Unitig == 0) continue;
        ///if the read is at alternative contigs, cId might be (uint32_t)-1
        ///if(cId == (uint32_t)-1)
        mode = self_offset; mode = mode << 33; mode = mode|(uint64_t)(cId);
        kv_push(uint64_t, u_vecs->a, mode);
    }
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %rbx
movl %r8d, %r15d
movq %rsi, %r9
movq 0x20(%rdi), %rsi
movl %ecx, %eax
cmpb $0x2, 0x4(%rsi,%rax,8)
jne 0xac5ff
shlq $0x21, %r15
movabsq $0x1ffffffff, %r14      # imm = 0x1FFFFFFFF
orq %r15, %r14
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0xac6eb
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rsi
cmovneq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
jmp 0xac6ef
movq %rdx, %r12
movq %rdi, %r10
imulq $0x18, %rax, %rax
addq %rax, %r9
shlq $0x21, %r15
pushq $0xc
popq %rbp
xorl %r14d, %r14d
pushq $-0x1
popq %r11
movq %rbx, 0x10(%rsp)
movq %r9, 0x8(%rsp)
movl 0xc(%r9), %eax
cmpq %rax, %r14
jae 0xac6fa
movq (%r9), %rax
movl (%rax,%rbp), %eax
movq 0x20(%r10), %rdi
cmpl $0x0, (%rdi,%rax,8)
movq (%r12), %rcx
js 0xac6c2
movl %r11d, %r13d
cmpq %rax, %rcx
jbe 0xac669
movq 0x8(%r12), %rcx
movl (%rcx,%rax,4), %eax
movl %r11d, %r13d
cmpl $-0x1, %eax
je 0xac669
testl %eax, %eax
jns 0xac6df
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %r13d
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0xac6af
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r10, %rbx
callq 0x2370
pushq $-0x1
popq %r11
movq %rbx, %r10
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %rbx
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movl %r13d, %edi
orq %r15, %rdi
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdi, (%rax,%rcx,8)
jmp 0xac6df
cmpq %rax, %rcx
jbe 0xac6df
movq 0x8(%r12), %rsi
movslq (%rsi,%rax,4), %rax
testq %rax, %rax
js 0xac6df
cmpl $0x0, (%rdi,%rax,8)
jns 0xac645
incq %r14
addq $0x28, %rbp
jmp 0xac624
movq 0x10(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r14, (%rax,%rcx,8)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq