// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: retrieve_cigar_err_dir(bit_extz_t*, long, long, long*, long*, long)
int64_t retrieve_cigar_err_dir(bit_extz_t *ez, int64_t s, int64_t e, int64_t *xk, int64_t *ck, int64_t is_back)
{	///[ez->ts, ez->te]/[ez->qs, ez->qe]/[s, e)
    if(!ez->cigar.n) return 0;
    int64_t cn = ez->cigar.n, op, err = 0; int64_t ws, we, os, oe, ovlp;
	if(!is_back) {
		if(((*ck) < 0) || ((*ck) > cn)) {//(*ck) == cn is allowed
			(*ck) = 0; (*xk) = ez->ts; 
		}

		while ((*ck) > 0 && (*xk) > s) {
			--(*ck);
			op = ez->cigar.a[(*ck)]>>14;
			if(op!=2) (*xk) -= (ez->cigar.a[(*ck)]&(0x3fff));
		}

		//some cigar will span s or e
		while ((*ck) < cn && (*xk) < e) {//[s, e)
			ws = (*xk); 
			op = ez->cigar.a[(*ck)]>>14;
			if(op!=2) (*xk) += (ez->cigar.a[(*ck)]&(0x3fff));
			we = (*xk);
			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;
			err += ovlp;
		}
	} else {
		if(((*ck) < 0) || ((*ck) >= cn)) {
			(*ck) = cn-1; (*xk) = ez->te + 1; ///[ez->ts, ez->te]
		}
		while (((*ck) < cn) && ((*xk) < e)) {
			++(*ck);
			op = ez->cigar.a[(*ck)]>>14;
			if(op!=2) (*xk) += (ez->cigar.a[(*ck)]&(0x3fff));
		}

		//some cigar will span s or e
		while ((*ck) >= 0 && (*xk) > s) {//[s, e)
			we = (*xk); 
			op = ez->cigar.a[(*ck)]>>14;
			if(op!=2) (*xk) -= (ez->cigar.a[(*ck)]&(0x3fff));
			ws = (*xk);
			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;
			err += ovlp;
		}
	}
    // int64_t debug_err = retrieve_cigar_err_debug(ez, s, e);
    // if(!(err == debug_err)) {
    //     fprintf(stderr, "[M::%s::] err::%ld, debug_err::%ld, s::%ld, e::%ld, ez->ts::%u, ez->te::%u\n", 
    //     __func__, err, debug_err, s, e, ez->ts, ez->te);
    // }
    // assert(err == debug_err);
    return err;
}

// Assembly:
movq 0xa8(%rdi), %r10
testq %r10, %r10
je 0x1bd83e
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq (%r8), %rax
testq %r9, %r9
je 0x1bd841
testq %rax, %rax
setns %r9b
cmpq %r10, %rax
setl %al
testb %al, %r9b
jne 0x1bd790
leaq -0x1(%r10), %rax
movq %rax, (%r8)
movslq 0x20(%rdi), %rax
incq %rax
movq %rax, (%rcx)
jmp 0x1bd790
cmpq %rdx, (%rcx)
jge 0x1bd798
leaq 0x1(%rax), %r9
movq %r9, (%r8)
movq 0xb8(%rdi), %r9
movzwl 0x2(%r9,%rax,2), %eax
movl %eax, %r9d
andl $0xc000, %r9d           # imm = 0xC000
cmpl $0x8000, %r9d           # imm = 0x8000
je 0x1bd790
andl $0x3fff, %eax           # imm = 0x3FFF
addq %rax, (%rcx)
movq (%r8), %rax
cmpq %r10, %rax
jl 0x1bd75c
movq (%r8), %r14
testq %r14, %r14
js 0x1bd931
xorl %r9d, %r9d
xorl %eax, %eax
movq (%rcx), %r10
cmpq %rsi, %r10
jle 0x1bd933
movq 0xb8(%rdi), %rbx
movzwl (%rbx,%r14,2), %r11d
movl %r11d, %ebp
shrl $0xe, %ebp
cmpl $0x2, %ebp
je 0x1bd7df
movl %r11d, %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
movq %r10, %r15
subq %r14, %r15
movq %r15, (%rcx)
movq (%rcx), %r14
cmpq %rsi, %r14
movq %rsi, %r15
cmovgq %r14, %r15
cmpq %rdx, %r10
cmovgeq %rdx, %r10
subq %r15, %r10
cmovleq %r9, %r10
cmpl $0x2, %ebp
jne 0x1bd818
cmpq %rsi, %r14
jl 0x1bd818
cmpq %rdx, %r14
jge 0x1bd818
movq (%r8), %r10
movzwl (%rbx,%r10,2), %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
movq (%r8), %rbx
leaq -0x1(%rbx), %r14
movq %r14, (%r8)
cmpl $0x4000, %r11d          # imm = 0x4000
cmovbq %r9, %r10
addq %r10, %rax
testq %rbx, %rbx
jg 0x1bd7a9
jmp 0x1bd933
xorl %eax, %eax
retq
testq %rax, %rax
setns %r9b
cmpq %r10, %rax
setle %al
testb %al, %r9b
jne 0x1bd897
movq $0x0, (%r8)
movslq 0x1c(%rdi), %rax
movq %rax, (%rcx)
jmp 0x1bd897
cmpq %rsi, (%rcx)
jle 0x1bd89f
leaq -0x1(%rax), %r9
movq %r9, (%r8)
movq 0xb8(%rdi), %r9
movzwl -0x2(%r9,%rax,2), %eax
movl %eax, %r9d
andl $0xc000, %r9d           # imm = 0xC000
cmpl $0x8000, %r9d           # imm = 0x8000
je 0x1bd897
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%rcx)
movq (%r8), %rax
testq %rax, %rax
jg 0x1bd863
movq (%r8), %r12
cmpq %r10, %r12
jge 0x1bd931
xorl %r9d, %r9d
xorl %eax, %eax
movq (%rcx), %r15
cmpq %rdx, %r15
jge 0x1bd933
movq 0xb8(%rdi), %rbx
movzwl (%rbx,%r12,2), %r11d
movl %r11d, %ebp
shrl $0xe, %ebp
cmpl $0x2, %ebp
je 0x1bd8df
movl %r11d, %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
addq %r15, %r14
movq %r14, (%rcx)
cmpq %rsi, %r15
movq %rsi, %r12
cmovgq %r15, %r12
movq (%rcx), %r14
cmpq %rdx, %r14
cmovgeq %rdx, %r14
subq %r12, %r14
cmovleq %r9, %r14
cmpq %rsi, %r15
jl 0x1bd913
cmpl $0x2, %ebp
jne 0x1bd913
movq (%r8), %r14
movzwl (%rbx,%r14,2), %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
movq (%r8), %r12
incq %r12
movq %r12, (%r8)
cmpl $0x4000, %r11d          # imm = 0x4000
cmovbq %r9, %r14
addq %r14, %rax
cmpq %r10, %r12
jl 0x1bd8b0
jmp 0x1bd933
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq