// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
testl %edx, %edx
je 0x19a539
movq %r8, %rbx
movq %rcx, %r14
movl %edx, %ebp
movq %rdi, %r12
movslq %edx, %r13
movl $0x28, %edx
movq %r13, %rsi
callq 0x1e3590
movq %rax, %r15
shlq $0x4, %r13
movq %r12, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1e36fd
movq %rax, %r12
movl %ebp, %r8d
testl %ebp, %ebp
jle 0x19a455
leaq (,%r8,8), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq %edx, %rdx
movq %rdx, %rsi
shlq $0x4, %rsi
movq 0x8(%rbx,%rsi), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
movzbl %dil, %edi
subl %edi, %esi
incl %esi
movq (%r14,%rcx), %rdi
shrdq $0x20, %rsi, %rdi
movq %rdi, (%r12,%rcx,2)
movq %rdx, %rsi
shlq $0x20, %rsi
movslq (%r14,%rcx), %rdi
orq %rsi, %rdi
movq %rdi, 0x8(%r12,%rcx,2)
addl (%r14,%rcx), %edx
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x19a40a
movq %r8, %r14
addq %r12, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x19712b
testl %ebp, %ebp
jle 0x19a52b
movq %r12, %rax
addq $0x8, %rax
shlq $0x3, %r14
leaq (%r14,%r14,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(%rbx,%r10), %r11d
movl %r9d, (%r15,%rdi)
movl %r8d, %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x4(%r15,%rdi), %r14d
andl %esi, %r14d
orl %ebp, %r14d
movl %r14d, 0x4(%r15,%rdi)
movq -0x8(%rax), %r14
movl %r14d, 0x1c(%r15,%rdi)
movq (%rbx,%r10), %r10
movq %r10, %r13
shrq $0x20, %r13
movl %r13d, 0x8(%r15,%rdi)
subl %r11d, %r10d
leal 0x1(%r10), %r10d
cmovll %edx, %r10d
movl %r10d, 0xc(%r15,%rdi)
shrq $0x20, %r14
movl %r14d, 0x14(%r15,%rdi)
addl %r8d, %r8d
sarl %r8d
addl %r9d, %r8d
movslq %r8d, %r8
shlq $0x4, %r8
movl -0x10(%rbx,%r8), %r9d
incl %r9d
movl %r9d, 0x10(%r15,%rdi)
movl -0x8(%rbx,%r8), %r8d
incl %r8d
movl %r8d, 0x18(%r15,%rdi)
addq $0x10, %rax
addq $0x28, %rdi
cmpq %rdi, %rcx
jne 0x19a486
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x1e3632
jmp 0x19a53c
xorl %r15d, %r15d
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq