// File: chhylp123[P]hifiasm/inter.cpp
// Function: ctg_rg2ug_gen(ma_utg_t*, kv_ul_ov_t*, ul_idx_t const*, unsigned long)
void ctg_rg2ug_gen(ma_utg_t *r_cl, kv_ul_ov_t *u_cl, const ul_idx_t *uref, uint64_t id)
{
	if(r_cl->n <= 0) return ;
	uint64_t k, i, m, l, is_push, vs, ls; ul_ov_t kp, lp, kp0; 
	u_cl->n = 0; kp.tn = kp.qn = kp0.tn = kp0.qn = ((uint32_t)-1); 

	k = 1; l = 0; ls = 0; vs = 0;
	get_unique_rctg_aln(uref, r_cl->a[l]>>32, l, ls, r_cl->len, &lp, NULL, ((uint32_t)-1)); ///get lp for l = 0
	vs += (uint32_t)(r_cl->a[l]);

	for (; k <= r_cl->n; k++) {
		is_push = 0;
		if(k < r_cl->n) {
			get_unique_rctg_aln(uref, r_cl->a[k]>>32, k, vs, r_cl->len, &kp, NULL, ((uint32_t)-1)); 
			if(cmp_shrink_ul_ov_t(&lp, &kp, k - l)) is_push = 1;
		} else {
			is_push = 1;
		}

		if(is_push) {
			kp0 = kp;
			if(k - l > 1) {
				for (i = l; i < k; i++) {///could be merged
					m = get_unique_rctg_aln(uref, r_cl->a[i]>>32, i, ls, r_cl->len, &kp, NULL, ((uint32_t)-1)); 
					assert(m == 1); assert(kp.tn == lp.tn); assert(kp.qn == lp.qn + i - l); assert(kp.sec == lp.sec + i - l);
					if(kp.qs < lp.qs) lp.qs = kp.qs; if(kp.qe > lp.qe) lp.qe = kp.qe;
					if(kp.ts < lp.ts) lp.ts = kp.ts; if(kp.te > lp.te) lp.te = kp.te;
					ls += (uint32_t)(r_cl->a[i]); 

					// if(id == 123) {
					// 	fprintf(stderr, "-0-[M::%s::]\tutg%.6dl(%c)\tq::[%d, %d)<l::%u>\tt::[%d, %d)<l::%u>\tsc::%d\n", __func__, 
					// 					(int32_t)(kp.tn>>1)+1, "+-"[kp.tn&1], kp.qs, kp.qe, kp.qe-kp.qs, kp.ts, kp.te, kp.te-kp.ts, kp.sec);
					// }
				}
				lp.sec = k - l;
				kv_push(ul_ov_t, *u_cl, lp);
			} else if(k - l == 1) {
				m = get_unique_rctg_aln(uref, r_cl->a[l]>>32, l, ls, r_cl->len, NULL, u_cl, 1); 
				// if(id == 123) {
				// 	fprintf(stderr, "-1-[M::%s::]\tutg%.6dl(%c)\tq::[%d, %d)<l::%u>\tt::[%d, %d)<l::%u>\tsc::%d\n", __func__, 
				// 						(int32_t)(kp.tn>>1)+1, "+-"[kp.tn&1], kp.qs, kp.qe, kp.qe-kp.qs, kp.ts, kp.te, kp.te-kp.ts, kp.sec);
				// }
			}

			l = k; lp = kp0; ls = vs;
		}

		if(k < r_cl->n) vs += (uint32_t)(r_cl->a[k]);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x10(%rax)
ja 0x2ddaea
jmp 0x2ddfd9
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movl $0xffffffff, -0xac(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xa0(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movq $0x1, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
shrq $0x20, %rsi
movq -0x40(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r8d
leaq -0x90(%rbp), %r9
xorl %eax, %eax
movq $0x0, (%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
callq 0x2dd600
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
ja 0x2ddfd9
movq $0x0, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x2ddc3f
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
shrq $0x20, %rsi
movq -0x28(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r8d
leaq -0x74(%rbp), %r9
xorl %eax, %eax
movq $0x0, (%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
callq 0x2dd600
movq -0x28(%rbp), %rdx
subq -0x40(%rbp), %rdx
leaq -0x90(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x2dda20
cmpq $0x0, %rax
je 0x2ddc3d
movq $0x1, -0x48(%rbp)
jmp 0x2ddc47
movq $0x1, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x2ddf9c
movq -0x74(%rbp), %rax
movq %rax, -0xac(%rbp)
movq -0x6c(%rbp), %rax
movq %rax, -0xa4(%rbp)
movq -0x64(%rbp), %rax
movq %rax, -0x9c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x28(%rbp), %rax
subq -0x40(%rbp), %rax
cmpq $0x1, %rax
jbe 0x2ddf00
movq -0x40(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x2dde30
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
shrq $0x20, %rsi
movq -0x30(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r8d
leaq -0x74(%rbp), %r9
xorl %eax, %eax
movq $0x0, (%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
callq 0x2dd600
movq %rax, -0x38(%rbp)
cmpq $0x1, -0x38(%rbp)
jne 0x2ddcfc
jmp 0x2ddd1b
leaq 0xb632e(%rip), %rdi     # 0x394031
leaq 0xb2b25(%rip), %rsi     # 0x39082f
movl $0x3fdb, %edx           # imm = 0x3FDB
leaq 0xb6322(%rip), %rcx     # 0x394038
callq 0x2130
movl -0x68(%rbp), %eax
cmpl -0x84(%rbp), %eax
jne 0x2ddd28
jmp 0x2ddd47
leaq 0xb6352(%rip), %rdi     # 0x394081
leaq 0xb2af9(%rip), %rsi     # 0x39082f
movl $0x3fdb, %edx           # imm = 0x3FDB
leaq 0xb62f6(%rip), %rcx     # 0x394038
callq 0x2130
movl -0x74(%rbp), %eax
movl -0x90(%rbp), %ecx
addq -0x30(%rbp), %rcx
subq -0x40(%rbp), %rcx
cmpq %rcx, %rax
jne 0x2ddd5f
jmp 0x2ddd7e
leaq 0xb632a(%rip), %rdi     # 0x394090
leaq 0xb2ac2(%rip), %rsi     # 0x39082f
movl $0x3fdb, %edx           # imm = 0x3FDB
leaq 0xb62bf(%rip), %rcx     # 0x394038
callq 0x2130
movl -0x5c(%rbp), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl %eax, %eax
movl -0x78(%rbp), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, %ecx
addq -0x30(%rbp), %rcx
subq -0x40(%rbp), %rcx
cmpq %rcx, %rax
jne 0x2ddda2
jmp 0x2dddc1
leaq 0xb62fe(%rip), %rdi     # 0x3940a7
leaq 0xb2a7f(%rip), %rsi     # 0x39082f
movl $0x3fdb, %edx           # imm = 0x3FDB
leaq 0xb627c(%rip), %rcx     # 0x394038
callq 0x2130
movl -0x70(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jae 0x2dddd5
movl -0x70(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x88(%rbp), %eax
jbe 0x2ddde9
movl -0x6c(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x80(%rbp), %eax
jae 0x2dddf7
movl -0x64(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jbe 0x2dde05
movl -0x60(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x2ddc96
movq -0x28(%rbp), %rax
subq -0x40(%rbp), %rax
movl %eax, %ecx
movl -0x78(%rbp), %eax
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl $0xc0000000, %eax       # imm = 0xC0000000
orl %ecx, %eax
movl %eax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x2ddeba
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2dde7e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xb8(%rbp)
jmp 0x2dde8c
movl $0x2, %eax
movq %rax, -0xb8(%rbp)
jmp 0x2dde8c
movq -0xb8(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
imulq $0x1c, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x8(%rdx)
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq -0x90(%rbp), %rcx
movq %rcx, (%rax)
movq -0x88(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x80(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x78(%rbp), %ecx
movl %ecx, 0x18(%rax)
jmp 0x2ddf5c
movq -0x28(%rbp), %rax
subq -0x40(%rbp), %rax
cmpq $0x1, %rax
jne 0x2ddf5a
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
shrq $0x20, %rsi
movq -0x40(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r8d
movq -0x10(%rbp), %rax
xorl %r9d, %r9d
movq %rax, (%rsp)
movl $0x1, 0x8(%rsp)
callq 0x2dd600
movq %rax, -0x38(%rbp)
jmp 0x2ddf5c
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0xac(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0xa4(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x9c(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x2ddfc6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x2ddfc8
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x2ddba0
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)