// File: /workspace/github_c_makefile_star2_O0/smilingthax[P]libfontembed/embed_pdf.c
// Function: emb_pdf_fw_cidwidths
EMB_PDF_FONTWIDTHS *emb_pdf_fw_cidwidths(const BITSET glyphs,int len,int default_width,int (*getGlyphWidth)(void *context,int gid),void *context) // {{{ glyphs==NULL -> output all
{
  assert(getGlyphWidth);

  FREQUENT *freq=NULL;
  if (default_width<0) {
    freq=frequent_new(3);
  }

  int iA,b,c;
  int size=0,in_region=0; // current number of elements in after region start

  // first pass: find continuous regions, calculate needed size, estimate dw
  for (iA=0,b=0,c=1;iA<len;iA++,c<<=1) {
    if (!c) {
      b++;
      c=1;
    }
    if ( (!glyphs)||(glyphs[b]&c) ) {
      if (freq) {
        const int w=(*getGlyphWidth)(context,iA);
        frequent_add(freq,w);
      }
      if (in_region) {
        in_region++;
      } else { // start new region
        size+=2; // len c
        in_region=1;
      }
    } else { // region end
      size+=in_region;
      in_region=0;
    }
  }
  size+=in_region;

  if (freq) {
    default_width=frequent_get(freq,0);
    free(freq);
  }
  assert(default_width>0);

  // now create the array
  EMB_PDF_FONTWIDTHS *ret=emb_pdf_fw_new(size+1);
  if (!ret) {
    return NULL;
  }
  ret->default_width=default_width;
  ret->warray=ret->data;

  // second pass
  in_region=0;
  size=0;
  int *rlen=0; // position of current len field  (only valid if in_region!=0)
  for (iA=0,b=0,c=1;iA<len;iA++,c<<=1) {
    if (!c) {
      b++;
      c=1;
    }
    if ( (!glyphs)||(glyphs[b]&c) ) {
      const int w=(*getGlyphWidth)(context,iA);
      if (in_region>0) { // in array region
        if ( (w==default_width)&&(ret->warray[size-1]==default_width) ) { // omit this and prev entry
          size--;
          *rlen=in_region-1; // !=0, as it does not start with >default_width
          in_region=0; // end region, immediate restart will take just the same amount of space
        } else if ( (in_region>=4)&&
                    (ret->warray[size-1]==w)&&(ret->warray[size-2]==w)&&
                    (ret->warray[size-3]==w)&&(ret->warray[size-4]==w) ) {
          // five in a row.  c1 c2 w [l c] is equally short and can be extended (-len c1 w)  [w/ cost of array-region restart]
          if (in_region==4) { // completely replace
            size-=6;
          } else { // first end previous region
            size-=4;
            *rlen=in_region-4;
          }
          in_region=-4; // start range region instead
          rlen=&ret->warray[size++];
          ret->warray[size++]=iA-4;
          ret->warray[size++]=w;
        } else { // just add
          in_region++;
          ret->warray[size++]=w;
        }
        continue;
      } else if (in_region<0) { // in range region
        if (ret->warray[size-1]==w) {
          in_region--; // just add
          continue;
        }
        *rlen=in_region; // end
        in_region=0;
      }
      if (w!=default_width) { // start new array region
        in_region=1;
        rlen=&ret->warray[size++];
        ret->warray[size++]=iA; // c
        ret->warray[size++]=w;
      }
    } else if (in_region) {
      // TODO? no need to stop range region? } else if (in_region<0) { inregion--; }
      *rlen=in_region;
      in_region=0;
    }
  }
  if (in_region) {
    *rlen=in_region;
  }
  ret->warray[size]=0; // terminator
  return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x8993
jmp 0x89b2
leaq 0x67f5(%rip), %rdi      # 0xf18f
leaq 0x66cf(%rip), %rsi      # 0xf070
movl $0xdd, %edx
leaq 0x67f0(%rip), %rcx      # 0xf19d
callq 0x2100
movq $0x0, -0x30(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x89ce
movl $0x3, %edi
callq 0xa2e0
movq %rax, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8a9c
cmpl $0x0, -0x3c(%rbp)
jne 0x8a13
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x8a2d
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl -0x3c(%rbp), %eax
cmpl $0x0, %eax
je 0x8a74
cmpq $0x0, -0x30(%rbp)
je 0x8a51
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %esi
callq *%rax
movl %eax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x48(%rbp), %rsi
callq 0xa3e0
cmpl $0x0, -0x44(%rbp)
je 0x8a62
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x8a72
movl -0x40(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
jmp 0x8a84
movl -0x44(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
jmp 0x8a86
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x3c(%rbp)
jmp 0x89f1
movl -0x44(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x8ac3
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0xa520
movl %eax, -0x18(%rbp)
movq -0x30(%rbp), %rdi
callq 0x21b0
cmpl $0x0, -0x18(%rbp)
jle 0x8acb
jmp 0x8aea
leaq 0x672a(%rip), %rdi      # 0xf1fc
leaq 0x6597(%rip), %rsi      # 0xf070
movl $0x103, %edx            # imm = 0x103
leaq 0x66b8(%rip), %rcx      # 0xf19d
callq 0x2100
movl -0x40(%rbp), %edi
addl $0x1, %edi
callq 0x88c0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x8b0d
movq $0x0, -0x8(%rbp)
jmp 0x8e26
movl -0x18(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x50(%rbp), %rcx
addq $0x20, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8dfc
cmpl $0x0, -0x3c(%rbp)
jne 0x8b74
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x8b92
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl -0x3c(%rbp), %eax
cmpl $0x0, %eax
je 0x8dcc
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %esi
callq *%rax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x44(%rbp)
jle 0x8d23
movl -0x5c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x8bee
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x18(%rbp), %eax
jne 0x8bee
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x44(%rbp)
jmp 0x8d1e
cmpl $0x4, -0x44(%rbp)
jl 0x8cf7
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x5c(%rbp), %eax
jne 0x8cf7
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x5c(%rbp), %eax
jne 0x8cf7
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
subl $0x3, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x5c(%rbp), %eax
jne 0x8cf7
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
subl $0x4, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x5c(%rbp), %eax
jne 0x8cf7
cmpl $0x4, -0x44(%rbp)
jne 0x8c7d
movl -0x40(%rbp), %eax
subl $0x6, %eax
movl %eax, -0x40(%rbp)
jmp 0x8c92
movl -0x40(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x40(%rbp)
movl -0x44(%rbp), %ecx
subl $0x4, %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl $0xfffffffc, -0x44(%rbp) # imm = 0xFFFFFFFC
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x40(%rbp)
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl -0x34(%rbp), %edx
subl $0x4, %edx
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x40(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x5c(%rbp), %edx
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x40(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x8d1c
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x5c(%rbp), %edx
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x40(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x8d1e
jmp 0x8de6
cmpl $0x0, -0x44(%rbp)
jge 0x8d60
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x5c(%rbp), %eax
jne 0x8d50
movl -0x44(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x8de6
movl -0x44(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x44(%rbp)
jmp 0x8d62
movl -0x5c(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0x8dca
movl $0x1, -0x44(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x40(%rbp)
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl -0x34(%rbp), %edx
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x40(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x5c(%rbp), %edx
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x40(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x8de4
cmpl $0x0, -0x44(%rbp)
je 0x8de2
movl -0x44(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x44(%rbp)
jmp 0x8de4
jmp 0x8de6
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x3c(%rbp)
jmp 0x8b52
cmpl $0x0, -0x44(%rbp)
je 0x8e0b
movl -0x44(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x40(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq