// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rdx, 0x18(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movq %rcx, (%rsp)
movq %rcx, %rdi
callq 0x3f1bd
movq $0x0, (%rbx)
movl 0x1c(%rbx), %esi
cmpl 0x10(%rbx), %esi
jbe 0x133d98
movl %esi, 0x10(%rbx)
movq 0x28(%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%rbx)
movq $0x0, (%rbx)
cmpl $0x0, 0x18(%rbx)
je 0x133f06
xorl %r14d, %r14d
leaq 0xc(%rsp), %r13
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
movq (%rax,%r14), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x1194bb
movq 0x28(%rbx), %rcx
movq %rax, 0x8(%rcx,%r14)
movslq 0xc(%rsp), %rax
movl %eax, (%rcx,%r14)
addq (%rbx), %rax
movq %rax, (%rbx)
incq %r15
movl 0x18(%rbx), %ecx
addq $0x10, %r14
cmpq %rcx, %r15
jb 0x133db4
cmpq 0x8(%rbx), %rax
jbe 0x133e10
movq %rax, 0x8(%rbx)
movq 0x30(%rbx), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x30(%rbx)
movl 0x18(%rbx), %ecx
leaq 0x8(%rbx), %rdx
testl %ecx, %ecx
je 0x133f0a
movq %rdx, 0x10(%rsp)
movl %ecx, %ecx
movq 0x20(%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rbx), %rsi
xorl %edi, %edi
movabsq $0x100000000, %r9       # imm = 0x100000000
xorl %r10d, %r10d
movq %rdi, %rax
shlq $0x4, %rax
movslq (%rsi,%rax), %r11
testq %r11, %r11
jle 0x133eef
leaq (%rsi,%rax), %r14
movq 0x8(%r14), %r12
movq 0x30(%rbx), %r14
movq 0x20(%rsp), %rdx
leaq (%rdx,%rax), %r13
addq $0x8, %r13
movl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl %eax, %r11d
movl $0xffffff, %ebp         # imm = 0xFFFFFF
cmovbl %r11d, %ebp
shll $0x8, %ebp
leaq (%r10,%r10,2), %rax
leaq (%r14,%rax,8), %r14
addq $0x10, %r14
xorl %eax, %eax
movq (%r12,%rax,8), %r8
movq (%r13), %r15
xorq %r8, %r15
shrq $0x38, %r8
movl %r8d, -0x4(%r14)
movl %edi, -0x8(%r14)
movl %ebp, (%r14)
movzbl 0x7(%r13), %r8d
orl %ebp, %r8d
movl %r8d, (%r14)
movq (%r12,%rax,8), %r8
movl %r8d, %edx
andl $0xfffffff, %edx        # imm = 0xFFFFFFF
shlq $0x21, %rdx
shrq $0x17, %r15
andq %r9, %r15
orq %rdx, %r15
shrq $0x1c, %r8
andl $0x7ffffff, %r8d        # imm = 0x7FFFFFF
orq %r15, %r8
movq %r8, -0x10(%r14)
incq %rax
addq $0x18, %r14
cmpq %rax, %r11
jne 0x133e8e
addq %rax, %r10
incq %rdi
cmpq %rcx, %rdi
jne 0x133e3f
movq (%rsp), %rax
movq 0x10(%rsp), %rdx
jmp 0x133f0e
leaq 0x8(%rbx), %rdx
movq (%rsp), %rax
movq (%rdx), %rsi
cmpq 0x10(%rax), %rsi
jb 0x133f2d
movq %rsi, 0x10(%rax)
movq (%rax), %rdi
shlq $0x4, %rsi
movq %rax, %r14
callq 0x2370
movq %rax, (%r14)
movq 0x18(%rsp), %rdi
callq 0x3d018
movq (%rbx), %rax
movq 0x30(%rbx), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x125ce1
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq