// File: /workspace/github_c_makefile_star2_O3/smilingthax[P]libfontembed/embed.c
// Function: emb_new
EMB_PARAMS *emb_new(FONTFILE *font,EMB_DESTINATION dest,EMB_CONSTRAINTS mode) // {{{
{
  assert(font);

  EMB_PARAMS *ret=calloc(1,sizeof(EMB_PARAMS));
  if (!ret) {
    fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
    if (mode&EMB_C_TAKE_FONTFILE) {
      fontfile_close(font);
    }
    return NULL;
  }
  ret->dest=dest;
  ret->font=font;
  if (mode&EMB_C_TAKE_FONTFILE) {
    ret->plan|=EMB_A_CLOSE_FONTFILE;
  }

  // check parameters
  if ( (mode&EMB_C_KEEP_T1)&&(mode&EMB_C_FORCE_MULTIBYTE) ) {
    fprintf(stderr,"Incompatible mode: KEEP_T1 and FORCE_MULTIBYTE\n");
    emb_close(ret);
    return NULL;
  }
  if ((mode&0x07)>5) {
    fprintf(stderr,"Bad subset specification\n");
    emb_close(ret);
    return NULL;
  }

  // determine intype
  int numGlyphs=0;
  if (font->sfnt) {
    if (font->sfnt->flags&OTF_F_FMT_CFF) {
      ret->intype=EMB_FMT_OTF;
    } else {
      ret->intype=EMB_FMT_TTF;
    }
    ret->rights=emb_otf_get_rights(ret->font->sfnt);
    numGlyphs=ret->font->sfnt->numGlyphs; // TODO
  } else if (font->stdname) {
    ret->intype=EMB_FMT_STDFONT;
    ret->rights=EMB_RIGHT_NONE;
  } else {
    assert(0);
  }
/*
  if ( (ret->intype==EMB_FMT_CFF)&&
       (ret->cffFont.is_cid()) ) {
     ?= || ( (ret->intype==EMB_FMT_OTF)&&(ret->sfnt->cffFont.is_cid()) ) // TODO?
    ret->plan|=EMB_A_MULTIBYTE;
  }
*/

  // determine outtype
  if (ret->intype==EMB_FMT_STDFONT) {
    ret->outtype=ret->intype;
    if (mode&EMB_C_FORCE_MULTIBYTE) {
      fprintf(stderr,"Multibyte stdfonts are not possible\n");
      emb_close(ret);
      return NULL;
    }
    return ret; // never subset, no multibyte
  } else if (ret->intype==EMB_FMT_T1) {
    if (mode&EMB_C_KEEP_T1) {
      ret->outtype=EMB_FMT_T1;
    } else {
      ret->plan|=EMB_A_T1_TO_CFF;
      ret->outtype=EMB_FMT_CFF;
    }
  } else {
    ret->outtype=ret->intype;
  }
  if (ret->outtype==EMB_FMT_CFF) {
    if (mode&EMB_C_PDF_OT) {
      ret->outtype=EMB_FMT_OTF;
      ret->plan|=EMB_A_CFF_TO_OTF;
    }
  } else if (ret->outtype==EMB_FMT_OTF) {
    // TODO: no support yet;  but we want to get the FontDescriptor/Name right
    mode|=EMB_C_NEVER_SUBSET;
    if (!(mode&EMB_C_PDF_OT)) { // TODO!?!
      ret->outtype=EMB_FMT_CFF;
      ret->plan|=EMB_A_OTF_TO_CFF;
    }
  }

  if (mode&EMB_C_FORCE_MULTIBYTE) {
    ret->plan|=EMB_A_MULTIBYTE;
  }

  // check rights (for subsetting)
  if (  (ret->rights&EMB_RIGHT_NONE)||
        (ret->rights&EMB_RIGHT_BITMAPONLY)||
        ( (ret->rights&EMB_RIGHT_READONLY)&&(mode&EMB_C_EDITABLE_SUBSET) )||
        ( (ret->rights&EMB_RIGHT_NO_SUBSET)&&(mode&EMB_C_MUST_SUBSET) )  ) {
    fprintf(stderr,"The font does not permit the requested embedding\n");
    emb_close(ret);
    return NULL;
  } else if ( (!(ret->rights&EMB_RIGHT_NO_SUBSET))&&
              (!(mode&EMB_C_NEVER_SUBSET)) ) {
    ret->plan|=EMB_A_SUBSET;
  }

  // alloc subset
  if (ret->plan&EMB_A_SUBSET) {
    ret->subset=bitset_new(numGlyphs);
    if (!ret->subset) {
      fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
      emb_close(ret);
      return NULL;
    }
  }

  return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x4fba
movl %edx, %ebp
movl %esi, %r15d
movq %rdi, %r14
movl $0x1, %edi
movl $0x28, %esi
callq 0x20f0
testq %rax, %rax
je 0x4eee
movq %rax, %rbx
movl %r15d, 0x8(%rax)
movq %r14, 0x10(%rax)
testw %bp, %bp
jns 0x4e2f
movl $0x8000, 0xc(%rbx)      # imm = 0x8000
movl %ebp, %eax
notl %eax
testb $0x28, %al
je 0x4f98
testb $0x6, %al
je 0x4fa2
movq (%r14), %rdi
testq %rdi, %rdi
je 0x4e8b
xorl %eax, %eax
btl $0x10, 0x8(%rdi)
adcl $0x1, %eax
movl %eax, (%rbx)
callq 0x53ac
movl %eax, 0x18(%rbx)
movq 0x10(%rbx), %rcx
movq (%rcx), %rcx
movzwl 0x4c(%rcx), %esi
movl (%rbx), %ecx
testl %ecx, %ecx
je 0x4ec1
cmpl $0x4, %ecx
je 0x4ea3
movl %ecx, 0x4(%rbx)
cmpl $0x2, %ecx
je 0x4f06
cmpl $0x3, %ecx
je 0x4ed2
jmp 0x4f1e
cmpq $0x0, 0x8(%r14)
je 0x4fd9
movl $0x4, (%rbx)
movl $0x2, 0x18(%rbx)
movl $0x4, 0x4(%rbx)
testb $0x8, %bpl
je 0x4fac
movq %rbx, %rdi
callq 0x298c
jmp 0x4faa
testb $0x20, %bpl
jne 0x4ee5
orb $0x4, 0xc(%rbx)
movl $0x3, 0x4(%rbx)
testb $0x10, %bpl
je 0x4f1e
movl $0x2, 0x4(%rbx)
orb $0x8, 0xc(%rbx)
jmp 0x4f1e
movl $0x0, 0x4(%rbx)
jmp 0x4f1e
movq %rsp, %rbx
movl %ebp, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2a07
movq (%rbx), %rbx
jmp 0x4fac
movl %ebp, %ecx
orl $0x4, %ecx
testb $0x10, %bpl
jne 0x4f1c
movl $0x3, 0x4(%rbx)
orb $0x10, 0xc(%rbx)
movl %ecx, %ebp
testb $0x8, %bpl
je 0x4f28
orb $0x1, 0xc(%rbx)
testl $0x202, %eax            # imm = 0x202
jne 0x4f8e
movl %eax, %ecx
andl $0x4, %ecx
shrl $0x2, %ecx
movl %ebp, %edx
shrb %dl
testb %cl, %dl
jne 0x4f8e
andl $0x100, %eax            # imm = 0x100
sete %cl
testb $0x1, %bpl
sete %dl
orb %cl, %dl
je 0x4f8e
andl $0x4, %ebp
orl %ebp, %eax
movl 0xc(%rbx), %eax
je 0x4f62
testb $0x2, %al
jne 0x4f68
jmp 0x4fac
orl $0x2, %eax
movl %eax, 0xc(%rbx)
addl $0x1f, %esi
shrl $0x3, %esi
andl $-0x4, %esi
movl $0x1, %edi
callq 0x20f0
movq %rax, 0x20(%rbx)
testq %rax, %rax
jne 0x4fac
movq %rbx, %rdi
callq 0x294c
jmp 0x4faa
movq %rbx, %rdi
callq 0x2923
jmp 0x4faa
movq %rbx, %rdi
callq 0x29de
jmp 0x4faa
movq %rbx, %rdi
callq 0x29b5
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x58ce(%rip), %rdi      # 0xa88f
leaq 0x58cc(%rip), %rsi      # 0xa894
leaq 0x58cd(%rip), %rcx      # 0xa89c
movl $0x48, %edx
callq 0x20e0
leaq 0x5ce0(%rip), %rdi      # 0xacc0
leaq 0x58ad(%rip), %rsi      # 0xa894
leaq 0x58ae(%rip), %rcx      # 0xa89c
movl $0x72, %edx
callq 0x20e0