// File: chhylp123[P]hifiasm/inter.cpp
// Function: rov2uov(unsigned long, ul_idx_t const*, utg_rid_dt*, uc_block_t*, ul_ov_t*, unsigned int, long)
uint64_t rov2uov(uint64_t rid, const ul_idx_t *uref, utg_rid_dt *ru_map, uc_block_t *rovlp, ul_ov_t *res, uint32_t adjust_rev, int64_t ulid)
{
	uint64_t ori = ru_map->u&1, ts, te;
	if(!ori) {
		ts = rovlp->ts; te = rovlp->te;
	} else {
		ts = uref->r_ug->rg->seq[rid].len - rovlp->te;
		te = uref->r_ug->rg->seq[rid].len - rovlp->ts;
	}
	// if(ulid == 14714) {
	// 	fprintf(stderr, "[M::%s::]\tori::%lu\trovlp->rev::%u\tro_t::[%u, %u)\tt::[%lu, %lu)\toff::%u\n", __func__, 
	// 		ori, rovlp->rev, rovlp->ts, rovlp->te, ts, te, ru_map->off);
	// }
	ts += ru_map->off; te += ru_map->off;
	if(ts >= 0 && te <= uref->ug->g->seq[ru_map->u>>1].len) {
		memset(res, 0, sizeof(*res));
		res->qn = 0; res->qs = rovlp->qs; res->qe = rovlp->qe; 
		res->tn = ru_map->u>>1; res->ts = ts; res->te = te; 
		res->el = rovlp->el; res->rev = (rovlp->rev == ori?0:1);
		if(adjust_rev && res->rev) {///for linear chaining
			res->ts = uref->ug->g->seq[res->tn].len - te;
			res->te = uref->ug->g->seq[res->tn].len - ts;
		}
		return 1;
	} 
	return 0;
	// if(ulid == 14714) {
	// 	fprintf(stderr, "[M::%s::]\tulen::%u\trlen::%u\tro_t::[%u, %u)\tt::[%lu, %lu)\toff::%u\n", __func__, 
	// 		uref->ug->g->seq[res->tn].len, uref->r_ug->rg->seq[rid].len, rovlp->ts, rovlp->te, ts, te, ru_map->off);
	// }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
andl $0x1, %eax
movl %eax, %eax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x2cfab2
movq -0x28(%rbp), %rax
movl 0xc(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x50(%rbp)
jmp 0x2cfb04
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x28(%rbp), %rcx
subl 0x10(%rcx), %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x28(%rbp), %rcx
subl 0xc(%rcx), %eax
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x48(%rbp)
jb 0x2cfc95
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movq -0x20(%rbp), %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
cmpq %rcx, %rax
ja 0x2cfc95
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x1c, %edx
callq 0x2100
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl (%rax), %ecx
shrl %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x28(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x7, %al
movzbl %al, %edx
movq -0x30(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x1, %edx
shll $0x1e, %edx
andl $0xbfffffff, %ecx       # imm = 0xBFFFFFFF
orl %edx, %ecx
movl %ecx, 0x18(%rax)
movq -0x28(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x40(%rbp), %rsi
movl $0x1, %edx
xorl %eax, %eax
cmpq %rsi, %rcx
cmovel %eax, %edx
movq -0x30(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x1, %edx
shll $0x1f, %edx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl %edx, %ecx
movl %ecx, 0x18(%rax)
cmpl $0x0, -0x34(%rbp)
je 0x2cfc8b
movq -0x30(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2cfc8b
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x14(%rax)
movq $0x1, -0x8(%rbp)
jmp 0x2cfc9d
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)