// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/horder.cpp
// Function: get_r_hits(kvec_pe_hit*, kvec_pe_hit*, asg_t*, ma_ug_t*, bubble_type*, unsigned long, unsigned long)
void get_r_hits(kvec_pe_hit *u_hits, kvec_pe_hit *r_hits, asg_t* r_g, ma_ug_t* ug, bubble_type* bub, uint64_t uID_bits, uint64_t pos_mode)
{
    uint64_t k, l, i, r_i, offset, rid, rev, rBeg, rEnd, ubits, p_mode, upos, rpos, update;
    ma_utg_t *u = NULL;
    memset(r_hits, 0, sizeof(*r_hits));
    r_hits->uID_bits = uID_bits; r_hits->pos_mode = pos_mode;
    //reset for reads
    for (ubits=1; (uint64_t)(1<<ubits)<(uint64_t)r_g->n_seq; ubits++);
    p_mode = ((uint64_t)-1) >> (ubits + 1);

    kv_malloc(r_hits->a, u_hits->a.n); r_hits->a.n = r_hits->a.m = u_hits->a.n;
    memcpy(r_hits->a.a, u_hits->a.a, r_hits->a.n*sizeof(pe_hit));
    radix_sort_pe_hit_idx_hn1(r_hits->a.a, r_hits->a.a + r_hits->a.n);
    for (k = 1, l = 0; k <= r_hits->a.n; ++k) {   
        if (k == r_hits->a.n || get_hit_suid(*r_hits, k) != get_hit_suid(*r_hits, l)) {//same suid
            ///already sort by spos
            u = &(ug->u.a[get_hit_suid(*r_hits, l)]); update = 0;
            for (i = offset = 0, r_i = l; i < u->n; i++) {
                rid = u->a[i]>>33; 
                rBeg = offset;
                rEnd = rBeg + r_g->seq[rid].len - 1;
                for (; r_i < k; r_i++) {
                    upos = get_hit_spos(*r_hits, r_i);///pos at unitig
                    
                    if(upos > rEnd) break;
                    if(upos >= rBeg && upos <= rEnd) {
                        if(bub) {
                            r_hits->a.a[r_i].id = (uint32_t)r_hits->a.a[r_i].id;
                            if(!IF_HOM(get_hit_suid(*r_hits, r_i), *bub)) {
                                r_hits->a.a[r_i].id += ((uint64_t)(1)<<32);
                            }
                        }

                        rpos = (((u->a[i]>>32)&1)? rEnd - upos : upos - rBeg);///pos at read
                        rev = ((u->a[i]>>32)&1) ^ (r_hits->a.a[r_i].s>>63);
                        r_hits->a.a[r_i].s = (rev<<63) | ((rid << (64-ubits))>>1) | (rpos & p_mode);
                        
                        update++;
                    }
                }
                offset += (uint32_t)u->a[i];
            }
            assert((r_i == k) && (update == (k-l)));
            // if(r_i != k || update != k - l) fprintf(stderr, "ERROR-r_i\n");
            l = k;
        }
    }

    radix_sort_pe_hit_idx_hn2(r_hits->a.a, r_hits->a.a + r_hits->a.n);
    for (k = 1, l = 0; k <= r_hits->a.n; ++k) {
        if (k == r_hits->a.n || get_hit_euid(*r_hits, k) != get_hit_euid(*r_hits, l)) {//same euid
            ///already sort by epos
            u = &(ug->u.a[get_hit_euid(*r_hits, l)]); update = 0;
            for (i = offset = 0, r_i = l; i < u->n; i++) {
                rid = u->a[i]>>33; 
                rBeg = offset;
                rEnd = rBeg + r_g->seq[rid].len - 1;
                for (; r_i < k; r_i++) {
                    upos = get_hit_epos(*r_hits, r_i);///pos at unitig

                    if(upos > rEnd) break;
                    if(upos >= rBeg && upos <= rEnd) {
                        if(bub) {
                            r_hits->a.a[r_i].id >>= 32;
                            r_hits->a.a[r_i].id <<= 32;
                            if(!IF_HOM(get_hit_euid(*r_hits, r_i), *bub)) {
                                r_hits->a.a[r_i].id += 1;
                            }
                        }

                        rpos = (((u->a[i]>>32)&1)? rEnd - upos : upos - rBeg);///pos at read
                        rev = ((u->a[i]>>32)&1) ^ (r_hits->a.a[r_i].e>>63);
                        r_hits->a.a[r_i].e = (rev<<63) | ((rid << (64-ubits))>>1) | (rpos & p_mode);
                        
                        update++;
                    }
                }
                offset += (uint32_t)u->a[i];
            }
            assert((r_i == k) && (update == (k - l)));
            // if(r_i != k || update != k - l) fprintf(stderr, "ERROR-r_i\n");
            l = k;
        }
    }

    r_hits->uID_bits = ubits;
    r_hits->pos_mode = p_mode;
    idx_hits(r_hits, r_g->n_seq);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, 0x20(%rsp)
movq %rcx, 0x60(%rsp)
movq %rsi, %r15
movq 0xb0(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
andq $0x0, 0x40(%rsi)
movq %r9, 0x48(%rsi)
movq %rax, 0x50(%rsi)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rdx, 0x48(%rsp)
andl 0x14(%rdx), %eax
movq %rdi, %r12
pushq $0x1
popq %r14
pushq $0x40
popq %r13
pushq $0x1
popq %rdx
movl %edx, %esi
movl %r14d, %ecx
shll %cl, %esi
incq %r14
decq %r13
cmpl %eax, %esi
jb 0x1322fb
pushq $-0x1
popq %rax
movl %r14d, %ecx
shrq %cl, %rax
movq %rax, 0x50(%rsp)
movq (%r12), %rbx
movq %rbx, %rbp
shlq $0x5, %rbp
movq %rbp, %rdi
callq 0x2280
movq %rax, 0x10(%r15)
movq %rbx, 0x8(%r15)
movq %rbx, (%r15)
movq 0x10(%r12), %rsi
movq %rax, %rdi
movq %rax, %rbx
movq %rbp, %rdx
callq 0x2160
addq %rbx, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x12eda6
movq (%r15), %rax
movq %rax, 0x18(%rsp)
decq %r14
pushq $0x1
popq %rbx
xorl %r8d, %r8d
movq %r15, 0x70(%rsp)
movq %r14, 0x58(%rsp)
movq %r13, 0x68(%rsp)
cmpq 0x18(%rsp), %rbx
ja 0x132584
movq 0x10(%r15), %r9
jne 0x1323af
movq %r8, %rax
shlq $0x5, %rax
movq (%r9,%rax), %rax
movq 0x48(%r15), %rcx
addq %rax, %rax
pushq $0x40
popq %rdx
subq %rcx, %rdx
movq %rdx, 0x8(%rsp)
negq %rcx
shrq %cl, %rax
jmp 0x1323ef
movq %rbx, %rax
shlq $0x5, %rax
movq (%r9,%rax), %rdi
addq %rdi, %rdi
movq 0x48(%r15), %rsi
movq %rsi, %rcx
negq %rcx
shrq %cl, %rdi
movq %r8, %rax
shlq $0x5, %rax
movq (%r9,%rax), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %rdi
je 0x13257c
pushq $0x40
popq %rcx
subq %rsi, %rcx
movq %rcx, 0x8(%rsp)
movq 0x60(%rsp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rax, %rax
leaq (%rcx,%rax), %rdx
movq %rdx, 0x30(%rsp)
movl 0x10(%rcx,%rax), %eax
movq %rax, 0x40(%rsp)
addq $0x10, %r9
movq %r9, 0x38(%rsp)
xorl %esi, %esi
movq %r8, 0x28(%rsp)
movq %r8, %rdi
xorl %ebp, %ebp
xorl %r9d, %r9d
cmpq 0x40(%rsp), %rsi
je 0x132557
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x10(%rsp)
movq (%rax,%rsi,8), %r10
shrq $0x21, %r10
movq 0x48(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%r10,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rax,%rbp), %r11
decq %r11
movl %r13d, %ecx
shlq %cl, %r10
shrq %r10
movq %rdi, %r12
shlq $0x5, %r12
addq 0x38(%rsp), %r12
cmpq %rbx, %rdi
jae 0x13253f
movq -0x10(%r12), %rax
movq 0x50(%r15), %r13
andq %rax, %r13
movq %r11, %r14
subq %r13, %r14
jb 0x13253f
subq %rbp, %r13
jb 0x132533
movq %rbx, %r8
cmpq $0x0, 0x20(%rsp)
je 0x1324ef
movl (%r12), %ebx
movq %rbx, (%r12)
leaq (%rax,%rax), %r15
movq 0x8(%rsp), %rcx
shrq %cl, %r15
movq 0x20(%rsp), %rdx
movq (%rdx), %rcx
movq 0x80(%rdx), %rdx
movl (%rcx,%r15,4), %ecx
movq 0x70(%rsp), %r15
incq %rdx
cmpq %rcx, %rdx
jb 0x1324ef
movabsq $0x100000000, %rcx      # imm = 0x100000000
orq %rcx, %rbx
movq %rbx, (%r12)
movq 0x10(%rsp), %rcx
movq (%rcx,%rsi,8), %rcx
movabsq $0x100000000, %rdx      # imm = 0x100000000
testq %rdx, %rcx
cmoveq %r13, %r14
shlq $0x1f, %rcx
xorq %rax, %rcx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
andq %rax, %rcx
orq %r10, %rcx
andq 0x50(%rsp), %r14
orq %rcx, %r14
movq %r14, -0x10(%r12)
incq %r9
movq %r8, %rbx
incq %rdi
addq $0x20, %r12
jmp 0x132475
movq 0x10(%rsp), %rax
movl (%rax,%rsi,8), %eax
addq %rax, %rbp
incq %rsi
movq 0x68(%rsp), %r13
jmp 0x132426
cmpq %rbx, %rdi
movq 0x58(%rsp), %r14
movq 0x28(%rsp), %rcx
jne 0x1327ce
movq %rbx, %rax
subq %rcx, %rax
movq %rbx, %r8
cmpq %rax, %r9
jne 0x1327ce
incq %rbx
jmp 0x132379
movq 0x10(%r15), %rdi
movq 0x18(%rsp), %rsi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x12f027
movq (%r15), %rax
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r8
xorl %r12d, %r12d
cmpq 0x18(%rsp), %r8
ja 0x13279d
movq 0x10(%r15), %r11
jne 0x1325df
movq %r12, %rax
shlq $0x5, %rax
movq 0x8(%r11,%rax), %rax
movq 0x48(%r15), %rcx
addq %rax, %rax
pushq $0x40
popq %rdx
subq %rcx, %rdx
movq %rdx, 0x8(%rsp)
negq %rcx
shrq %cl, %rax
jmp 0x132621
movq %r8, %rax
shlq $0x5, %rax
movq 0x8(%r11,%rax), %rdi
addq %rdi, %rdi
movq 0x48(%r15), %rdx
movq %rdx, %rcx
negq %rcx
shrq %cl, %rdi
movq %r12, %rax
shlq $0x5, %rax
movq 0x8(%r11,%rax), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %rdi
je 0x132795
pushq $0x40
popq %rcx
subq %rdx, %rcx
movq %rcx, 0x8(%rsp)
movq 0x60(%rsp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rax, %rax
leaq (%rcx,%rax), %rdx
movq %rdx, 0x30(%rsp)
movl 0x10(%rcx,%rax), %eax
movq %rax, 0x40(%rsp)
addq $0x10, %r11
xorl %edx, %edx
movq %r12, 0x28(%rsp)
xorl %ebp, %ebp
xorl %r9d, %r9d
movq %r11, 0x38(%rsp)
cmpq 0x40(%rsp), %rdx
je 0x132778
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x10(%rsp)
movq (%rax,%rdx,8), %r10
shrq $0x21, %r10
movq 0x48(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%r10,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rbp, %rax
decq %rax
movl %r13d, %ecx
shlq %cl, %r10
shrq %r10
movq %r12, %rdi
shlq $0x5, %rdi
addq %r11, %rdi
cmpq %r8, %r12
jae 0x13275b
movq -0x8(%rdi), %r14
movq 0x50(%r15), %r13
andq %r14, %r13
movq %rax, %r11
subq %r13, %r11
jb 0x13275b
subq %rbp, %r13
jb 0x13274f
cmpq $0x0, 0x20(%rsp)
je 0x13270f
movl 0x4(%rdi), %ebx
shlq $0x20, %rbx
movq %rbx, (%rdi)
leaq (%r14,%r14), %r15
movq 0x8(%rsp), %rcx
shrq %cl, %r15
movq 0x20(%rsp), %rsi
movq (%rsi), %rcx
movq 0x80(%rsi), %rsi
movl (%rcx,%r15,4), %ecx
movq 0x70(%rsp), %r15
incq %rsi
cmpq %rcx, %rsi
jb 0x13270f
orq $0x1, %rbx
movq %rbx, (%rdi)
movq 0x10(%rsp), %rcx
movq (%rcx,%rdx,8), %rcx
movabsq $0x100000000, %rsi      # imm = 0x100000000
testq %rsi, %rcx
cmoveq %r13, %r11
shlq $0x1f, %rcx
xorq %rcx, %r14
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
andq %rcx, %r14
orq %r10, %r14
andq 0x50(%rsp), %r11
orq %r14, %r11
movq %r11, -0x8(%rdi)
incq %r9
incq %r12
addq $0x20, %rdi
jmp 0x1326a1
movq 0x10(%rsp), %rax
movl (%rax,%rdx,8), %eax
addq %rax, %rbp
incq %rdx
movq 0x68(%rsp), %r13
movq 0x38(%rsp), %r11
jmp 0x132655
cmpq %r8, %r12
movq 0x58(%rsp), %r14
movq 0x28(%rsp), %rcx
jne 0x1327ed
movq %r8, %rax
subq %rcx, %rax
movq %r8, %r12
cmpq %rax, %r9
jne 0x1327ed
incq %r8
jmp 0x1325a8
movq %r14, 0x48(%r15)
movq 0x50(%rsp), %rax
movq %rax, 0x50(%r15)
movq 0x48(%rsp), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %r15, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x131b3a
leaq 0xbcef6(%rip), %rdi     # 0x1ef6cb
leaq 0xbcf0f(%rip), %rsi     # 0x1ef6eb
leaq 0xbcf13(%rip), %rcx     # 0x1ef6f6
movl $0x279, %edx            # imm = 0x279
callq 0x2110
leaq 0xbcf67(%rip), %rdi     # 0x1ef75b
leaq 0xbcef0(%rip), %rsi     # 0x1ef6eb
leaq 0xbcef4(%rip), %rcx     # 0x1ef6f6
movl $0x29e, %edx            # imm = 0x29E
callq 0x2110