// File: /workspace/github_cpp_makefile_star2_O3/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:
movq 0xa8(%rdi), %r11
testq %r11, %r11
je 0x1c809b
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %rbx
movq %rcx, %r9
movq %rdx, %r10
movq %rsi, %r13
movq (%rbx), %rbp
testq %rbp, %rbp
setns %al
cmpq %r11, %rbp
setle %cl
testb %cl, %al
jne 0x1c7e64
movq $0x0, (%rbx)
movslq 0x1c(%rdi), %rax
movq %rax, (%r9)
movslq 0x10(%rdi), %rax
movq %rax, (%r8)
movq (%rbx), %rbp
movq 0xa0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
testq %rbp, %rbp
jle 0x1c7ec6
cmpq %r13, (%r9)
jle 0x1c7ec6
leaq -0x1(%rbp), %rax
movq %rax, (%rbx)
movq 0xb8(%rdi), %rcx
movzwl -0x2(%rcx,%rbp,2), %eax
movl %eax, %edx
shrl $0xe, %edx
cmpl $0x2, %edx
je 0x1c7eb9
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%r9)
cmpl $0x3, %edx
je 0x1c7ec1
movq (%rbx), %rax
movzwl (%rcx,%rax,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%r8)
movq (%rbx), %rbp
jmp 0x1c7e7e
xorl %ecx, %ecx
movq %rcx, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
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)
cmpq %r11, %rbp
jge 0x1c80a4
xorl %ecx, %ecx
movq %r8, 0x30(%rsp)
movq %r9, 0x40(%rsp)
movq %r10, 0x28(%rsp)
movq %r11, 0x20(%rsp)
movq %rdi, 0x38(%rsp)
movq %rcx, 0x18(%rsp)
movq (%r9), %rdx
cmpq %r10, %rdx
jge 0x1c809f
movq (%r8), %r14
movq 0xb8(%rdi), %rsi
movzwl (%rsi,%rbp,2), %eax
movl %eax, %edi
shrl $0xe, %edi
movq %r14, %r12
movl %eax, %ecx
cmpl $0x2, %edi
je 0x1c7f6f
movl %eax, %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rdx, %rcx
movq %rcx, (%r9)
movq (%r8), %r12
cmpl $0x3, %edi
je 0x1c7f7e
movq (%rbx), %rcx
movzwl (%rsi,%rcx,2), %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rcx, %r12
movq %r12, (%r8)
movq (%r9), %rcx
cmpq %r13, %rdx
movq %r13, %r9
cmovgq %rdx, %r9
cmpq %r10, %rcx
movq %r10, %r15
cmovlq %rcx, %r15
subq %r9, %r15
movl $0x0, %r9d
cmovleq %r9, %r15
movq (%rbx), %rbp
cmpq %r13, %rdx
jl 0x1c7fb8
cmpl $0x2, %edi
jne 0x1c7fb8
movzwl (%rsi,%rbp,2), %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
incq %rbp
movq %rbp, (%rbx)
movzwl %ax, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x1c8069
testq %r15, %r15
je 0x1c8069
cmpq $0x0, 0x10(%rsp)
je 0x1c800e
movl $0x1, %r9d
movq 0x10(%rsp), %rdi
movq 0x98(%rsp), %rsi
leaq 0x50(%rsp), %r8
callq 0x1c7d4a
movq 0x20(%rsp), %r11
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r8
testq %rax, %rax
jne 0x1c8069
cmpq $0x0, 0x8(%rsp)
je 0x1c807e
movq 0xb0(%rsp), %rcx
movq %rcx, %rdx
subq %r12, %rdx
subq %r14, %rcx
cmpq $0x0, 0xb8(%rsp)
cmoveq %r14, %rdx
cmoveq %r12, %rcx
movl $0x1, %r9d
movq 0x8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
leaq 0x48(%rsp), %r8
callq 0x1c7d4a
movq 0x20(%rsp), %r11
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r8
testq %rax, %rax
je 0x1c807e
cmpq %r11, %rbp
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %rdi
jl 0x1c7f29
jmp 0x1c809f
movq 0x18(%rsp), %rcx
addq %r15, %rcx
cmpq %r11, %rbp
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %rdi
jl 0x1c7f24
jmp 0x1c80a4
xorl %ecx, %ecx
jmp 0x1c80b2
movq 0x18(%rsp), %rcx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movq %rcx, %rax
retq