// File: /workspace/github_cpp_makefile_star2_O2/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, 0x8(%rsp)
movq %rcx, 0x38(%rsp)
movq %rsi, %rbx
movq 0x90(%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, 0x28(%rsp)
andl 0x14(%rdx), %eax
movq %rdi, %r13
pushq $0x1
popq %rcx
pushq $0x40
popq %r12
pushq $0x1
popq %rdx
movl %edx, %esi
shll %cl, %esi
incq %rcx
decq %r12
cmpl %eax, %esi
jb 0x13c38e
pushq $-0x1
popq %rax
shrq %cl, %rax
movq %rax, 0x30(%rsp)
movq (%r13), %r14
movq %r14, %rbp
shlq $0x5, %rbp
movq %rbp, %rdi
movq %rcx, %r15
callq 0x2280
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movq %r14, (%rbx)
movq 0x10(%r13), %rsi
movq %rax, %rdi
movq %rax, %r14
movq %rbp, %rdx
callq 0x2160
addq %r14, %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x12eda6
decq %r15
movq %r15, 0x48(%rsp)
pushq $0x1
popq %r8
xorl %r10d, %r10d
xorl %esi, %esi
movq 0x8(%rsp), %r9
movq %r12, 0x40(%rsp)
cmpq (%rbx), %r8
ja 0x13c618
movq 0x10(%rbx), %rdx
movq %r8, 0x10(%rsp)
je 0x13c450
movq %r8, %rcx
shlq $0x5, %rcx
movq (%rdx,%rcx), %rdi
addq %rdi, %rdi
movb 0x48(%rbx), %r8b
xorl %ecx, %ecx
subb %r8b, %cl
shrq %cl, %rdi
movq %r10, %r8
shlq $0x5, %r8
movq (%rdx,%r8), %r8
addq %r8, %r8
shrq %cl, %r8
cmpq %r8, %rdi
movq 0x10(%rsp), %r8
je 0x13c610
movq %r10, %rcx
shlq $0x5, %rcx
testq %r9, %r9
je 0x13c48c
movq (%rdx,%rcx), %rdx
addq %rdx, %rdx
movb 0x48(%rbx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rdx
movq (%r9), %rcx
movq 0x80(%r9), %rdi
movl (%rcx,%rdx,4), %ecx
incq %rdi
cmpq %rcx, %rdi
jae 0x13c49f
movq %r8, %r10
jmp 0x13c610
movq (%rdx,%rcx), %rdx
addq %rdx, %rdx
movb 0x48(%rbx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rdx
imulq $0x28, %rdx, %r9
movq 0x38(%rsp), %rax
addq 0x10(%rax), %r9
xorl %r11d, %r11d
movq %r10, 0x50(%rsp)
xorl %edx, %edx
xorl %edi, %edi
movl 0x10(%r9), %ecx
cmpq %rcx, %r11
jae 0x13c5eb
movq 0x18(%r9), %rcx
movq (%rcx,%r11,8), %r15
shrq $0x21, %r15
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rcx
movl (%rcx,%r15,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl %r12d, %ecx
shlq %cl, %r15
leaq (%rdx,%r14), %rax
decq %rax
shrq %r15
movq %r15, 0x20(%rsp)
movq %r10, %r14
shlq $0x5, %r14
movq %rax, 0x18(%rsp)
cmpq %r8, %r10
jae 0x13c5d3
movq 0x10(%rbx), %rbp
movq (%rbp,%r14), %r13
movq 0x50(%rbx), %r12
andq %r13, %r12
movq %rax, %rcx
subq %r12, %rcx
jb 0x13c5d3
subq %rdx, %r12
jb 0x13c5c7
leaq (%r14,%rbp), %rax
movq 0x18(%r9), %r8
movq (%r8,%r11,8), %r8
movq %rdx, %r15
movq %r11, %rdx
movq %rbx, %r11
movq %rdi, %rbx
movq %r9, %rdi
movabsq $0x100000000, %r9       # imm = 0x100000000
testq %r9, %r8
movq %rdi, %r9
movq %rbx, %rdi
movq %r11, %rbx
movq %rdx, %r11
movq %r15, %rdx
cmoveq %r12, %rcx
shlq $0x1f, %r8
xorq %r13, %r8
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
andq %r12, %r8
orq 0x20(%rsp), %r8
andq 0x30(%rsp), %rcx
orq %r8, %rcx
movq 0x10(%rsp), %r8
movq %rcx, (%rbp,%r14)
movq $0x1, 0x10(%rbp,%r14)
leaq 0x1(%rsi), %rcx
shlq $0x5, %rsi
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movq 0x18(%rsp), %rax
movups %xmm1, 0x10(%rbp,%rsi)
movups %xmm0, (%rbp,%rsi)
incq %rdi
movq %rcx, %rsi
incq %r10
addq $0x20, %r14
jmp 0x13c506
movq 0x18(%r9), %rax
movl (%rax,%r11,8), %eax
addq %rax, %rdx
incq %r11
movq 0x40(%rsp), %r12
jmp 0x13c4b8
cmpq %r8, %r10
movq 0x8(%rsp), %r9
movq 0x50(%rsp), %rcx
jne 0x13c860
movq %r8, %rax
subq %rcx, %rax
movq %r8, %r10
cmpq %rax, %rdi
jne 0x13c860
incq %r8
jmp 0x13c403
movq %rsi, (%rbx)
movq 0x10(%rbx), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x12f027
movq 0x8(%rsp), %r9
pushq $0x1
popq %r8
xorl %r10d, %r10d
xorl %esi, %esi
cmpq (%rbx), %r8
ja 0x13c827
movq 0x10(%rbx), %rdx
movq %r8, 0x18(%rsp)
je 0x13c688
movq %r8, %rcx
shlq $0x5, %rcx
movq 0x8(%rdx,%rcx), %rdi
addq %rdi, %rdi
movb 0x48(%rbx), %r8b
xorl %ecx, %ecx
subb %r8b, %cl
shrq %cl, %rdi
movq %r10, %r8
shlq $0x5, %r8
movq 0x8(%rdx,%r8), %r8
addq %r8, %r8
shrq %cl, %r8
cmpq %r8, %rdi
movq 0x18(%rsp), %r8
je 0x13c81f
movq %r10, %rcx
shlq $0x5, %rcx
testq %r9, %r9
je 0x13c6c5
movq 0x8(%rdx,%rcx), %rdx
addq %rdx, %rdx
movb 0x48(%rbx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rdx
movq (%r9), %rcx
movq 0x80(%r9), %rdi
movl (%rcx,%rdx,4), %ecx
incq %rdi
cmpq %rcx, %rdi
jae 0x13c6d9
movq %r8, %r10
jmp 0x13c81f
movq 0x8(%rdx,%rcx), %rdx
addq %rdx, %rdx
movb 0x48(%rbx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rdx
imulq $0x28, %rdx, %rdx
movq 0x38(%rsp), %rax
addq 0x10(%rax), %rdx
xorl %r9d, %r9d
movq %r10, 0x10(%rsp)
xorl %r11d, %r11d
xorl %edi, %edi
movq %rdx, 0x20(%rsp)
movl 0x10(%rdx), %ecx
cmpq %rcx, %r9
jae 0x13c802
movq 0x18(%rdx), %rcx
movq (%rcx,%r9,8), %rdx
shrq $0x21, %rdx
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rcx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%r11,%rcx), %r14
decq %r14
movl %r12d, %ecx
shlq %cl, %rdx
shrq %rdx
movq %r10, %rcx
shlq $0x5, %rcx
orq $0x10, %rcx
cmpq %r8, %r10
jae 0x13c7e5
movq 0x10(%rbx), %rbp
movq -0x8(%rbp,%rcx), %r13
movq 0x50(%rbx), %r12
andq %r13, %r12
movq %r14, %r15
subq %r12, %r15
jb 0x13c7e5
subq %r11, %r12
jb 0x13c7d9
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r9,8), %rax
movabsq $0x100000000, %r8       # imm = 0x100000000
testq %r8, %rax
movq 0x18(%rsp), %r8
cmoveq %r12, %r15
shlq $0x1f, %rax
xorq %r13, %rax
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
andq %r12, %rax
orq %rdx, %rax
andq 0x30(%rsp), %r15
orq %rax, %r15
movq %r15, -0x8(%rbp,%rcx)
movq $0x1, (%rbp,%rcx)
leaq 0x1(%rsi), %rax
shlq $0x5, %rsi
movups (%rbp,%rcx), %xmm0
movups %xmm0, 0x10(%rbp,%rsi)
movups -0x10(%rbp,%rcx), %xmm0
movups %xmm0, (%rbp,%rsi)
incq %rdi
movq %rax, %rsi
incq %r10
addq $0x20, %rcx
jmp 0x13c73d
movq 0x20(%rsp), %rdx
movq 0x18(%rdx), %rax
movl (%rax,%r9,8), %eax
addq %rax, %r11
incq %r9
movq 0x40(%rsp), %r12
jmp 0x13c6f8
cmpq %r8, %r10
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %rcx
jne 0x13c87f
movq %r8, %rax
subq %rcx, %rax
movq %r8, %r10
cmpq %rax, %rdi
jne 0x13c87f
incq %r8
jmp 0x13c639
movq %rsi, (%rbx)
movq 0x48(%rsp), %rax
movq %rax, 0x48(%rbx)
movq 0x30(%rsp), %rax
movq %rax, 0x50(%rbx)
movq 0x28(%rsp), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x131b3a
leaq 0xb2e64(%rip), %rdi     # 0x1ef6cb
leaq 0xb2e7d(%rip), %rsi     # 0x1ef6eb
leaq 0xb353b(%rip), %rcx     # 0x1efdb0
movl $0x1178, %edx           # imm = 0x1178
callq 0x2110
leaq 0xb2ed5(%rip), %rdi     # 0x1ef75b
leaq 0xb2e5e(%rip), %rsi     # 0x1ef6eb
leaq 0xb351c(%rip), %rcx     # 0x1efdb0
movl $0x1199, %edx           # imm = 0x1199
callq 0x2110