// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: infer_rovlp(ul_ov_t*, ul_ov_t*, uc_block_t*, uc_block_t*, All_reads*, ma_ug_t*)
int64_t infer_rovlp(ul_ov_t *li, ul_ov_t *lj, uc_block_t *bi, uc_block_t *bj, All_reads *ridx, ma_ug_t *ug)
{
	int64_t in, is, ie, irev, iqs, iqe, jn, js, je, jrev, jqs, jqe, ir, jr, ts, te, max_s, min_e, s_shift, e_shift;
	
	if(li) {
		in = ug?ug->u.a[li->tn].len:Get_READ_LENGTH(R_INF, li->tn); 
		is = li->ts; ie = li->te; irev = li->rev; iqs = li->qs; iqe = li->qe;
	} else if(bi) {
		in = ug?ug->u.a[bi->hid].len:Get_READ_LENGTH(R_INF, bi->hid); 
		is = bi->ts; ie = bi->te; irev = bi->rev; iqs = bi->qs; iqe = bi->qe;
	} else {
		return 0;
	}

	if(lj) {
		jn = ug?ug->u.a[lj->tn].len:Get_READ_LENGTH(R_INF, lj->tn); 
		js = lj->ts; je = lj->te; jrev = lj->rev; jqs = lj->qs; jqe = lj->qe;
	} else if(bj) {
		jn = ug?ug->u.a[bj->hid].len:Get_READ_LENGTH(R_INF, bj->hid); 
		js = bj->ts; je = bj->te; jrev = bj->rev; jqs = bj->qs; jqe = bj->qe;
	} else {
		return 0;
	}

	max_s = MAX(iqs, jqs); min_e = MIN(iqe, jqe);
	if(min_e <= max_s) return 0;
	s_shift = get_offset_adjust(max_s - iqs, iqe-iqs, ie-is);
	e_shift = get_offset_adjust(iqe - min_e, iqe-iqs, ie-is);
	if(irev) {
		ts = s_shift; s_shift = e_shift; e_shift = ts;
	}
	is += s_shift; ie-= e_shift;

	// if(li && lj && li->tn == 324 && lj->tn == 319 && li->qs == 63841) {
	// 	fprintf(stderr, "+++in:%ld, is:%ld, ie:%ld, irev:%ld, jn:%ld, js:%ld, je:%ld, jrev:%ld\n", in, is, ie, irev, jn, js, je, jrev);
	// }

	s_shift = get_offset_adjust(max_s - jqs, jqe-jqs, je-js);
    e_shift = get_offset_adjust(jqe - min_e, jqe-jqs, je-js);
    if(jrev) {
        ts = s_shift; s_shift = e_shift; e_shift = ts;
    }
    js += s_shift; je-= e_shift;

	if(irev) {
		ts = in - ie; te = in - is;
		is = ts; ie = te;
	}

	if(jrev) {
		ts = jn - je; te = jn - js;
		js = ts; je = te;
	}
	
	// if(li && lj && li->tn == 324 && lj->tn == 319 && li->qs == 63841) {
	// 	fprintf(stderr, "---in:%ld, is:%ld, ie:%ld, irev:%ld, jn:%ld, js:%ld, je:%ld, jrev:%ld\n", in, is, ie, irev, jn, js, je, jrev);
	// }

	if(is <= js) {
		js -= is; is = 0;
	} else {
		is -= js; js = 0;
	}

	ir = in - ie; jr = jn - je;

	if(ir <= jr){
        ie = in; je += ir;        
    }
    else {
		je = jn; ie += jr;
    }

	ir = ie - is; jr = je - js;
	return MAX(ir, jr);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
testq %rdi, %rdi
je 0x3d28d
testq %r9, %r9
je 0x3d2b2
movq 0x10(%r9), %rax
movl 0xc(%rdi), %edx
imulq $0x28, %rdx, %rdx
movl (%rax,%rdx), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
jmp 0x3d2c4
testq %rdx, %rdx
je 0x3d4fb
testq %r9, %r9
je 0x3d2d6
movq 0x10(%r9), %rax
movl (%rdx), %edi
imulq $0x28, %rdi, %rdi
movl (%rax,%rdi), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
jmp 0x3d2e7
leaq 0x216f77(%rip), %rax    # 0x254230
movq 0x18(%rax), %rax
movl 0xc(%rdi), %edx
movq (%rax,%rdx,8), %r8
leaq 0x10(%rdi), %rax
leaq 0x14(%rdi), %r14
movl 0x18(%rdi), %r10d
shrl $0x1f, %r10d
jmp 0x3d2ff
leaq 0x216f53(%rip), %rax    # 0x254230
movq 0x18(%rax), %rax
movl (%rdx), %edi
movq (%rax,%rdi,8), %r8
leaq 0xc(%rdx), %rax
leaq 0x10(%rdx), %r14
movzbl 0x20(%rdx), %r10d
shrl $0x5, %r10d
andl $0x1, %r10d
movq %rdx, %rdi
movl 0x4(%rdi), %ebx
movl 0x8(%rdi), %r11d
movl (%r14), %edx
movl (%rax), %r12d
testq %rsi, %rsi
je 0x3d32e
testq %r9, %r9
je 0x3d353
movq 0x10(%r9), %rax
movl 0xc(%rsi), %ecx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
jmp 0x3d365
testq %rcx, %rcx
je 0x3d4fb
testq %r9, %r9
je 0x3d377
movq 0x10(%r9), %rax
movl (%rcx), %esi
imulq $0x28, %rsi, %rsi
movl (%rax,%rsi), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
jmp 0x3d388
leaq 0x216ed6(%rip), %rax    # 0x254230
movq 0x18(%rax), %rax
movl 0xc(%rsi), %ecx
movq (%rax,%rcx,8), %r9
leaq 0x10(%rsi), %rax
leaq 0x14(%rsi), %r15
movl 0x18(%rsi), %r14d
shrl $0x1f, %r14d
jmp 0x3d3a0
leaq 0x216eb2(%rip), %rax    # 0x254230
movq 0x18(%rax), %rax
movl (%rcx), %esi
movq (%rax,%rsi,8), %r9
leaq 0xc(%rcx), %rax
leaq 0x10(%rcx), %r15
movzbl 0x20(%rcx), %r14d
shrl $0x5, %r14d
andl $0x1, %r14d
movq %rcx, %rsi
movl 0x4(%rsi), %ebp
movl 0x8(%rsi), %r13d
cmpl %ebp, %ebx
movl %ebp, %edi
cmoval %ebx, %edi
cmpl %r13d, %r11d
movl %r13d, %ecx
cmovbl %r11d, %ecx
cmpl %edi, %ecx
jbe 0x3d4fb
movl %edi, %esi
subl %ebp, %esi
movq %r8, -0x8(%rsp)
movl %r13d, %r8d
subl %ebp, %r8d
movl (%rax), %eax
movq %r12, %rbp
movl (%r15), %r12d
cvtsi2sd %rsi, %xmm0
movl %r12d, %esi
subl %eax, %esi
cvtsi2sd %r8, %xmm1
cvtsi2sd %rsi, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rsi
subl %ecx, %r13d
cvtsi2sd %r13, %xmm0
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r8
movl %esi, %esi
movl %r8d, %r8d
testq %r14, %r14
movq %r8, %r13
cmoveq %rsi, %r13
cmoveq %r8, %rsi
addq %rax, %r13
subq %rsi, %r12
movq %r9, %rax
subq %r13, %rax
testq %r14, %r14
cmoveq %r12, %rax
movq %r9, %r15
subq %r12, %r15
testq %r14, %r14
cmoveq %r13, %r15
subl %ebx, %edi
movl %r11d, %esi
subl %ebx, %esi
cvtsi2sd %rdi, %xmm0
cvtsi2sd %rsi, %xmm1
movl %edx, %esi
subl %ebp, %esi
cvtsi2sd %rsi, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rsi
subl %ecx, %r11d
cvtsi2sd %r11, %xmm0
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rcx
movl %esi, %esi
movl %ecx, %ecx
testq %r10, %r10
movq %rcx, %rdi
cmoveq %rsi, %rdi
cmoveq %rcx, %rsi
addq %rbp, %rdi
subq %rsi, %rdx
movq -0x8(%rsp), %r8
movq %r8, %rcx
subq %rdi, %rcx
testq %r10, %r10
cmoveq %rdx, %rcx
movq %r8, %rsi
movq %r8, %r11
subq %rdx, %rsi
testq %r10, %r10
cmoveq %rdi, %rsi
xorl %edx, %edx
movq %rsi, %rdi
subq %r15, %rdi
movq %rdi, %r8
cmovgq %rdx, %r8
negq %rdi
cmpq %r15, %rsi
cmovleq %rdx, %rdi
movq %r11, %rdx
subq %rcx, %rdx
movq %r9, %rsi
subq %rax, %rsi
addq %rdx, %rax
addq %rsi, %rcx
cmpq %rsi, %rdx
cmovleq %r11, %rcx
cmovgq %r9, %rax
addq %rdi, %rcx
addq %r8, %rax
cmpq %rax, %rcx
cmovgq %rcx, %rax
jmp 0x3d4fd
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq