// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: retrieve_cigar_xcoord(bit_extz_t*, long, long, long*, long*, long*, long*, long*)
void retrieve_cigar_xcoord(bit_extz_t *ez, int64_t is, int64_t ie, int64_t *yk, int64_t *xk, int64_t *ck, int64_t *rxs, int64_t *rxe)
{
    if(!ez->cigar.n) return;
    int64_t cn = ez->cigar.n, op; int64_t ws, we, ws0, we0, os, oe;
    if(((*ck) < 0) || ((*ck) > cn)) {//(*ck) == cn is allowed
        (*ck) = 0; (*yk) = ez->ps; (*xk) = ez->ts; 
    }

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

    //some cigar will span s or e
    while ((*ck) < cn && (*yk) < ie) {//[s, e)
        ws = (*yk); ws0 = (*xk); 
        op = ez->cigar.a[(*ck)]>>14;
        if(op!=3) (*yk) += (ez->cigar.a[(*ck)]&(0x3fff));
        if(op!=2) (*xk) += (ez->cigar.a[(*ck)]&(0x3fff));
        we = (*yk); we0 = (*xk); 
        if(ws >= is && we <= ie) {///the cigar is fully contained
            if((*rxs) > ws0) (*rxs) = ws0;
            if((*rxe) < we0) (*rxe) = we0;
        } else if(op == 0 || op == 1) {///overlap, it is hard to handle indels
            os = MAX(is, ws); oe = MIN(ie, we);
            if(oe > os) {
                ws0 += (os - ws);
                we0 = ws0 + (oe - os);
                if((*rxs) > ws0) (*rxs) = ws0;
                if((*rxe) < we0) (*rxe) = we0;
            }
        }
        (*ck)++;
    }
}

// Assembly:
movq 0xa8(%rdi), %rax
testq %rax, %rax
je 0x42e65
movq (%r9), %r10
testq %r10, %r10
setns %r11b
cmpq %rax, %r10
setle %r10b
testb %r10b, %r11b
jne 0x42d1b
andq $0x0, (%r9)
movslq 0x10(%rdi), %r10
movq %r10, (%rcx)
movslq 0x1c(%rdi), %r10
movq %r10, (%r8)
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x38(%rsp), %r11
movq (%r9), %r15
testq %r15, %r15
jle 0x42e52
cmpq %rsi, (%rcx)
jle 0x42e52
leaq -0x1(%r15), %r10
movq %r10, (%r9)
movq 0xb8(%rdi), %rbx
movzwl -0x2(%rbx,%r15,2), %r10d
movl %r10d, %ebp
shrl $0xe, %ebp
cmpl $0x3, %ebp
je 0x42d75
andl $0x3fff, %r10d          # imm = 0x3FFF
subq %r10, (%rcx)
cmpl $0x2, %ebp
je 0x42d2a
movq (%r9), %r10
movzwl (%rbx,%r10,2), %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
subq %r10, (%r8)
jmp 0x42d2a
movq (%rcx), %r14
cmpq %rdx, %r14
jge 0x42e5b
movq (%r8), %rbx
movq 0xb8(%rdi), %r10
movzwl (%r10,%r15,2), %r13d
movl %r13d, %ebp
shrl $0xe, %ebp
movq %rbx, %r12
movl %r13d, %r15d
cmpl $0x3, %ebp
je 0x42dcd
movl %r13d, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
addq %r14, %r15
movq %r15, (%rcx)
movq (%r8), %r12
cmpl $0x2, %ebp
je 0x42ddd
movq (%r9), %r15
movzwl (%r10,%r15,2), %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
addq %r15, %r12
movq %r12, (%r8)
movq (%rcx), %r15
cmpq %rsi, %r14
jl 0x42dfe
cmpq %rdx, %r15
jg 0x42dfe
cmpq %rbx, (%r11)
jle 0x42def
movq %rbx, (%r11)
movq 0x40(%rsp), %r10
cmpq %r12, (%r10)
jge 0x42e49
movq %r12, (%r10)
jmp 0x42e49
cmpw $0x4000, %r13w          # imm = 0x4000
setae %r10b
cmpl $0x1, %ebp
setne %bpl
testb %bpl, %r10b
jne 0x42e49
cmpq %rsi, %r14
movq %rsi, %r10
cmovgq %r14, %r10
cmpq %rdx, %r15
jl 0x42e26
movq %rdx, %r15
subq %r10, %r15
jle 0x42e49
subq %r14, %r10
addq %r10, %rbx
cmpq %rbx, (%r11)
jle 0x42e39
movq %rbx, (%r11)
addq %rbx, %r15
movq %r15, %r12
movq 0x40(%rsp), %r10
cmpq %r15, (%r10)
jl 0x42df9
movq (%r9), %r15
incq %r15
movq %r15, (%r9)
cmpq %rax, %r15
jl 0x42d81
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq