// File: /workspace/github_cpp_makefile_star2_O3/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), %r11
testq %r11, %r11
je 0x1c7599
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq (%r8), %r10
testq %r9, %r9
je 0x1c759c
testq %r10, %r10
setns %al
cmpq %r11, %r10
setl %r9b
testb %r9b, %al
jne 0x1c7697
leaq -0x1(%r11), %rax
movq %rax, (%r8)
movslq 0x20(%rdi), %rax
incq %rax
movq %rax, (%rcx)
jmp 0x1c7694
xorl %eax, %eax
retq
testq %r10, %r10
setns %al
cmpq %r11, %r10
setle %r9b
testb %r9b, %al
jne 0x1c75bf
movq $0x0, (%r8)
movslq 0x1c(%rdi), %rax
movq %rax, (%rcx)
movq (%r8), %r10
testq %r10, %r10
jle 0x1c75fa
cmpq %rsi, (%rcx)
jle 0x1c75fa
leaq -0x1(%r10), %r9
movq %r9, (%r8)
movq 0xb8(%rdi), %rax
movzwl -0x2(%rax,%r10,2), %eax
movl %eax, %ebx
andl $0xc000, %ebx           # imm = 0xC000
movq %r9, %r10
cmpl $0x8000, %ebx           # imm = 0x8000
je 0x1c75bf
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%rcx)
jmp 0x1c75bc
cmpq %r11, %r10
jge 0x1c768d
xorl %r9d, %r9d
xorl %eax, %eax
movq (%rcx), %r12
cmpq %rdx, %r12
jge 0x1c7775
movq 0xb8(%rdi), %r15
movzwl (%r15,%r10,2), %ebx
movl %ebx, %ebp
shrl $0xe, %ebp
movq %r12, %r14
cmpl $0x2, %ebp
je 0x1c7640
movl %ebx, %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
addq %r12, %r14
movq %r14, (%rcx)
movq (%r8), %r10
cmpq %rsi, %r12
movq %rsi, %r13
cmovgq %r12, %r13
cmpq %rdx, %r14
jl 0x1c7652
movq %rdx, %r14
subq %r13, %r14
jg 0x1c765a
xorl %r14d, %r14d
cmpq %rsi, %r12
jl 0x1c7670
cmpl $0x2, %ebp
jne 0x1c7670
movzwl (%r15,%r10,2), %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
incq %r10
movq %r10, (%r8)
cmpl $0x4000, %ebx           # imm = 0x4000
cmovbq %r9, %r14
addq %r14, %rax
cmpq %r11, %r10
jl 0x1c7608
jmp 0x1c7775
xorl %eax, %eax
jmp 0x1c7775
movq (%r8), %r10
cmpq %r11, %r10
jge 0x1c76d2
cmpq %rdx, (%rcx)
jge 0x1c76d2
leaq 0x1(%r10), %r9
movq %r9, (%r8)
movq 0xb8(%rdi), %rax
movzwl 0x2(%rax,%r10,2), %eax
movl %eax, %ebx
andl $0xc000, %ebx           # imm = 0xC000
movq %r9, %r10
cmpl $0x8000, %ebx           # imm = 0x8000
je 0x1c7697
andl $0x3fff, %eax           # imm = 0x3FFF
addq %rax, (%rcx)
jmp 0x1c7694
testq %r10, %r10
js 0x1c768d
xorl %r9d, %r9d
xorl %eax, %eax
movq (%rcx), %r11
cmpq %rsi, %r11
jle 0x1c7775
movq 0xb8(%rdi), %r14
movzwl (%r14,%r10,2), %ebx
movl %ebx, %ebp
andl $0xc000, %ebp           # imm = 0xC000
movq %r11, %r15
cmpl $0x8000, %ebp           # imm = 0x8000
je 0x1c771d
movl %ebx, %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
movq %r11, %r15
subq %r10, %r15
movq %r15, (%rcx)
movq (%r8), %r10
movq %r15, %r12
cmpq %rsi, %r15
jg 0x1c7728
movq %rsi, %r12
cmpq %rdx, %r11
cmovgeq %rdx, %r11
subq %r12, %r11
jg 0x1c7737
xorl %r11d, %r11d
cmpl $0x8000, %ebp           # imm = 0x8000
jne 0x1c7755
cmpq %rsi, %r15
jl 0x1c7755
cmpq %rdx, %r15
jge 0x1c7755
movzwl (%r14,%r10,2), %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
leaq -0x1(%r10), %r14
cmpl $0x4000, %ebx           # imm = 0x4000
cmovbq %r9, %r11
movq %r14, (%r8)
addq %r11, %rax
testq %r10, %r10
movq %r14, %r10
jg 0x1c76dc
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq