// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x5298
jmp 0x52b7
leaq 0x8d99(%rip), %rdi      # 0xe038
leaq 0x97e9(%rip), %rsi      # 0xea8f
movl $0x384, %edx            # imm = 0x384
leaq 0x9404(%rip), %rcx      # 0xe6b6
callq 0x2100
cmpl $0x0, -0x14(%rbp)
jl 0x52c8
cmpl $0x10000, -0x14(%rbp)   # imm = 0x10000
jge 0x52c8
jmp 0x52e7
leaq 0x9418(%rip), %rdi      # 0xe6e7
leaq 0x97b9(%rip), %rsi      # 0xea8f
movl $0x385, %edx            # imm = 0x385
leaq 0x93d4(%rip), %rcx      # 0xe6b6
callq 0x2100
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
jne 0x5321
movq -0x10(%rbp), %rdi
callq 0x4a90
cmpl $0x0, %eax
je 0x531f
leaq 0x99f9(%rip), %rdi      # 0xed00
leaq 0x9781(%rip), %rsi      # 0xea8f
movl $0x38b, %edx            # imm = 0x38B
leaq 0x939c(%rip), %rcx      # 0xe6b6
callq 0x2100
jmp 0x5321
movq -0x10(%rbp), %rax
cmpq $0x0, 0x78(%rax)
jne 0x534f
movq 0xdcad(%rip), %rax      # 0x12fe0
movq (%rax), %rdi
leaq 0x93c8(%rip), %rsi      # 0xe705
movb $0x0, %al
callq 0x21e0
movw $0x0, -0x2(%rbp)
jmp 0x54df
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
addq $0x6, %rdi
callq 0x46f0
movw %ax, -0x16(%rbp)
movl -0x14(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x18(%rbp)
movl -0x14(%rbp), %eax
movb %al, -0x17(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
addq $0xe, %rax
movq %rax, -0x40(%rbp)
leaq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
addq $0x8, %rdi
callq 0x46f0
movzwl %ax, %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
addq $0xa, %rdi
callq 0x46f0
movzwl %ax, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
addq $0xc, %rdi
callq 0x46f0
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x30(%rbp), %ecx
movl -0x2c(%rbp), %r8d
movzwl %ax, %r9d
movl $0x2, %edx
movl $0x1, (%rsp)
callq 0x54f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x78(%rcx), %rcx
addq $0xe, %rcx
movzwl -0x16(%rbp), %edx
movslq %edx, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jb 0x5430
leaq 0x98e8(%rip), %rdi      # 0xed00
leaq 0x9670(%rip), %rsi      # 0xea8f
movl $0x39e, %edx            # imm = 0x39E
leaq 0x928b(%rip), %rcx      # 0xe6b6
callq 0x2100
movzwl -0x16(%rbp), %ecx
addl $0x2, %ecx
movq -0x20(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x46f0
movw %ax, -0x22(%rbp)
movzwl -0x22(%rbp), %eax
cmpl -0x14(%rbp), %eax
jle 0x5463
movw $0x0, -0x2(%rbp)
jmp 0x54df
movzwl -0x16(%rbp), %ecx
shll %ecx
movq -0x20(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x46f0
movw %ax, -0x24(%rbp)
cmpw $0x0, -0x24(%rbp)
je 0x54b3
movq -0x20(%rbp), %rdi
movzwl -0x24(%rbp), %eax
cltq
addq %rax, %rdi
movl -0x14(%rbp), %eax
movzwl -0x22(%rbp), %ecx
subl %ecx, %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x46f0
movw %ax, -0x2(%rbp)
jmp 0x54df
movq -0x20(%rbp), %rdi
movzwl -0x16(%rbp), %eax
movslq %eax, %rcx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rdi
callq 0x4a70
movw %ax, -0x26(%rbp)
movswl -0x26(%rbp), %eax
addl -0x14(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movw %ax, -0x2(%rbp)
movw -0x2(%rbp), %ax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)