// File: /workspace/github_cpp_makefile_star2_O2/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 0x17be33
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq (%r8), %rax
testq %r9, %r9
je 0x17be36
testq %rax, %rax
setns %r9b
cmpq %r10, %rax
setl %al
testb %al, %r9b
jne 0x17bd8d
leaq -0x1(%r10), %rax
movq %rax, (%r8)
movslq 0x20(%rdi), %rax
incq %rax
movq %rax, (%rcx)
jmp 0x17bd8d
cmpq %rdx, (%rcx)
jge 0x17bd95
leaq 0x1(%r9), %rax
movq %rax, (%r8)
movq 0xb8(%rdi), %rax
movzwl 0x2(%rax,%r9,2), %eax
movl %eax, %r9d
andl $0xc000, %r9d           # imm = 0xC000
cmpl $0x8000, %r9d           # imm = 0x8000
je 0x17bd8d
andl $0x3fff, %eax           # imm = 0x3FFF
addq %rax, (%rcx)
movq (%r8), %r9
cmpq %r10, %r9
jl 0x17bd59
xorl %r10d, %r10d
xorl %eax, %eax
testq %r9, %r9
js 0x17bf13
movq (%rcx), %r11
cmpq %rsi, %r11
jle 0x17bf13
movq 0xb8(%rdi), %r14
movzwl (%r14,%r9,2), %ebx
movl %ebx, %ebp
andl $0xc000, %ebp           # imm = 0xC000
movq %r11, %r15
cmpl $0x8000, %ebp           # imm = 0x8000
je 0x17bde4
movl %ebx, %r9d
andl $0x3fff, %r9d           # imm = 0x3FFF
movq %r11, %r15
subq %r9, %r15
movq %r15, (%rcx)
movq (%r8), %r9
movq %r15, %r12
cmpq %rsi, %r15
jg 0x17bdef
movq %rsi, %r12
cmpq %rdx, %r11
cmovgeq %rdx, %r11
subq %r12, %r11
jg 0x17bdfe
xorl %r11d, %r11d
cmpl $0x8000, %ebp           # imm = 0x8000
jne 0x17be1c
cmpq %rsi, %r15
jl 0x17be1c
cmpq %rdx, %r15
jge 0x17be1c
movzwl (%r14,%r9,2), %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
decq %r9
movq %r9, (%r8)
cmpw $0x4000, %bx            # imm = 0x4000
cmovbq %r10, %r11
addq %r11, %rax
jmp 0x17bd9a
xorl %eax, %eax
retq
testq %rax, %rax
setns %r9b
cmpq %r10, %rax
setle %al
testb %al, %r9b
jne 0x17be89
andq $0x0, (%r8)
movslq 0x1c(%rdi), %rax
movq %rax, (%rcx)
jmp 0x17be89
cmpq %rsi, (%rcx)
jle 0x17be91
leaq -0x1(%r9), %rax
movq %rax, (%r8)
movq 0xb8(%rdi), %rax
movzwl -0x2(%rax,%r9,2), %eax
movl %eax, %r9d
andl $0xc000, %r9d           # imm = 0xC000
cmpl $0x8000, %r9d           # imm = 0x8000
je 0x17be89
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%rcx)
movq (%r8), %r9
testq %r9, %r9
jg 0x17be55
xorl %r11d, %r11d
xorl %eax, %eax
cmpq %r10, %r9
jge 0x17bf13
movq (%rcx), %r12
cmpq %rdx, %r12
jge 0x17bf13
movq 0xb8(%rdi), %r15
movzwl (%r15,%r9,2), %ebx
movl %ebx, %ebp
shrl $0xe, %ebp
movq %r12, %r14
cmpl $0x2, %ebp
je 0x17becf
movl %ebx, %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
addq %r12, %r14
movq %r14, (%rcx)
movq (%r8), %r9
cmpq %rsi, %r12
movq %rsi, %r13
cmovgq %r12, %r13
cmpq %rdx, %r14
jl 0x17bee1
movq %rdx, %r14
subq %r13, %r14
jg 0x17bee9
xorl %r14d, %r14d
cmpq %rsi, %r12
jl 0x17beff
cmpl $0x2, %ebp
jne 0x17beff
movzwl (%r15,%r9,2), %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
incq %r9
movq %r9, (%r8)
cmpw $0x4000, %bx            # imm = 0x4000
cmovbq %r11, %r14
addq %r14, %rax
jmp 0x17be96
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq