// File: /workspace/github_cpp_makefile_star2_O1/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 0x755f2
movq (%r9), %r10
testq %r10, %r10
setns %r11b
cmpq %rax, %r10
setle %r10b
testb %r10b, %r11b
jne 0x754ad
movq $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 (%r9), %r10
testq %r10, %r10
jle 0x75508
cmpq %rsi, (%rcx)
jle 0x75508
leaq -0x1(%r10), %r11
movq %r11, (%r9)
movq 0xb8(%rdi), %r11
movzwl -0x2(%r11,%r10,2), %ebx
movl %ebx, %r10d
shrl $0xe, %r10d
cmpl $0x3, %r10d
je 0x754f4
andl $0x3fff, %ebx           # imm = 0x3FFF
subq %rbx, (%rcx)
cmpl $0x2, %r10d
je 0x754b7
movq (%r9), %r10
movzwl (%r11,%r10,2), %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
subq %r10, (%r8)
jmp 0x754b7
movq (%r9), %r15
cmpq %rax, %r15
jge 0x755e8
movq 0x38(%rsp), %r11
movq (%rcx), %r14
cmpq %rdx, %r14
jge 0x755e8
movq (%r8), %rbx
movq 0xb8(%rdi), %r13
movzwl (%r13,%r15,2), %r12d
movl %r12d, %ebp
shrl $0xe, %ebp
cmpl $0x3, %ebp
je 0x75555
movl %r12d, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
addq %r14, %r15
movq %r15, (%rcx)
cmpl $0x2, %ebp
je 0x75568
movq (%r9), %r15
movzwl (%r13,%r15,2), %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
addq %r15, (%r8)
movq (%rcx), %r15
cmpq %rsi, %r14
setl %r13b
cmpq %rdx, %r15
setg %r10b
orb %r13b, %r10b
je 0x755be
cmpl $0x4000, %r12d          # imm = 0x4000
setae %r10b
cmpl $0x1, %ebp
setne %bpl
testb %bpl, %r10b
jne 0x755d6
cmpq %rsi, %r14
movq %rsi, %r12
cmovgq %r14, %r12
cmpq %rdx, %r15
cmovgeq %rdx, %r15
subq %r12, %r15
jle 0x755d6
subq %r14, %r12
addq %r12, %rbx
cmpq %rbx, (%r11)
jle 0x755b9
movq %rbx, (%r11)
addq %rbx, %r15
jmp 0x755c9
movq (%r8), %r15
cmpq %rbx, (%r11)
jle 0x755c9
movq %rbx, (%r11)
movq 0x40(%rsp), %r10
cmpq %r15, (%r10)
jge 0x755d6
movq %r15, (%r10)
movq (%r9), %r15
incq %r15
movq %r15, (%r9)
cmpq %rax, %r15
jl 0x75519
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq