// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rcx, %rdi
callq 0x3fd1c
movq $0x0, (%rbx)
movl 0x1c(%rbx), %esi
cmpl 0x10(%rbx), %esi
jbe 0x12dba8
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 0x12dbfb
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 0x1139ee
movq 0x28(%rbx), %rcx
movq %rax, 0x8(%rcx,%r14)
movl 0xc(%rsp), %eax
movl %eax, (%rcx,%r14)
movslq 0xc(%rsp), %rax
addq %rax, (%rbx)
incq %r15
movl 0x18(%rbx), %eax
addq $0x10, %r14
cmpq %rax, %r15
jb 0x12dbc0
movq (%rbx), %rax
cmpq 0x8(%rbx), %rax
jbe 0x12dc1d
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), %eax
testq %rax, %rax
je 0x12dd02
movq 0x20(%rbx), %rcx
movq %rcx, 0x20(%rsp)
movq 0x28(%rbx), %rdx
xorl %esi, %esi
movabsq $0x100000000, %r8       # imm = 0x100000000
xorl %r9d, %r9d
movq %rsi, %r14
shlq $0x4, %r14
movslq (%rdx,%r14), %r10
testq %r10, %r10
jle 0x12dcf6
leaq (%rdx,%r14), %r11
movq 0x8(%r11), %r11
movq 0x30(%rbx), %r15
movq 0x20(%rsp), %rcx
leaq (%rcx,%r14), %r12
addq $0x8, %r12
movl $0xffffff, %ecx         # imm = 0xFFFFFF
cmpl %ecx, %r10d
movl $0xffffff, %ebp         # imm = 0xFFFFFF
cmovbl %r10d, %ebp
shll $0x8, %ebp
leaq (%r9,%r9,2), %r14
leaq (%r15,%r14,8), %r13
addq $0x10, %r13
xorl %r14d, %r14d
movq (%r11,%r14,8), %rdi
movq (%r12), %r15
xorq %rdi, %r15
shrq $0x38, %rdi
movl %edi, -0x4(%r13)
movl %esi, -0x8(%r13)
movl %ebp, (%r13)
movzbl 0x7(%r12), %edi
orl %ebp, %edi
movl %edi, (%r13)
movq (%r11,%r14,8), %rdi
movl %edi, %ecx
andl $0xfffffff, %ecx        # imm = 0xFFFFFFF
shlq $0x21, %rcx
shrq $0x17, %r15
andq %r8, %r15
orq %rcx, %r15
shrq $0x1c, %rdi
andl $0x7ffffff, %edi        # imm = 0x7FFFFFF
orq %r15, %rdi
movq %rdi, -0x10(%r13)
incq %r14
addq $0x18, %r13
cmpq %r14, %r10
jne 0x12dc95
addq %r14, %r9
incq %rsi
cmpq %rax, %rsi
jne 0x12dc45
movq 0x8(%rbx), %rsi
movq 0x10(%rsp), %r14
cmpq 0x10(%r14), %rsi
jb 0x12dd24
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r14)
movq 0x18(%rsp), %rdi
callq 0x3db37
movq (%rbx), %rax
movq 0x30(%rbx), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x11fe18
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq