// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: get_longest_hit(char*, unsigned long, unsigned long, unsigned long, unsigned long, kvec_vote*, ha_ug_index const*, unsigned long*, unsigned long, unsigned long*)
uint64_t get_longest_hit(char *r, uint64_t len, uint64_t k_mer, uint64_t self_p, uint64_t self_rev, kvec_vote* buf, const ha_ug_index* idx, 
uint64_t *pos_list, uint64_t cnt, uint64_t* c_sfx)
{
    uint64_t max_p, map_p_occ, i, j, m, rev, ref_p, u_len, uID, k_len;
    s_hit *p = NULL;
    ///each k-mer at different unitigs
    ///rev:uID:pos
    if(c_sfx) (*c_sfx) = (uint64_t)-1;
    for (j = 0; j < cnt; j++)
    {
        ///get
        kv_pushp(s_hit, buf->a, &p);
        rev = (pos_list[j]>>63) != self_rev;
        ref_p = pos_list[j] & idx->pos_mode;
        uID = (pos_list[j] << 1) >> (64 - idx->uID_bits);
        u_len = idx->ug->u.a[uID].len;
        if(rev) ref_p = u_len - 1 - (ref_p + 1 - k_mer);
        p->off_cnt = self_p | ((uint64_t)k_mer << 32); ///high bits should be the legnth

        p->ref = ref_p >= self_p? (ref_p-self_p) 
                        : (self_p-ref_p) + ((uint64_t)1 << (idx->pos_bits - 1));
        p->ref = (rev << 63)|(pos_list[j] & idx->uID_mode)|(p->ref&idx->pos_mode);


        ///extend
        k_len = check_exact_match(r, self_p + 1, len, idx->ug->u.a[uID].s, ref_p + 1, u_len, len, rev, 0);
        if(c_sfx && cnt == idx->hap_cnt && k_len < (*c_sfx)) (*c_sfx) = k_len;
        
        p->off_cnt += ((uint64_t)k_len << 32) + k_len;

        if(self_p >= k_mer && ref_p >= k_mer)
        {
            k_len = check_exact_match(r, self_p - k_mer, len, idx->ug->u.a[uID].s, 
                                                    ref_p - k_mer, u_len, len, rev, 1);
            p->off_cnt += ((uint64_t)k_len << 32);
        }
        // if(cnt > 0) fprintf(stderr, "inner j: %lu, rev: %lu, uID: %lu, ref_p: %lu, self_p: %u, len: %lu\n", j, rev, uID, ref_p, (uint32_t)p->off_cnt, p->off_cnt>>32);
    }

    p = buf->a.a + buf->a.n - cnt;
    if(cnt > 1) radix_sort_hc_s_hit_off_cnt(p, p + cnt);
    max_p = map_p_occ = 0; 
    for (j = 1, i = 0; j <= cnt; ++j)
    {
        if(j == cnt || p[j].off_cnt != p[i].off_cnt)
        {
            if((max_p>>32) < (p[i].off_cnt>>32))
            {
                max_p = p[i].off_cnt;
                map_p_occ = j - i;
            }
            else if(((max_p>>32) == (p[i].off_cnt>>32)) && ((j - i) > map_p_occ))
            {
                max_p = p[i].off_cnt;
                map_p_occ = j - i;
            }
            i = j;///must
        }
    }

    buf->a.n -= cnt;
    for (j = m = 0; j < cnt; j++)
    {
        if(p[j].off_cnt == max_p)
        {
            p[m] = p[j];
            m++;
        }
    }
    cnt = m;
    buf->a.n += cnt;
    
    // if(cnt > 0) fprintf(stderr, "max_p_offset: %u, max_p_len: %lu, map_p_occ: %lu\n", (uint32_t)max_p, max_p>>32, map_p_occ);
    return max_p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r14
movq %r8, 0x28(%rsp)
movq %rcx, %r11
movq %rdx, (%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq 0xa8(%rsp), %rax
testq %rax, %rax
je 0xf5106
orq $-0x1, (%rax)
movq 0xa0(%rsp), %r15
movq (%rsp), %rax
leaq -0x1(%rax), %rcx
movq %rcx, 0x50(%rsp)
movq %rax, %rcx
shlq $0x20, %rcx
orq %r11, %rcx
movq %rcx, 0x48(%rsp)
leaq 0x1(%r11), %rcx
movq %rcx, 0x40(%rsp)
movq %r11, %rcx
subq %rax, %rcx
movq %rcx, 0x38(%rsp)
xorl %ebx, %ebx
pushq $0x1
popq %rbp
movq %r14, 0x10(%rsp)
movq %r11, 0x8(%rsp)
cmpq %rbx, %r15
je 0xf533c
movq (%r14), %rax
movq 0x10(%r14), %rbp
cmpq 0x8(%r14), %rax
jne 0xf5190
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
shlq $0x4, %rsi
movq %rbp, %rdi
callq 0x2370
movq 0x8(%rsp), %r11
movq %rax, %rbp
movq %rax, 0x10(%r14)
movq (%r14), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%r14)
shlq $0x4, %rax
movq 0x98(%rsp), %rcx
movq %rbx, 0x30(%rsp)
movq (%rcx,%rbx,8), %r8
movq %r8, %rdi
shrq $0x3f, %rdi
movq 0x90(%rsp), %r15
movq (%r15), %rsi
movq 0x30(%r15), %rdx
movq %rdx, %r9
andq %r8, %r9
addq %r8, %r8
movb 0x18(%r15), %r10b
xorl %ecx, %ecx
subb %r10b, %cl
shrq %cl, %r8
movq 0x10(%rsi), %rsi
imulq $0x28, %r8, %r13
movl (%rsi,%r13), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r9, %r14
notq %r14
addq 0x50(%rsp), %r14
addq %r12, %r14
xorl %ebx, %ebx
cmpq 0x28(%rsp), %rdi
cmoveq %r9, %r14
setne %r9b
movq 0x48(%rsp), %rcx
movq %rcx, 0x8(%rbp,%rax)
movq %r14, %r8
subq %r11, %r8
jae 0xf522c
subq %r14, %r11
movb 0x28(%r15), %cl
decb %cl
pushq $0x1
popq %r8
shlq %cl, %r8
addq %r11, %r8
addq %rax, %rbp
movb %r9b, %bl
xorl %eax, %eax
cmpq 0x28(%rsp), %rdi
setne %al
movq %r8, (%rbp)
shlq $0x3f, %rax
movq 0x20(%r15), %rcx
movq 0x98(%rsp), %rdi
movq 0x30(%rsp), %r9
andq (%rdi,%r9,8), %rcx
orq %rax, %rcx
andq %r8, %rdx
orq %rcx, %rdx
movq %rdx, (%rbp)
movq 0x20(%rsi,%r13), %rcx
leaq 0x1(%r14), %r8
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r12, %r9
pushq $0x0
pushq %rbx
pushq %rdx
callq 0x120bf6
addq $0x20, %rsp
movq 0xa8(%rsp), %rcx
testq %rcx, %rcx
movq 0xa0(%rsp), %r15
je 0xf52bd
movq 0x90(%rsp), %rdx
cmpq %r15, 0x48(%rdx)
jne 0xf52bd
cmpq (%rcx), %rax
jae 0xf52bd
movq %rax, (%rcx)
movabsq $0x100000001, %rcx      # imm = 0x100000001
imulq %rcx, %rax
addq %rax, 0x8(%rbp)
movq 0x8(%rsp), %r11
cmpq (%rsp), %r11
jb 0xf5327
subq (%rsp), %r14
jb 0xf5327
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x20(%rax,%r13), %rcx
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r14, %r8
movq %r12, %r9
pushq $0x1
pushq %rbx
pushq %rdx
callq 0x120bf6
movq 0x28(%rsp), %r11
addq $0x20, %rsp
shlq $0x20, %rax
addq %rax, 0x8(%rbp)
movq 0x30(%rsp), %rbx
incq %rbx
movq 0x10(%rsp), %r14
pushq $0x1
popq %rbp
jmp 0xf514d
movq (%r14), %rbx
movq 0x10(%r14), %r12
shlq $0x4, %rbx
leaq (%r12,%rbx), %rsi
movq %r15, %r13
shlq $0x4, %r13
movq %rsi, %r14
subq %r13, %r14
cmpq $0x2, %r15
jb 0xf5366
movq %r14, %rdi
callq 0xf3184
subq %r13, %rbx
leaq (%rbx,%r12), %rcx
addq $0x18, %rcx
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
movq 0x10(%rsp), %r10
cmpq %r15, %rbp
ja 0xf53e7
movq %rsi, %rdi
jne 0xf5391
shlq $0x4, %rdi
movq 0x8(%r14,%rdi), %rdi
jmp 0xf539f
shlq $0x4, %rdi
movq 0x8(%r14,%rdi), %rdi
cmpq %rdi, (%rcx)
je 0xf53d8
movq %rax, %r8
shrq $0x20, %r8
movq %rdi, %r9
shrq $0x20, %r9
cmpq %r9, %r8
jae 0xf53ba
movq %rbp, %rdx
subq %rsi, %rdx
jmp 0xf53d0
cmpl %r9d, %r8d
jne 0xf53d5
movq %rbp, %r8
subq %rsi, %r8
cmpq %rdx, %r8
cmovaq %r8, %rdx
cmovbeq %rax, %rdi
movq %rbp, %rsi
jmp 0xf53db
movq %rbp, %rsi
movq %rax, %rdi
incq %rbp
addq $0x10, %rcx
movq %rdi, %rax
jmp 0xf537c
subq %r15, (%r10)
leaq (%r12,%rbx), %rdx
addq $0x8, %rdx
xorl %ecx, %ecx
subq $0x1, %r15
jb 0xf541b
cmpq %rax, (%rdx)
jne 0xf5415
leaq -0x8(%rdx), %rsi
movq %rcx, %rdi
shlq $0x4, %rdi
movups (%rsi), %xmm0
movups %xmm0, (%r14,%rdi)
incq %rcx
addq $0x10, %rdx
jmp 0xf53f4
addq %rcx, (%r10)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq