// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: mg_lchain_gen(void*, int, int, unsigned long*, mg128_t*, ma_ug_t const*)
mg_lchain_t *mg_lchain_gen(void *km, int qlen, int n_u, uint64_t *u, mg128_t *a, const ma_ug_t *ug)
{
	mg128_t *z;
	mg_lchain_t *r;
	int i, k;

	if (n_u == 0) return 0;
	KCALLOC(km, r, n_u); KMALLOC(km, z, n_u);
	// u[] is sorted by query position
	for (i = k = 0; i < n_u; ++i) {
		/**
		 * a[].x: ref_id(31)rev(1)r_pos(32)
		 * a[].y: weight(8)query_id(8)flag(8)span(8)q_pos(32)
		**/
		///u[]: sc(32)occ(32)
		int32_t qs = (int32_t)a[k].y + 1 - (a[k].y>>32 & 0xff);
		z[i].x = (uint64_t)qs << 32 | u[i] >> 32;
		z[i].y = (uint64_t)k << 32 | (int32_t)u[i];
		k += (int32_t)u[i];
	}
	radix_sort_128x(z, z + n_u);//sort by qs|sc

	// populate r[]
	for (i = 0; i < n_u; ++i) {
		mg_lchain_t *ri = &r[i];
		/**
		 * z[].x: query start pos| chain score
		 * z[].y: idx in a[] | chain occ
		 * a[].x: ref_id(31)rev(1)r_pos(32)
		 * a[].y: weight(8)query_id(8)flag(8)span(8)q_pos(32)
		 * **/
		int32_t k = z[i].y >> 32, q_span = a[k].y >> 32 & 0xff;
		ri->off = k;
		ri->cnt = (int32_t)z[i].y;
		ri->score = (uint32_t)z[i].x;
		ri->v = a[k].x >> 32;///ref_id|rev
		ri->rs = (int32_t)a[k].x + 1 > q_span? (int32_t)a[k].x + 1 - q_span : 0; // for HPC k-mer
		ri->qs = z[i].x >> 32;
		ri->re = (int32_t)a[k + ri->cnt - 1].x + 1;
		ri->qe = (int32_t)a[k + ri->cnt - 1].y + 1;
		// fprintf(stderr, "+0+\tA\tutg%.6d%c\t%c\tqs:%u\tqe:%u\tql:%d\tts:%u\tte:%u\ttl:%u\n", 
        //         (ri->v>>1)+1, "lc"[ug->u.a[ri->v>>1].circ], "+-"[ri->v&1], ri->qs, ri->qe, qlen, ri->rs, ri->re, ug->u.a[ri->v>>1].len);
		// extend_coordinates(ri, qlen, ug->u.a[ri->v>>1].len);
		// fprintf(stderr, "-0-\tA\tutg%.6d%c\t%c\tqs:%u\tqe:%u\tql:%d\tts:%u\tte:%u\ttl:%u\n", 
        //         (ri->v>>1)+1, "lc"[ug->u.a[ri->v>>1].circ], "+-"[ri->v&1], ri->qs, ri->qe, qlen, ri->rs, ri->re, ug->u.a[ri->v>>1].len);
	}
	kfree(km, z);
	return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testl %edx, %edx
je 0x1a31b7
movq %r8, %r15
movq %rcx, %rbx
movl %edx, %ebp
movq %rdi, %r12
movslq %edx, %r13
movl $0x28, %edx
movq %r13, %rsi
callq 0x1edfb4
movq %rax, %r14
shlq $0x4, %r13
movq %r12, 0x10(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1ee115
movq %rax, %r12
testl %ebp, %ebp
jle 0x1a31bc
movl %ebp, %eax
movq %rax, 0x8(%rsp)
leaq (,%rax,8), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq %edx, %rdx
movq %rdx, %rsi
shlq $0x4, %rsi
movq 0x8(%r15,%rsi), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
movzbl %dil, %edi
subl %edi, %esi
incl %esi
movq (%rbx,%rcx), %rdi
shrdq $0x20, %rsi, %rdi
movq %rdi, (%r12,%rcx,2)
movq %rdx, %rsi
shlq $0x20, %rsi
movslq (%rbx,%rcx), %rdi
orq %rsi, %rdi
movq %rdi, 0x8(%r12,%rcx,2)
addl (%rbx,%rcx), %edx
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x1a3095
addq %r12, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x19febb
testl %ebp, %ebp
jle 0x1a31ca
movq %r12, %rax
addq $0x8, %rax
movq 0x8(%rsp), %rcx
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
movl $0x80000000, %esi       # imm = 0x80000000
xorl %edi, %edi
movq (%rax), %r8
movq %r8, %r9
shrq $0x20, %r9
movq %r8, %r10
sarq $0x20, %r10
shlq $0x4, %r10
movzbl 0xc(%r15,%r10), %r11d
movl %r9d, (%r14,%rdi)
movl %r8d, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movl 0x4(%r14,%rdi), %ebp
andl %esi, %ebp
orl %ebx, %ebp
movl %ebp, 0x4(%r14,%rdi)
movq -0x8(%rax), %rbx
movl %ebx, 0x1c(%r14,%rdi)
movq (%r15,%r10), %r10
movq %r10, %r13
shrq $0x20, %r13
movl %r13d, 0x8(%r14,%rdi)
subl %r11d, %r10d
leal 0x1(%r10), %r10d
cmovll %edx, %r10d
movl %r10d, 0xc(%r14,%rdi)
shrq $0x20, %rbx
movl %ebx, 0x14(%r14,%rdi)
addl %r8d, %r8d
sarl %r8d
addl %r9d, %r8d
movslq %r8d, %r8
shlq $0x4, %r8
movl -0x10(%r15,%r8), %r9d
incl %r9d
movl %r9d, 0x10(%r14,%rdi)
movl -0x8(%r15,%r8), %r8d
incl %r8d
movl %r8d, 0x18(%r14,%rdi)
addq $0x10, %rax
addq $0x28, %rdi
cmpq %rdi, %rcx
jne 0x1a3112
jmp 0x1a31ca
xorl %r14d, %r14d
jmp 0x1a31d7
movq %r12, %rsi
addq %r13, %rsi
movq %r12, %rdi
callq 0x19febb
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x1ee04f
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq