// File: Ciaxur[P]yagb-emu/include/imgui/imstb_truetype.h
// Function: stbtt_InitFont_internal(stbtt_fontinfo*, unsigned char*, int)
static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart)
{
   stbtt_uint32 cmap, t;
   stbtt_int32 i,numTables;

   info->data = data;
   info->fontstart = fontstart;
   info->cff = stbtt__new_buf(NULL, 0);

   cmap = stbtt__find_table(data, fontstart, "cmap");       // required
   info->loca = stbtt__find_table(data, fontstart, "loca"); // required
   info->head = stbtt__find_table(data, fontstart, "head"); // required
   info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required
   info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required
   info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required
   info->kern = stbtt__find_table(data, fontstart, "kern"); // not required
   info->gpos = stbtt__find_table(data, fontstart, "GPOS"); // not required

   if (!cmap || !info->head || !info->hhea || !info->hmtx)
      return 0;
   if (info->glyf) {
      // required for truetype
      if (!info->loca) return 0;
   } else {
      // initialization for CFF / Type2 fonts (OTF)
      stbtt__buf b, topdict, topdictidx;
      stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;
      stbtt_uint32 cff;

      cff = stbtt__find_table(data, fontstart, "CFF ");
      if (!cff) return 0;

      info->fontdicts = stbtt__new_buf(NULL, 0);
      info->fdselect = stbtt__new_buf(NULL, 0);

      // @TODO this should use size from table (not 512MB)
      info->cff = stbtt__new_buf(data+cff, 512*1024*1024);
      b = info->cff;

      // read the header
      stbtt__buf_skip(&b, 2);
      stbtt__buf_seek(&b, stbtt__buf_get8(&b)); // hdrsize

      // @TODO the name INDEX could list multiple fonts,
      // but we just use the first one.
      stbtt__cff_get_index(&b);  // name INDEX
      topdictidx = stbtt__cff_get_index(&b);
      topdict = stbtt__cff_index_get(topdictidx, 0);
      stbtt__cff_get_index(&b);  // string INDEX
      info->gsubrs = stbtt__cff_get_index(&b);

      stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);
      stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
      stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);
      stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);
      info->subrs = stbtt__get_subrs(b, topdict);

      // we only support Type 2 charstrings
      if (cstype != 2) return 0;
      if (charstrings == 0) return 0;

      if (fdarrayoff) {
         // looks like a CID font
         if (!fdselectoff) return 0;
         stbtt__buf_seek(&b, fdarrayoff);
         info->fontdicts = stbtt__cff_get_index(&b);
         info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size-fdselectoff);
      }

      stbtt__buf_seek(&b, charstrings);
      info->charstrings = stbtt__cff_get_index(&b);
   }

   t = stbtt__find_table(data, fontstart, "maxp");
   if (t)
      info->numGlyphs = ttUSHORT(data+t+4);
   else
      info->numGlyphs = 0xffff;

   // find a cmap encoding table we understand *now* to avoid searching
   // later. (todo: could make this installable)
   // the same regardless of glyph.
   numTables = ttUSHORT(data + cmap + 2);
   info->index_map = 0;
   for (i=0; i < numTables; ++i) {
      stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
      // find an encoding we understand:
      switch(ttUSHORT(data+encoding_record)) {
         case STBTT_PLATFORM_ID_MICROSOFT:
            switch (ttUSHORT(data+encoding_record+2)) {
               case STBTT_MS_EID_UNICODE_BMP:
               case STBTT_MS_EID_UNICODE_FULL:
                  // MS/Unicode
                  info->index_map = cmap + ttULONG(data+encoding_record+4);
                  break;
            }
            break;
        case STBTT_PLATFORM_ID_UNICODE:
            // Mac/iOS has these
            // all the encodingIDs are unicode, so we don't bother to check it
            info->index_map = cmap + ttULONG(data+encoding_record+4);
            break;
      }
   }
   if (info->index_map == 0)
      return 0;

   info->indexToLocFormat = ttUSHORT(data+info->head + 50);
   return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x11ef40
