// File: /workspace/github_c_makefile_star2_O1/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 0x6fec
movl %edx, %ebx
xorl %r15d, %r15d
testl %edx, %edx
movq %rdi, 0x18(%rsp)
js 0x6d48
xorl %r12d, %r12d
jmp 0x6d5a
movl $0x3, %edi
callq 0x7dd4
movq 0x18(%rsp), %rdi
movq %rax, %r12
cmpl $0x0, 0x4(%rsp)
jle 0x6de0
movq %rbx, 0x20(%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 0x6d90
movl %r14d, %eax
testl %ebp, (%rdi,%rax,4)
je 0x6dc6
testq %r12, %r12
je 0x6db0
movq 0x30(%rsp), %rdi
movl %ebx, %esi
callq *0x28(%rsp)
movslq %eax, %rsi
movq %r12, %rdi
callq 0x7e55
movq 0x18(%rsp), %rdi
testl %r13d, %r13d
je 0x6dba
incl %r13d
jmp 0x6dcc
addl $0x2, %r15d
movl $0x1, %r13d
jmp 0x6dcc
addl %r13d, %r15d
xorl %r13d, %r13d
incl %ebx
addl %ebp, %ebp
cmpl %ebx, 0x4(%rsp)
jne 0x6d76
incl %r13d
movq 0x20(%rsp), %rbx
jmp 0x6de6
movl $0x1, %r13d
testq %r12, %r12
je 0x6e00
movq %r12, %rdi
xorl %esi, %esi
callq 0x7ef1
movq %rax, %rbx
movq %r12, %rdi
callq 0x21a0
testl %ebx, %ebx
jle 0x700b
addl %r15d, %r13d
movl %r13d, %edi
callq 0x6ccc
movl %ebx, 0x10(%rax)
leaq 0x20(%rax), %rcx
movq %rax, 0x10(%rsp)
movq %rcx, 0x18(%rax)
cmpl $0x0, 0x4(%rsp)
jle 0x6fb5
movq %rbx, 0x20(%rsp)
movl $0x1, %ebx
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x18(%rsp), %rsi
cmpl $0x1, %ebx
adcl $0x0, %r13d
cmpl $0x1, %ebx
adcl $0x0, %ebx
testq %rsi, %rsi
je 0x6e6f
movl %r13d, %eax
testl %ebx, (%rsi,%rax,4)
je 0x6f2b
movq 0x30(%rsp), %rdi
movl %r12d, %esi
callq *0x28(%rsp)
testl %ebp, %ebp
jle 0x6f0e
cmpl 0x20(%rsp), %eax
jne 0x6eb8
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq %r14d, %rdx
movq 0x20(%rsp), %rsi
cmpl %esi, -0x4(%rcx,%rdx,4)
jne 0x6eb8
decl %r14d
decl %ebp
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
xorl %ebp, %ebp
movq 0x18(%rsp), %rsi
jmp 0x6f6e
cmpl $0x4, %ebp
movq 0x18(%rsp), %rsi
jl 0x6ef8
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq %r14d, %rdx
cmpl %eax, -0x4(%rcx,%rdx,4)
jne 0x6ef8
cmpl %eax, -0x8(%rcx,%rdx,4)
jne 0x6ef8
cmpl %eax, -0xc(%rcx,%rdx,4)
jne 0x6ef8
cmpl %eax, -0x10(%rcx,%rdx,4)
jne 0x6ef8
cmpl $0x4, %ebp
jne 0x6f80
addl $-0x6, %r14d
jmp 0x6f8e
incl %ebp
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq %r14d, %rdx
incl %r14d
movl %eax, (%rcx,%rdx,4)
jmp 0x6f6e
movq 0x18(%rsp), %rsi
jns 0x6f41
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movslq %r14d, %rdx
cmpl %eax, -0x4(%rcx,%rdx,4)
jne 0x6f3a
decl %ebp
jmp 0x6f6e
testl %ebp, %ebp
je 0x6f36
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
xorl %ebp, %ebp
jmp 0x6f6e
movq 0x8(%rsp), %rcx
movl %ebp, (%rcx)
xorl %ebp, %ebp
cmpl 0x20(%rsp), %eax
je 0x6f6e
movq 0x10(%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 0x6e51
jmp 0x6fb0
addl $-0x4, %r14d
addl $-0x4, %ebp
movq 0x8(%rsp), %rdx
movl %ebp, (%rdx)
movslq %r14d, %r14
leaq (%rcx,%r14,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 0x6f6e
movslq %r14d, %rdx
jmp 0x6fc2
xorl %edx, %edx
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
testl %ebp, %ebp
je 0x6fcd
movq 0x8(%rsp), %rax
movl %ebp, (%rax)
movq 0x10(%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 0x4154(%rip), %rdi      # 0xb147
leaq 0x402e(%rip), %rsi      # 0xb028
leaq 0x4154(%rip), %rcx      # 0xb155
movl $0xdd, %edx
callq 0x20e0
leaq 0x41a2(%rip), %rdi      # 0xb1b4
leaq 0x400f(%rip), %rsi      # 0xb028
leaq 0x4135(%rip), %rcx      # 0xb155
movl $0x103, %edx            # imm = 0x103
callq 0x20e0