// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: infer_utrans_ovlp_len(u_trans_t*, u_trans_t*, ma_ug_t*)
int64_t infer_utrans_ovlp_len(u_trans_t *li, u_trans_t *lj, 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;
	
    in = ug->u.a[li->tn].len; 
    is = li->ts; ie = li->te; irev = li->rev; iqs = li->qs; iqe = li->qe;
	jn = ug->u.a[lj->tn].len; 
    js = lj->ts; je = lj->te; jrev = lj->rev; jqs = lj->qs; jqe = lj->qe;

	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;

	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(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
movl (%rdi), %r10d
movl 0x4(%rdi), %r8d
movl (%rsi), %ebp
movl 0x4(%rsi), %r15d
cmpl %ebp, %r10d
movl %ebp, %r11d
cmoval %r10d, %r11d
cmpl %r15d, %r8d
movl %r15d, %r9d
cmovbl %r8d, %r9d
cmpl %r11d, %r9d
jbe 0xc30eb
movl 0xc(%rsi), %eax
movl 0x10(%rsi), %ebx
movq 0x10(%rdx), %rdx
movl 0x14(%rsi), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rdx,%rcx,8), %ecx
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
andq %r14, %rcx
movl %r11d, %r12d
subl %ebp, %r12d
movl %r15d, %r13d
subl %ebp, %r13d
movl %ebx, %ebp
subl %eax, %ebp
cvtsi2sd %r12, %xmm0
cvtsi2sd %r13, %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %rbp, %xmm2
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r12
movl %r12d, %r12d
subl %r9d, %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r15
movl %r15d, %r15d
movb 0x28(%rsi), %bpl
testb $0x40, %bpl
movq %r15, %r13
cmoveq %r12, %r13
cmoveq %r15, %r12
addq %rax, %r13
subq %r12, %rbx
movq %rcx, %rsi
subq %rbx, %rsi
testb $0x40, %bpl
cmoveq %r13, %rsi
movq %rcx, %rax
subq %r13, %rax
testb $0x40, %bpl
movl 0xc(%rdi), %r12d
movl 0x10(%rdi), %r15d
movl 0x14(%rdi), %r13d
leaq (%r13,%r13,4), %r13
movl (%rdx,%r13,8), %edx
cmoveq %rbx, %rax
andq %r14, %rdx
subl %r10d, %r11d
movl %r8d, %ebx
subl %r10d, %ebx
movl %r15d, %r10d
subl %r12d, %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r11, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r10, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r10
movl %r10d, %r10d
subl %r9d, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r8
movl %r8d, %r8d
movb 0x28(%rdi), %r9b
testb $0x40, %r9b
movq %r8, %r11
cmoveq %r10, %r11
cmoveq %r8, %r10
addq %r12, %r11
subq %r10, %r15
movq %rdx, %rdi
subq %r15, %rdi
testb $0x40, %r9b
cmoveq %r11, %rdi
movq %rdx, %r8
subq %r11, %r8
testb $0x40, %r9b
cmoveq %r15, %r8
movq %rdi, %r9
subq %rsi, %r9
negq %r9
xorl %r10d, %r10d
subq %rsi, %rdi
cmovgq %r10, %rdi
cmovleq %r10, %r9
movq %rdx, %rsi
subq %r8, %rsi
movq %rcx, %r10
subq %rax, %r10
addq %rsi, %rax
addq %r10, %r8
cmpq %r10, %rsi
cmovgq %rcx, %rax
cmovleq %rdx, %r8
addq %r9, %r8
addq %rdi, %rax
cmpq %rax, %r8
cmovgq %r8, %rax
jmp 0xc30ed
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq