// File: 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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x71b56
cmpq $0x0, -0x38(%rbp)
je 0x71aed
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0xe0(%rbp)
jmp 0x71b0a
leaq 0x3537dc(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x68(%rbp)
jmp 0x71c06
cmpq $0x0, -0x20(%rbp)
je 0x71bf7
cmpq $0x0, -0x38(%rbp)
je 0x71b8f
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0xe8(%rbp)
jmp 0x71bab
leaq 0x35373a(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x68(%rbp)
jmp 0x71c04
movq $0x0, -0x8(%rbp)
jmp 0x720d1
jmp 0x71c06
cmpq $0x0, -0x18(%rbp)
je 0x71cb2
cmpq $0x0, -0x38(%rbp)
je 0x71c40
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0xf0(%rbp)
jmp 0x71c5d
leaq 0x353689(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0xc(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
movl %eax, %eax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x98(%rbp)
jmp 0x71d6b
cmpq $0x0, -0x28(%rbp)
je 0x71d5c
cmpq $0x0, -0x38(%rbp)
je 0x71ceb
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0xf8(%rbp)
jmp 0x71d07
leaq 0x3535de(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movl 0xc(%rax), %eax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x90(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x98(%rbp)
jmp 0x71d69
movq $0x0, -0x8(%rbp)
jmp 0x720d1
jmp 0x71d6b
movq -0x60(%rbp), %rax
cmpq -0x90(%rbp), %rax
jl 0x71d85
movq -0x60(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0x71d93
movq -0x90(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x98(%rbp), %rax
jg 0x71dbb
movq -0x68(%rbp), %rax
movq %rax, -0x108(%rbp)
jmp 0x71dc9
movq -0x98(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jg 0x71df4
movq $0x0, -0x8(%rbp)
jmp 0x720d1
movq -0xc0(%rbp), %rax
subq -0x60(%rbp), %rax
movl %eax, %edi
movq -0x68(%rbp), %rax
subq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %eax
movq %rax, -0xd0(%rbp)
movq -0x68(%rbp), %rax
subq -0xc8(%rbp), %rax
movl %eax, %edi
movq -0x68(%rbp), %rax
subq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %eax
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x71e83
movq -0xd0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xd8(%rbp), %rcx
movq -0x50(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0xc0(%rbp), %rax
subq -0x90(%rbp), %rax
movl %eax, %edi
movq -0x98(%rbp), %rax
subq -0x90(%rbp), %rax
movl %eax, %esi
movq -0x80(%rbp), %rax
subq -0x78(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %eax
movq %rax, -0xd0(%rbp)
movq -0x98(%rbp), %rax
subq -0xc8(%rbp), %rax
movl %eax, %edi
movq -0x98(%rbp), %rax
subq -0x90(%rbp), %rax
movl %eax, %esi
movq -0x80(%rbp), %rax
subq -0x78(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %eax
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x71f48
movq -0xd0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0xd8(%rbp), %rcx
movq -0x80(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x71fa4
movq -0x40(%rbp), %rax
subq -0x50(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x71fe2
movq -0x70(%rbp), %rax
subq -0x80(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x70(%rbp), %rax
subq -0x78(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x78(%rbp), %rax
jg 0x72005
movq -0x48(%rbp), %rcx
movq -0x78(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x48(%rbp)
jmp 0x7201c
movq -0x78(%rbp), %rcx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x40(%rbp), %rax
subq -0x50(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x70(%rbp), %rax
subq -0x80(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jg 0x72063
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0xa0(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x7207a
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xa8(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x80(%rbp), %rax
subq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jl 0x720b8
movq -0xa0(%rbp), %rax
movq %rax, -0x110(%rbp)
jmp 0x720c6
movq -0xa8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop