// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: cal_exact_len(bit_extz_t*, long, ul_ov_t*, unsigned long, long, long*, long*, long*, long*, long)
int64_t cal_exact_len(bit_extz_t *ez, int64_t rev, ul_ov_t *sa, uint64_t sn, int64_t ylen, int64_t *xoff, int64_t *yoff, int64_t *exac, int64_t *err, int64_t backward)
{
	(*xoff) = (*yoff) = (*exac) = (*err) = 0;
	if(!ez->cigar.n) return 0;
	int64_t cn = ez->cigar.n, op, ck, xk, yk, xs, xe, ys, ye, se;
	if(!backward) {
		xk = ez->ts; yk = ez->ps; 
		for (ck = 0; ck < cn; ck++) {
			xs = xk; ys = yk;
			op = ez->cigar.a[ck]>>14; 
			se = (ez->cigar.a[ck]&(0x3fff));
			if(op!=2) xk += se;
			if(op!=3) yk += se;
			xe = xk; ye = yk; 
			if(!op) {///exact match
				(*exac) += se; continue;
			}
			///if there is an unmasked error
			if((!sn) || (!is_mask_err_full(sa, sn, xs, xe, ((rev)?(ylen-ye):(ys)), ((rev)?(ylen-ys):(ye))))) {
				(*xoff) = xs - ez->ts; (*yoff) = ys - ez->ps;
				return 0;
			}
			(*exac) = 0; (*err) += se;///if the error has been masked
		}
	} else {
		xk = ez->te+1; yk = ez->pe+1;
		for (ck = cn-1; ck >= 0; ck--) {
			xe = xk; ye = yk;
			op = ez->cigar.a[ck]>>14;
			se = (ez->cigar.a[ck]&(0x3fff));
			if(op!=2) xk -= se;
			if(op!=3) yk -= se;
			xs = xk; ys = yk;
			if(!op) {///exact match
				(*exac) += se; continue;
			}
			///if there is an unmasked error
			if((!sn) || (!is_mask_err_full(sa, sn, xs, xe, ((rev)?(ylen-ye):(ys)), ((rev)?(ylen-ys):(ye))))) {
				(*xoff) = ez->te+1-xe; (*yoff) = ez->pe+1-ye;
				return 0;
			}
			(*exac) = 0; (*err) += se;///if the error has been masked
		}
		
	}
	(*xoff) = ez->te+1-ez->ts; (*yoff) = ez->pe+1-ez->ps;
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x30(%rsp)
movq 0x80(%rsp), %rax
andq $0x0, (%rax)
movq 0x78(%rsp), %r8
andq $0x0, (%r8)
movq 0x70(%rsp), %rax
andq $0x0, (%rax)
movq %rdx, 0x28(%rsp)
andq $0x0, (%r9)
movq %rsi, 0x20(%rsp)
movq 0xa8(%rdi), %rbp
testq %rbp, %rbp
je 0x17efab
movq %rcx, %r10
movq %r9, 0x8(%rsp)
cmpq $0x0, 0x88(%rsp)
movq %rdi, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
je 0x17efb2
movslq 0x20(%rdi), %rbx
incq %rbx
movslq 0x14(%rdi), %r15
incq %r15
xorl %esi, %esi
testq %rbp, %rbp
jle 0x17f083
movq 0xb8(%rdi), %rax
movzwl -0x2(%rax,%rbp,2), %ecx
movl %ecx, %eax
shrl $0xe, %eax
movl %ecx, %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
cmpl $0x2, %eax
movq %r14, %rdx
cmoveq %rsi, %rdx
movq %rbx, %r13
subq %rdx, %r13
cmpl $0x3, %eax
movq %r14, %rax
cmoveq %rsi, %rax
movq %r15, %r12
subq %rax, %r12
movq %r8, %rax
cmpw $0x4000, %cx            # imm = 0x4000
jb 0x17ef9a
testq %r10, %r10
je 0x17f0a8
movq 0x30(%rsp), %r9
movq %r9, %r8
subq %r15, %r8
subq %r12, %r9
cmpq $0x0, 0x20(%rsp)
cmoveq %r12, %r8
cmoveq %r15, %r9
movq 0x28(%rsp), %rdi
movq %r10, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1a3311
xorl %esi, %esi
movq 0x78(%rsp), %r8
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %rdi
testq %rax, %rax
je 0x17f0a8
andq $0x0, (%r8)
movq 0x80(%rsp), %rax
addq %r14, (%rax)
decq %rbp
movq %r13, %rbx
movq %r12, %r15
jmp 0x17eeee
xorl %eax, %eax
jmp 0x17f0ef
movslq 0x1c(%rdi), %r15
movslq 0x10(%rdi), %rax
movq %rax, (%rsp)
xorl %esi, %esi
testq %rbp, %rbp
cmovleq %rsi, %rbp
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x17f083
movq 0xb8(%rdi), %rax
movzwl (%rax,%r12,2), %ecx
movl %ecx, %eax
shrl $0xe, %eax
movl %ecx, %r13d
andl $0x3fff, %r13d          # imm = 0x3FFF
cmpl $0x2, %eax
movq %r13, %rbx
cmoveq %rsi, %rbx
addq %r15, %rbx
cmpl $0x3, %eax
movq %r13, %r14
cmoveq %rsi, %r14
addq (%rsp), %r14
movq %r8, %rax
cmpw $0x4000, %cx            # imm = 0x4000
jb 0x17f071
testq %r10, %r10
je 0x17f0ca
movq 0x30(%rsp), %r9
movq %r9, %r8
subq %r14, %r8
movq (%rsp), %rax
subq %rax, %r9
cmpq $0x0, 0x20(%rsp)
cmoveq %rax, %r8
cmoveq %r14, %r9
movq 0x28(%rsp), %rdi
movq %r10, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x1a3311
xorl %esi, %esi
movq 0x78(%rsp), %r8
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %rdi
testq %rax, %rax
je 0x17f0ca
andq $0x0, (%r8)
movq 0x80(%rsp), %rax
addq %r13, (%rax)
incq %r12
movq %rbx, %r15
movq %r14, (%rsp)
jmp 0x17efca
movl 0x20(%rdi), %eax
subl 0x1c(%rdi), %eax
incl %eax
cltq
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movl 0x14(%rdi), %eax
subl 0x10(%rdi), %eax
incl %eax
cltq
movq %rax, (%rsp)
pushq $0x1
popq %rax
jmp 0x17f0e3
movslq 0x20(%rdi), %rax
subq %rbx, %rax
incq %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movslq 0x14(%rdi), %rax
subq %r15, %rax
incq %rax
movq %rax, (%rsp)
jmp 0x17f0e1
movslq 0x1c(%rdi), %rax
subq %rax, %r15
movq 0x8(%rsp), %rax
movq %r15, (%rax)
movslq 0x10(%rdi), %rax
subq %rax, (%rsp)
xorl %eax, %eax
movq 0x70(%rsp), %rcx
movq (%rsp), %rdx
movq %rdx, (%rcx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq