// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/htscodecs/htscodecs/rANS_static4x16pr.c
// Function: rans_uncompress_O0_4x16
unsigned char *rans_uncompress_O0_4x16(unsigned char *in, unsigned int in_size,
                                       unsigned char *out, unsigned int out_sz) {
    if (in_size < 16) // 4-states at least
        return NULL;

    if (out_sz >= INT_MAX)
        return NULL; // protect against some overflow cases

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
    if (out_sz > 100000)
        return NULL;
#endif

    /* Load in the static tables */
    unsigned char *cp = in, *out_free = NULL;
    unsigned char *cp_end = in + in_size - 8; // within 8 => be extra safe
    int i, j;
    unsigned int x, y;
    uint16_t sfreq[TOTFREQ+32];
    uint16_t sbase[TOTFREQ+32]; // faster to use 32-bit on clang
    uint8_t  ssym [TOTFREQ+64]; // faster to use 16-bit on clang

    if (!out)
        out_free = out = malloc(out_sz);
    if (!out)
        return NULL;

    // Precompute reverse lookup of frequency.
    uint32_t F[256] = {0}, fsum;
    int fsz = decode_freq(cp, cp_end, F, &fsum);
    if (!fsz)
        goto err;
    cp += fsz;

    normalise_freq_shift(F, fsum, TOTFREQ);

    // Build symbols; fixme, do as part of decode, see the _d variant
    for (j = x = 0; j < 256; j++) {
        if (F[j]) {
            if (F[j] > TOTFREQ - x)
                goto err;
            for (y = 0; y < F[j]; y++) {
                ssym [y + x] = j;
                sfreq[y + x] = F[j];
                sbase[y + x] = y;
            }
            x += F[j];
        }
    }

    if (x != TOTFREQ)
        goto err;

    if (cp+16 > cp_end+8)
        goto err;

    RansState R[4];
    RansDecInit(&R[0], &cp); if (R[0] < RANS_BYTE_L) goto err;
    RansDecInit(&R[1], &cp); if (R[1] < RANS_BYTE_L) goto err;
    RansDecInit(&R[2], &cp); if (R[2] < RANS_BYTE_L) goto err;
    RansDecInit(&R[3], &cp); if (R[3] < RANS_BYTE_L) goto err;

// Simple version is comparable to below, but only with -O3
//
//    for (i = 0; cp < cp_end-8 && i < (out_sz&~7); i+=8) {
//        for(j=0; j<8;j++) {
//          RansState m = RansDecGet(&R[j%4], TF_SHIFT);
//          R[j%4] = sfreq[m] * (R[j%4] >> TF_SHIFT) + sbase[m];
//          out[i+j] = ssym[m];
//          RansDecRenorm(&R[j%4], &cp);
//        }
//    }

    for (i = 0; cp < cp_end-8 && i < (out_sz&~7); i+=8) {
        for (j = 0; j < 8; j+=4) {
            RansState m0 = RansDecGet(&R[0], TF_SHIFT);
            RansState m1 = RansDecGet(&R[1], TF_SHIFT);
            out[i+j+0] = ssym[m0];
            out[i+j+1] = ssym[m1];

            R[0] = sfreq[m0] * (R[0] >> TF_SHIFT) + sbase[m0];
            R[1] = sfreq[m1] * (R[1] >> TF_SHIFT) + sbase[m1];

            RansState m2 = RansDecGet(&R[2], TF_SHIFT);
            RansState m3 = RansDecGet(&R[3], TF_SHIFT);

            RansDecRenorm(&R[0], &cp);
            RansDecRenorm(&R[1], &cp);

            R[2] = sfreq[m2] * (R[2] >> TF_SHIFT) + sbase[m2];
            R[3] = sfreq[m3] * (R[3] >> TF_SHIFT) + sbase[m3];

            RansDecRenorm(&R[2], &cp);
            RansDecRenorm(&R[3], &cp);

            out[i+j+2] = ssym[m2];
            out[i+j+3] = ssym[m3];
        }
    }

    // remainder
    for (; i < out_sz; i++) {
        RansState m = RansDecGet(&R[i%4], TF_SHIFT);
        R[i%4] = sfreq[m] * (R[i%4] >> TF_SHIFT) + sbase[m];
        out[i] = ssym[m];
        RansDecRenormSafe(&R[i%4], &cp, cp_end+8);
    }

    //fprintf(stderr, "    0 Decoded %d bytes\n", (int)(cp-in)); //c-size

    return out;

 err:
    free(out_free);
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5550, %rsp           # imm = 0x5550
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
cmpl $0x10, -0x14(%rbp)
jae 0x8f48c
movq $0x0, -0x8(%rbp)
jmp 0x8fa46
cmpl $0x7fffffff, -0x24(%rbp) # imm = 0x7FFFFFFF
jb 0x8f4a2
movq $0x0, -0x8(%rbp)
jmp 0x8fa46
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addq %rcx, %rax
addq $-0x8, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x8f4dd
movl -0x24(%rbp), %eax
movl %eax, %edi
callq 0x4650
movq %rax, -0x20(%rbp)
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x8f4f1
movq $0x0, -0x8(%rbp)
jmp 0x8fa46
leaq -0x5510(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x4250
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
leaq -0x5510(%rbp), %rdx
leaq -0x5514(%rbp), %rcx
callq 0x8fa60
movl %eax, -0x5518(%rbp)
cmpl $0x0, -0x5518(%rbp)
jne 0x8f533
jmp 0x8fa35
movl -0x5518(%rbp), %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
leaq -0x5510(%rbp), %rdi
movl -0x5514(%rbp), %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x8fb50
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x48(%rbp)
cmpl $0x100, -0x48(%rbp)     # imm = 0x100
jge 0x8f632
movslq -0x48(%rbp), %rax
cmpl $0x0, -0x5510(%rbp,%rax,4)
je 0x8f622
movslq -0x48(%rbp), %rax
movl -0x5510(%rbp,%rax,4), %eax
movl $0x1000, %ecx           # imm = 0x1000
subl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jbe 0x8f5a7
jmp 0x8fa35
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movslq -0x48(%rbp), %rcx
cmpl -0x5510(%rbp,%rcx,4), %eax
jae 0x8f611
movl -0x48(%rbp), %eax
movb %al, %cl
movl -0x50(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, %eax
movb %cl, -0x5110(%rbp,%rax)
movslq -0x48(%rbp), %rax
movl -0x5510(%rbp,%rax,4), %eax
movw %ax, %cx
movl -0x50(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, %eax
movw %cx, -0x2090(%rbp,%rax,2)
movl -0x50(%rbp), %eax
movw %ax, %cx
movl -0x50(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, %eax
movw %cx, -0x40d0(%rbp,%rax,2)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x8f5ae
movslq -0x48(%rbp), %rax
movl -0x5510(%rbp,%rax,4), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x8f624
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x8f56c
cmpl $0x1000, -0x4c(%rbp)    # imm = 0x1000
je 0x8f640
jmp 0x8fa35
movq -0x30(%rbp), %rax
addq $0x10, %rax
movq -0x40(%rbp), %rcx
addq $0x8, %rcx
cmpq %rcx, %rax
jbe 0x8f65a
jmp 0x8fa35
leaq -0x5530(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x8fbd0
cmpl $0x8000, -0x5530(%rbp)  # imm = 0x8000
jae 0x8f67b
jmp 0x8fa35
leaq -0x5530(%rbp), %rdi
addq $0x4, %rdi
leaq -0x30(%rbp), %rsi
callq 0x8fbd0
cmpl $0x8000, -0x552c(%rbp)  # imm = 0x8000
jae 0x8f6a0
jmp 0x8fa35
leaq -0x5530(%rbp), %rdi
addq $0x8, %rdi
leaq -0x30(%rbp), %rsi
callq 0x8fbd0
cmpl $0x8000, -0x5528(%rbp)  # imm = 0x8000
jae 0x8f6c5
jmp 0x8fa35
leaq -0x5530(%rbp), %rdi
addq $0xc, %rdi
leaq -0x30(%rbp), %rsi
callq 0x8fbd0
cmpl $0x8000, -0x5524(%rbp)  # imm = 0x8000
jae 0x8f6ea
jmp 0x8fa35
movl $0x0, -0x44(%rbp)
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rdx
addq $-0x8, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x5545(%rbp)
jae 0x8f71e
movl -0x44(%rbp), %eax
movl -0x24(%rbp), %ecx
andl $-0x8, %ecx
cmpl %ecx, %eax
setb %al
movb %al, -0x5545(%rbp)
movb -0x5545(%rbp), %al
testb $0x1, %al
jne 0x8f72d
jmp 0x8f950
movl $0x0, -0x48(%rbp)
cmpl $0x8, -0x48(%rbp)
jge 0x8f940
leaq -0x5530(%rbp), %rdi
movl $0xc, %esi
callq 0x8fc50
movl %eax, -0x5534(%rbp)
leaq -0x5530(%rbp), %rdi
addq $0x4, %rdi
movl $0xc, %esi
callq 0x8fc50
movl %eax, -0x5538(%rbp)
movl -0x5534(%rbp), %eax
movb -0x5110(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
addl -0x48(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x5538(%rbp), %eax
movb -0x5110(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
addl -0x48(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x5534(%rbp), %eax
movzwl -0x2090(%rbp,%rax,2), %eax
movl -0x5530(%rbp), %ecx
shrl $0xc, %ecx
imull %ecx, %eax
movl -0x5534(%rbp), %ecx
movzwl -0x40d0(%rbp,%rcx,2), %ecx
addl %ecx, %eax
movl %eax, -0x5530(%rbp)
movl -0x5538(%rbp), %eax
movzwl -0x2090(%rbp,%rax,2), %eax
movl -0x552c(%rbp), %ecx
shrl $0xc, %ecx
imull %ecx, %eax
movl -0x5538(%rbp), %ecx
movzwl -0x40d0(%rbp,%rcx,2), %ecx
addl %ecx, %eax
movl %eax, -0x552c(%rbp)
leaq -0x5530(%rbp), %rdi
addq $0x8, %rdi
movl $0xc, %esi
callq 0x8fc50
movl %eax, -0x553c(%rbp)
leaq -0x5530(%rbp), %rdi
addq $0xc, %rdi
movl $0xc, %esi
callq 0x8fc50
movl %eax, -0x5540(%rbp)
leaq -0x5530(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x8fc80
leaq -0x5530(%rbp), %rdi
addq $0x4, %rdi
leaq -0x30(%rbp), %rsi
callq 0x8fc80
movl -0x553c(%rbp), %eax
movzwl -0x2090(%rbp,%rax,2), %eax
movl -0x5528(%rbp), %ecx
shrl $0xc, %ecx
imull %ecx, %eax
movl -0x553c(%rbp), %ecx
movzwl -0x40d0(%rbp,%rcx,2), %ecx
addl %ecx, %eax
movl %eax, -0x5528(%rbp)
movl -0x5540(%rbp), %eax
movzwl -0x2090(%rbp,%rax,2), %eax
movl -0x5524(%rbp), %ecx
shrl $0xc, %ecx
imull %ecx, %eax
movl -0x5540(%rbp), %ecx
movzwl -0x40d0(%rbp,%rcx,2), %ecx
addl %ecx, %eax
movl %eax, -0x5524(%rbp)
leaq -0x5530(%rbp), %rdi
addq $0x8, %rdi
leaq -0x30(%rbp), %rsi
callq 0x8fc80
leaq -0x5530(%rbp), %rdi
addq $0xc, %rdi
leaq -0x30(%rbp), %rsi
callq 0x8fc80
movl -0x553c(%rbp), %eax
movb -0x5110(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
addl -0x48(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x5540(%rbp), %eax
movb -0x5110(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movl -0x44(%rbp), %ecx
addl -0x48(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x48(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x48(%rbp)
jmp 0x8f734
jmp 0x8f942
movl -0x44(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x44(%rbp)
jmp 0x8f6f1
jmp 0x8f952
movl -0x44(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x8fa2b
movl -0x44(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movslq %edx, %rax
leaq -0x5530(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
movl $0xc, %esi
callq 0x8fc50
movl %eax, -0x5544(%rbp)
movl -0x5544(%rbp), %eax
movzwl -0x2090(%rbp,%rax,2), %ecx
movl -0x44(%rbp), %eax
movl $0x4, %esi
cltd
idivl %esi
movslq %edx, %rax
movl -0x5530(%rbp,%rax,4), %eax
shrl $0xc, %eax
imull %eax, %ecx
movl -0x5544(%rbp), %eax
movzwl -0x40d0(%rbp,%rax,2), %eax
addl %eax, %ecx
movl -0x44(%rbp), %eax
movl $0x4, %esi
cltd
idivl %esi
movslq %edx, %rax
movl %ecx, -0x5530(%rbp,%rax,4)
movl -0x5544(%rbp), %eax
movb -0x5110(%rbp,%rax), %dl
movq -0x20(%rbp), %rax
movslq -0x44(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x44(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movslq %edx, %rax
leaq -0x5530(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rdx
addq $0x8, %rdx
leaq -0x30(%rbp), %rsi
callq 0x8fcf0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x8f952
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x8fa46
movq -0x38(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x5550, %rsp           # imm = 0x5550
popq %rbp
retq
nopw %cs:(%rax,%rax)