movq %rax, -0x40(%rbp)
movq %rdx, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, 0x40(%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c714(%rip), %rdx     # 0x18afa6
callq 0x11efa0
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c703(%rip), %rdx     # 0x18afab
callq 0x11efa0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c6ec(%rip), %rdx     # 0x18afb0
callq 0x11efa0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c6d5(%rip), %rdx     # 0x18afb5
callq 0x11efa0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c6be(%rip), %rdx     # 0x18afba
callq 0x11efa0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c6a7(%rip), %rdx     # 0x18afbf
callq 0x11efa0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c690(%rip), %rdx     # 0x18afc4
callq 0x11efa0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c679(%rip), %rdx     # 0x18afc9
callq 0x11efa0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
cmpl $0x0, -0x20(%rbp)
je 0x11e982
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x11e982
movq -0x10(%rbp), %rax
cmpl $0x0, 0x24(%rax)
je 0x11e982
movq -0x10(%rbp), %rax
cmpl $0x0, 0x28(%rax)
jne 0x11e98e
movl $0x0, -0x4(%rbp)
jmp 0x11ef33
movq -0x10(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0x11e9b3
movq -0x10(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x11e9ae
movl $0x0, -0x4(%rbp)
jmp 0x11ef33
jmp 0x11ed93
movl $0x2, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c5f1(%rip), %rdx     # 0x18afce
callq 0x11efa0
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jne 0x11e9fd
movl $0x0, -0x4(%rbp)
jmp 0x11ef33
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x11ef40
movq %rax, -0x98(%rbp)
movq %rdx, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, 0x80(%rax)
movq -0x90(%rbp), %rcx
movq %rcx, 0x88(%rax)
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x11ef40
movq %rax, -0xa8(%rbp)
movq %rdx, -0xa0(%rbp)
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq %rcx, 0x90(%rax)
movq -0xa0(%rbp), %rcx
movq %rcx, 0x98(%rax)
movq -0x18(%rbp), %rdi
movl -0x84(%rbp), %eax
addq %rax, %rdi
movl $0x20000000, %esi       # imm = 0x20000000
callq 0x11ef40
movq %rax, -0xb8(%rbp)
movq %rdx, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq %rcx, 0x40(%rax)
movq -0xb0(%rbp), %rcx
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rcx
movq %rcx, -0x50(%rbp)
movq 0x48(%rax), %rax
movq %rax, -0x48(%rbp)
leaq -0x50(%rbp), %rdi
movl $0x2, %esi
callq 0x11f090
leaq -0x50(%rbp), %rdi
callq 0x11f150
movzbl %al, %esi
leaq -0x50(%rbp), %rdi
callq 0x11f0c0
leaq -0x50(%rbp), %rdi
callq 0x11f1a0
movq %rax, -0xc8(%rbp)
movq %rdx, -0xc0(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x11f1a0
movq %rax, -0xd8(%rbp)
movq %rdx, -0xd0(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
xorl %edx, %edx
callq 0x11f280
movq %rax, -0xe8(%rbp)
movq %rdx, -0xe0(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x11f1a0
movq %rax, -0x108(%rbp)
movq %rdx, -0x100(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x11f1a0
movq %rax, -0x118(%rbp)
movq %rdx, -0x110(%rbp)
movq -0x10(%rbp), %rax
movq -0x118(%rbp), %rcx
movq %rcx, 0x60(%rax)
movq -0x110(%rbp), %rcx
movq %rcx, 0x68(%rax)
leaq -0x60(%rbp), %rdi
movl $0x11, %esi
movl $0x1, %edx
leaq -0x78(%rbp), %rcx
callq 0x11f3c0
leaq -0x60(%rbp), %rdi
movl $0x106, %esi            # imm = 0x106
movl $0x1, %edx
leaq -0x74(%rbp), %rcx
callq 0x11f3c0
leaq -0x60(%rbp), %rdi
movl $0x124, %esi            # imm = 0x124
movl $0x1, %edx
leaq -0x7c(%rbp), %rcx
callq 0x11f3c0
leaq -0x60(%rbp), %rdi
movl $0x125, %esi            # imm = 0x125
movl $0x1, %edx
leaq -0x80(%rbp), %rcx
callq 0x11f3c0
movq -0x50(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x138(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq -0x148(%rbp), %rdx
movq -0x140(%rbp), %rcx
callq 0x11f440
movq %rax, -0x128(%rbp)
movq %rdx, -0x120(%rbp)
movq -0x10(%rbp), %rax
movq -0x128(%rbp), %rcx
movq %rcx, 0x70(%rax)
movq -0x120(%rbp), %rcx
movq %rcx, 0x78(%rax)
cmpl $0x2, -0x74(%rbp)
je 0x11eca5
movl $0x0, -0x4(%rbp)
jmp 0x11ef33
cmpl $0x0, -0x78(%rbp)
jne 0x11ecb7
movl $0x0, -0x4(%rbp)
jmp 0x11ef33
cmpl $0x0, -0x7c(%rbp)
je 0x11ed56
cmpl $0x0, -0x80(%rbp)
jne 0x11ecd3
movl $0x0, -0x4(%rbp)
jmp 0x11ef33
movl -0x7c(%rbp), %esi
leaq -0x50(%rbp), %rdi
callq 0x11f0c0
leaq -0x50(%rbp), %rdi
callq 0x11f1a0
movq %rax, -0x158(%rbp)
movq %rdx, -0x150(%rbp)
movq -0x10(%rbp), %rax
movq -0x158(%rbp), %rcx
movq %rcx, 0x80(%rax)
movq -0x150(%rbp), %rcx
movq %rcx, 0x88(%rax)
movl -0x80(%rbp), %esi
movl -0x44(%rbp), %edx
subl -0x80(%rbp), %edx
leaq -0x50(%rbp), %rdi
callq 0x11f530
movq %rax, -0x168(%rbp)
movq %rdx, -0x160(%rbp)
movq -0x10(%rbp), %rax
movq -0x168(%rbp), %rcx
movq %rcx, 0x90(%rax)
movq -0x160(%rbp), %rcx
movq %rcx, 0x98(%rax)
movl -0x78(%rbp), %esi
leaq -0x50(%rbp), %rdi
callq 0x11f0c0
leaq -0x50(%rbp), %rdi
callq 0x11f1a0
movq %rax, -0x178(%rbp)
movq %rdx, -0x170(%rbp)
movq -0x10(%rbp), %rax
movq -0x178(%rbp), %rcx
movq %rcx, 0x50(%rax)
movq -0x170(%rbp), %rcx
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0x6c232(%rip), %rdx     # 0x18afd3
callq 0x11efa0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x11edce
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %eax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x11f5b0
movzwl %ax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
jmp 0x11edd9
movq -0x10(%rbp), %rax
movl $0xffff, 0x14(%rax)     # imm = 0xFFFF
movq -0x18(%rbp), %rdi
movl -0x20(%rbp), %eax
addq %rax, %rdi
addq $0x2, %rdi
callq 0x11f5b0
movzwl %ax, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, 0x34(%rax)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x11eef7
movl -0x20(%rbp), %edx
movl -0x28(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x4(%rax,%rcx,8), %eax
movl %eax, -0x17c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x17c(%rbp), %eax
addq %rax, %rdi
callq 0x11f5b0
movzwl %ax, %ecx
movl %ecx, -0x180(%rbp)
testw %ax, %ax
je 0x11eeb9
jmp 0x11ee46
movl -0x180(%rbp), %eax
subl $0x3, %eax
jne 0x11eee7
jmp 0x11ee57
movq -0x18(%rbp), %rax
movl -0x17c(%rbp), %ecx
leaq 0x2(%rax,%rcx), %rdi
callq 0x11f5b0
movzwl %ax, %ecx
movl %ecx, -0x184(%rbp)
subw $0x1, %ax
je 0x11ee89
jmp 0x11ee7c
movl -0x184(%rbp), %eax
subl $0xa, %eax
jne 0x11eeb7
jmp 0x11ee89
movl -0x20(%rbp), %eax
movl %eax, -0x188(%rbp)
movq -0x18(%rbp), %rdi
movl -0x17c(%rbp), %eax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x11e7b0
movl -0x188(%rbp), %ecx
addl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
jmp 0x11eee7
movl -0x20(%rbp), %eax
movl %eax, -0x18c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x17c(%rbp), %eax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x11e7b0
movl -0x18c(%rbp), %ecx
addl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
jmp 0x11eee9
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x11ee04
movq -0x10(%rbp), %rax
cmpl $0x0, 0x34(%rax)
jne 0x11ef0a
movl $0x0, -0x4(%rbp)
jmp 0x11ef33
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0x1c(%rax), %rax
addq %rax, %rdi
addq $0x32, %rdi
callq 0x11f5b0
movzwl %ax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nop