// File: /workspace/github_cpp_makefile_star2_O3/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 0x75065
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq (%r9), %r15
testq %r15, %r15
setns %r10b
cmpq %rax, %r15
setle %r11b
testb %r11b, %r10b
jne 0x74f22
movq $0x0, (%r9)
movslq 0x10(%rdi), %r10
movq %r10, (%rcx)
movslq 0x1c(%rdi), %r10
movq %r10, (%r8)
movq (%r9), %r15
testq %r15, %r15
jle 0x74f6e
cmpq %rsi, (%rcx)
jle 0x74f6e
leaq -0x1(%r15), %r10
movq %r10, (%r9)
movq 0xb8(%rdi), %r11
movzwl -0x2(%r11,%r15,2), %r10d
movl %r10d, %ebx
shrl $0xe, %ebx
cmpl $0x3, %ebx
je 0x74f62
andl $0x3fff, %r10d          # imm = 0x3FFF
subq %r10, (%rcx)
cmpl $0x2, %ebx
je 0x74f1f
movq (%r9), %r10
movzwl (%r11,%r10,2), %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
subq %r10, (%r8)
jmp 0x74f1f
cmpq %rax, %r15
jge 0x7505b
movq 0x38(%rsp), %r11
movq (%rcx), %r14
cmpq %rdx, %r14
jge 0x7505b
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 0x74fc8
movl %r13d, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
addq %r14, %r15
movq %r15, (%rcx)
movq (%r8), %r12
cmpl $0x2, %ebp
je 0x74fd8
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 0x74ff9
cmpq %rdx, %r15
jg 0x74ff9
cmpq %rbx, (%r11)
jle 0x74fea
movq %rbx, (%r11)
movq 0x40(%rsp), %r10
cmpq %r12, (%r10)
jge 0x75049
movq %r12, (%r10)
jmp 0x75049
movzwl %r13w, %r10d
cmpl $0x4000, %r10d          # imm = 0x4000
setae %r10b
cmpl $0x1, %ebp
setne %bpl
testb %bpl, %r10b
jne 0x75049
cmpq %rsi, %r14
movq %rsi, %r10
cmovgq %r14, %r10
cmpq %rdx, %r15
jl 0x75026
movq %rdx, %r15
subq %r10, %r15
jle 0x75049
subq %r14, %r10
addq %r10, %rbx
cmpq %rbx, (%r11)
jle 0x75039
movq %rbx, (%r11)
addq %rbx, %r15
movq %r15, %r12
movq 0x40(%rsp), %r10
cmpq %r15, (%r10)
jl 0x74ff4
movq (%r9), %r15
incq %r15
movq %r15, (%r9)
cmpq %rax, %r15
jl 0x74f7c
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq