// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/htscodecs/htscodecs/rANS_static4x16pr.c
// Function: rans_compute_shift
int rans_compute_shift(uint32_t *F0, uint32_t (*F)[256], uint32_t *T,
                       uint32_t *S) {
    int i, j;

    double e10 = 0, e12 = 0;
    int max_tot = 0;
    for (i = 0; i < 256; i++) {
        if (F0[i] == 0)
            continue;
        unsigned int max_val = round2(T[i]);
        int ns = 0;
#define MAX(a,b) ((a)>(b)?(a):(b))

        // Number of samples that get their freq bumped to 1
        int sm10 = 0, sm12 = 0;
        for (j = 0; j < 256; j++) {
            if (F[i][j] && max_val / F[i][j] > TOTFREQ_O1_FAST)
                sm10++;
            if (F[i][j] && max_val / F[i][j] > TOTFREQ_O1)
                sm12++;
        }

        double l10 = log(TOTFREQ_O1_FAST + sm10);
        double l12 = log(TOTFREQ_O1      + sm12);
        double T_slow = (double)TOTFREQ_O1/T[i];
        double T_fast = (double)TOTFREQ_O1_FAST/T[i];

        for (j = 0; j < 256; j++) {
            if (F[i][j]) {
                ns++;

                e10 -= F[i][j] * (fast_log(MAX(F[i][j]*T_fast,1)) - l10);
                e12 -= F[i][j] * (fast_log(MAX(F[i][j]*T_slow,1)) - l12);

                // Estimation of compressed symbol freq table too.
                e10 += 1.3;
                e12 += 4.7;
            }
        }

        // Order-1 frequencies often end up totalling under TOTFREQ.
        // In this case it's smaller to output the real frequencies
        // prior to normalisation and normalise after (with an extra
        // normalisation step needed in the decoder too).
        //
        // Thus we normalise to a power of 2 only, store those,
        // and renormalise later here (and in decoder) by bit-shift
        // to get to the fixed size.
        if (ns < 64 && max_val > 128) max_val /= 2;
        if (max_val > 1024)           max_val /= 2;
        if (max_val > TOTFREQ_O1)     max_val = TOTFREQ_O1;
        S[i] = max_val; // scale to max this
        if (max_tot < max_val)
            max_tot = max_val;
    }
    int shift = e10/e12 < 1.01 || max_tot <= TOTFREQ_O1_FAST
        ? TF_SHIFT_O1_FAST
        : TF_SHIFT_O1;

//    fprintf(stderr, "e10/12 = %f %f %f, shift %d\n",
//          e10/log(256), e12/log(256), e10/e12, shift);

    return shift;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x100, -0x24(%rbp)     # imm = 0x100
jge 0x90133
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x8fdc9
jmp 0x90125
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x8ede0
movl %eax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x100, -0x28(%rbp)     # imm = 0x100
jge 0x8fe9d
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x8fe49
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x24(%rbp), %rdx
shlq $0xa, %rdx
addq %rdx, %rcx
movslq -0x28(%rbp), %rsi
xorl %edx, %edx
divl (%rcx,%rsi,4)
cmpl $0x400, %eax            # imm = 0x400
jbe 0x8fe49
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x8fe8d
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x24(%rbp), %rdx
shlq $0xa, %rdx
addq %rdx, %rcx
movslq -0x28(%rbp), %rsi
xorl %edx, %edx
divl (%rcx,%rsi,4)
cmpl $0x1000, %eax           # imm = 0x1000
jbe 0x8fe8d
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x8fe8f
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x8fdf8
movl -0x48(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
cvtsi2sd %eax, %xmm0
callq 0x4380
movsd %xmm0, -0x58(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1000, %eax           # imm = 0x1000
cvtsi2sd %eax, %xmm0
callq 0x4380
movsd %xmm0, -0x60(%rbp)
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm1
movsd 0x6653f(%rip), %xmm0    # 0xf6420
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm1
movsd 0x66516(%rip), %xmm0    # 0xf6418
divsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x100, -0x28(%rbp)     # imm = 0x100
jge 0x900cf
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x900bf
movl -0x44(%rbp), %eax
incl %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x80(%rbp)
mulsd -0x70(%rbp), %xmm0
movsd 0x5d097(%rip), %xmm1    # 0xed008
ucomisd %xmm1, %xmm0
jbe 0x8ffa1
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
mulsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
jmp 0x8ffb3
movsd 0x5d05f(%rip), %xmm0    # 0xed008
movsd %xmm0, -0x88(%rbp)
jmp 0x8ffb3
movsd -0x88(%rbp), %xmm0
callq 0x90190
movsd -0x80(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x58(%rbp), %xmm0
subsd %xmm0, %xmm2
movsd -0x30(%rbp), %xmm0
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x90(%rbp)
mulsd -0x68(%rbp), %xmm0
movsd 0x5cff5(%rip), %xmm1    # 0xed008
ucomisd %xmm1, %xmm0
jbe 0x90043
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
mulsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
jmp 0x90055
movsd 0x5cfbd(%rip), %xmm0    # 0xed008
movsd %xmm0, -0x98(%rbp)
jmp 0x90055
movsd -0x98(%rbp), %xmm0
callq 0x90190
movaps %xmm0, %xmm2
movsd -0x90(%rbp), %xmm0
subsd -0x60(%rbp), %xmm2
movsd -0x38(%rbp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd 0x5f58d(%rip), %xmm0    # 0xef630
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0x66373(%rip), %xmm0    # 0xf6428
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x900c1
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x8ff12
cmpl $0x40, -0x44(%rbp)
jge 0x900e6
cmpl $0x80, -0x40(%rbp)
jbe 0x900e6
movl -0x40(%rbp), %eax
shrl %eax
movl %eax, -0x40(%rbp)
cmpl $0x400, -0x40(%rbp)     # imm = 0x400
jbe 0x900f7
movl -0x40(%rbp), %eax
shrl %eax
movl %eax, -0x40(%rbp)
cmpl $0x1000, -0x40(%rbp)    # imm = 0x1000
jbe 0x90107
movl $0x1000, -0x40(%rbp)    # imm = 0x1000
movl -0x40(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x90123
movl -0x40(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x90125
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x8fda9
movsd -0x30(%rbp), %xmm1
divsd -0x38(%rbp), %xmm1
movb $0x1, %al
movsd 0x65429(%rip), %xmm0    # 0xf5570
ucomisd %xmm1, %xmm0
movb %al, -0x99(%rbp)
ja 0x90163
cmpl $0x400, -0x3c(%rbp)     # imm = 0x400
setle %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %dl
movl $0xc, %eax
movl $0xa, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)