// File: chhylp123[P]hifiasm/inter.cpp
// Function: gl_ug2rg_gen(asg_t const*, ul_vec_t*, ma_ug_t*, mg_lchain_t*, vec_mg_lchain_t*, long, long, kv_ul_ov_t*, kv_ul_ov_t*)
void gl_ug2rg_gen(const asg_t *rg, ul_vec_t *rch, ma_ug_t *ug, mg_lchain_t *uo, vec_mg_lchain_t *res, int64_t tOff, 
int64_t ulid, kv_ul_ov_t *raw_idx, kv_ul_ov_t *raw_chn)
{
	// fprintf(stderr, "\n[M::%s::] uo->qs:%d, uo->qe:%d\n", __func__, uo->qs, uo->qe);
	///uo is a unitig alignment
	ma_utg_t *u = &(ug->u.a[uo->v>>1]); int64_t res_n0 = res->n;
	uint64_t rs = uo->rs, re = uo->re, i, l; utg_ct_t p; 

	for (i = l = 0; i < u->n; i++) {
		p.x = u->a[i]>>32; p.s = l; p.e = l + Get_READ_LENGTH(R_INF, (u->a[i]>>33));
		l += (uint32_t)u->a[i];
		if(p.e <= rs) continue; 
		if(p.s >= re) break;
		
		assert(extract_rovlp_by_ug(&p, uo, res, tOff));
		// if(!extract_rovlp_by_ug(&p, uo, res, tOff)) {
		// 	fprintf(stderr, "[M::%s::ulid::%ld]u_rs->%lu, u_re->%lu, p.s->%u, p.e->%u\n", __func__, ulid, rs, re, p.s, p.e); 
		// 	exit(1);
		// }
		res->a[res->n-1].score = uo->v; res->a[res->n-1].cnt = i;
		// fprintf(stderr, "[M::%s::]u_rs->%lu, u_re->%lu, p.s->%u, p.e->%u\n", __func__, rs, re, p.s, p.e); 

		// int64_t read_rs, read_re, read_qs, read_qe;
		// get_r_offset(ug, &(res->a[res->n-1]), &read_rs, &read_re, &read_qs, &read_qe);
		// fprintf(stderr, "\t\t\t(k->%u) r_rs->%ld, r_re->%ld, r_qs->%ld, r_qe->%ld\n", (uint32_t)res->n-1, read_rs, read_re, read_qs, read_qe); 
		///for res->a[res->n-1]
		///ts and te are the coordinates in unitig (res->a[res->n-1].score>>1), instead of HiFi read (res->a[res->n-1].v>>1)
		///qs and qe are the coordinates in UL, 
	}

	mg_lchain_t *a = res->a + res_n0, t; uint64_t a_n = res->n - res_n0; 
	if(uo->v&1) {
		for (i = 0; i < (a_n>>1); i++) {
			t = a[i]; a[i] = a[a_n-i-1]; a[a_n-i-1] = t;
		}
	}

	update_existing_anchors(rg, rch, ug, u, res, res_n0, uo, raw_idx, raw_chn);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movslq 0xc(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movslq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x2d8e68
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rax
movl %eax, -0x68(%rbp)
movq -0x60(%rbp), %rax
leaq 0xec562(%rip), %rcx     # 0x3c52d0
movq 0x18(%rcx), %rcx
movq -0x38(%rbp), %rdx
movq 0x18(%rdx), %rdx
movq -0x58(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
addq (%rcx,%rdx,8), %rax
movl %eax, -0x64(%rbp)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x64(%rbp), %eax
cmpq -0x48(%rbp), %rax
ja 0x2d8db5
jmp 0x2d8e57
movl -0x68(%rbp), %eax
cmpq -0x50(%rbp), %rax
jb 0x2d8dc3
jmp 0x2d8e68
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x6c(%rbp), %rdi
callq 0x2d81b0
cmpq $0x0, %rax
je 0x2d8de0
jmp 0x2d8dff
leaq 0xbab41(%rip), %rdi     # 0x393928
leaq 0xb7a41(%rip), %rsi     # 0x39082f
movl $0x3c17, %edx           # imm = 0x3C17
leaq 0xbab55(%rip), %rcx     # 0x39394f
callq 0x2130
movq -0x20(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rdx
movq 0x8(%rdx), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movq -0x58(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq $0x1, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, 0x4(%rax)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2d8d31
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x40(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2d8f4b
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0xa8(%rbp), %rcx
shrq %rcx
cmpq %rcx, %rax
jae 0x2d8f49
movq -0x78(%rbp), %rsi
imulq $0x28, -0x58(%rbp), %rax
addq %rax, %rsi
leaq -0xa0(%rbp), %rdi
movl $0x28, %edx
callq 0x2180
movq -0x78(%rbp), %rsi
movq -0xa8(%rbp), %rax
subq -0x58(%rbp), %rax
subq $0x1, %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq -0x78(%rbp), %rdi
imulq $0x28, -0x58(%rbp), %rax
addq %rax, %rdi
movl $0x28, %edx
callq 0x2180
movq -0x78(%rbp), %rdi
movq -0xa8(%rbp), %rax
subq -0x58(%rbp), %rax
subq $0x1, %rax
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq -0xa0(%rbp), %rsi
movl $0x28, %edx
callq 0x2180
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2d8ea9
jmp 0x2d8f4b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x40(%rbp), %r9
movq -0x20(%rbp), %r11
movq 0x18(%rbp), %r10
movq 0x20(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x2d8380
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)