// File: /workspace/github_cpp_makefile_star2_O1/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
movq %r8, -0x10(%rsp)
movq %rdx, -0x40(%rsp)
movq %rsi, -0x18(%rsp)
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %rdx
xorl %eax, %eax
movq %rax, (%rsi)
movq %rax, (%r8)
movq %rax, (%rdx)
movq %rax, (%r9)
movq %rdi, -0x28(%rsp)
movq 0xa8(%rdi), %r14
testq %r14, %r14
je 0x1c1076
movq %r9, -0x30(%rsp)
cmpq $0x0, 0x50(%rsp)
je 0x1c0f00
testq %r14, %r14
jle 0x1c101c
movq -0x28(%rsp), %rax
movslq 0x20(%rax), %rdx
incq %rdx
movslq 0x14(%rax), %r15
incq %r15
movq 0xb8(%rax), %r12
movq %rcx, %r13
negq %r13
addq $0x14, -0x40(%rsp)
xorl %ebp, %ebp
movq %r15, %r11
movq %rdx, -0x38(%rsp)
movq %rdx, %rax
movq %rdx, %r10
movq %r15, %r9
movq %r12, -0x20(%rsp)
movzwl -0x2(%r12,%r14,2), %esi
movl %esi, %edx
shrl $0xe, %edx
movl %esi, %ebx
andl $0x3fff, %ebx           # imm = 0x3FFF
cmpl $0x2, %edx
movq %rbx, %rdi
cmoveq %rbp, %rdi
subq %rdi, %r10
cmpl $0x3, %edx
movq %rbx, %rdx
cmoveq %rbp, %rdx
subq %rdx, %r9
movq %r8, %rdx
cmpl $0x4000, %esi           # imm = 0x4000
jb 0x1c0ee1
testq %rcx, %rcx
je 0x1c1045
movq -0x10(%rsp), %rdi
movq %rdi, %r8
subq %r11, %r8
subq %r9, %rdi
cmpq $0x0, -0x18(%rsp)
cmoveq %r9, %r8
cmoveq %r11, %rdi
movb $0x1, %dl
movl $0x1, %esi
movq -0x40(%rsp), %r12
movl -0x10(%r12), %ebp
cmpq %rbp, %r10
jb 0x1c0ea3
movl -0xc(%r12), %ebp
cmpq %rbp, %rax
ja 0x1c0ea3
movl -0x4(%r12), %ebp
cmpq %rbp, %r8
jb 0x1c0ea3
movl (%r12), %ebp
cmpq %rbp, %rdi
jbe 0x1c0ebd
cmpq %rcx, %rsi
setb %dl
leaq (%rsi,%r13), %rbp
incq %rbp
incq %rsi
addq $0x1c, %r12
cmpq $0x1, %rbp
jne 0x1c0e7c
testb $0x1, %dl
movq 0x40(%rsp), %r8
movq -0x20(%rsp), %r12
movl $0x0, %ebp
je 0x1c1045
movq $0x0, (%r8)
movq 0x48(%rsp), %rdx
leaq -0x1(%r14), %rsi
addq %rbx, (%rdx)
movq %r9, %r11
movq %r10, %rax
cmpq $0x1, %r14
movq %rsi, %r14
jg 0x1c0e0f
jmp 0x1c101c
testq %r14, %r14
jle 0x1c101c
movq -0x28(%rsp), %rax
movslq 0x1c(%rax), %r9
movslq 0x10(%rax), %r15
movq 0xb8(%rax), %rax
movq %rcx, %rbp
negq %rbp
addq $0x14, -0x40(%rsp)
xorl %edi, %edi
movq %r15, -0x38(%rsp)
movq %r9, -0x8(%rsp)
xorl %r10d, %r10d
movq %rax, -0x20(%rsp)
movzwl (%rax,%r10,2), %esi
movl %esi, %edx
shrl $0xe, %edx
movl %esi, %r12d
andl $0x3fff, %r12d          # imm = 0x3FFF
cmpl $0x2, %edx
movq %r12, %r11
cmoveq %rdi, %r11
addq %r9, %r11
cmpl $0x3, %edx
movq %r12, %rbx
cmoveq %rdi, %rbx
addq %r15, %rbx
movq %r8, %rdx
cmpl $0x4000, %esi           # imm = 0x4000
jb 0x1c1007
testq %rcx, %rcx
je 0x1c105a
movq -0x10(%rsp), %r13
movq %r13, %r8
subq %rbx, %r8
subq %r15, %r13
cmpq $0x0, -0x18(%rsp)
cmoveq %r15, %r8
cmoveq %rbx, %r13
movb $0x1, %dil
movl $0x1, %edx
movq -0x40(%rsp), %rsi
movl -0x10(%rsi), %eax
cmpq %rax, %r9
jb 0x1c0fcb
movl -0xc(%rsi), %eax
cmpq %rax, %r11
ja 0x1c0fcb
movl -0x4(%rsi), %eax
cmpq %rax, %r8
jb 0x1c0fcb
movl (%rsi), %eax
cmpq %rax, %r13
jbe 0x1c0fe6
cmpq %rcx, %rdx
setb %dil
leaq (%rdx,%rbp), %rax
incq %rax
incq %rdx
addq $0x1c, %rsi
cmpq $0x1, %rax
jne 0x1c0fac
testb $0x1, %dil
movq 0x40(%rsp), %r8
movq -0x20(%rsp), %rax
movl $0x0, %edi
je 0x1c105a
movq $0x0, (%r8)
movq 0x48(%rsp), %rdx
addq %r12, (%rdx)
incq %r10
movq %rbx, %r15
movq %r11, %r9
cmpq %r14, %r10
jne 0x1c0f3d
movq -0x28(%rsp), %rdx
movl 0x20(%rdx), %eax
subl 0x1c(%rdx), %eax
incl %eax
cltq
movq -0x30(%rsp), %rcx
movq %rax, (%rcx)
movl 0x14(%rdx), %eax
subl 0x10(%rdx), %eax
incl %eax
movslq %eax, %r15
movl $0x1, %eax
jmp 0x1c106e
movq -0x38(%rsp), %rcx
subq %rax, %rcx
movq -0x30(%rsp), %rax
movq %rcx, (%rax)
subq %r11, %r15
jmp 0x1c106c
subq -0x8(%rsp), %r9
movq -0x30(%rsp), %rax
movq %r9, (%rax)
subq -0x38(%rsp), %r15
xorl %eax, %eax
movq 0x38(%rsp), %rcx
movq %r15, (%rcx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq