// File: /workspace/github_c_makefile_star2_O1/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 0x4fdc
movl %edx, %ebp
movl %esi, %r15d
movq %rdi, %r14
movl $0x1, %edi
movl $0x28, %esi
callq 0x20f0
testq %rax, %rax
je 0x4fa5
movq %rax, %rbx
movl %r15d, 0x8(%rax)
movq %r14, 0x10(%rax)
testw %bp, %bp
jns 0x4e5c
orb $-0x80, 0xd(%rbx)
movl %ebp, %eax
notl %eax
testb $0x28, %al
je 0x4fba
testb $0x6, %al
je 0x4fc4
movq (%r14), %rdi
testq %rdi, %rdi
je 0x4ea2
xorl %eax, %eax
btl $0x10, 0x8(%rdi)
adcl $0x1, %eax
movl %eax, (%rbx)
callq 0x53d4
movl %eax, 0x18(%rbx)
movq 0x10(%rbx), %rax
movq (%rax), %rax
movzwl 0x4c(%rax), %esi
addl $0x1f, %esi
shrl $0x3, %esi
andl $-0x4, %esi
jmp 0x4ebc
cmpq $0x0, 0x8(%r14)
je 0x4ffb
movl $0x4, (%rbx)
movl $0x2, 0x18(%rbx)
xorl %esi, %esi
movl (%rbx), %eax
testl %eax, %eax
je 0x4ee5
cmpl $0x4, %eax
jne 0x4ef6
movl $0x4, 0x4(%rbx)
testb $0x8, %bpl
je 0x4fce
movq %rbx, %rdi
callq 0x291b
jmp 0x4fcc
xorl %eax, %eax
testb $0x20, %bpl
jne 0x4ef6
orb $0x4, 0xc(%rbx)
movl $0x3, %eax
movl %eax, 0x4(%rbx)
cmpl $0x2, %eax
je 0x4f15
cmpl $0x3, %eax
jne 0x4f36
testb $0x10, %bpl
je 0x4f36
movl $0x8, %eax
movl $0x2, %ecx
jmp 0x4f2c
movl %ebp, %edx
orl $0x4, %edx
testb $0x10, %bpl
jne 0x4f34
movl $0x10, %eax
movl $0x3, %ecx
movl %edx, %ebp
movl %ecx, 0x4(%rbx)
orl %eax, 0xc(%rbx)
jmp 0x4f36
movl %edx, %ebp
testb $0x8, %bpl
je 0x4f40
orb $0x1, 0xc(%rbx)
movl 0x18(%rbx), %eax
testl $0x202, %eax            # imm = 0x202
jne 0x4f9b
movl %eax, %ecx
andl $0x4, %ecx
shrl $0x2, %ecx
movl %ebp, %edx
shrb %dl
testb %cl, %dl
jne 0x4f9b
andl $0x100, %eax            # imm = 0x100
sete %cl
testb $0x1, %bpl
sete %dl
orb %cl, %dl
je 0x4f9b
andl $0x4, %ebp
orl %ebp, %eax
jne 0x4f78
orb $0x2, 0xc(%rbx)
testb $0x2, 0xc(%rbx)
je 0x4fce
movl $0x1, %edi
callq 0x20f0
movq %rax, 0x20(%rbx)
testq %rax, %rax
jne 0x4fce
movq %rbx, %rdi
callq 0x296d
jmp 0x4fcc
movq %rbx, %rdi
callq 0x2944
jmp 0x4fcc
movq %rsp, %rbx
movl %ebp, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x29ff
movq (%rbx), %rbx
jmp 0x4fce
movq %rbx, %rdi
callq 0x29d6
jmp 0x4fcc
movq %rbx, %rdi
callq 0x29ad
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x58ac(%rip), %rdi      # 0xa88f
leaq 0x58aa(%rip), %rsi      # 0xa894
leaq 0x58ab(%rip), %rcx      # 0xa89c
movl $0x48, %edx
callq 0x20e0
leaq 0x5cbe(%rip), %rdi      # 0xacc0
leaq 0x588b(%rip), %rsi      # 0xa894
leaq 0x588c(%rip), %rcx      # 0xa89c
movl $0x72, %edx
callq 0x20e0