// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
testq %rcx, %rcx
je 0x66d0
movl %esi, %ebx
movq %rdi, %r15
xorl %r13d, %r13d
testl %edx, %edx
movq %rdx, 0x10(%rsp)
js 0x6461
xorl %eax, %eax
jmp 0x6469
pushq $0x3
popq %rdi
callq 0x7590
testl %ebx, %ebx
cmovlel %r13d, %ebx
movl %ebx, 0x1c(%rsp)
pushq $0x1
popq %rbx
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %r15, 0x8(%rsp)
cmpl %r13d, 0x1c(%rsp)
je 0x64ed
cmpl $0x1, %ebx
adcl $0x0, %ebp
cmpl $0x1, %ebx
adcl $0x0, %ebx
testq %r15, %r15
je 0x64a3
movl %ebp, %edx
testl %ebx, (%r15,%rdx,4)
je 0x64e0
testq %rax, %rax
movq %rax, %r15
je 0x64c2
movq 0x30(%rsp), %rdi
movl %r13d, %esi
callq *0x28(%rsp)
movslq %eax, %rsi
movq %r15, %rdi
callq 0x7600
testl %r14d, %r14d
je 0x64cc
incl %r14d
jmp 0x64d6
addl $0x2, %r12d
pushq $0x1
popq %rdx
movl %edx, %r14d
movq %r15, %rax
movq 0x8(%rsp), %r15
jmp 0x64e6
addl %r14d, %r12d
xorl %r14d, %r14d
incl %r13d
addl %ebx, %ebx
jmp 0x6483
testq %rax, %rax
movq 0x10(%rsp), %rbx
je 0x650f
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
callq 0x767b
movq %rax, %rbx
movq %r15, %rdi
callq 0x21b0
testl %ebx, %ebx
jle 0x66ef
leal (%r14,%r12), %edi
incl %edi
callq 0x63a5
movq %rbx, 0x10(%rsp)
movl %ebx, 0x10(%rax)
leaq 0x20(%rax), %rcx
movq %rax, 0x20(%rsp)
movq %rcx, 0x18(%rax)
pushq $0x1
popq %r14
xorl %esi, %esi
xorl %edi, %edi
xorl %r15d, %r15d
xorl %r13d, %r13d
movq $0x0, (%rsp)
movq 0x8(%rsp), %rbx
movslq %r15d, %rbp
cmpl %esi, 0x1c(%rsp)
je 0x66a8
cmpl $0x1, %r14d
adcl $0x0, %edi
cmpl $0x1, %r14d
adcl $0x0, %r14d
testq %rbx, %rbx
je 0x657f
movl %edi, %eax
testl %r14d, (%rbx,%rax,4)
je 0x6630
movl %edi, 0x8(%rsp)
movq 0x30(%rsp), %rdi
movq %rsi, %r12
callq *0x28(%rsp)
testl %r13d, %r13d
jle 0x6613
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
cmpl 0x10(%rsp), %eax
jne 0x65c9
movq 0x10(%rsp), %rdx
cmpl %edx, -0x4(%rcx,%rbp,4)
jne 0x65c9
decl %r15d
decl %r13d
movq (%rsp), %rax
movl %r13d, (%rax)
xorl %ecx, %ecx
movl 0x8(%rsp), %edi
movq %r12, %rsi
jmp 0x666f
cmpl $0x4, %r13d
movl 0x8(%rsp), %edi
movq %r12, %rsi
jb 0x6604
cmpl %eax, -0x4(%rcx,%rbp,4)
jne 0x6604
cmpl %eax, -0x8(%rcx,%rbp,4)
jne 0x6604
cmpl %eax, -0xc(%rcx,%rbp,4)
jne 0x6604
cmpl %eax, -0x10(%rcx,%rbp,4)
jne 0x6604
cmpl $0x4, %r13d
jne 0x667c
addl $-0x6, %r15d
movslq %r15d, %rbp
jmp 0x668f
incl %r13d
leal 0x1(%rbp), %r15d
movl %eax, (%rcx,%rbp,4)
movl %r13d, %ecx
jmp 0x666f
movl 0x8(%rsp), %edi
movq %r12, %rsi
jns 0x6647
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
cmpl %eax, -0x4(%rcx,%rbp,4)
jne 0x6640
decl %r13d
jmp 0x660e
xorl %ecx, %ecx
testl %r13d, %r13d
je 0x666f
movq (%rsp), %rax
movl %r13d, (%rax)
jmp 0x666f
movq (%rsp), %rcx
movl %r13d, (%rcx)
xorl %ecx, %ecx
cmpl 0x10(%rsp), %eax
je 0x666f
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
leaq (%rcx,%rbp,4), %rdx
movl %esi, 0x4(%rdx)
addl $0x3, %ebp
movq %rdx, (%rsp)
movl %eax, 0x8(%rdx)
movl %ebp, %r15d
pushq $0x1
popq %rcx
incl %esi
addl %r14d, %r14d
movl %ecx, %r13d
jmp 0x6552
leal -0x4(%rbp), %r15d
addq $-0x4, %rbp
addl $-0x4, %r13d
movq (%rsp), %rdx
movl %r13d, (%rdx)
leaq (%rcx,%rbp,4), %rdx
leal -0x4(%rsi), %ecx
movl %ecx, 0x4(%rdx)
addl $0x3, %r15d
movq %rdx, (%rsp)
movl %eax, 0x8(%rdx)
pushq $-0x4
jmp 0x666e
testl %r13d, %r13d
je 0x66b4
movq (%rsp), %rax
movl %r13d, (%rax)
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rcx
andl $0x0, (%rcx,%rbp,4)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4a70(%rip), %rdi      # 0xb147
leaq 0x494a(%rip), %rsi      # 0xb028
leaq 0x4a70(%rip), %rcx      # 0xb155
movl $0xdd, %edx
callq 0x20f0
leaq 0x4abe(%rip), %rdi      # 0xb1b4
leaq 0x492b(%rip), %rsi      # 0xb028
leaq 0x4a51(%rip), %rcx      # 0xb155
movl $0x103, %edx            # imm = 0x103
callq 0x20f0