// File: /workspace/github_c_makefile_star2_O2/smilingthax[P]libfontembed/sfnt_write.c
// Function: otf_write_sfnt
int otf_write_sfnt(struct _OTF_WRITE_INFO *otw,struct _OTF_WRITE_WOFF *woff,OUTPUT_FN output,void *context) // {{{
{
  int iA;
  const int sfntStartSize=12+16*otw->numTables;

  char *sfntStart=malloc(sfntStartSize);
  if (!sfntStart) {
    fprintf(stderr,"Bad alloc: %s\n", strerror(errno));
    return -1;
  }

  // first pass: calculate table directory / offsets and checksums
  unsigned int globalSum=0;
  int offset=sfntStartSize;
  int headAt=-1;
  for (iA=0;iA<otw->numTables;iA++) {
    struct _OTF_WRITE_TABLE *self=otwTableEntry(otw,iA);

    if (self->tag==OTF_TAG('h','e','a','d')) {
      headAt=iA;
    }

    assert(self->action); // must already be filtered out by otf_intersect_tables
    (*self->action)(self);
    self->info.table.offset=offset;

    offset+=otf_padded(self->info.table.length);
    globalSum+=self->info.table.checkSum;
  }

  // the sfnt header
  const int res=otf_write_header(otw,sfntStart);
  assert(res==sfntStartSize);
  globalSum+=otf_checksum(sfntStart,sfntStartSize);

  int ret=0;
  if (!woff) { // simple sfnt
    // write header + directory
    ret=sfntStartSize;
    (*output)(sfntStart,ret,context);
  }
  free(sfntStart);

  // fix head table checksum
  if (headAt!=-1) {
    struct _OTF_WRITE_TABLE *self=otwTableEntry(otw,headAt);
    if (action_load_data(self)!=0) {
      return -1;
    }
    set_ULONG(self->info.data+8,0xb1b0afba-globalSum); // fix global checksum
    // TODO? >modify head time-stamp?
  }

  // pass 1.5: compress data, write woff header
  if (woff) {
    const int origLength=offset;
    const int woffStartSize=44+20*otw->numTables;

    offset=woffStartSize;
    for (iA=0;iA<otw->numTables;iA++) {
      struct _OTF_WRITE_TABLE *self=otwTableEntry(otw,iA);
      if ( (action_load_data(self)!=0)||
           (action_hlp_compress(self)!=0) ) {
        ret=-1;
        goto done;
      }
      self->info.table.offset=offset;
      offset+=otf_padded(self->info.table.compLength);
    }

    OTF_WOFF_HEADER woffHdr;
    if (otf_prepare_woff_header(&woffHdr,woff,origLength,offset)!=0) {
      ret=-1;
      goto done;
    }
    offset=woffHdr.length;
    ret=otf_write_woff_header(otw,&woffHdr,output,context);
    assert(ret==woffStartSize);
  }

  // second pass: write tables
  for (iA=0;iA<otw->numTables;iA++) {
    struct _OTF_WRITE_TABLE *self=otwTableEntry(otw,iA);
    if (action_load_data(self)!=0) {
      ret=-1;
      goto done;
    }

    const int pad_len=otf_padded(self->info.table.compLength);
    (*output)(self->info.data,pad_len,context);

    (*self->info.free)(self);
    ret+=pad_len;
  }

  if (woff) {
    ret+=otf_write_woff_footer(woff,output,context);
  }
  assert(ret==offset);

done:
  if (ret==-1) {
    for (iA=0;iA<otw->numTables;iA++) {
      (*otw->tables[iA].info.free)(&otw->tables[iA]);
    }
  }
  if (woff) {
    free(woff->metaCompData);
    woff->metaCompData=NULL;
  }
  return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movzwl 0x4(%rdi), %ebx
movl %ebx, %r15d
shll $0x4, %r15d
orq $0xc, %r15
movq %r15, %rdi
callq 0x21d0
testq %rax, %rax
je 0x8c93
movq %rax, %rbp
pushq $-0x1
popq %r12
movq %rbx, %rax
xorl %ebx, %ebx
movq %rax, 0x28(%rsp)
movq %r15, 0x68(%rsp)
movl %r15d, 0xc(%rsp)
xorl %r15d, %r15d
movzwl %ax, %eax
cmpq %rax, %rbx
jae 0x8a12
movq 0x10(%r14), %rcx
movq %rbx, %rax
testq %rcx, %rcx
je 0x89c2
movslq (%rcx,%rbx,4), %rax
movq 0x8(%r14), %r13
imulq $0x50, %rax, %rcx
movq 0x8(%r13,%rcx), %rax
testq %rax, %rax
je 0x8fdb
addq %rcx, %r13
cmpq $0x68656164, (%r13)     # imm = 0x68656164
cmovel %ebx, %r12d
movq %r13, %rdi
callq *%rax
movl 0xc(%rsp), %ecx
movl %ecx, 0x30(%r13)
movl 0x34(%r13), %eax
addl $0x3, %eax
andl $-0x4, %eax
addl %eax, %ecx
movl %ecx, 0xc(%rsp)
addl 0x2c(%r13), %r15d
incq %rbx
movzwl 0x4(%r14), %eax
jmp 0x89aa
movl (%r14), %ecx
movl %ecx, %edx
shrl $0x18, %edx
movb %dl, (%rbp)
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x1(%rbp)
movb %ch, 0x2(%rbp)
movb %cl, 0x3(%rbp)
movb %ah, 0x4(%rbp)
movb %al, 0x5(%rbp)
testw %ax, %ax
je 0x8ffa
pushq $0x1
popq %rdx
movw $0xffff, %cx            # imm = 0xFFFF
cmpl %eax, %edx
jg 0x8a4b
addl %edx, %edx
incl %ecx
jmp 0x8a41
leal (,%rdx,8), %esi
shll $0x4, %eax
movl %eax, %ebx
subl %esi, %ebx
shrl $0x5, %edx
movb %dl, 0x6(%rbp)
movb %sil, 0x7(%rbp)
movb %ch, 0x8(%rbp)
movb %cl, 0x9(%rbp)
movb %bh, 0xa(%rbp)
movb %bl, 0xb(%rbp)
movq 0x8(%r14), %rdx
addq $0x34, %rdx
xorl %ecx, %ecx
movq 0x28(%rsp), %rdi
cmpq %rcx, %rax
je 0x8ab0
movl -0xc(%rdx), %esi
bswapl %esi
movl %esi, 0xc(%rbp,%rcx)
movl -0x8(%rdx), %esi
bswapl %esi
movl %esi, 0x10(%rbp,%rcx)
movl -0x4(%rdx), %esi
bswapl %esi
movl %esi, 0x14(%rbp,%rcx)
movl (%rdx), %esi
bswapl %esi
movl %esi, 0x18(%rbp,%rcx)
addq $0x10, %rcx
addq $0x50, %rdx
jmp 0x8a7e
shll $0x4, %edi
cmpl %ecx, %edi
jne 0x9017
movq %rbp, %rdi
movq 0x68(%rsp), %rbx
movl %ebx, %esi
callq 0x8930
movl %eax, %r13d
xorl %eax, %eax
cmpq $0x0, 0x10(%rsp)
jne 0x8ae7
movq %rbp, %rdi
movl %ebx, %esi
movq 0x18(%rsp), %rdx
callq *0x20(%rsp)
movl %ebx, %eax
movl %eax, 0x28(%rsp)
movq %rbp, %rdi
callq 0x21b0
cmpl $-0x1, %r12d
je 0x8b3c
movq 0x8(%r14), %rbp
movq 0x10(%r14), %rcx
movslq %r12d, %rax
testq %rcx, %rcx
je 0x8b0d
movslq (%rcx,%rax,4), %rax
imulq $0x50, %rax, %rax
addq %rax, %rbp
movq %rbp, %rdi
callq 0x9074
testl %eax, %eax
je 0x8b28
pushq $-0x1
popq %rax
jmp 0x8f9d
movq 0x20(%rbp), %rax
addl %r13d, %r15d
movl $0xb1b0afba, %ecx       # imm = 0xB1B0AFBA
subl %r15d, %ecx
bswapl %ecx
movl %ecx, 0x8(%rax)
cmpq $0x0, 0x10(%rsp)
je 0x8c12
movzwl 0x4(%r14), %ebx
imull $0x14, %ebx, %r15d
addl $0x2c, %r15d
xorl %r12d, %r12d
movl %ebx, %eax
movl %r15d, 0x28(%rsp)
movzwl %ax, %eax
cmpq %rax, %r12
jae 0x8c1c
movq 0x10(%r14), %rcx
movq %r12, %rax
testq %rcx, %rcx
je 0x8b7b
movslq (%rcx,%r12,4), %rax
imulq $0x50, %rax, %rbp
addq 0x8(%r14), %rbp
movq %rbp, %rdi
callq 0x9074
testl %eax, %eax
jne 0x8fac
movq 0x20(%rbp), %rdi
movl 0x34(%rbp), %esi
leaq 0x30(%rsp), %rdx
callq 0x90b3
testq %rax, %rax
je 0x8be9
movq %rax, %r13
movq %rbp, %rdi
callq *0x48(%rbp)
movq 0x30(%rsp), %rdi
leal 0x3(%rdi), %eax
movslq %eax, %rdx
andq $-0x4, %rdx
subq %rdi, %rdx
jbe 0x8bd5
addq %r13, %rdi
xorl %esi, %esi
callq 0x20d0
movq 0x30(%rsp), %rdi
movq %r13, 0x20(%rbp)
movl %edi, 0x38(%rbp)
leaq -0x37d(%rip), %rax      # 0x8866
movq %rax, 0x48(%rbp)
jmp 0x8bf5
cmpq $-0x1, 0x30(%rsp)
je 0x8ce8
movl %r15d, 0x30(%rbp)
movl 0x38(%rbp), %eax
addl $0x3, %eax
andl $-0x4, %eax
addl %eax, %r15d
incq %r12
movzwl 0x4(%r14), %eax
jmp 0x8b5f
movl 0xc(%rsp), %r12d
jmp 0x8e82
movq 0x10(%rsp), %rax
movzwl (%rax), %ecx
movl %ecx, 0x68(%rsp)
movzwl 0x2(%rax), %r13d
movq 0x8(%rax), %rdi
xorl %edx, %edx
testq %rdi, %rdi
je 0x8cc2
movl 0x10(%rax), %esi
movl $0x0, %r9d
movl $0x0, %r8d
movl $0x0, %ecx
testl %esi, %esi
je 0x8cca
movq %rbx, %rbp
leaq 0x30(%rsp), %r12
movq %r12, %rdx
movq %rax, %rbx
callq 0x90b3
movq %rax, 0x28(%rbx)
movq (%r12), %rdx
movq %rdx, %rcx
notq %rcx
orq %rax, %rcx
je 0x8fac
movl 0x10(%rbx), %r8d
leal 0x3(%rdx), %ecx
andl $-0x4, %ecx
movl %r15d, %r9d
movq %rbx, %rax
movq %rbp, %rbx
jmp 0x8cca
movq 0x6346(%rip), %rax      # 0xefe0
movq (%rax), %rbx
callq 0x2040
movl (%rax), %edi
callq 0x2070
leaq 0x16a9(%rip), %rsi      # 0xa359
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21e0
jmp 0x8b20
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %ecx, %ecx
movq %rdx, %rdi
cmpq $0x0, 0x18(%rax)
movq %rbx, 0x70(%rsp)
je 0x8cf3
movl 0x20(%rax), %edx
addl %r15d, %ecx
testl %edx, %edx
movl %ecx, %eax
cmovel %edx, %eax
jmp 0x8cfa
movq %rbp, %rdi
callq *0x48(%rbp)
jmp 0x8fac
addl %r15d, %ecx
xorl %eax, %eax
xorl %edx, %edx
leaq 0x30(%rsp), %rbp
movl $0x46464f77, (%rbp)     # imm = 0x46464F77
movl (%r14), %ebx
movl %ebx, %esi
shrl $0x18, %esi
movb %sil, 0x4(%rbp)
movl %ebx, %esi
shrl $0x10, %esi
movb %sil, 0x5(%rbp)
movb %bh, 0x36(%rsp)
movb %bl, 0x7(%rbp)
addl %edx, %ecx
movl %ecx, %esi
shrl $0x18, %esi
movb %sil, 0x8(%rbp)
movl %ecx, %esi
shrl $0x10, %esi
movb %sil, 0x9(%rbp)
movb %ch, 0x3a(%rsp)
movl %ecx, %r12d
movb %cl, 0xb(%rbp)
movzwl 0x4(%r14), %ebx
movb %bh, 0x3c(%rsp)
movb %bl, 0xd(%rbp)
andw $0x0, 0xe(%rbp)
movl 0xc(%rsp), %ebx
movl %ebx, %esi
shrl $0x18, %esi
movb %sil, 0x10(%rbp)
movl %ebx, %esi
shrl $0x10, %esi
movb %sil, 0x11(%rbp)
movb %bh, 0x42(%rsp)
movb %bl, 0x13(%rbp)
movl 0x68(%rsp), %ecx
movb %ch, 0x44(%rsp)
movb %cl, 0x15(%rbp)
movl %r13d, %ecx
movb %ch, 0x46(%rsp)
movb %cl, 0x17(%rbp)
movl %r9d, %ecx
movl %r9d, %esi
shrl $0x18, %esi
movb %sil, 0x18(%rbp)
movl %r9d, %esi
shrl $0x10, %esi
movb %sil, 0x19(%rbp)
movb %ch, 0x4a(%rsp)
movb %cl, 0x1b(%rbp)
movq %rdi, %rbx
movl %ebx, %ecx
shrl $0x18, %ecx
movb %cl, 0x1c(%rbp)
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0x1d(%rbp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x1f(%rbp)
movl %r8d, %ebx
movl %r8d, %ecx
shrl $0x18, %ecx
movb %cl, 0x20(%rbp)
movl %r8d, %ecx
shrl $0x10, %ecx
movb %cl, 0x21(%rbp)
movb %bh, 0x52(%rsp)
movb %bl, 0x23(%rbp)
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0x24(%rbp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x25(%rbp)
movb %ah, 0x56(%rsp)
movb %al, 0x27(%rbp)
movl %edx, %eax
shrl $0x18, %eax
movb %al, 0x28(%rbp)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x29(%rbp)
movb %dh, 0x5a(%rsp)
movb %dl, 0x2b(%rbp)
pushq $0x2c
popq %rsi
movq %rbp, %rdi
movq 0x18(%rsp), %rdx
callq *0x20(%rsp)
xorl %ebx, %ebx
pushq $0x14
popq %r13
xorl %r15d, %r15d
movzwl 0x4(%r14), %eax
cmpq %rax, %r15
jae 0x8e75
movq 0x8(%r14), %rax
movl 0x28(%rax,%rbx), %ecx
bswapl %ecx
movl %ecx, 0x30(%rsp)
movl 0x30(%rax,%rbx), %ecx
bswapl %ecx
movl %ecx, 0x34(%rsp)
movl 0x38(%rax,%rbx), %ecx
bswapl %ecx
movl %ecx, 0x38(%rsp)
movl 0x34(%rax,%rbx), %ecx
bswapl %ecx
movl %ecx, 0x3c(%rsp)
movl 0x2c(%rax,%rbx), %eax
bswapl %eax
movl %eax, 0x40(%rsp)
movq %rbp, %rdi
movl %r13d, %esi
movq 0x18(%rsp), %rdx
callq *0x20(%rsp)
incq %r15
addq $0x50, %rbx
jmp 0x8e1d
imull $0x50, 0x70(%rsp), %eax
cmpl %ebx, %eax
jne 0x9055
xorl %ebx, %ebx
movzwl 0x4(%r14), %eax
cmpq %rax, %rbx
jae 0x8edf
movq 0x10(%r14), %rcx
movq %rbx, %rax
testq %rcx, %rcx
je 0x8e9e
movslq (%rcx,%rbx,4), %rax
imulq $0x50, %rax, %r13
addq 0x8(%r14), %r13
movq %r13, %rdi
callq 0x9074
testl %eax, %eax
jne 0x8fac
movl 0x38(%r13), %ebp
addl $0x3, %ebp
andl $-0x4, %ebp
movq 0x20(%r13), %rdi
movl %ebp, %esi
movq 0x18(%rsp), %rdx
callq *0x20(%rsp)
movq %r13, %rdi
callq *0x48(%r13)
addl %ebp, 0x28(%rsp)
incq %rbx
jmp 0x8e84
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x8efb
movq 0x28(%rax), %rdi
testq %rdi, %rdi
je 0x8f01
leaq 0x10(%rax), %rbx
movl 0x10(%rax), %esi
jmp 0x8f15
movl 0x28(%rsp), %eax
jmp 0x8f6e
movq 0x8(%rax), %rdi
movl 0x10(%rax), %esi
testq %rdi, %rdi
je 0x8fd4
leaq 0x10(%rax), %rbx
movq 0x18(%rsp), %rdx
callq *0x20(%rsp)
movl (%rbx), %esi
movl %esi, %ebx
movq 0x10(%rsp), %rax
leal 0x3(%rsi), %ebp
andl $-0x4, %ebp
subl %esi, %ebp
je 0x8f4b
leaq 0x30(%rsp), %rdi
andl $0x0, (%rdi)
movl %ebp, %esi
movq 0x18(%rsp), %rdx
callq *0x20(%rsp)
movq 0x10(%rsp), %rax
addl %ebp, %ebx
movq 0x18(%rax), %rdi
testq %rdi, %rdi
je 0x8f68
movl 0x20(%rax), %esi
movq 0x18(%rsp), %rdx
callq *0x20(%rsp)
movq 0x10(%rsp), %rax
addl 0x20(%rax), %ebx
movl 0x28(%rsp), %eax
addl %ebx, %eax
movl %r12d, %ecx
cmpl %r12d, %eax
jne 0x9036
cmpl $-0x1, %ecx
je 0x8fac
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x8f9a
movq 0x28(%rax), %rdi
movq %rax, %rbx
callq 0x21b0
andq $0x0, 0x28(%rbx)
movl %r12d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
pushq $-0x1
popq %r12
xorl %r15d, %r15d
movzwl 0x4(%r14), %eax
cmpq %rax, %r15
jae 0x8f7f
movq 0x8(%r14), %rax
leaq (%rax,%rbx), %rdi
callq *0x48(%rax,%rbx)
incq %r15
addq $0x50, %rbx
jmp 0x8fb5
xorl %ebx, %ebx
jmp 0x8f27
leaq 0x2b5b(%rip), %rdi      # 0xbb3d
leaq 0x2b14(%rip), %rsi      # 0xbafd
leaq 0x2b5a(%rip), %rcx      # 0xbb4a
movl $0x1ac, %edx            # imm = 0x1AC
callq 0x20f0
leaq 0x2c47(%rip), %rdi      # 0xbc48
leaq 0x2af5(%rip), %rsi      # 0xbafd
leaq 0x2c3f(%rip), %rcx      # 0xbc4e
pushq $0x12
popq %rdx
callq 0x20f0
leaq 0x2b86(%rip), %rdi      # 0xbba4
leaq 0x2ad8(%rip), %rsi      # 0xbafd
leaq 0x2b1e(%rip), %rcx      # 0xbb4a
movl $0x1b6, %edx            # imm = 0x1B6
callq 0x20f0
leaq 0x2b8d(%rip), %rdi      # 0xbbca
leaq 0x2ab9(%rip), %rsi      # 0xbafd
leaq 0x2aff(%rip), %rcx      # 0xbb4a
movl $0x1f8, %edx            # imm = 0x1F8
callq 0x20f0
leaq 0x2b5b(%rip), %rdi      # 0xbbb7
leaq 0x2a9a(%rip), %rsi      # 0xbafd
leaq 0x2ae0(%rip), %rcx      # 0xbb4a
movl $0x1e3, %edx            # imm = 0x1E3
callq 0x20f0