// File: sbingner[P]kerneldec/lzfse/src/lzfse_fse.c
// Function: fse_init_decoder_table
int fse_init_decoder_table(int nstates, int nsymbols,
                           const uint16_t *__restrict freq,
                           int32_t *__restrict t) {
  assert(nsymbols <= 256);
  assert(fse_check_freq(freq, nsymbols, nstates) == 0);
  int n_clz = __builtin_clz(nstates);
  int sum_of_freq = 0;
  for (int i = 0; i < nsymbols; i++) {
    int f = (int)freq[i];
    if (f == 0)
      continue; // skip this symbol, no occurrences

    sum_of_freq += f;

    if (sum_of_freq > nstates) {
      return -1;
    }

    int k =
        __builtin_clz(f) - n_clz; // shift needed to ensure N <= (F<<K) < 2*N
    int j0 = ((2 * nstates) >> k) - f;

    // Initialize all states S reached by this symbol: OFFSET <= S < OFFSET + F
    for (int j = 0; j < f; j++) {
      fse_decoder_entry e;

      e.symbol = (uint8_t)i;
      if (j < j0) {
        e.k = (int8_t)k;
        e.delta = (int16_t)(((f + j) << k) - nstates);
      } else {
        e.k = (int8_t)(k - 1);
        e.delta = (int16_t)((j - j0) << (k - 1));
      }

      memcpy(t, &e, sizeof(e));
      t++;
    }
  }

  return 0; // OK
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl %edi, -0x2c(%rbp)
movl %esi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
cmpl $0x100, -0x30(%rbp)     # imm = 0x100
jg 0xdbe1
jmp 0xdc00
leaq 0x2f00(%rip), %rdi      # 0x10ae8
leaq 0x2f09(%rip), %rsi      # 0x10af8
movl $0x3c, %edx
leaq 0x2f0d(%rip), %rcx      # 0x10b08
callq 0x3070
movq -0x38(%rbp), %rdx
movslq -0x30(%rbp), %rcx
movslq -0x2c(%rbp), %rax
movq %rdx, -0x8(%rbp)
movq %rcx, -0x10(%rbp)
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movslq -0x24(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0xdc50
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xdc27
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rsi, %rdx
cmoval %ecx, %eax
cmpl $0x0, %eax
jne 0xdc6c
jmp 0xdc8b
leaq 0x2ee7(%rip), %rdi      # 0x10b5a
leaq 0x2e7e(%rip), %rsi      # 0x10af8
movl $0x3d, %edx
leaq 0x2e82(%rip), %rcx      # 0x10b08
callq 0x3070
movl -0x2c(%rbp), %eax
bsrl %eax, %eax
xorl $0x1f, %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0xdd89
movq -0x38(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jne 0xdccb
jmp 0xdd7b
movl -0x50(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jle 0xdce8
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
jmp 0xdd90
movl -0x50(%rbp), %eax
bsrl %eax, %eax
xorl $0x1f, %eax
subl -0x44(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x2c(%rbp), %eax
shll %eax
movl -0x54(%rbp), %ecx
sarl %cl, %eax
subl -0x50(%rbp), %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0xdd79
movl -0x4c(%rbp), %eax
movb %al, -0x5f(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0xdd3e
movl -0x54(%rbp), %eax
movb %al, -0x60(%rbp)
movl -0x50(%rbp), %eax
addl -0x5c(%rbp), %eax
movl -0x54(%rbp), %ecx
shll %cl, %eax
subl -0x2c(%rbp), %eax
movw %ax, -0x5e(%rbp)
jmp 0xdd59
movl -0x54(%rbp), %eax
subl $0x1, %eax
movb %al, -0x60(%rbp)
movl -0x5c(%rbp), %eax
subl -0x58(%rbp), %eax
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
shll %cl, %eax
movw %ax, -0x5e(%rbp)
movq -0x40(%rbp), %rax
movl -0x60(%rbp), %ecx
movl %ecx, (%rax)
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xdd0e
jmp 0xdd7b
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xdca5
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)