// File: /workspace/github_c_makefile_star2_O3/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)
movl %esi, 0x4(%rsp)
movq %rcx, 0x28(%rsp)
testq %rcx, %rcx
je 0x6f5c
movl %edx, %ebx
xorl %r15d, %r15d
testl %edx, %edx
movq %rdi, 0x10(%rsp)
js 0x6cd8
xorl %r12d, %r12d
jmp 0x6cea
movl $0x3, %edi
callq 0x7d40
movq 0x10(%rsp), %rdi
movq %rax, %r12
cmpl $0x0, 0x4(%rsp)
jle 0x6d70
movq %rbx, 0x18(%rsp)
movl $0x1, %ebp
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpl $0x1, %ebp
adcl $0x0, %r14d
cmpl $0x1, %ebp
adcl $0x0, %ebp
testq %rdi, %rdi
je 0x6d20
movl %r14d, %eax
testl %ebp, (%rdi,%rax,4)
je 0x6d56
testq %r12, %r12
je 0x6d40
movq 0x30(%rsp), %rdi
movl %ebx, %esi
callq *0x28(%rsp)
movslq %eax, %rsi
movq %r12, %rdi
callq 0x7db6
movq 0x10(%rsp), %rdi
testl %r13d, %r13d
je 0x6d4a
incl %r13d
jmp 0x6d5c
addl $0x2, %r15d
movl $0x1, %r13d
jmp 0x6d5c
addl %r13d, %r15d
xorl %r13d, %r13d
incl %ebx
addl %ebp, %ebp
cmpl %ebx, 0x4(%rsp)
jne 0x6d06
incl %r13d
movq 0x18(%rsp), %rbx
jmp 0x6d76
movl $0x1, %r13d
testq %r12, %r12
je 0x6d90
movq %r12, %rdi
xorl %esi, %esi
callq 0x7e4b
movq %rax, %rbx
movq %r12, %rdi
callq 0x21a0
testl %ebx, %ebx
jle 0x6f7b
addl %r15d, %r13d
movl %r13d, %edi
callq 0x6c5c
leaq 0x20(%rax), %rcx
xorl %ebp, %ebp
movl %ebx, 0x10(%rax)
movq %rax, 0x20(%rsp)
movq %rcx, 0x18(%rax)
cmpl $0x0, 0x4(%rsp)
jle 0x6f3b
movq %rbx, 0x18(%rsp)
movl $0x1, %ebx
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x10(%rsp), %rdi
cmpl $0x1, %ebx
adcl $0x0, %r13d
cmpl $0x1, %ebx
adcl $0x0, %ebx
testq %rdi, %rdi
je 0x6dff
movl %r13d, %eax
testl %ebx, (%rdi,%rax,4)
je 0x6ea5
movq 0x30(%rsp), %rdi
movl %r12d, %esi
callq *0x28(%rsp)
testl %ebp, %ebp
jle 0x6e88
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
cmpl 0x18(%rsp), %eax
jne 0x6e44
movslq %r14d, %rdx
movq 0x18(%rsp), %rsi
cmpl %esi, -0x4(%rcx,%rdx,4)
jne 0x6e44
decl %r14d
decl %ebp
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
jmp 0x6ee8
movslq %r14d, %rdx
cmpl $0x4, %ebp
movq 0x10(%rsp), %rdi
jb 0x6e7e
cmpl %eax, -0x4(%rcx,%rdx,4)
jne 0x6e7e
cmpl %eax, -0x8(%rcx,%rdx,4)
jne 0x6e7e
cmpl %eax, -0xc(%rcx,%rdx,4)
jne 0x6e7e
cmpl %eax, -0x10(%rcx,%rdx,4)
jne 0x6e7e
cmpl $0x4, %ebp
jne 0x6efa
addl $-0x6, %r14d
movslq %r14d, %rdx
jmp 0x6f0c
incl %ebp
incl %r14d
movl %eax, (%rcx,%rdx,4)
jmp 0x6ee8
movq 0x10(%rsp), %rdi
jns 0x6ebb
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq %r14d, %rdx
cmpl %eax, -0x4(%rcx,%rdx,4)
jne 0x6eb4
decl %ebp
jmp 0x6ee8
testl %ebp, %ebp
je 0x6eb0
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
xorl %ebp, %ebp
jmp 0x6ee8
movq 0x8(%rsp), %rcx
movl %ebp, (%rcx)
xorl %ebp, %ebp
cmpl 0x18(%rsp), %eax
je 0x6ee8
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq %r14d, %rdx
leaq (%rcx,%rdx,4), %rdx
movl %r12d, 0x4(%rdx)
addl $0x3, %r14d
movq %rdx, 0x8(%rsp)
movl %eax, 0x8(%rdx)
movl $0x1, %ebp
incl %r12d
addl %ebx, %ebx
cmpl %r12d, 0x4(%rsp)
jne 0x6de1
jmp 0x6f2b
addl $-0x4, %r14d
addq $-0x4, %rdx
addl $-0x4, %ebp
movq 0x8(%rsp), %rsi
movl %ebp, (%rsi)
leaq (%rcx,%rdx,4), %rdx
leal -0x4(%r12), %ecx
movl %ecx, 0x4(%rdx)
addl $0x3, %r14d
movq %rdx, 0x8(%rsp)
movl %eax, 0x8(%rdx)
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
jmp 0x6ee8
movslq %r14d, %rdx
testl %ebp, %ebp
je 0x6f3d
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
jmp 0x6f3d
xorl %edx, %edx
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rcx
movl $0x0, (%rcx,%rdx,4)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x41e4(%rip), %rdi      # 0xb147
leaq 0x40be(%rip), %rsi      # 0xb028
leaq 0x41e4(%rip), %rcx      # 0xb155
movl $0xdd, %edx
callq 0x20e0
leaq 0x4232(%rip), %rdi      # 0xb1b4
leaq 0x409f(%rip), %rsi      # 0xb028
leaq 0x41c5(%rip), %rcx      # 0xb155
movl $0x103, %edx            # imm = 0x103
callq 0x20e0