// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testq %rdi, %rdi
je 0x4430
movl %esi, %ebx
cmpl $0x10000, %esi          # imm = 0x10000
jae 0x444f
movq %rdi, %r14
cmpq $0x0, 0x70(%rdi)
jne 0x42db
movq %r14, %rdi
callq 0x3dad
movq 0x78(%r14), %rax
testq %rax, %rax
je 0x4412
movzbl 0x6(%rax), %ecx
movq %rcx, 0x20(%rsp)
movzbl 0x7(%rax), %ecx
movq %rcx, 0x10(%rsp)
movl %ebx, 0xc(%rsp)
movl %ebx, %ecx
rolw $0x8, %cx
leaq 0xa(%rsp), %r15
movw %cx, (%r15)
leaq 0xe(%rax), %r13
movzwl 0x8(%rax), %ecx
movzwl 0xa(%rax), %edx
rolw $0x8, %cx
movzwl %cx, %ebx
rolw $0x8, %dx
movzwl %dx, %ebp
movzwl 0xc(%rax), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
leaq (%rax,%rcx), %r14
addq $0xe, %r14
pushq $0x2
popq %r12
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x20a0
testl %eax, %eax
movq %r13, 0x18(%rsp)
cmovsq %r13, %r14
testl %ebp, %ebp
jle 0x437a
shrl %ebx
leaq (%r14,%rbx), %r13
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x20a0
testl %eax, %eax
js 0x4376
movq %r13, %r14
decl %ebp
jmp 0x4357
movq 0x20(%rsp), %r15
shll $0x8, %r15d
addq 0x10(%rsp), %r15
leaq 0xa(%rsp), %rdi
pushq $0x2
popq %rdx
movq %r14, %rsi
callq 0x20a0
xorl %ecx, %ecx
testl %eax, %eax
cmovgl %ebx, %ecx
addq %r14, %rcx
movq 0x18(%rsp), %rax
addq %r15, %rax
cmpq %rax, %rcx
jae 0x446e
movzwl 0x2(%rcx,%r15), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movl 0xc(%rsp), %esi
movl %esi, %eax
subl %edx, %eax
jge 0x43ce
xorl %eax, %eax
jmp 0x4403
addq %r15, %rcx
addq $0x2, %rcx
leaq (%rcx,%r15,2), %rcx
movzwl (%rcx), %edx
rolw $0x8, %dx
testw %dx, %dx
je 0x43f7
movzwl %dx, %edx
addq %rdx, %rcx
cltq
movzwl (%rcx,%rax,2), %eax
rolw $0x8, %ax
jmp 0x4403
subq %r15, %rcx
movzwl (%rcx), %eax
rolw $0x8, %ax
addl %esi, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xabc7(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x62df(%rip), %rdi      # 0xa702
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x43ca
leaq 0x5c01(%rip), %rdi      # 0xa038
leaq 0x6611(%rip), %rsi      # 0xaa4f
leaq 0x626e(%rip), %rcx      # 0xa6b3
movl $0x384, %edx            # imm = 0x384
callq 0x20f0
leaq 0x628e(%rip), %rdi      # 0xa6e4
leaq 0x65f2(%rip), %rsi      # 0xaa4f
leaq 0x624f(%rip), %rcx      # 0xa6b3
movl $0x385, %edx            # imm = 0x385
callq 0x20f0
leaq 0x684b(%rip), %rdi      # 0xacc0
leaq 0x65d3(%rip), %rsi      # 0xaa4f
leaq 0x6230(%rip), %rcx      # 0xa6b3
movl $0x39e, %edx            # imm = 0x39E
callq 0x20f0
nopl (%rax)