// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/htscodecs/htscodecs/rANS_static16_int.h
// Function: encode_freq1
static inline int encode_freq1(uint8_t *in, uint32_t in_size, int Nway,
                               RansEncSymbol syms[256][256], uint8_t **cp_p) {
    int i, j, z;
    uint8_t *out = *cp_p, *cp = out;

    // Compute O1 frequency statistics
    uint32_t (*F)[256] = htscodecs_tls_calloc(256, (sizeof(*F)));
    if (!F)
        return -1;
    uint32_t T[256+MAGIC] = {0};
    int isz4 = in_size/Nway;
    if (hist1_4(in, in_size, F, T) < 0)
        goto err;
    for (z = 1; z < Nway; z++)
        F[0][in[z*isz4]]++;
    T[0]+=Nway-1;

    // Potential fix for the wrap-around bug in AVX2 O1 encoder with shift=12.
    // This occurs when we have one single symbol, giving freq=4096.
    // We fix it elsewhere for now by looking for the wrap-around.
    // See "if (1)" statements in the AVX2 code, which is an alternative
    // to the "if (0)" here.
//    if (0) {
//      int x = -1, y = -1;
//      int n1, n2;
//      for (x = 0; x < 256; x++) {
//          n1 = n2 = -1;
//          for (y = 0; y < 256; y++) {
//              if (F[x][y])
//                  n2 = n1, n1 = y;
//          }
//          if (n2!=-1 || n1 == -1)
//              continue;
//
//          for (y = 0; y < 256; y++)
//              if (!F[x][y])
//                  break;
//          assert(y<256);
//          F[x][y]++;
//          F[0][y]++; T[y]++; F0[y]=1;
//          F[0][x]++; T[x]++; F0[x]=1;
//      }
//    }

    // Encode the order-0 stats
    int tmp_T0 = T[0];
    T[0] = 1;
    *cp++ = 0; // marker for uncompressed (may change)
    cp += encode_alphabet(cp, T);
    T[0] = tmp_T0;

    // Decide between 10-bit and 12-bit freqs.
    // Fills out S[] to hold the new scaled maximum value.
    uint32_t S[256] = {0};
    int shift = rans_compute_shift(T, F, T, S);

    // Normalise so T[i] == TOTFREQ_O1
    for (i = 0; i < 256; i++) {
        unsigned int x;

        if (T[i] == 0)
            continue;

        uint32_t max_val = S[i];
        if (shift == TF_SHIFT_O1_FAST && max_val > TOTFREQ_O1_FAST)
            max_val = TOTFREQ_O1_FAST;

        if (normalise_freq(F[i], T[i], max_val) < 0)
            goto err;
        T[i]=max_val;

        // Encode our frequency array
        cp += encode_freq_d(cp, T, F[i]);

        normalise_freq_shift(F[i], T[i], 1<<shift); T[i]=1<<shift;

        // Initialise Rans Symbol struct too.
        uint32_t *F_i_ = F[i];
        for (x = j = 0; j < 256; j++) {
            RansEncSymbolInit(&syms[i][j], x, F_i_[j], shift);
            x += F_i_[j];
        }
    }

    *out = shift<<4;
    if (cp - out > 1000) {
        uint8_t *op = out;
        // try rans0 compression of header
        unsigned int u_freq_sz = cp-(op+1);
        unsigned int c_freq_sz;
        unsigned char *c_freq = rans_compress_O0_4x16(op+1, u_freq_sz, NULL,
                                                      &c_freq_sz);
        if (c_freq && c_freq_sz + 6 < cp-op) {
            *op++ |= 1; // compressed
            op += var_put_u32(op, NULL, u_freq_sz);
            op += var_put_u32(op, NULL, c_freq_sz);
            memcpy(op, c_freq, c_freq_sz);
            cp = op+c_freq_sz;
        }
        free(c_freq);
    }

    *cp_p = cp;
    htscodecs_tls_free(F);
    return shift;

 err:
    htscodecs_tls_free(F);
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x8b0, %rsp            # imm = 0x8B0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x100, %edi            # imm = 0x100
movl $0x400, %esi            # imm = 0x400
callq 0xce8a0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x93476
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x938bd
leaq -0x470(%rbp), %rdi
xorl %esi, %esi
movl $0x420, %edx            # imm = 0x420
callq 0x4250
movl -0x14(%rbp), %eax
xorl %edx, %edx
divl -0x18(%rbp)
movl %eax, -0x474(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x50(%rbp), %rdx
leaq -0x470(%rbp), %rcx
callq 0x938d0
cmpl $0x0, %eax
jge 0x934b8
jmp 0x938ad
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x934f4
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movl -0x34(%rbp), %edx
imull -0x474(%rbp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x934bf
movl -0x18(%rbp), %eax
subl $0x1, %eax
addl -0x470(%rbp), %eax
movl %eax, -0x470(%rbp)
movl -0x470(%rbp), %eax
movl %eax, -0x478(%rbp)
movl $0x1, -0x470(%rbp)
movq -0x48(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x48(%rbp)
movb $0x0, (%rax)
movq -0x48(%rbp), %rdi
leaq -0x470(%rbp), %rsi
callq 0x92a80
movl %eax, %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl -0x478(%rbp), %eax
movl %eax, -0x470(%rbp)
leaq -0x880(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x4250
leaq -0x470(%rbp), %rdi
movq -0x50(%rbp), %rsi
leaq -0x470(%rbp), %rdx
leaq -0x880(%rbp), %rcx
callq 0x8fd70
movl %eax, -0x884(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x100, -0x2c(%rbp)     # imm = 0x100
jge 0x9373b
movslq -0x2c(%rbp), %rax
cmpl $0x0, -0x470(%rbp,%rax,4)
jne 0x935b8
jmp 0x9372d
movslq -0x2c(%rbp), %rax
movl -0x880(%rbp,%rax,4), %eax
movl %eax, -0x88c(%rbp)
cmpl $0xa, -0x884(%rbp)
jne 0x935e8
cmpl $0x400, -0x88c(%rbp)    # imm = 0x400
jbe 0x935e8
movl $0x400, -0x88c(%rbp)    # imm = 0x400
movq -0x50(%rbp), %rdi
movslq -0x2c(%rbp), %rax
shlq $0xa, %rax
addq %rax, %rdi
movslq -0x2c(%rbp), %rax
movl -0x470(%rbp,%rax,4), %esi
movl -0x88c(%rbp), %edx
callq 0x8ee40
cmpl $0x0, %eax
jge 0x93617
jmp 0x938ad
movl -0x88c(%rbp), %ecx
movslq -0x2c(%rbp), %rax
movl %ecx, -0x470(%rbp,%rax,4)
movq -0x48(%rbp), %rdi
leaq -0x470(%rbp), %rsi
movq -0x50(%rbp), %rdx
movslq -0x2c(%rbp), %rax
shlq $0xa, %rax
addq %rax, %rdx
callq 0x93d90
movl %eax, %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rdi
movslq -0x2c(%rbp), %rax
shlq $0xa, %rax
addq %rax, %rdi
movslq -0x2c(%rbp), %rax
movl -0x470(%rbp,%rax,4), %esi
movl -0x884(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
callq 0x8fb50
movl -0x884(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movslq -0x2c(%rbp), %rax
movl %ecx, -0x470(%rbp,%rax,4)
movq -0x50(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, -0x898(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x888(%rbp)
cmpl $0x100, -0x30(%rbp)     # imm = 0x100
jge 0x9372b
movq -0x20(%rbp), %rdi
movslq -0x2c(%rbp), %rax
shlq $0xc, %rax
addq %rax, %rdi
movslq -0x30(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movl -0x888(%rbp), %esi
movq -0x898(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0x884(%rbp), %ecx
callq 0x8f160
movq -0x898(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x888(%rbp), %eax
movl %eax, -0x888(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x936c4
jmp 0x9372d
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x93598
movl -0x884(%rbp), %eax
shll $0x4, %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq %rcx, %rax
cmpq $0x3e8, %rax            # imm = 0x3E8
jle 0x9388e
movq -0x40(%rbp), %rax
movq %rax, -0x8a0(%rbp)
movq -0x48(%rbp), %rax
movq -0x8a0(%rbp), %rcx
addq $0x1, %rcx
subq %rcx, %rax
movl %eax, -0x8a4(%rbp)
movq -0x8a0(%rbp), %rdi
addq $0x1, %rdi
movl -0x8a4(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
leaq -0x8a8(%rbp), %rcx
callq 0x8e440
movq %rax, -0x8b0(%rbp)
cmpq $0x0, -0x8b0(%rbp)
je 0x93882
movl -0x8a8(%rbp), %eax
addl $0x6, %eax
movl %eax, %eax
movq -0x48(%rbp), %rcx
movq -0x8a0(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jge 0x93882
movq -0x8a0(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x8a0(%rbp)
movzbl (%rax), %ecx
orl $0x1, %ecx
movb %cl, (%rax)
movq -0x8a0(%rbp), %rdi
movl -0x8a4(%rbp), %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x91410
movl %eax, %ecx
movq -0x8a0(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x8a0(%rbp)
movq -0x8a0(%rbp), %rdi
movl -0x8a8(%rbp), %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x91410
movl %eax, %ecx
movq -0x8a0(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x8a0(%rbp)
movq -0x8a0(%rbp), %rdi
movq -0x8b0(%rbp), %rsi
movl -0x8a8(%rbp), %eax
movl %eax, %edx
callq 0x43b0
movq -0x8a0(%rbp), %rax
movl -0x8a8(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x8b0(%rbp), %rdi
callq 0x45a0
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rdi
callq 0xce8f0
movl -0x884(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x938bd
movq -0x50(%rbp), %rdi
callq 0xce8f0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x8b0, %rsp            # imm = 0x8B0
popq %rbp
retq
nopl (%rax)