// File: /workspace/github_c_makefile_star2_O0/smilingthax[P]libfontembed/sfnt_subset.c
// Function: otf_subset2
int otf_subset2(OTF_FILE *otf,BITSET glyphs,SUBSET_DESTINATION dest,struct _OTF_WRITE_WOFF *woff,OUTPUT_FN output,void *context) // {{{ - returns number of bytes written
{
  assert(otf);
  assert(glyphs);
  assert(output);

  int iA,b,c;

  // first pass: include all required glyphs
  bit_set(glyphs,0); // .notdef always required
  int glyfSize=0;
  for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
    if (!c) {
      b++;
      c=1;
    }
    if (glyphs[b]&c) {
      int len=otf_get_glyph(otf,iA);
      if (len<0) {
        assert(0);
        return -1;
      } else if (len>0) {
        glyfSize+=len;
        len=otf_subset_glyf(otf,iA,iA,glyphs);
        if (len<0) {
          assert(0);
          return -1;
        }
        glyfSize+=len;
      }
    }
  }

  // second pass: calculate new glyf and loca
  int locaSize=(otf->numGlyphs+1)*(otf->indexToLocFormat+1)*2;

  char *new_loca=malloc(otf_padded(locaSize));
  char *new_glyf=malloc(otf_padded(glyfSize));
  if ( (!new_loca)||(!new_glyf) ) {
    fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
    assert(0);
    free(new_loca);
    free(new_glyf);
    return -1;
  }

  int offset=0;
  for (iA=0,b=0,c=1;iA<otf->numGlyphs;iA++,c<<=1) {
    if (!c) {
      b++;
      c=1;
    }

    assert(offset%2==0);
    // TODO? change format? if glyfSize<0x20000
    if (otf->indexToLocFormat==0) {
      set_USHORT(new_loca+iA*2,offset/2);
    } else { // ==1
      set_ULONG(new_loca+iA*4,offset);
    }

    if (glyphs[b]&c) {
      const int len=otf_get_glyph(otf,iA);
      assert(len>=0);
      memcpy(new_glyf+offset,otf->gly,len);
      offset+=len;
    }
  }
  // last entry
  if (otf->indexToLocFormat==0) {
    set_USHORT(new_loca+otf->numGlyphs*2,offset/2);
  } else { // ==1
    set_ULONG(new_loca+otf->numGlyphs*4,offset);
  }
  assert(offset==glyfSize);

  // determine new tables.
  struct _OTF_WRITE_TABLE tables[]={ // sorted
    // TODO: cmap only required in non-CID context   or always in CFF
      {OTF_TAG('O','S','/','2'),NULL,.copy={otf,}}, // 0
      {OTF_TAG('c','m','a','p'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('c','v','t',' '),otf_action_copy,.copy={otf,}},
      {OTF_TAG('f','p','g','m'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('g','l','y','f'),otf_action_replace,.replace={new_glyf,glyfSize}},
      {OTF_TAG('h','e','a','d'),otf_action_copy,.copy={otf,}}, // 5 // _copy_head
      {OTF_TAG('h','h','e','a'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('h','m','t','x'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('l','o','c','a'),otf_action_replace,.replace={new_loca,locaSize}},
      {OTF_TAG('m','a','x','p'),otf_action_copy,.copy={otf,}},
      {OTF_TAG('n','a','m','e'),otf_action_copy,.copy={otf,}}, // 10
      {OTF_TAG('p','o','s','t'),NULL,.copy={otf,}},
      {OTF_TAG('p','r','e','p'),otf_action_copy,.copy={otf,}},
      // vhea vmtx (never used in PDF, but possible in PS>=3011)
      {0,0,}};
  if (dest==SUBSET_DST_BROWSER) {
    // esp. chrome:
    tables[0].action=otf_action_copy; // OS/2
    tables[11].action=otf_action_copy; // post
  }

  // and write them
  int numTables=otf_intersect_tables(otf,tables);
  struct _OTF_WRITE_INFO otw={
    .version=otf->version,
    .numTables=numTables,
    .tables=tables,
    .order=otf_tagorder_win_sort(tables,numTables)
  };
  const int ret=otf_write_sfnt(&otw,woff,output,context);

  free(otw.order);
  free(new_loca);
  free(new_glyf);
  return ret;

  //TODO ? reduce cmap [to (1,0) ;-)]
  //TODO (cmap for non-cid)
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4f0, %rsp            # imm = 0x4F0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0xaa7b
jmp 0xaa9a
leaq 0x35b6(%rip), %rdi      # 0xe038
leaq 0x4e7c(%rip), %rsi      # 0xf905
movl $0x88, %edx
leaq 0x4f08(%rip), %rcx      # 0xf99d
callq 0x2100
cmpq $0x0, -0x10(%rbp)
je 0xaaa3
jmp 0xaac2
leaq 0x4f58(%rip), %rdi      # 0xfa02
leaq 0x4e54(%rip), %rsi      # 0xf905
movl $0x89, %edx
leaq 0x4ee0(%rip), %rcx      # 0xf99d
callq 0x2100
cmpq $0x0, -0x28(%rbp)
je 0xaacb
jmp 0xaaea
leaq 0x3f81(%rip), %rdi      # 0xea53
leaq 0x4e2c(%rip), %rsi      # 0xf905
movl $0x8a, %edx
leaq 0x4eb8(%rip), %rcx      # 0xf99d
callq 0x2100
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xb2e0
movl $0x0, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
movzwl 0x4c(%rcx), %ecx
cmpl %ecx, %eax
jge 0xabf7
cmpl $0x0, -0x3c(%rbp)
jne 0xab3a
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl -0x3c(%rbp), %eax
cmpl $0x0, %eax
je 0xabdf
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %eax
movzwl %ax, %esi
callq 0x5040
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0xab88
leaq 0x4190(%rip), %rdi      # 0xed00
leaq 0x4d8e(%rip), %rsi      # 0xf905
movl $0x99, %edx
leaq 0x4e1a(%rip), %rcx      # 0xf99d
callq 0x2100
cmpl $0x0, -0x44(%rbp)
jle 0xabdb
movl -0x44(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movl -0x34(%rbp), %edx
movq -0x10(%rbp), %rcx
callq 0xb310
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0xabd2
leaq 0x4146(%rip), %rdi      # 0xed00
leaq 0x4d44(%rip), %rsi      # 0xf905
movl $0x9f, %edx
leaq 0x4dd0(%rip), %rcx      # 0xf99d
callq 0x2100
movl -0x44(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0xabdd
jmp 0xabdf
jmp 0xabe1
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x3c(%rbp)
jmp 0xab11
movq -0x8(%rbp), %rax
movzwl 0x4c(%rax), %eax
addl $0x1, %eax
movq -0x8(%rbp), %rcx
movzwl 0x4a(%rcx), %ecx
addl $0x1, %ecx
imull %ecx, %eax
shll %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %edi
callq 0xb510
movslq %eax, %rdi
callq 0x21d0
movq %rax, -0x50(%rbp)
movl -0x40(%rbp), %edi
callq 0xb510
movslq %eax, %rdi
callq 0x21d0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0xac4b
cmpq $0x0, -0x58(%rbp)
jne 0xac9f
movq 0x838e(%rip), %rax      # 0x12fe0
movq (%rax), %rax
movq %rax, -0x4f0(%rbp)
callq 0x2040
movl (%rax), %edi
callq 0x2070
movq -0x4f0(%rbp), %rdi
movq %rax, %rdx
leaq 0x36e3(%rip), %rsi      # 0xe35c
movb $0x0, %al
callq 0x21e0
leaq 0x4079(%rip), %rdi      # 0xed00
leaq 0x4c77(%rip), %rsi      # 0xf905
movl $0xae, %edx
leaq 0x4d03(%rip), %rcx      # 0xf99d
callq 0x2100
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
movzwl 0x4c(%rcx), %ecx
cmpl %ecx, %eax
jge 0xade8
cmpl $0x0, -0x3c(%rbp)
jne 0xace4
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x5c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0xacf6
jmp 0xad15
leaq 0x4d0c(%rip), %rdi      # 0xfa09
leaq 0x4c01(%rip), %rsi      # 0xf905
movl $0xbb, %edx
leaq 0x4c8d(%rip), %rcx      # 0xf99d
callq 0x2100
movq -0x8(%rbp), %rax
movzwl 0x4a(%rax), %eax
cmpl $0x0, %eax
jne 0xad45
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
movl -0x5c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movzwl %ax, %esi
callq 0xb530
jmp 0xad5c
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %eax
shll $0x2, %eax
cltq
addq %rax, %rdi
movl -0x5c(%rbp), %esi
callq 0xb570
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl -0x3c(%rbp), %eax
cmpl $0x0, %eax
je 0xadd0
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %eax
movzwl %ax, %esi
callq 0x5040
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jl 0xad89
jmp 0xada8
leaq 0x4c85(%rip), %rdi      # 0xfa15
leaq 0x4b6e(%rip), %rsi      # 0xf905
movl $0xc5, %edx
leaq 0x4bfa(%rip), %rcx      # 0xf99d
callq 0x2100
movq -0x58(%rbp), %rdi
movslq -0x5c(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rax
movq 0x80(%rax), %rsi
movslq -0x60(%rbp), %rdx
callq 0x2120
movl -0x60(%rbp), %eax
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0xadd2
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x3c(%rbp)
jmp 0xacbb
movq -0x8(%rbp), %rax
movzwl 0x4a(%rax), %eax
cmpl $0x0, %eax
jne 0xae1d
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rax
movzwl 0x4c(%rax), %eax
shll %eax
cltq
addq %rax, %rdi
movl -0x5c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movzwl %ax, %esi
callq 0xb530
jmp 0xae39
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rax
movzwl 0x4c(%rax), %eax
shll $0x2, %eax
cltq
addq %rax, %rdi
movl -0x5c(%rbp), %esi
callq 0xb570
movl -0x5c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jne 0xae43
jmp 0xae62
leaq 0x4bd2(%rip), %rdi      # 0xfa1c
leaq 0x4ab4(%rip), %rsi      # 0xf905
movl $0xd0, %edx
leaq 0x4b40(%rip), %rcx      # 0xf99d
callq 0x2100
leaq -0x4c0(%rbp), %rdi
xorl %esi, %esi
movl $0x50, %edx
callq 0x20e0
movq $0x4f532f32, -0x4c0(%rbp) # imm = 0x4F532F32
movq -0x8(%rbp), %rax
movq %rax, -0x4b0(%rbp)
leaq -0x4c0(%rbp), %rdi
addq $0x50, %rdi
movq $0x636d6170, -0x470(%rbp) # imm = 0x636D6170
leaq 0x1148(%rip), %rax      # 0xbff0
movq %rax, -0x468(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x460(%rbp)
movl $0x0, -0x458(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0xa0, %rdi
movq $0x63767420, -0x420(%rbp) # imm = 0x63767420
leaq 0x10fc(%rip), %rax      # 0xbff0
movq %rax, -0x418(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x410(%rbp)
movl $0x0, -0x408(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0xf0, %rdi
movq $0x6670676d, -0x3d0(%rbp) # imm = 0x6670676D
leaq 0x10b0(%rip), %rax      # 0xbff0
movq %rax, -0x3c8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x3c0(%rbp)
movl $0x0, -0x3b8(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movq $0x676c7966, -0x380(%rbp) # imm = 0x676C7966
leaq 0x1204(%rip), %rax      # 0xc190
movq %rax, -0x378(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x370(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x368(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x190, %rdi            # imm = 0x190
movq $0x68656164, -0x330(%rbp) # imm = 0x68656164
leaq 0x1019(%rip), %rax      # 0xbff0
movq %rax, -0x328(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x320(%rbp)
movl $0x0, -0x318(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
movq $0x68686561, -0x2e0(%rbp) # imm = 0x68686561
leaq 0xfcd(%rip), %rax       # 0xbff0
movq %rax, -0x2d8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movl $0x0, -0x2c8(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
movq $0x686d7478, -0x290(%rbp) # imm = 0x686D7478
leaq 0xf81(%rip), %rax       # 0xbff0
movq %rax, -0x288(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x280(%rbp)
movl $0x0, -0x278(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x280, %rdi            # imm = 0x280
movq $0x6c6f6361, -0x240(%rbp) # imm = 0x6C6F6361
leaq 0x10d5(%rip), %rax      # 0xc190
movq %rax, -0x238(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x230(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x228(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
movq $0x6d617870, -0x1f0(%rbp) # imm = 0x6D617870
leaq 0xeea(%rip), %rax       # 0xbff0
movq %rax, -0x1e8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movl $0x0, -0x1d8(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x320, %rdi            # imm = 0x320
movq $0x6e616d65, -0x1a0(%rbp) # imm = 0x6E616D65
leaq 0xe9e(%rip), %rax       # 0xbff0
movq %rax, -0x198(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x190(%rbp)
movl $0x0, -0x188(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x370, %rdi            # imm = 0x370
xorl %esi, %esi
movl $0x50, %edx
callq 0x20e0
movq $0x706f7374, -0x150(%rbp) # imm = 0x706F7374
movq -0x8(%rbp), %rax
movq %rax, -0x140(%rbp)
leaq -0x4c0(%rbp), %rdi
addq $0x3c0, %rdi            # imm = 0x3C0
movq $0x70726570, -0x100(%rbp) # imm = 0x70726570
leaq 0xe22(%rip), %rax       # 0xbff0
movq %rax, -0xf8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl $0x0, -0xe8(%rbp)
addq $0x20, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x20e0
leaq -0x4c0(%rbp), %rdi
addq $0x410, %rdi            # imm = 0x410
xorl %esi, %esi
movl $0x50, %edx
callq 0x20e0
cmpl $0x1, -0x14(%rbp)
jne 0xb236
leaq 0xdcf(%rip), %rax       # 0xbff0
movq %rax, -0x4b8(%rbp)
leaq 0xdc1(%rip), %rax       # 0xbff0
movq %rax, -0x148(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x4c0(%rbp), %rsi
callq 0xa830
movl %eax, -0x4c4(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x4e0(%rbp)
movl -0x4c4(%rbp), %eax
movw %ax, -0x4dc(%rbp)
leaq -0x4c0(%rbp), %rax
movq %rax, -0x4d8(%rbp)
leaq -0x4c0(%rbp), %rdi
movl -0x4c4(%rbp), %esi
callq 0xbc50
movq %rax, -0x4d0(%rbp)
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x4e0(%rbp), %rdi
callq 0xc320
movl %eax, -0x4e4(%rbp)
movq -0x4d0(%rbp), %rdi
callq 0x21b0
movq -0x50(%rbp), %rdi
callq 0x21b0
movq -0x58(%rbp), %rdi
callq 0x21b0
movl -0x4e4(%rbp), %eax
addq $0x4f0, %rsp            # imm = 0x4F0
popq %rbp
retq
nopl (%rax,%rax)