// File: attractivechaos[P]udb2/stb/./stb.h
// Function: stb_arith_state_create
stb_arith_symstate *stb_arith_state_create(int num_sym)
{
   stb_arith_symstate *s = (stb_arith_symstate *) malloc(sizeof(*s) + (num_sym-1) * sizeof(s->data[0])); 
   if (s) {
      int i, cf, cf_next, next;
      int start_freq, extra;
      s->num_sym = num_sym;
      s->pow2 = 4;
      while (s->pow2 < 15 && (1 << s->pow2) < 3*num_sym) {
         ++s->pow2;
      }
      start_freq = (1 << s->pow2) / num_sym;
      assert(start_freq >= 1);
      extra = (1 << s->pow2) % num_sym;
      // now set up the initial stats

      if (s->pow2 < POW2_LIMIT)
         next = 0;
      else
         next = 1;

      cf = cf_next = 0;
      for (i=0; i < extra; ++i) {
         s->data[i].cumfreq = cf;
         s->data[i].samples = next;
         cf += start_freq+1;
         cf_next += next;
      }
      for (; i < num_sym; ++i) {
         s->data[i].cumfreq = cf;
         s->data[i].samples = next;
         cf += start_freq;
         cf_next += next;
      }
      assert(cf == (1 << s->pow2));
      // now, how long should we go until we have 2 << s->pow2 samples?
      s->countdown = (2 << s->pow2) - cf - cf_next;
   }
   return s;
}

// Assembly:
subq $0x38, %rsp
movl %edi, 0x34(%rsp)
movl 0x34(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
addq $0x10, %rdi
callq 0x2260
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
je 0x1c732
movl 0x34(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, (%rax)
movq 0x28(%rsp), %rax
movl $0x4, 0x4(%rax)
movq 0x28(%rsp), %rcx
xorl %eax, %eax
cmpl $0xf, 0x4(%rcx)
movb %al, 0xf(%rsp)
jae 0x1c575
movq 0x28(%rsp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
imull $0x3, 0x34(%rsp), %ecx
cmpl %ecx, %eax
setl %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x1c57f
jmp 0x1c58f
movq 0x28(%rsp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
jmp 0x1c547
movq 0x28(%rsp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
cltd
idivl 0x34(%rsp)
movl %eax, 0x14(%rsp)
cmpl $0x1, 0x14(%rsp)
jl 0x1c5b0
jmp 0x1c5cf
leaq 0x3820(%rip), %rdi      # 0x1fdd7
leaq 0x2dcb(%rip), %rsi      # 0x1f389
movl $0x2be1, %edx           # imm = 0x2BE1
leaq 0x381d(%rip), %rcx      # 0x1fde7
callq 0x2140
movq 0x28(%rsp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
cltd
idivl 0x34(%rsp)
movl %edx, 0x10(%rsp)
movq 0x28(%rsp), %rax
cmpl $0xc, 0x4(%rax)
jae 0x1c5fc
movl $0x0, 0x18(%rsp)
jmp 0x1c604
movl $0x1, 0x18(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x10(%rsp), %eax
jge 0x1c67a
movl 0x20(%rsp), %eax
movw %ax, %dx
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rcx
movw %dx, 0xc(%rax,%rcx,4)
movl 0x18(%rsp), %eax
movw %ax, %dx
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rcx
movw %dx, 0xe(%rax,%rcx,4)
movl 0x14(%rsp), %eax
addl $0x1, %eax
addl 0x20(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x18(%rsp), %eax
addl 0x1c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x1c61c
jmp 0x1c67c
movl 0x24(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x1c6d7
movl 0x20(%rsp), %eax
movw %ax, %dx
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rcx
movw %dx, 0xc(%rax,%rcx,4)
movl 0x18(%rsp), %eax
movw %ax, %dx
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rcx
movw %dx, 0xe(%rax,%rcx,4)
movl 0x14(%rsp), %eax
addl 0x20(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x18(%rsp), %eax
addl 0x1c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x1c67c
movl 0x20(%rsp), %eax
movq 0x28(%rsp), %rcx
movl 0x4(%rcx), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jne 0x1c6f2
jmp 0x1c711
leaq 0x371e(%rip), %rdi      # 0x1fe17
leaq 0x2c89(%rip), %rsi      # 0x1f389
movl $0x2bf7, %edx           # imm = 0x2BF7
leaq 0x36db(%rip), %rcx      # 0x1fde7
callq 0x2140
movq 0x28(%rsp), %rax
movl 0x4(%rax), %ecx
movl $0x2, %eax
shll %cl, %eax
movl %eax, %ecx
subl 0x20(%rsp), %ecx
subl 0x1c(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x28(%rsp), %rax
addq $0x38, %rsp
retq
nopl (%rax)