// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rcx, 0x20(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r13
movzwl 0x4(%rdi), %r15d
movl %r15d, %edi
shll $0x4, %edi
orq $0xc, %rdi
movq %rdi, 0x8(%rsp)
callq 0x21c0
testq %rax, %rax
je 0x945c
movq %rax, %rbp
movq %rbx, 0x18(%rsp)
testq %r15, %r15
movq %r15, %r8
movq %r14, 0x28(%rsp)
je 0x9180
movq %r8, 0x10(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
movl %eax, 0x4(%rsp)
movq 0x10(%r13), %rcx
movq %r14, %rax
testq %rcx, %rcx
je 0x911b
movslq (%rcx,%r14,4), %rax
movq %r13, %rbx
movq 0x8(%r13), %r13
leaq (%rax,%rax,4), %rcx
shlq $0x4, %rcx
movq 0x8(%r13,%rcx), %rax
testq %rax, %rax
je 0x97c4
addq %rcx, %r13
cmpq $0x68656164, (%r13)     # imm = 0x68656164
cmovel %r14d, %r15d
movq %r13, %rdi
callq *%rax
movl 0x4(%rsp), %ecx
movl %ecx, 0x30(%r13)
movl 0x34(%r13), %eax
addl $0x3, %eax
andl $-0x4, %eax
addl 0x2c(%r13), %r12d
addl %eax, %ecx
movl %ecx, 0x4(%rsp)
incq %r14
movzwl 0x4(%rbx), %eax
cmpq %rax, %r14
movq %rbx, %r13
jb 0x910b
movl %eax, %ecx
movq 0x10(%rsp), %r8
jmp 0x9196
xorl %r12d, %r12d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
movl %eax, 0x4(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
movl (%r13), %edx
movl %edx, %esi
shrl $0x18, %esi
movb %sil, (%rbp)
movl %edx, %esi
shrl $0x10, %esi
movb %sil, 0x1(%rbp)
movb %dh, 0x2(%rbp)
movb %dl, 0x3(%rbp)
movb %ah, 0x4(%rbp)
movb %al, 0x5(%rbp)
testw %ax, %ax
je 0x97e3
movl $0x1, %edi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edi, %esi
leal (%rsi,%rsi), %edi
incl %edx
cmpl %ecx, %edi
jle 0x91cb
movl %esi, %ecx
shll $0x4, %ecx
movl %esi, %edi
shrl $0x4, %edi
movb %dil, 0x6(%rbp)
movb %cl, 0x7(%rbp)
movb %dh, 0x8(%rbp)
movb %dl, 0x9(%rbp)
movl %eax, %ecx
subl %esi, %ecx
movl %ecx, %edx
shll $0x4, %edx
shrl $0x4, %ecx
movb %cl, 0xa(%rbp)
movb %dl, 0xb(%rbp)
movq 0x8(%r13), %rcx
movzwl %ax, %eax
shll $0x4, %eax
addq $0x34, %rcx
xorl %edx, %edx
movl -0xc(%rcx), %esi
bswapl %esi
movl %esi, 0xc(%rbp,%rdx)
movl -0x8(%rcx), %esi
bswapl %esi
movl %esi, 0x10(%rbp,%rdx)
movl -0x4(%rcx), %esi
bswapl %esi
movl %esi, 0x14(%rbp,%rdx)
movl (%rcx), %esi
bswapl %esi
movl %esi, 0x18(%rbp,%rdx)
addq $0x10, %rdx
addq $0x50, %rcx
cmpq %rdx, %rax
jne 0x920f
movq %r13, 0x10(%rsp)
movl %r8d, %eax
shll $0x4, %eax
cmpl %edx, %eax
jne 0x9802
leaq 0x3(,%r8,4), %rax
xorl %ecx, %ecx
xorl %r13d, %r13d
movl (%rbp,%rcx,4), %edx
bswapl %edx
addl %edx, %r13d
incq %rcx
cmpl %ecx, %eax
jne 0x925f
xorl %eax, %eax
cmpq $0x0, 0x28(%rsp)
jne 0x928e
movq %rbp, %rdi
movq 0x8(%rsp), %rbx
movl %ebx, %esi
movq 0x20(%rsp), %rdx
callq *0x18(%rsp)
movl %ebx, %eax
movl %eax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x21a0
cmpl $-0x1, %r15d
je 0x92ed
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rbp
movq 0x10(%rax), %rcx
movslq %r15d, %rax
testq %rcx, %rcx
je 0x92b9
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
addq %rax, %rbp
movq %rbp, %rdi
callq 0x985f
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x97ac
movq 0x20(%rbp), %rax
addl %r13d, %r12d
movl $0xb1b0afba, %ecx       # imm = 0xB1B0AFBA
subl %r12d, %ecx
bswapl %ecx
movl %ecx, 0x8(%rax)
movq 0x10(%rsp), %r13
movzwl 0x4(%r13), %eax
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
je 0x9441
movzwl %ax, %eax
leal (%rax,%rax,4), %ecx
leal 0x2c(,%rcx,4), %r15d
movl %r15d, 0x8(%rsp)
testw %ax, %ax
je 0x93e3
xorl %r12d, %r12d
leaq 0x30(%rsp), %rbx
movl 0x8(%rsp), %r15d
movq 0x10(%r13), %rcx
movq %r12, %rax
testq %rcx, %rcx
je 0x933e
movslq (%rcx,%r12,4), %rax
leaq (%rax,%rax,4), %rbp
shlq $0x4, %rbp
addq 0x8(%r13), %rbp
movq %rbp, %rdi
callq 0x985f
testl %eax, %eax
movq %r13, %r14
jne 0x974f
movq 0x20(%rbp), %rdi
movl 0x34(%rbp), %esi
movq %rbx, %rdx
callq 0x98a0
testq %rax, %rax
je 0x93b1
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 0x939d
addq %r13, %rdi
xorl %esi, %esi
callq 0x20c0
movq 0x30(%rsp), %rdi
movq %r13, 0x20(%rbp)
movl %edi, 0x38(%rbp)
leaq -0x3ed(%rip), %rax      # 0x8fbe
movq %rax, 0x48(%rbp)
jmp 0x93bd
cmpq $-0x1, 0x30(%rsp)
je 0x9744
movl %r15d, 0x30(%rbp)
movl 0x38(%rbp), %eax
addl $0x3, %eax
andl $-0x4, %eax
addl %eax, %r15d
incq %r12
movq 0x10(%rsp), %r13
movzwl 0x4(%r13), %eax
cmpq %rax, %r12
jb 0x932e
movq 0x28(%rsp), %rbx
movzwl (%rbx), %r14d
movzwl 0x2(%rbx), %ebp
movq 0x8(%rbx), %rdi
xorl %r8d, %r8d
testq %rdi, %rdi
je 0x9452
movl 0x10(%rbx), %esi
movl $0x0, %r10d
movl $0x0, %r9d
movl $0x0, %ecx
testl %esi, %esi
movq 0x18(%rsp), %r12
je 0x947e
leaq 0x30(%rsp), %r12
movq %r12, %rdx
callq 0x98a0
movq %rax, 0x28(%rbx)
movq (%r12), %r8
movq %r8, %rcx
notq %rcx
orq %rax, %rcx
jne 0x946b
movq %r13, %r14
jmp 0x974f
movl 0x4(%rsp), %edx
movl %edx, (%rsp)
movq 0x18(%rsp), %r12
jmp 0x9632
xorl %r10d, %r10d
xorl %r9d, %r9d
xorl %ecx, %ecx
jmp 0x9479
callq 0x2ee8
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jmp 0x97ac
movl 0x10(%rbx), %r9d
leal 0x3(%r8), %ecx
andl $-0x4, %ecx
movl %r15d, %r10d
movq 0x18(%rsp), %r12
cmpq $0x0, 0x18(%rbx)
je 0x9494
movl 0x20(%rbx), %edx
addl %r15d, %ecx
testl %edx, %edx
movl %ecx, %eax
cmovel %edx, %eax
jmp 0x949b
addl %r15d, %ecx
xorl %eax, %eax
xorl %edx, %edx
movl %ecx, %ebx
leaq 0x30(%rsp), %rdi
movl $0x46464f77, (%rdi)     # imm = 0x46464F77
movl (%r13), %ecx
movl %ecx, %esi
shrl $0x18, %esi
movb %sil, 0x4(%rdi)
movl %ecx, %esi
shrl $0x10, %esi
movb %sil, 0x5(%rdi)
movb %ch, 0x36(%rsp)
movb %cl, 0x7(%rdi)
addl %edx, %ebx
movl %ebx, %ecx
shrl $0x18, %ecx
movb %cl, 0x8(%rdi)
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0x9(%rdi)
movb %bh, 0x3a(%rsp)
movl %ebx, (%rsp)
movb %bl, 0xb(%rdi)
movzwl 0x4(%r13), %ecx
movb %ch, 0x3c(%rsp)
movb %cl, 0xd(%rdi)
movw $0x0, 0xe(%rdi)
movl 0x4(%rsp), %ebx
movl %ebx, %ecx
shrl $0x18, %ecx
movb %cl, 0x10(%rdi)
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0x11(%rdi)
movb %bh, 0x42(%rsp)
movb %bl, 0x13(%rdi)
movl %r14d, %ecx
movb %ch, 0x44(%rsp)
movb %cl, 0x15(%rdi)
movl %ebp, %ecx
movb %ch, 0x46(%rsp)
movb %cl, 0x17(%rdi)
movl %r10d, %ebx
movl %r10d, %ecx
shrl $0x18, %ecx
movb %cl, 0x18(%rdi)
movl %r10d, %ecx
shrl $0x10, %ecx
movb %cl, 0x19(%rdi)
movb %bh, 0x4a(%rsp)
movb %bl, 0x1b(%rdi)
movq %r8, %rbx
movl %ebx, %ecx
shrl $0x18, %ecx
movb %cl, 0x1c(%rdi)
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0x1d(%rdi)
movb %bh, 0x4e(%rsp)
movb %bl, 0x1f(%rdi)
movl %r9d, %ebx
movl %r9d, %ecx
shrl $0x18, %ecx
movb %cl, 0x20(%rdi)
movl %r9d, %ecx
shrl $0x10, %ecx
movb %cl, 0x21(%rdi)
movb %bh, 0x52(%rsp)
movb %bl, 0x23(%rdi)
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0x24(%rdi)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x25(%rdi)
movb %ah, 0x56(%rsp)
movb %al, 0x27(%rdi)
movl %edx, %eax
shrl $0x18, %eax
movb %al, 0x28(%rdi)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x29(%rdi)
movb %dh, 0x5a(%rsp)
movb %dl, 0x2b(%rdi)
movl $0x2c, %ebx
movl $0x2c, %esi
movq 0x20(%rsp), %rdx
callq *%r12
cmpw $0x0, 0x4(%r13)
je 0x9626
movl $0xe, %ebx
xorl %r14d, %r14d
leaq 0x30(%rsp), %rbp
movq 0x20(%rsp), %r15
movq 0x8(%r13), %rax
movl -0x10(%rax,%rbx,4), %ecx
bswapl %ecx
movl %ecx, 0x30(%rsp)
movl -0x8(%rax,%rbx,4), %ecx
bswapl %ecx
movl %ecx, 0x34(%rsp)
movl (%rax,%rbx,4), %ecx
bswapl %ecx
movl %ecx, 0x38(%rsp)
movl -0x4(%rax,%rbx,4), %ecx
bswapl %ecx
movl %ecx, 0x3c(%rsp)
movl -0xc(%rax,%rbx,4), %eax
bswapl %eax
movl %eax, 0x40(%rsp)
movq %rbp, %rdi
movl $0x14, %esi
movq %r15, %rdx
callq *%r12
incq %r14
movzwl 0x4(%r13), %eax
addq $0x14, %rbx
cmpq %rax, %r14
jb 0x95cd
addl $0x1e, %ebx
jmp 0x9628
xorl %eax, %eax
cmpl 0x8(%rsp), %ebx
jne 0x9840
testw %ax, %ax
je 0x96a2
xorl %ebx, %ebx
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r12
movq 0x10(%r13), %rcx
movq %rbx, %rax
testq %rcx, %rcx
je 0x9653
movslq (%rcx,%rbx,4), %rax
leaq (%rax,%rax,4), %r13
shlq $0x4, %r13
addq 0x8(%r14), %r13
movq %r13, %rdi
callq 0x985f
testl %eax, %eax
jne 0x974f
movl 0x38(%r13), %ebp
addl $0x3, %ebp
andl $-0x4, %ebp
movq 0x20(%r13), %rdi
movl %ebp, %esi
movq 0x20(%rsp), %rdx
callq *%r12
movq %r13, %rdi
callq *0x48(%r13)
addl %ebp, 0x8(%rsp)
incq %rbx
movzwl 0x4(%r14), %eax
cmpq %rax, %rbx
movq %r14, %r13
jb 0x9643
movq 0x28(%rsp), %r15
testq %r15, %r15
je 0x96c4
movq 0x28(%r15), %rdi
testq %rdi, %rdi
je 0x96cd
leaq 0x10(%r15), %rbx
movl 0x10(%r15), %esi
movq 0x20(%rsp), %r14
jmp 0x96e7
movl 0x8(%rsp), %eax
movl (%rsp), %ecx
jmp 0x9730
movq 0x8(%r15), %rdi
movl 0x10(%r15), %esi
testq %rdi, %rdi
movq 0x20(%rsp), %r14
je 0x97bd
leaq 0x10(%r15), %rbx
movq %r14, %rdx
callq *%r12
movl (%rbx), %esi
movl %esi, %ebx
leal 0x3(%rsi), %ebp
andl $-0x4, %ebp
subl %esi, %ebp
je 0x9710
leaq 0x30(%rsp), %rdi
movl $0x0, (%rdi)
movl %ebp, %esi
movq %r14, %rdx
callq *%r12
addl %ebp, %ebx
movq 0x18(%r15), %rdi
testq %rdi, %rdi
je 0x9727
movl 0x20(%r15), %esi
movq %r14, %rdx
callq *%r12
addl 0x20(%r15), %ebx
movl (%rsp), %ecx
movl 0x8(%rsp), %eax
addl %ebx, %eax
cmpl %ecx, %eax
jne 0x9821
cmpl $-0x1, %ecx
movq 0x10(%rsp), %r14
jne 0x978e
jmp 0x974f
movq %rbp, %rdi
callq *0x48(%rbp)
movq 0x10(%rsp), %r14
cmpw $0x0, 0x4(%r14)
je 0x9787
movq %r14, %r12
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x8(%r12), %rax
leaq (%rax,%rbx), %rdi
callq *0x48(%rax,%rbx)
incq %r14
movzwl 0x4(%r12), %eax
addq $0x50, %rbx
cmpq %rax, %r14
jb 0x9766
jmp 0x978e
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rbx
testq %rbx, %rbx
je 0x97a9
movq 0x28(%rbx), %rdi
callq 0x21a0
movq $0x0, 0x28(%rbx)
movl (%rsp), %ecx
movl %ecx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x96f1
leaq 0x2372(%rip), %rdi      # 0xbb3d
leaq 0x232b(%rip), %rsi      # 0xbafd
leaq 0x2371(%rip), %rcx      # 0xbb4a
movl $0x1ac, %edx            # imm = 0x1AC
callq 0x20e0
leaq 0x245e(%rip), %rdi      # 0xbc48
leaq 0x230c(%rip), %rsi      # 0xbafd
leaq 0x2456(%rip), %rcx      # 0xbc4e
movl $0x12, %edx
callq 0x20e0
leaq 0x239b(%rip), %rdi      # 0xbba4
leaq 0x22ed(%rip), %rsi      # 0xbafd
leaq 0x2333(%rip), %rcx      # 0xbb4a
movl $0x1b6, %edx            # imm = 0x1B6
callq 0x20e0
leaq 0x23a2(%rip), %rdi      # 0xbbca
leaq 0x22ce(%rip), %rsi      # 0xbafd
leaq 0x2314(%rip), %rcx      # 0xbb4a
movl $0x1f8, %edx            # imm = 0x1F8
callq 0x20e0
leaq 0x2370(%rip), %rdi      # 0xbbb7
leaq 0x22af(%rip), %rsi      # 0xbafd
leaq 0x22f5(%rip), %rcx      # 0xbb4a
movl $0x1e3, %edx            # imm = 0x1E3
callq 0x20e0