// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/horder.cpp
// Function: gen_r_hits(kvec_pe_hit*, kvec_pe_hit*, asg_t*, ma_ug_t*, bubble_type*, unsigned long, unsigned long)
void gen_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, m, 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 = m = 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
            if((!bub) || ((!IF_HOM(get_hit_suid(*r_hits, l), *bub)))) {
                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) {
                            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);
                            r_hits->a.a[r_i].id = 1; r_hits->a.a[m++] = r_hits->a.a[r_i];
                            
                            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->a.n = m;

    radix_sort_pe_hit_idx_hn2(r_hits->a.a, r_hits->a.a + r_hits->a.n);
    for (k = 1, l = m = 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
            if((!bub) || ((!IF_HOM(get_hit_euid(*r_hits, l), *bub)))) {
                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) {
                            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);
                            r_hits->a.a[r_i].id = 1; r_hits->a.a[m++] = r_hits->a.a[r_i];
                            
                            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->a.n = m;

    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 $0x58, %rsp
movq %r8, %r15
movq %rcx, 0x38(%rsp)
movq %rdi, %r12
movq 0x90(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
movq $0x0, 0x40(%rsi)
movq %r9, 0x48(%rsi)
movq %rsi, 0x8(%rsp)
movq %rax, 0x50(%rsi)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rdx, 0x20(%rsp)
andl 0x14(%rdx), %eax
movl $0x1, %ecx
movl $0x40, %esi
movl $0x1, %edx
shll %cl, %edx
incq %rcx
decq %rsi
cmpl %eax, %edx
jb 0x17efb0
movq %rsi, 0x40(%rsp)
movq $-0x1, %rax
movq %rcx, 0x48(%rsp)
shrq %cl, %rax
movq %rax, 0x28(%rsp)
movq (%r12), %r14
movq %r14, %r13
shlq $0x5, %r13
movq %r13, %rdi
callq 0x2280
movq %rax, %rbp
movq 0x8(%rsp), %rbx
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movq %r14, (%rbx)
movq 0x10(%r12), %rsi
movq %rax, %rdi
movq %r13, %rdx
callq 0x2160
addq %rbp, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x172a07
movq (%rbx), %rdx
testq %rdx, %rdx
movq %r15, 0x30(%rsp)
je 0x17f274
movl $0x1, %r8d
xorl %esi, %esi
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
cmpq %rdx, %r8
movq %r8, 0x10(%rsp)
je 0x17f08a
movq %r8, %rcx
shlq $0x5, %rcx
movq (%rdi,%rcx), %r8
addq %r8, %r8
movq 0x8(%rsp), %rax
movb 0x48(%rax), %r9b
xorl %ecx, %ecx
subb %r9b, %cl
shrq %cl, %r8
movq %rbx, %r9
shlq $0x5, %r9
movq (%rdi,%r9), %r9
addq %r9, %r9
shrq %cl, %r9
cmpq %r9, %r8
movq 0x10(%rsp), %r8
je 0x17f266
movq %rbx, %rcx
shlq $0x5, %rcx
testq %r15, %r15
je 0x17f0d0
movq (%rdi,%rcx), %rdi
addq %rdi, %rdi
movq 0x8(%rsp), %rax
movb 0x48(%rax), %r8b
xorl %ecx, %ecx
subb %r8b, %cl
shrq %cl, %rdi
movq (%r15), %rcx
movq 0x80(%r15), %r8
movl (%rcx,%rdi,4), %ecx
incq %r8
cmpq %rcx, %r8
movq 0x10(%rsp), %r8
jae 0x17f0e6
movq %r8, %rbx
jmp 0x17f266
movq (%rdi,%rcx), %rdi
addq %rdi, %rdi
movq 0x8(%rsp), %rax
movb 0x48(%rax), %dl
xorl %ecx, %ecx
subb %dl, %cl
shrq %cl, %rdi
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdx
leaq (%rdi,%rdi,4), %rdi
movl 0x10(%rdx,%rdi,8), %ecx
testl %ecx, %ecx
movq %rbx, 0x50(%rsp)
je 0x17f237
leaq (%rdx,%rdi,8), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rax), %r13
xorl %edi, %edi
xorl %r10d, %r10d
movq %rbx, %r9
xorl %r11d, %r11d
movq (%r13,%r11,8), %rdx
cmpq %r8, %r9
jae 0x17f222
shrq $0x21, %rdx
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
movl (%rcx,%rdx,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq 0x40(%rsp), %rcx
shlq %cl, %rdx
leaq (%r10,%r14), %r12
decq %r12
shrq %rdx
movq %r9, %r13
shlq $0x5, %r13
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbp
movq (%rbp,%r13), %r14
movq 0x50(%rax), %r15
andq %r14, %r15
movq %r12, %rcx
subq %r15, %rcx
jb 0x17f211
subq %r10, %r15
jb 0x17f1fe
movq %rbp, %r8
addq %r13, %r8
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r11,8), %rax
movq %rdi, %rbx
movabsq $0x100000000, %rdi      # imm = 0x100000000
testq %rdi, %rax
movq %rbx, %rdi
cmoveq %r15, %rcx
shlq $0x1f, %rax
xorq %r14, %rax
movabsq $-0x8000000000000000, %r14 # imm = 0x8000000000000000
andq %r14, %rax
orq %rdx, %rax
andq 0x28(%rsp), %rcx
orq %rax, %rcx
movq %rcx, (%rbp,%r13)
movq $0x1, 0x10(%rbp,%r13)
leaq 0x1(%rsi), %rax
shlq $0x5, %rsi
movups (%r8), %xmm0
movups 0x10(%r8), %xmm1
movq 0x10(%rsp), %r8
movups %xmm1, 0x10(%rbp,%rsi)
movups %xmm0, (%rbp,%rsi)
incq %rdi
movq %rax, %rsi
incq %r9
addq $0x20, %r13
cmpq %r9, %r8
jne 0x17f15b
movq %r8, %r9
movq 0x18(%rsp), %rax
movq 0x18(%rax), %r13
movl 0x10(%rax), %ecx
movl (%r13,%r11,8), %edx
movl %edx, %eax
addq %rax, %r10
incq %r11
movl %ecx, %eax
cmpq %rax, %r11
jb 0x17f11c
jmp 0x17f23c
movq %rbx, %r9
xorl %edi, %edi
cmpq %r8, %r9
jne 0x17f50a
movq %r8, %rax
subq 0x50(%rsp), %rax
cmpq %rax, %rdi
jne 0x17f50a
movq 0x8(%rsp), %rax
movq (%rax), %rdx
movq %r8, %rbx
movq 0x30(%rsp), %r15
incq %r8
cmpq %rdx, %r8
jbe 0x17f039
jmp 0x17f276
xorl %esi, %esi
movq 0x8(%rsp), %rbx
movq %rsi, (%rbx)
movq 0x10(%rbx), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x172c90
movq (%rbx), %rsi
testq %rsi, %rsi
je 0x17f4ca
movl $0x1, %edx
xorl %eax, %eax
xorl %r11d, %r11d
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rdi
cmpq %rsi, %rdx
je 0x17f2ed
movq %rdx, %rcx
shlq $0x5, %rcx
movq 0x8(%rdi,%rcx), %r8
addq %r8, %r8
movq 0x8(%rsp), %rcx
movb 0x48(%rcx), %r9b
xorl %ecx, %ecx
subb %r9b, %cl
shrq %cl, %r8
movq %r11, %r9
shlq $0x5, %r9
movq 0x8(%rdi,%r9), %r9
addq %r9, %r9
shrq %cl, %r9
cmpq %r9, %r8
je 0x17f4bc
movq %r11, %rcx
shlq $0x5, %rcx
testq %r15, %r15
je 0x17f32f
movq 0x8(%rdi,%rcx), %rdi
addq %rdi, %rdi
movq 0x8(%rsp), %rcx
movb 0x48(%rcx), %r8b
xorl %ecx, %ecx
subb %r8b, %cl
shrq %cl, %rdi
movq (%r15), %rcx
movq 0x80(%r15), %r8
movl (%rcx,%rdi,4), %ecx
incq %r8
cmpq %rcx, %r8
jae 0x17f348
movq %rdx, %r11
jmp 0x17f4bc
movq 0x8(%rdi,%rcx), %rdi
addq %rdi, %rdi
movq 0x8(%rsp), %rcx
movb 0x48(%rcx), %sil
xorl %ecx, %ecx
subb %sil, %cl
shrq %cl, %rdi
movq 0x38(%rsp), %rcx
movq 0x10(%rcx), %rsi
leaq (%rdi,%rdi,4), %rdi
movl 0x10(%rsi,%rdi,8), %ecx
testl %ecx, %ecx
movq %r11, 0x10(%rsp)
je 0x17f491
leaq (%rsi,%rdi,8), %rsi
movq %rsi, 0x18(%rsp)
movq 0x18(%rsi), %r13
xorl %ebx, %ebx
xorl %r10d, %r10d
movq %r11, %r9
xorl %r11d, %r11d
movq (%r13,%r11,8), %rsi
cmpq %rdx, %r9
jae 0x17f47c
shrq $0x21, %rsi
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %rcx
movl (%rcx,%rsi,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%r10,%rcx), %r13
decq %r13
movq 0x40(%rsp), %rcx
shlq %cl, %rsi
shrq %rsi
movq %r9, %rcx
shlq $0x5, %rcx
orq $0x10, %rcx
movq 0x8(%rsp), %r8
movq 0x10(%r8), %rbp
movq -0x8(%rbp,%rcx), %r14
movq 0x50(%r8), %r15
andq %r14, %r15
movq %r13, %r12
subq %r15, %r12
jb 0x17f46b
subq %r10, %r15
jb 0x17f458
movq 0x18(%rsp), %r8
movq 0x18(%r8), %r8
movq (%r8,%r11,8), %r8
movq %rdx, %rdi
movabsq $0x100000000, %rdx      # imm = 0x100000000
testq %rdx, %r8
movq %rdi, %rdx
cmoveq %r15, %r12
shlq $0x1f, %r8
xorq %r14, %r8
movabsq $-0x8000000000000000, %r14 # imm = 0x8000000000000000
andq %r14, %r8
orq %rsi, %r8
andq 0x28(%rsp), %r12
orq %r8, %r12
movq %r12, -0x8(%rbp,%rcx)
movq $0x1, (%rbp,%rcx)
leaq 0x1(%rax), %r8
shlq $0x5, %rax
movups (%rbp,%rcx), %xmm0
movups %xmm0, 0x10(%rbp,%rax)
movups -0x10(%rbp,%rcx), %xmm0
movups %xmm0, (%rbp,%rax)
incq %rbx
movq %r8, %rax
incq %r9
addq $0x20, %rcx
cmpq %r9, %rdx
jne 0x17f3bf
movq %rdx, %r9
movq 0x18(%rsp), %rcx
movq 0x18(%rcx), %r13
movl 0x10(%rcx), %ecx
movl (%r13,%r11,8), %esi
movl %esi, %esi
addq %rsi, %r10
incq %r11
movl %ecx, %esi
cmpq %rsi, %r11
jb 0x17f37e
jmp 0x17f496
movq %r11, %r9
xorl %ebx, %ebx
cmpq %rdx, %r9
jne 0x17f529
movq %rdx, %rcx
subq 0x10(%rsp), %rcx
cmpq %rcx, %rbx
jne 0x17f529
movq 0x8(%rsp), %rcx
movq (%rcx), %rsi
movq %rdx, %r11
movq 0x30(%rsp), %r15
incq %rdx
cmpq %rsi, %rdx
jbe 0x17f2a4
jmp 0x17f4cc
xorl %eax, %eax
movq 0x48(%rsp), %rcx
decq %rcx
movq 0x8(%rsp), %rdi
movq %rax, (%rdi)
movq %rcx, 0x48(%rdi)
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rdi)
movq 0x20(%rsp), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1755fa
leaq 0xbe2fa(%rip), %rdi     # 0x23d80b
leaq 0xbe313(%rip), %rsi     # 0x23d82b
leaq 0xbe9d1(%rip), %rcx     # 0x23def0
movl $0x1178, %edx           # imm = 0x1178
callq 0x2110
leaq 0xbe36b(%rip), %rdi     # 0x23d89b
leaq 0xbe2f4(%rip), %rsi     # 0x23d82b
leaq 0xbe9b2(%rip), %rcx     # 0x23def0
movl $0x1199, %edx           # imm = 0x1199
callq 0x2110