// File: Ciaxur[P]yagb-emu/include/imgui/imstb_truetype.h
// Function: stbtt_FindGlyphIndex(stbtt_fontinfo const*, int)
STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)
{
   stbtt_uint8 *data = info->data;
   stbtt_uint32 index_map = info->index_map;

   stbtt_uint16 format = ttUSHORT(data + index_map + 0);
   if (format == 0) { // apple byte encoding
      stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
      if (unicode_codepoint < bytes-6)
         return ttBYTE(data + index_map + 6 + unicode_codepoint);
      return 0;
   } else if (format == 6) {
      stbtt_uint32 first = ttUSHORT(data + index_map + 6);
      stbtt_uint32 count = ttUSHORT(data + index_map + 8);
      if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)
         return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
      return 0;
   } else if (format == 2) {
      STBTT_assert(0); // @TODO: high-byte mapping for japanese/chinese/korean
      return 0;
   } else if (format == 4) { // standard mapping for windows fonts: binary search collection of ranges
      stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1;
      stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1;
      stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10);
      stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1;

      // do a binary search of the segments
      stbtt_uint32 endCount = index_map + 14;
      stbtt_uint32 search = endCount;

      if (unicode_codepoint > 0xffff)
         return 0;

      // they lie from endCount .. endCount + segCount
      // but searchRange is the nearest power of two, so...
      if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))
         search += rangeShift*2;

      // now decrement to bias correctly to find smallest
      search -= 2;
      while (entrySelector) {
         stbtt_uint16 end;
         searchRange >>= 1;
         end = ttUSHORT(data + search + searchRange*2);
         if (unicode_codepoint > end)
            search += searchRange*2;
         --entrySelector;
      }
      search += 2;

      {
         stbtt_uint16 offset, start;
         stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1);

         STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));
         start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
         if (unicode_codepoint < start)
            return 0;

         offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
         if (offset == 0)
            return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));

         return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
      }
   } else if (format == 12 || format == 13) {
      stbtt_uint32 ngroups = ttULONG(data+index_map+12);
      stbtt_int32 low,high;
      low = 0; high = (stbtt_int32)ngroups;
      // Binary search the right group.
      while (low < high) {
         stbtt_int32 mid = low + ((high-low) >> 1); // rounds down, so low <= mid < high
         stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12);
         stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4);
         if ((stbtt_uint32) unicode_codepoint < start_char)
            high = mid;
         else if ((stbtt_uint32) unicode_codepoint > end_char)
            low = mid+1;
         else {
            stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);
            if (format == 12)
               return start_glyph + unicode_codepoint-start_char;
            else // format == 13
               return start_glyph;
         }
      }
      return 0; // not found
   }
   // @TODO
   STBTT_assert(0);
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
callq 0x11f5b0
movw %ax, -0x26(%rbp)
movzwl -0x26(%rbp), %eax
cmpl $0x0, %eax
jne 0x1190b1
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x2, %rdi
callq 0x11f5b0
movzwl %ax, %eax
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl -0x2c(%rbp), %ecx
subl $0x6, %ecx
cmpl %ecx, %eax
jge 0x1190a5
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
addq %rcx, %rax
movslq -0x14(%rbp), %rcx
movzbl 0x6(%rax,%rcx), %eax
movl %eax, -0x4(%rbp)
jmp 0x11954d
movl $0x0, -0x4(%rbp)
jmp 0x11954d
movzwl -0x26(%rbp), %eax
cmpl $0x6, %eax
jne 0x119138
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x6, %rdi
callq 0x11f5b0
movzwl %ax, %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x8, %rdi
callq 0x11f5b0
movzwl %ax, %eax
movl %eax, -0x34(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x30(%rbp), %eax
jb 0x11912c
movl -0x14(%rbp), %eax
movl -0x30(%rbp), %ecx
addl -0x34(%rbp), %ecx
cmpl %ecx, %eax
jae 0x11912c
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0xa, %rdi
movl -0x14(%rbp), %eax
subl -0x30(%rbp), %eax
shll %eax
movl %eax, %eax
addq %rax, %rdi
callq 0x11f5b0
movzwl %ax, %eax
movl %eax, -0x4(%rbp)
jmp 0x11954d
movl $0x0, -0x4(%rbp)
jmp 0x11954d
movzwl -0x26(%rbp), %eax
cmpl $0x2, %eax
jne 0x119162
jmp 0x119143
leaq 0x5bfc0(%rip), %rdi     # 0x17510a
leaq 0x71e99(%rip), %rsi     # 0x18afea
movl $0x5c7, %edx            # imm = 0x5C7
leaq 0x72087(%rip), %rcx     # 0x18b1e4
callq 0xd180
movzwl -0x26(%rbp), %eax
cmpl $0x4, %eax
jne 0x119418
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x6, %rdi
callq 0x11f5b0
movzwl %ax, %eax
sarl %eax
movw %ax, -0x36(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x8, %rdi
callq 0x11f5b0
movzwl %ax, %eax
sarl %eax
movw %ax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0xa, %rdi
callq 0x11f5b0
movw %ax, -0x3a(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0xc, %rdi
callq 0x11f5b0
movzwl %ax, %eax
sarl %eax
movw %ax, -0x3c(%rbp)
movl -0x24(%rbp), %eax
addl $0xe, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0xffff, -0x14(%rbp)    # imm = 0xFFFF
jle 0x1191fe
movl $0x0, -0x4(%rbp)
jmp 0x11954d
movl -0x14(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x20(%rbp), %rdi
movl -0x44(%rbp), %eax
addq %rax, %rdi
movzwl -0x3c(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x11f5b0
movw %ax, %cx
movl -0x6c(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
jl 0x119237
movzwl -0x3c(%rbp), %eax
shll %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x44(%rbp)
cmpw $0x0, -0x3a(%rbp)
je 0x119294
movzwl -0x38(%rbp), %eax
sarl %eax
movw %ax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movl -0x44(%rbp), %eax
addq %rax, %rdi
movzwl -0x38(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x11f5b0
movw %ax, -0x46(%rbp)
movl -0x14(%rbp), %eax
movzwl -0x46(%rbp), %ecx
cmpl %ecx, %eax
jle 0x119286
movzwl -0x38(%rbp), %eax
shll %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movw -0x3a(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0x3a(%rbp)
jmp 0x119240
movl -0x44(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
subl -0x40(%rbp), %eax
shrl %eax
movw %ax, -0x4c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x20(%rbp), %rdi
movl -0x40(%rbp), %eax
addq %rax, %rdi
movzwl -0x4c(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x11f5b0
movw %ax, %cx
movl -0x70(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
jg 0x1192d8
jmp 0x1192f7
leaq 0x71f3b(%rip), %rdi     # 0x18b21a
leaq 0x71d04(%rip), %rsi     # 0x18afea
movl $0x5eb, %edx            # imm = 0x5EB
leaq 0x71ef2(%rip), %rcx     # 0x18b1e4
callq 0xd180
jmp 0x1192f9
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0xe, %rdi
movzwl -0x36(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
addq $0x2, %rdi
movzwl -0x4c(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x11f5b0
movw %ax, -0x4a(%rbp)
movl -0x14(%rbp), %eax
movzwl -0x4a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x119341
movl $0x0, -0x4(%rbp)
jmp 0x11954d
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0xe, %rdi
movzwl -0x36(%rbp), %eax
imull $0x6, %eax, %eax
cltq
addq %rax, %rdi
addq $0x2, %rdi
movzwl -0x4c(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x11f5b0
movw %ax, -0x48(%rbp)
movzwl -0x48(%rbp), %eax
cmpl $0x0, %eax
jne 0x1193c6
movl -0x14(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0xe, %rdi
movzwl -0x36(%rbp), %eax
shll $0x2, %eax
cltq
addq %rax, %rdi
addq $0x2, %rdi
movzwl -0x4c(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x11f960
movw %ax, %cx
movl -0x74(%rbp), %eax
movswl %cx, %ecx
addl %ecx, %eax
movzwl %ax, %eax
movl %eax, -0x4(%rbp)
jmp 0x11954d
movq -0x20(%rbp), %rdi
movzwl -0x48(%rbp), %eax
cltq
addq %rax, %rdi
movl -0x14(%rbp), %eax
movzwl -0x4a(%rbp), %ecx
subl %ecx, %eax
shll %eax
cltq
addq %rax, %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0xe, %rdi
movzwl -0x36(%rbp), %eax
imull $0x6, %eax, %eax
cltq
addq %rax, %rdi
addq $0x2, %rdi
movzwl -0x4c(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x11f5b0
movzwl %ax, %eax
movl %eax, -0x4(%rbp)
jmp 0x11954d
movzwl -0x26(%rbp), %eax
cmpl $0xc, %eax
je 0x11942e
movzwl -0x26(%rbp), %eax
cmpl $0xd, %eax
jne 0x119524
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0xc, %rdi
callq 0x11e7b0
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x11951b
movl -0x54(%rbp), %eax
movl -0x58(%rbp), %ecx
subl -0x54(%rbp), %ecx
sarl %ecx
addl %ecx, %eax
movl %eax, -0x5c(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x10, %rdi
imull $0xc, -0x5c(%rbp), %eax
cltq
addq %rax, %rdi
callq 0x11e7b0
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x10, %rdi
imull $0xc, -0x5c(%rbp), %eax
cltq
addq %rax, %rdi
addq $0x4, %rdi
callq 0x11e7b0
movl %eax, -0x64(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x1194bf
movl -0x5c(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x119516
movl -0x14(%rbp), %eax
cmpl -0x64(%rbp), %eax
jbe 0x1194d2
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x119514
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x10, %rdi
imull $0xc, -0x5c(%rbp), %eax
cltq
addq %rax, %rdi
addq $0x8, %rdi
callq 0x11e7b0
movl %eax, -0x68(%rbp)
movzwl -0x26(%rbp), %eax
cmpl $0xc, %eax
jne 0x11950c
movl -0x68(%rbp), %eax
addl -0x14(%rbp), %eax
subl -0x60(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11954d
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x11954d
jmp 0x119516
jmp 0x119451
movl $0x0, -0x4(%rbp)
jmp 0x11954d
jmp 0x119526
jmp 0x119528
jmp 0x11952a
jmp 0x11952c
jmp 0x11952e
leaq 0x5bbd5(%rip), %rdi     # 0x17510a
leaq 0x71aae(%rip), %rsi     # 0x18afea
movl $0x60e, %edx            # imm = 0x60E
leaq 0x71c9c(%rip), %rcx     # 0x18b1e4
callq 0xd180
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)