// File: /workspace/github_cpp_makefile_star2_O2/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
xorl %r14d, %r14d
testl %edx, %edx
je 0x15bbef
movq %r8, %rbx
movq %rcx, %rbp
movq %rdi, %r12
movq %rdx, (%rsp)
movslq (%rsp), %r13
pushq $0x28
popq %rdx
movq %r13, %rsi
callq 0x1a3848
movq %rax, %r15
shlq $0x4, %r13
movq %r12, 0x10(%rsp)
movq %r12, %rdi
movq %r13, 0x8(%rsp)
movq %r13, %rsi
movq (%rsp), %r13
callq 0x1a39c4
movq %rax, %r12
testl %r13d, %r13d
cmovlel %r14d, %r13d
leaq (,%r13,8), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x15bb12
movslq %r14d, %r14
movq %r14, %rdx
shlq $0x4, %rdx
movq 0x8(%rbx,%rdx), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
movzbl %sil, %esi
subl %esi, %edx
incl %edx
movq (%rbp,%rcx), %rsi
shrdq $0x20, %rdx, %rsi
movq %rsi, (%r12,%rcx,2)
movq %r14, %rdx
shlq $0x20, %rdx
movslq (%rbp,%rcx), %rsi
orq %rdx, %rsi
movq %rsi, 0x8(%r12,%rcx,2)
addl (%rbp,%rcx), %r14d
addq $0x8, %rcx
jmp 0x15bac2
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %r12, %rdi
callq 0x158af1
imulq $0x28, %r13, %rax
movq %r12, %rcx
addq $0x8, %rcx
xorl %edx, %edx
movl $0x80000000, %esi       # imm = 0x80000000
xorl %edi, %edi
cmpq %rdi, %rax
je 0x15bbe0
movq (%rcx), %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(%rcx), %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 $0x28, %rdi
addq $0x10, %rcx
jmp 0x15bb36
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x1a38cf
jmp 0x15bbf2
xorl %r15d, %r15d
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq