// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/anchor.cpp
// Function: h_ec_lchain_re_gen_srt(ha_abuf_s*, ha_pt_s*, overlap_region_alloc*, Candidates_list*)
void h_ec_lchain_re_gen_srt(ha_abuf_t *ab, ha_pt_t *ha_idx, overlap_region_alloc *olst, Candidates_list *cl)
{
	uint64_t i, k; int j, n; ha_mz1_t *z; seed1_t *s; 
    
    clear_Candidates_list(cl); ab->n_a = 0;

	// minimizer of queried read
    if (ab->mz.m > ab->old_mz_m) {
        ab->old_mz_m = ab->mz.m;
        REALLOC(ab->seed, ab->old_mz_m);
    }

    for (i = 0, ab->n_a = 0; i < ab->mz.n; ++i) {
        ab->seed[i].a = ha_pt_get(ha_idx, ab->mz.a[i].x, &n);
        ab->seed[i].n = n;
        ab->n_a += n;
    }

	if (ab->n_a > ab->m_a) {
        ab->m_a = ab->n_a;
        REALLOC(ab->a, ab->m_a);
    }

	for (i = 0, k = 0; i < ab->mz.n; ++i) {
        ///z is one of the minimizer
        z = &ab->mz.a[i]; s = &ab->seed[i];

        for (j = 0; j < s->n; ++j) {
            const ha_idxpos_t *y = &s->a[j];
            anchor1_t *an = &ab->a[k++];
            uint8_t rev = z->rev == y->rev? 0 : 1;

			an->other_off = y->span;
			an->self_off = i;
            ///an->cnt: cnt<<8|span
            an->cnt = s->n; if(an->cnt > ((uint32_t)(0xffffffu))) an->cnt = 0xffffffu;
            an->cnt <<= 8; an->cnt |= ((z->span <= ((uint32_t)(0xffu)))?z->span:((uint32_t)(0xffu)));
			an->srt = (uint64_t)y->rid<<33 | (uint64_t)rev<<32 | y->pos;
        }
    }

	// copy over to _cl_
    if (ab->m_a >= (uint64_t)cl->size) {
        cl->size = ab->m_a;
        REALLOC(cl->list, cl->size);
    }
	clear_overlap_region_alloc(olst);
	radix_sort_ha_an1(ab->a, ab->a + ab->n_a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movq %rcx, 0x8(%rsp)
movq %rcx, %rdi
callq 0x13d24
andq $0x0, (%rbx)
movl 0x1c(%rbx), %esi
cmpl 0x10(%rbx), %esi
jbe 0xed703
movl %esi, 0x10(%rbx)
movq 0x28(%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%rbx)
andq $0x0, (%rbx)
xorl %ebp, %ebp
leaq 0x4(%rsp), %r13
xorl %ecx, %ecx
xorl %r14d, %r14d
movl 0x18(%rbx), %eax
cmpq %rax, %r14
jae 0xed74e
movq 0x20(%rbx), %rax
movq (%rax,%rbp), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0xd4de2
movq 0x28(%rbx), %rdx
movq %rax, 0x8(%rdx,%rbp)
movslq 0x4(%rsp), %rcx
movl %ecx, (%rdx,%rbp)
addq (%rbx), %rcx
movq %rcx, (%rbx)
incq %r14
addq $0x10, %rbp
jmp 0xed713
cmpq 0x8(%rbx), %rcx
jbe 0xed76c
movq %rcx, 0x8(%rbx)
movq 0x30(%rbx), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x30(%rbx)
movl 0x18(%rbx), %eax
movabsq $0x100000000, %rsi      # imm = 0x100000000
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %rax, %r8
je 0xed83d
movq 0x20(%rbx), %r11
movq 0x28(%rbx), %r9
movq %r8, %r14
shlq $0x4, %r14
movl (%r9,%r14), %r10d
addq %r14, %r9
addq %r14, %r11
addq $0x8, %r11
movl $0xffffff, %ecx         # imm = 0xFFFFFF
cmpl %ecx, %r10d
movl $0xffffff, %ebp         # imm = 0xFFFFFF
cmovbl %r10d, %ebp
shll $0x8, %ebp
testl %r10d, %r10d
movl $0x0, %ecx
cmovlel %ecx, %r10d
imulq $0x18, %rdi, %r13
xorl %r12d, %r12d
cmpq %r12, %r10
je 0xed832
movq 0x8(%r9), %rcx
movq 0x30(%rbx), %r14
movq (%rcx,%r12,8), %rdx
movq (%r11), %r15
xorq %rdx, %r15
shrq $0x38, %rdx
movl %edx, 0xc(%r14,%r13)
movl %r8d, 0x8(%r14,%r13)
movl %ebp, 0x10(%r14,%r13)
movzbl 0x7(%r11), %edx
orl %ebp, %edx
movl %edx, 0x10(%r14,%r13)
movq (%rcx,%r12,8), %rcx
movl %ecx, %edx
andl $0xfffffff, %edx        # imm = 0xFFFFFFF
shlq $0x21, %rdx
shrq $0x17, %r15
andq %rsi, %r15
orq %rdx, %r15
shrq $0x1c, %rcx
andl $0x7ffffff, %ecx        # imm = 0x7FFFFFF
orq %r15, %rcx
movq %rcx, (%r14,%r13)
incq %r12
addq $0x18, %r13
jmp 0xed7c8
incq %r8
addq %r12, %rdi
jmp 0xed77b
movq 0x8(%rbx), %rsi
movq 0x8(%rsp), %rax
cmpq 0x10(%rax), %rsi
jb 0xed862
movq %rsi, 0x10(%rax)
movq (%rax), %rdi
shlq $0x4, %rsi
movq %rax, %r14
callq 0x2370
movq %rax, (%r14)
movq 0x10(%rsp), %rdi
callq 0x11935
movq 0x30(%rbx), %rdi
imulq $0x18, (%rbx), %rsi
addq %rdi, %rsi
callq 0xe1068
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq