// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/htscodecs/htscodecs/fqzcomp_qual.c
// Function: fqz_update_ctx
static inline unsigned int fqz_update_ctx(fqz_param *pm, fqz_state *state, int q) {
    unsigned int last = 0; // pm->context
    state->qctx = (state->qctx << pm->qshift) + pm->qtab[q];
    last += (state->qctx & pm->qmask) << pm->qloc;

    // The final shifts have been factored into the tables already.
    last += pm->ptab[MIN(1023, state->p)];      // << pm->ploc
    last += pm->dtab[MIN(255,  state->delta)];  // << pm->dloc
    last += state->s << pm->sloc;

    // On the fly average is slow work.
    // However it can be slightly better than using a selector bit
    // as it's something we can compute on the fly and thus doesn't
    // consume output bits for storing the selector itself.
    //
    // Q4 (novaseq.bam)
    // qtot+=q*q -DQ1=8.84 -DQ2=8.51 -DQ3=7.70; 7203598 (-0.7%)
    // qtot+=q   -DQ1=2.96 -DQ2=2.85 -DQ3=2.69; 7207315
    // vs old delta;                            7255614 (default params)
    // vs 2 bit selector (no delta)             7203006 (-x 0x8261000e80)
    // vs 2 bit selector (no delta)             7199153 (-x 0x7270000e70) -0.8%
    // vs 2 bit selector (no delta)             7219668 (-x 0xa243000ea0)
    //{
    //  double qa = state->qtot / (state->qlen+.01);
    //  //fprintf(stderr, "%f\n", qa);
    //  int x = 0;
    //  if (qa>=Q1) x=3;
    //  else if (qa>=Q2) x=2;
    //  else if (qa>=Q3) x=1;
    //  else x=0;
    //  last += x << pm->dloc; // tmp reuse of delta pos
    //  state->qtot += q*q;
    //  state->qlen++;
    //}

    // Only update delta after 1st base.
    state->delta += (state->prevq != q);
    state->prevq = q;

    state->p--;

    return last & (CTX_SIZE-1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
movl 0x1c48(%rcx), %ecx
shll %cl, %eax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rdx
addl 0x448(%rax,%rdx,4), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
andl 0x1c58(%rcx), %eax
movq -0x8(%rbp), %rcx
movl 0x20(%rcx), %ecx
shll %cl, %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
addq $0x848, %rax            # imm = 0x848
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movl $0x3ff, %eax            # imm = 0x3FF
cmpl 0x4(%rcx), %eax
jae 0x8ac94
movl $0x3ff, %eax            # imm = 0x3FF
movl %eax, -0x24(%rbp)
jmp 0x8ac9e
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
addq $0x1848, %rax           # imm = 0x1848
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rcx
movl $0xff, %eax
cmpl 0x8(%rcx), %eax
jae 0x8acd6
movl $0xff, %eax
movl %eax, -0x34(%rbp)
jmp 0x8ace0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x8(%rbp), %rcx
movl 0x38(%rcx), %ecx
shll %cl, %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
cmpl -0x14(%rbp), %eax
setne %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x10(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x4(%rax)
movl -0x18(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
popq %rbp
retq
nopw %cs:(%rax,%rax)