// File: chhylp123[P]hifiasm/inter.cpp
// Function: ctg_trans_gp_chain(unsigned int, kv_ul_ov_t*, ul_idx_t const*, ug_opt_t const*, long, double, long, Chain_Data*, ma_ug_t*, unsigned int, asg64_v*, double)
void ctg_trans_gp_chain(uint32_t qid, kv_ul_ov_t *res, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, double diff_ec_ul, int64_t qlen, Chain_Data* dp, ma_ug_t *ref, uint32_t is_self, asg64_v *b, double unmatch_max)
{
	uint64_t k, l, m = 0, rn = res->n; ul_ov_t rr; uint32_t avid = ((is_self)?(qid):((uint32_t)-1));
	for (k = 0; k < rn; k++) {
		res->a[k].tn <<= 1; res->a[k].tn |= res->a[k].rev;
	}
	radix_sort_ul_ov_srt_tn(res->a, res->a + rn);

	for (k = 1, l = 0, b->n = 0; k <= rn; k++) {
		if((k == rn) || ((res->a[l].tn>>1) != (res->a[k].tn>>1))) {
			// fprintf(stderr, "+[M::%s]\tutg%.6ul\n", __func__, (res->a[l].tn>>1) + 1);
			if(((res->a[l].tn>>1) != avid) && (linear_ctg_trans_chain_dp(qid, res->a+l, k-l, uref, uopt, bw, diff_ec_ul, /**0.333333**/0.4, unmatch_max, qlen, ref->u.a[(res->a[l].tn>>1)].len, UG_SKIP_N, UG_ITER_N, UG_DIS_N, dp, &rr, b))) {
				res->a[m++] = rr; 
				// fprintf(stderr, "-[M::%s]\tutg%.6ul\n", __func__, rr.tn + 1);
			}
			l = k;
		}
	}
	res->n = m;

	// for (k = 0; k < res->n; k++) {
    //     fprintf(stderr, "[M::%s]\tutg%.6ul(len::%ld)\tq::[%u, %u)\t%c\tutg%.6ul(len::%u)\tt::[%u, %u)\tel::%u\ttp::%u\n", __func__, qid + 1, qlen, res->a[k].qs, res->a[k].qe, "+-"[res->a[k].rev], res->a[k].tn+1, ref->u.a[res->a[k].tn].len, res->a[k].ts, res->a[k].te, res->a[k].el, res->a[k].qn);
    // }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0xd0, %rsp
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x14(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movq %r9, -0x48(%rbp)
movsd %xmm1, -0x50(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
cmpl $0x0, 0x20(%rbp)
je 0x2e2623
movl -0x14(%rbp), %eax
movl %eax, -0x94(%rbp)
jmp 0x2e2630
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x94(%rbp)
jmp 0x2e2630
movl -0x94(%rbp), %eax
movl %eax, -0x90(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0x2e269d
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x58(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
shll %ecx
movl %ecx, 0xc(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x58(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
shrl $0x1f, %ecx
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x58(%rbp), %rdx
addq %rdx, %rax
orl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2e2644
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
imulq $0x1c, -0x70(%rbp), %rax
addq %rax, %rsi
callq 0x27bdd0
movq $0x1, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq 0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x58(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0x2e2852
movq -0x58(%rbp), %rax
cmpq -0x70(%rbp), %rax
je 0x2e271b
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x60(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl %eax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x58(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0x2e283f
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x60(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl %eax
cmpl -0x90(%rbp), %eax
je 0x2e2837
movl -0x14(%rbp), %edi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
imulq $0x1c, -0x60(%rbp), %rax
addq %rax, %rsi
movq -0x58(%rbp), %rdx
subq -0x60(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
movsd -0x40(%rbp), %xmm0
movsd -0x50(%rbp), %xmm2
movq -0x48(%rbp), %r14
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %r10
movq (%r10), %r10
imulq $0x1c, -0x60(%rbp), %r11
addq %r11, %r10
movl 0xc(%r10), %r10d
shrl %r10d
movl %r10d, %r10d
imulq $0x28, %r10, %r10
addq %r10, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ebx
movq 0x10(%rbp), %r11
movq 0x28(%rbp), %rax
movsd 0x9fa86(%rip), %xmm1    # 0x382238
leaq -0x8c(%rbp), %r10
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq $0x64, 0x10(%rsp)
movq $0x1388, 0x18(%rsp)     # imm = 0x1388
movq $0xc350, 0x20(%rsp)     # imm = 0xC350
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x2e11c0
cmpq $0x0, %rax
je 0x2e2837
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x68(%rbp)
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq -0x8c(%rbp), %rcx
movq %rcx, (%rax)
movq -0x84(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x7c(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x74(%rbp), %ecx
movl %ecx, 0x18(%rax)
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x2e2841
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2e26d3
movq -0x68(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nopw (%rax,%rax)