// File: /workspace/github_c_makefile_star2_O3/lh3[P]minigraph/lchain.c
// Function: mg_lchain_gen
mg_lchain_t *mg_lchain_gen(void *km, uint32_t hash, int qlen, int n_u, uint64_t *u, mg128_t *a)
{
	mg128_t *z;
	mg_lchain_t *r;
	int i, k;

	if (n_u == 0) return 0;
	KCALLOC(km, r, n_u);

	// sort by query position
	KMALLOC(km, z, n_u);
	for (i = k = 0; i < n_u; ++i) {
		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);

	// populate r[]
	for (i = 0; i < n_u; ++i) {
		mg_lchain_t *ri = &r[i];
		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;
		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;
	}
	kfree(km, z);
	return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testl %ecx, %ecx
je 0x18858
movq %r9, %r15
movq %r8, %rbx
movl %ecx, %ebp
movq %rdi, %r12
movslq %ecx, %r13
movl $0x28, %edx
movq %r13, %rsi
callq 0x2f84
movq %rax, %r14
shlq $0x4, %r13
movq %r12, 0x10(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x30e5
movq %rax, %r12
testl %ebp, %ebp
jle 0x1885d
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 0x18736
addq %r12, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xf27f
testl %ebp, %ebp
jle 0x1886b
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 0x187b3
jmp 0x1886b
xorl %r14d, %r14d
jmp 0x18878
movq %r12, %rsi
addq %r13, %rsi
movq %r12, %rdi
callq 0xf27f
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x301f
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq