// File: /workspace/github_cpp_makefile_star2_O1/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 0x1be252
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r9
movq %rsi, %r13
movq (%rbx), %rax
testq %rax, %rax
setns %cl
cmpq %r11, %rax
setle %al
testb %al, %cl
jne 0x1be033
movq $0x0, (%rbx)
movslq 0x1c(%rdi), %rax
movq %rax, (%r15)
movslq 0x10(%rdi), %rax
movq %rax, (%r14)
movq 0x90(%rsp), %rax
movq %rax, (%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
movq (%rbx), %rax
testq %rax, %rax
jle 0x1be095
cmpq %r13, (%r15)
jle 0x1be095
leaq -0x1(%rax), %rcx
movq %rcx, (%rbx)
movq 0xb8(%rdi), %rcx
movzwl -0x2(%rcx,%rax,2), %edx
movl %edx, %eax
shrl $0xe, %eax
cmpl $0x2, %eax
je 0x1be084
andl $0x3fff, %edx           # imm = 0x3FFF
subq %rdx, (%r15)
cmpl $0x3, %eax
je 0x1be04c
movq (%rbx), %rax
movzwl (%rcx,%rax,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%r14)
jmp 0x1be04c
xorl %ecx, %ecx
movq %rcx, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
cmpq $0x0, 0x88(%rsp)
movq 0x8(%rsp), %rax
cmovleq %rcx, %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x98(%rsp)
movq (%rsp), %rax
cmovleq %rcx, %rax
movq %rax, (%rsp)
movq (%rbx), %rax
cmpq %r11, %rax
jge 0x1be25b
xorl %ecx, %ecx
movq %r9, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movq %r11, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq (%r15), %rdx
cmpq %r9, %rdx
jge 0x1be256
movq (%r14), %rcx
movq %rcx, 0x30(%rsp)
movq 0xb8(%rdi), %rsi
movzwl (%rsi,%rax,2), %eax
movl %eax, %r10d
shrl $0xe, %r10d
cmpl $0x2, %r10d
je 0x1be12f
movl %eax, %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rdx, %rcx
movq %rcx, (%r15)
cmpl $0x3, %r10d
je 0x1be13f
movq (%rbx), %rcx
movzwl (%rsi,%rcx,2), %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rcx, (%r14)
movq (%r15), %rcx
cmpq %r13, %rdx
movq %r13, %r8
cmovgq %rdx, %r8
cmpq %r9, %rcx
movq %r9, %r12
cmovlq %rcx, %r12
subq %r8, %r12
movl $0x0, %r8d
cmovleq %r8, %r12
movq (%r14), %rbp
cmpq %r13, %rdx
jl 0x1be180
cmpl $0x2, %r10d
jne 0x1be180
movq (%rbx), %r8
movzwl (%rsi,%r8,2), %r12d
andl $0x3fff, %r12d          # imm = 0x3FFF
incq (%rbx)
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x1be22e
testq %r12, %r12
je 0x1be22e
cmpq $0x0, 0x8(%rsp)
je 0x1be1d0
movl $0x1, %r9d
movq 0x8(%rsp), %rdi
movq 0x88(%rsp), %rsi
leaq 0x40(%rsp), %r8
callq 0x1bdf07
movq 0x18(%rsp), %r11
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r9
testq %rax, %rax
jne 0x1be22e
cmpq $0x0, (%rsp)
je 0x1be23c
movq 0xa0(%rsp), %rcx
movq %rcx, %rdx
subq %rbp, %rdx
movq 0x30(%rsp), %rax
subq %rax, %rcx
cmpq $0x0, 0xa8(%rsp)
cmoveq %rax, %rdx
cmoveq %rbp, %rcx
movl $0x1, %r9d
movq (%rsp), %rdi
movq 0x98(%rsp), %rsi
leaq 0x38(%rsp), %r8
callq 0x1bdf07
movq 0x18(%rsp), %r11
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r9
testq %rax, %rax
je 0x1be23c
movq (%rbx), %rax
cmpq %r11, %rax
jl 0x1be0ef
jmp 0x1be256
movq 0x10(%rsp), %rcx
addq %r12, %rcx
movq (%rbx), %rax
cmpq %r11, %rax
jl 0x1be0ea
jmp 0x1be25b
xorl %ecx, %ecx
jmp 0x1be269
movq 0x10(%rsp), %rcx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movq %rcx, %rax
retq