// File: /workspace/github_c_makefile_star2_O1/smilingthax[P]libfontembed/cff.c
// Function: cff_load_index
static int cff_load_index(CFF_FILE *cff,int pos,struct CFF_Index *idx) // {{{
{
  assert(idx);
  assert(idx->count==0); // not already initialized

  idx->offset=NULL;
  if (pos+2>=cff->length) {
    return -1;
  }
  const char *buf=cff->font+pos;
  idx->count=cff_read_Card16(&buf);
  if (idx->count==0) {
    return buf-cff->font; // pos+2
  }

  if (pos+3+idx->count+1>=cff->length) {
    return -1;
  }
  idx->offSize=cff_read_OffSize(&buf);
  if (idx->offSize==OFFERR) {
    return -1;
  } else if (pos+3+(idx->count+1)*idx->offSize>=cff->length) {
    return -1;
  }

  idx->offset=malloc((idx->count+1)*sizeof(*idx->offset));
  if (!idx->offset) {
    fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
    return -1;
  }

  uint32_t last=1;
  int iA;
  const int len=idx->count+1;
  for (iA=0;iA<len;iA++) {
    const uint32_t res=cff_read_Offset(&buf,idx->offSize);
    if (res<last) {
      free(idx->offset);
      idx->offset=NULL;
      fprintf(stderr,"Bad INDEX\n");
      return -1;
    }
    last=res;
    idx->offset[iA]=res;
  }

  idx->dataStart=buf;

  // pos+3+(idx->count+1)*idx->offSize+last-1;
  const int ret=buf+last-1-cff->font;
  if (ret>cff->length) { // == is ok (file ends directly after us)
    return -1;
  }
  return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdx, %rdx
je 0x746c
movq %rdx, %r15
cmpw $0x0, (%rdx)
jne 0x748b
movq %rdi, %r14
movq $0x0, 0x8(%r15)
leal 0x2(%rsi), %ecx
movq 0x8(%rdi), %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
jbe 0x742a
movq (%r14), %r13
movl %esi, %ecx
movzwl (%r13,%rcx), %edx
rolw $0x8, %dx
movw %dx, (%r15)
testw %dx, %dx
je 0x741f
movzwl %dx, %r12d
leal (%r12,%rsi), %edx
addl $0x4, %edx
movslq %edx, %rdx
cmpq %rdx, %rax
jbe 0x742a
addq %rcx, %r13
movzbl 0x2(%r13), %ecx
leal -0x5(%rcx), %edx
xorl %edi, %edi
cmpb $-0x4, %dl
cmovael %ecx, %edi
movzbl %dil, %ecx
movl %ecx, 0x4(%r15)
testb %cl, %cl
je 0x742a
addl $0x3, %esi
incl %r12d
imull %r12d, %ecx
addl %esi, %ecx
cmpq %rcx, %rax
jbe 0x742a
leaq 0x8(%r15), %rbp
leal (,%r12,4), %edi
callq 0x4150
movq %rax, (%rbp)
testq %rax, %rax
je 0x7465
movq %rbp, %rdx
addq $0x3, %r13
movl %r12d, %esi
movl $0x1, %r12d
xorl %ebp, %ebp
leaq 0x143f(%rip), %r8       # 0x87b8
movl 0x4(%r15), %eax
decl %eax
cmpl $0x3, %eax
ja 0x74aa
movslq (%r8,%rax,4), %rax
addq %r8, %rax
jmpq *%rax
movzbl (%r13), %ebx
incq %r13
jmp 0x73d6
movzbl (%r13), %eax
shll $0x10, %eax
movzbl 0x1(%r13), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x2(%r13), %ebx
orl %ecx, %ebx
addq $0x3, %r13
jmp 0x73d6
movl (%r13), %ebx
bswapl %ebx
addq $0x4, %r13
jmp 0x73d6
movzwl (%r13), %eax
rolw $0x8, %ax
movzwl %ax, %ebx
addq $0x2, %r13
movq (%rdx), %rdi
cmpl %r12d, %ebx
jb 0x73f5
movl %ebx, (%rdi,%rbp,4)
movl %ebx, %eax
cmpl %r12d, %ebx
jb 0x743b
incq %rbp
movl %eax, %r12d
cmpq %rbp, %rsi
jne 0x7379
jmp 0x7442
movq %rdx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq 0x8(%rsp), %rsi
callq 0x4a91
leaq 0x13a8(%rip), %r8       # 0x87b8
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movl %r12d, %eax
jmp 0x73e3
leaq (%rcx,%r13), %rbx
addq $0x2, %rbx
subl %r13d, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x742a
movq %r13, 0x10(%r15)
movl %eax, %eax
movq (%r14), %rcx
notq %rcx
addq %r13, %rcx
addq %rax, %rcx
movslq %ecx, %rbx
cmpq 0x8(%r14), %rbx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmoval %eax, %ebx
jmp 0x742a
callq 0x4abb
jmp 0x742a
leaq 0x1672(%rip), %rdi      # 0x8ae5
leaq 0xc43(%rip), %rsi       # 0x80bd
leaq 0x1668(%rip), %rcx      # 0x8ae9
movl $0x18e, %edx            # imm = 0x18E
callq 0x40c0
leaq 0x168f(%rip), %rdi      # 0x8b21
leaq 0xc24(%rip), %rsi       # 0x80bd
leaq 0x1649(%rip), %rcx      # 0x8ae9
movl $0x18f, %edx            # imm = 0x18F
callq 0x40c0
leaq 0x2538(%rip), %rdi      # 0x99e9
leaq 0x1682(%rip), %rsi      # 0x8b3a
leaq 0x1687(%rip), %rcx      # 0x8b46
movl $0x49, %edx
callq 0x40c0
nopl (%rax)