// File: /workspace/github_c_makefile_star2_O1/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 0x4db9
movl %esi, %ebx
cmpl $0x10000, %esi          # imm = 0x10000
jae 0x4dd8
movq %rdi, %r14
cmpq $0x0, 0x70(%rdi)
jne 0x4c85
movq %r14, %rdi
callq 0x47d8
movq 0x78(%r14), %rdi
testq %rdi, %rdi
je 0x4db2
movzwl 0x6(%rdi), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %ebx, %ecx
rolw $0x8, %cx
movw %cx, 0x6(%rsp)
leaq 0xe(%rdi), %rcx
movzwl 0x8(%rdi), %edx
movzwl 0xc(%rdi), %esi
rolw $0x8, %dx
movzwl %dx, %edx
movzbl 0xa(%rdi), %r8d
shll $0x8, %r8d
rolw $0x8, %si
movzwl %si, %esi
addq %rdi, %rsi
addq $0xe, %rsi
movzwl (%rsi), %r9d
rolw $0x8, %r9w
movzbl 0xb(%rdi), %edi
movzwl %bx, %r10d
movzwl %r9w, %r9d
cmpl %r9d, %r10d
cmovsq %rcx, %rsi
movl %r8d, %r9d
orl %edi, %r9d
je 0x4d29
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 0x4d22
addq %rdx, %rsi
decl %edi
cmpl $0x1, %edi
jg 0x4d0a
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 0x4df7
movzwl 0x2(%rdi,%rax), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
movl %ebx, %ecx
subl %edx, %ecx
jge 0x4d73
xorl %eax, %eax
jmp 0x4daa
leaq (%rdi,%rax), %rdx
addq $0x2, %rdx
leaq (%rdx,%rax,2), %rdx
movzwl (%rdx), %esi
rolw $0x8, %si
testw %si, %si
je 0x4d9e
movzwl %si, %eax
addq %rax, %rdx
movslq %ecx, %rax
movzwl (%rdx,%rax,2), %eax
rolw $0x8, %ax
jmp 0x4daa
subq %rax, %rdx
movzwl (%rdx), %eax
rolw $0x8, %ax
addl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
callq 0x28ff
jmp 0x4d6f
leaq 0x5278(%rip), %rdi      # 0xa038
leaq 0x5c88(%rip), %rsi      # 0xaa4f
leaq 0x58e5(%rip), %rcx      # 0xa6b3
movl $0x384, %edx            # imm = 0x384
callq 0x20e0
leaq 0x5905(%rip), %rdi      # 0xa6e4
leaq 0x5c69(%rip), %rsi      # 0xaa4f
leaq 0x58c6(%rip), %rcx      # 0xa6b3
movl $0x385, %edx            # imm = 0x385
callq 0x20e0
leaq 0x5ec2(%rip), %rdi      # 0xacc0
leaq 0x5c4a(%rip), %rsi      # 0xaa4f
leaq 0x58a7(%rip), %rcx      # 0xa6b3
movl $0x39e, %edx            # imm = 0x39E
callq 0x20e0
nop