// File: /workspace/github_cpp_makefile_star2_O3/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, %r13d
movq %rdi, %r15
movq 0x20(%rdi), %rdi
movl %ecx, %eax
cmpb $0x2, 0x4(%rdi,%rax,8)
jne 0xeb23d
shlq $0x21, %r13
movabsq $0x1ffffffff, %r14      # imm = 0x1FFFFFFFF
orq %r13, %r14
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0xeb32d
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
jmp 0xeb331
leaq (%rax,%rax,2), %rax
movl 0xc(%rsi,%rax,8), %ecx
testl %ecx, %ecx
je 0xeb33c
leaq (%rsi,%rax,8), %r12
shlq $0x21, %r13
movl $0xc, %ebp
xorl %r14d, %r14d
movq %rbx, 0x10(%rsp)
movq (%r12), %rax
movl (%rax,%rbp), %eax
movq 0x20(%r15), %rsi
cmpl $0x0, (%rsi,%rax,8)
movq (%rdx), %r10
js 0xeb2fd
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpq %rax, %r10
jbe 0xeb29f
movq 0x8(%rdx), %rsi
movl (%rsi,%rax,4), %eax
cmpl $-0x1, %eax
je 0xeb29f
testl %eax, %eax
jns 0xeb319
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %edi
movq (%rbx), %r10
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %r10
jne 0xeb2eb
leaq (%r10,%r10), %rsi
testq %r10, %r10
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movl %edi, 0xc(%rsp)
movq %rax, %rdi
movq %rdx, %rbx
callq 0x2370
movl 0xc(%rsp), %edi
movq %rbx, %rdx
movq 0x10(%rsp), %rbx
movq %rax, 0x10(%rbx)
movq (%rbx), %r10
movl 0xc(%r12), %ecx
movl %edi, %esi
orq %r13, %rsi
leaq 0x1(%r10), %rdi
movq %rdi, (%rbx)
movq %rsi, (%rax,%r10,8)
jmp 0xeb319
cmpq %rax, %r10
jbe 0xeb319
movq 0x8(%rdx), %rdi
movslq (%rdi,%rax,4), %rax
testq %rax, %rax
js 0xeb319
cmpl $0x0, (%rsi,%rax,8)
jns 0xeb27a
incq %r14
movl %ecx, %eax
addq $0x28, %rbp
cmpq %rax, %r14
jb 0xeb262
jmp 0xeb33c
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