// File: /workspace/github_c_makefile_star2_O3/smilingthax[P]libfontembed/sfnt.c
// Function: otf_from_unicode
unsigned short otf_from_unicode(OTF_FILE *otf,int unicode) // {{{ 0 = missing
{
  assert(otf);
  assert( (unicode>=0)&&(unicode<65536) );
//  assert(!is_CFF(otf)); // not for CFF, other method!

  // ensure >cmap and >unimap is there
  if (!otf->cmap) {
    if (otf_load_cmap(otf)!=0) {
      assert(0);
      return 0; // TODO?
    }
  }
  if (!otf->unimap) {
    fprintf(stderr,"Unicode (3,1) cmap in format 4 not found\n");
    return 0;
  }

#if 0
  // linear search is cache friendly and should be quite fast
#else
  const unsigned short segCountX2=get_USHORT(otf->unimap+6);
  char target[]={unicode>>8,unicode}; // set_USHORT(target,unicode);
  char *result=otf_bsearch((char *)otf->unimap+14,target,2,
                           get_USHORT(otf->unimap+8),
                           get_USHORT(otf->unimap+10),
                           get_USHORT(otf->unimap+12),1);
  if (result>=otf->unimap+14+segCountX2) { // outside of endCode[segCount]
    assert(0); // bad font, no 0xffff sentinel
    return 0;
  }

  result+=2+segCountX2; // jump over padding into startCode[segCount]
  const unsigned short startCode=get_USHORT(result);
  if (startCode>unicode) {
    return 0;
  }
  result+=2*segCountX2;
  const unsigned short rangeOffset=get_USHORT(result);
  if (rangeOffset) {
    return get_USHORT(result+rangeOffset+2*(unicode-startCode)); // the so called "obscure indexing trick" into glyphIdArray[]
    // NOTE: this is according to apple spec; microsoft says we must add delta (probably incorrect; fonts probably have delta==0)
  } else {
    const short delta=get_SHORT(result-segCountX2);
    return (delta+unicode)&0xffff;
  }
#endif
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x4d8b
movl %esi, %ebx
cmpl $0x10000, %esi          # imm = 0x10000
jae 0x4daa
movq %rdi, %r14
cmpq $0x0, 0x70(%rdi)
jne 0x4c55
movq %r14, %rdi
callq 0x47aa
movq 0x78(%r14), %rsi
testq %rsi, %rsi
je 0x4d84
movzwl 0x6(%rsi), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %ebx, %ecx
rolw $0x8, %cx
movw %cx, 0x6(%rsp)
leaq 0xe(%rsi), %rcx
movzwl 0x8(%rsi), %edx
movzwl 0xc(%rsi), %r9d
rolw $0x8, %dx
movzwl %dx, %edx
movzbl 0xa(%rsi), %edi
movzbl 0xb(%rsi), %r8d
rolw $0x8, %r9w
movzwl %r9w, %r9d
addq %r9, %rsi
addq $0xe, %rsi
movzwl (%rsi), %r9d
rolw $0x8, %r9w
shll $0x8, %edi
movzwl %bx, %r10d
movzwl %r9w, %r9d
cmpl %r9d, %r10d
cmovsq %rcx, %rsi
movl %edi, %r9d
orl %r8d, %r9d
je 0x4cfb
addl %r8d, %edi
incl %edi
movzwl 0x6(%rsp), %r8d
rolw $0x8, %r8w
movzwl %r8w, %r8d
shrl %edx
movzwl (%rsi,%rdx), %r9d
rolw $0x8, %r9w
movzwl %r9w, %r9d
cmpl %r9d, %r8d
js 0x4cf4
addq %rdx, %rsi
decl %edi
cmpl $0x1, %edi
ja 0x4cdc
movzwl 0x6(%rsp), %edi
movzwl (%rsi), %r8d
rolw $0x8, %di
rolw $0x8, %r8w
movzwl %di, %r9d
movzwl %r8w, %edi
subl %edi, %r9d
xorl %edi, %edi
testl %r9d, %r9d
cmovgl %edx, %edi
addq %rsi, %rdi
addq %rax, %rcx
cmpq %rcx, %rdi
jae 0x4dc9
movzwl 0x2(%rdi,%rax), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
movl %ebx, %ecx
subl %edx, %ecx
jge 0x4d45
xorl %eax, %eax
jmp 0x4d7c
leaq (%rdi,%rax), %rdx
addq $0x2, %rdx
leaq (%rdx,%rax,2), %rdx
movzwl (%rdx), %esi
rolw $0x8, %si
testw %si, %si
je 0x4d70
movzwl %si, %eax
addq %rax, %rdx
movslq %ecx, %rax
movzwl (%rdx,%rax,2), %eax
rolw $0x8, %ax
jmp 0x4d7c
subq %rax, %rdx
movzwl (%rdx), %eax
rolw $0x8, %ax
addl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
callq 0x2907
jmp 0x4d41
leaq 0x52a6(%rip), %rdi      # 0xa038
leaq 0x5cb6(%rip), %rsi      # 0xaa4f
leaq 0x5913(%rip), %rcx      # 0xa6b3
movl $0x384, %edx            # imm = 0x384
callq 0x20e0
leaq 0x5933(%rip), %rdi      # 0xa6e4
leaq 0x5c97(%rip), %rsi      # 0xaa4f
leaq 0x58f4(%rip), %rcx      # 0xa6b3
movl $0x385, %edx            # imm = 0x385
callq 0x20e0
leaq 0x5ef0(%rip), %rdi      # 0xacc0
leaq 0x5c78(%rip), %rsi      # 0xaa4f
leaq 0x58d5(%rip), %rcx      # 0xa6b3
movl $0x39e, %edx            # imm = 0x39E
callq 0x20e0