// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: retrieve_npc_cigar_err(bit_extz_t*, long, long, long*, long*, long*, hpc_ss_t*, long, hpc_ss_t*, long, long, long)
int64_t retrieve_npc_cigar_err(bit_extz_t *ez, int64_t s, int64_t e, int64_t *xk, int64_t *yk, int64_t *ck, 
hpc_ss_t *fx, int64_t nx, hpc_ss_t *fy, int64_t ny, int64_t ylen, int64_t is_rev)///[s, e) for x
{
	if(!ez->cigar.n) return 0;
	int64_t cn = ez->cigar.n, op, err = 0; int64_t ws, we, ws1, we1, os, oe, ovlp, xi, yi;
	if(((*ck) < 0) || ((*ck) > cn)) {//(*ck) == cn is allowed
		(*ck) = 0; (*xk) = ez->ts; (*yk) = ez->ps;
	}
	while ((*ck) > 0 && (*xk) > s) {
		--(*ck);
		op = ez->cigar.a[(*ck)]>>14;
		if(op!=2) (*xk) -= (ez->cigar.a[(*ck)]&(0x3fff));
		if(op!=3) (*yk) -= (ez->cigar.a[(*ck)]&(0x3fff));
	}
	xi = yi = 0; if(nx<=0) fx = NULL; if(ny<=0) fy = NULL;

	//some cigar will span s or e
	while ((*ck) < cn && (*xk) < e) {//[s, e)
		ws = (*xk); ws1 = (*yk);
		op = ez->cigar.a[(*ck)]>>14;
		if(op!=2) (*xk) += (ez->cigar.a[(*ck)]&(0x3fff));
		if(op!=3) (*yk) += (ez->cigar.a[(*ck)]&(0x3fff));
		we = (*xk); we1 = (*yk);
		os = MAX(s, ws); oe = MIN(e, we);
		ovlp = ((oe>os)? (oe-os):0);
		if((op==2) && (ws>=s) && (ws<e)) {
			ovlp = (ez->cigar.a[(*ck)]&(0x3fff));
		}
		(*ck)++;
		if((!ovlp) || (!op)) continue;
		if(fx && hpc_check(fx, nx, ws, we, &xi, 1)) continue; 
		if(fy && hpc_check(fy, ny, ((is_rev)?(ylen-we1):(ws1)), ((is_rev)?(ylen-ws1):(we1)), &yi, 1)) continue; 
		err += ovlp;
	}
	return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %rbx
movq 0xa8(%rdi), %r9
testq %r9, %r9
je 0x17c7f1
movq %rdx, %r10
movq %rsi, %r13
movq (%rbx), %rax
testq %rax, %rax
setns %dl
cmpq %r9, %rax
setle %al
testb %al, %dl
jne 0x17c5ca
andq $0x0, (%rbx)
movslq 0x1c(%rdi), %rax
movq %rax, (%rcx)
movslq 0x10(%rdi), %rax
movq %rax, (%r8)
movq 0xa0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movq (%rbx), %rbp
testq %rbp, %rbp
jle 0x17c62c
cmpq %r13, (%rcx)
jle 0x17c62c
leaq -0x1(%rbp), %rax
movq %rax, (%rbx)
movq 0xb8(%rdi), %rsi
movzwl -0x2(%rsi,%rbp,2), %eax
movl %eax, %edx
shrl $0xe, %edx
cmpl $0x2, %edx
je 0x17c622
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%rcx)
cmpl $0x3, %edx
je 0x17c5e4
movq (%rbx), %rax
movzwl (%rsi,%rax,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%r8)
jmp 0x17c5e4
movq %r9, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %r8, 0x30(%rsp)
leaq 0x48(%rsp), %rax
andq $0x0, (%rax)
leaq 0x50(%rsp), %rax
andq $0x0, (%rax)
xorl %ecx, %ecx
cmpq $0x0, 0x98(%rsp)
movq 0x10(%rsp), %rax
cmovleq %rcx, %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0xa8(%rsp)
movq 0x8(%rsp), %rax
cmovleq %rcx, %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movq %r10, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
cmpq 0x38(%rsp), %rbp
movq 0x40(%rsp), %r9
jge 0x17c7ea
movq (%r9), %rdx
cmpq %r10, %rdx
jge 0x17c7ea
movq 0x30(%rsp), %r8
movq (%r8), %r12
movq 0xb8(%rdi), %rsi
movzwl (%rsi,%rbp,2), %eax
movl %eax, %r11d
shrl $0xe, %r11d
movq %r12, %r14
movl %eax, %ecx
cmpl $0x2, %r11d
je 0x17c6ef
movl %eax, %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rdx, %rcx
movq %rcx, (%r9)
movq (%r8), %r14
cmpl $0x3, %r11d
je 0x17c6fe
movq (%rbx), %rcx
movzwl (%rsi,%rcx,2), %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rcx, %r14
movq %r14, (%r8)
movq (%r9), %rcx
cmpq %r13, %rdx
movq %r13, %r8
cmovgq %rdx, %r8
cmpq %r10, %rcx
movq %r10, %r15
cmovlq %rcx, %r15
subq %r8, %r15
movl $0x0, %r8d
cmovleq %r8, %r15
movq (%rbx), %rbp
cmpq %r13, %rdx
jl 0x17c739
cmpl $0x2, %r11d
jne 0x17c739
movzwl (%rsi,%rbp,2), %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
incq %rbp
movq %rbp, (%rbx)
cmpw $0x4000, %ax            # imm = 0x4000
jb 0x17c690
testq %r15, %r15
je 0x17c690
cmpq $0x0, 0x10(%rsp)
je 0x17c788
movq 0x10(%rsp), %rdi
movq 0x98(%rsp), %rsi
leaq 0x50(%rsp), %r8
pushq $0x1
popq %r9
callq 0x17c4b6
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r10
testq %rax, %rax
jne 0x17c690
cmpq $0x0, 0x8(%rsp)
je 0x17c7e0
movq 0xb0(%rsp), %rcx
movq %rcx, %rdx
subq %r14, %rdx
subq %r12, %rcx
cmpq $0x0, 0xb8(%rsp)
cmoveq %r12, %rdx
cmoveq %r14, %rcx
movq 0x8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
leaq 0x48(%rsp), %r8
pushq $0x1
popq %r9
callq 0x17c4b6
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r10
testq %rax, %rax
jne 0x17c690
addq %r15, 0x18(%rsp)
jmp 0x17c690
movq 0x18(%rsp), %rax
jmp 0x17c7f3
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq