// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq 0x20(%rbp), %rax
movq $0x0, (%rax)
movq 0x18(%rbp), %rax
movq $0x0, (%rax)
movq 0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x38(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa8(%rax)
jne 0x2c908a
movq $0x0, -0x8(%rbp)
jmp 0x2c94ed
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, 0x28(%rbp)
jne 0x2c92a3
movq -0x10(%rbp), %rax
movslq 0x1c(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x2c929e
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
movq %rax, -0x88(%rbp)
cmpq $0x2, -0x48(%rbp)
je 0x2c9135
movq -0x88(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x3, -0x48(%rbp)
je 0x2c914b
movq -0x88(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x2c9178
movq -0x88(%rbp), %rcx
movq 0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x2c928d
cmpq $0x0, -0x28(%rbp)
je 0x2c9238
movq -0x20(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2c91c7
movq -0x30(%rbp), %rax
subq -0x80(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x2c91d2
movq -0x78(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2c91f8
movq -0x30(%rbp), %rax
subq -0x78(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x2c9203
movq -0x80(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %r8
movq -0x90(%rbp), %rcx
movq -0x98(%rbp), %rdx
movq -0xa0(%rbp), %rsi
movq -0xa8(%rbp), %rdi
movq -0xc0(%rbp), %r9
callq 0x30d250
cmpq $0x0, %rax
jne 0x2c9271
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movslq 0x1c(%rax), %rax
subq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x78(%rbp), %rcx
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rax
subq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x2c94ed
movq 0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rcx
movq 0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2c90c4
jmp 0x2c94af
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jl 0x2c94ad
movq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
movq %rax, -0x88(%rbp)
cmpq $0x2, -0x48(%rbp)
je 0x2c9340
movq -0x88(%rbp), %rcx
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x58(%rbp)
cmpq $0x3, -0x48(%rbp)
je 0x2c9359
movq -0x88(%rbp), %rcx
movq -0x60(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x2c9386
movq -0x88(%rbp), %rcx
movq 0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x2c949c
cmpq $0x0, -0x28(%rbp)
je 0x2c9446
movq -0x20(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2c93d5
movq -0x30(%rbp), %rax
subq -0x80(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x2c93e0
movq -0x78(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2c9406
movq -0x30(%rbp), %rax
subq -0x78(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0x2c9411
movq -0x80(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf0(%rbp), %r8
movq -0xc8(%rbp), %rcx
movq -0xd0(%rbp), %rdx
movq -0xd8(%rbp), %rsi
movq -0xe0(%rbp), %rdi
movq -0xf8(%rbp), %r9
callq 0x30d250
cmpq $0x0, %rax
jne 0x2c9480
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
addl $0x1, %eax
movslq %eax, %rcx
subq -0x70(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
addl $0x1, %eax
movslq %eax, %rcx
subq -0x80(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x2c94ed
movq 0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x88(%rbp), %rcx
movq 0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2c92cf
jmp 0x2c94af
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
addl $0x1, %eax
movq -0x10(%rbp), %rcx
subl 0x1c(%rcx), %eax
movslq %eax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
addl $0x1, %eax
movq -0x10(%rbp), %rcx
subl 0x10(%rcx), %eax
movslq %eax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq $0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw (%rax,%rax)