// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdx, 0x20(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movzwl 0x4(%rdi), %r12d
movl %r12d, %edi
shll $0x4, %edi
orq $0xc, %rdi
movq %rdi, 0x18(%rsp)
callq 0x21c0
testq %rax, %rax
je 0x9912
movq %rax, %rbp
movq %r15, 0x30(%rsp)
movzwl 0x4(%r14), %eax
testw %ax, %ax
movq %rbx, 0x28(%rsp)
movq %r14, %r8
movq %r14, 0x8(%rsp)
je 0x9247
movq %r12, 0x38(%rsp)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movl %eax, 0x14(%rsp)
movq 0x10(%r8), %rax
testq %rax, %rax
je 0x91e5
movslq (%rax,%rbx,4), %rax
leaq (%rax,%rax,4), %r15
shlq $0x4, %r15
addq 0x8(%r8), %r15
jmp 0x91ec
movq 0x8(%r8), %r15
addq %r12, %r15
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x9941
cmpq $0x68656164, (%r15)     # imm = 0x68656164
cmovel %ebx, %r14d
movq %r15, %rdi
callq *%rax
movl 0x14(%rsp), %ecx
movl %ecx, 0x30(%r15)
movl 0x34(%r15), %eax
addl $0x3, %eax
andl $-0x4, %eax
addl %eax, %ecx
movl %ecx, 0x14(%rsp)
addl 0x2c(%r15), %r13d
incq %rbx
movq 0x8(%rsp), %r8
movzwl 0x4(%r8), %eax
addq $0x50, %r12
cmpq %rax, %rbx
jb 0x91ca
movq 0x28(%rsp), %rbx
movq 0x38(%rsp), %r12
jmp 0x9259
xorl %r13d, %r13d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rcx
movl %ecx, 0x14(%rsp)
movzwl %ax, %eax
movl (%r8), %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 0x9960
movl $0x1, %esi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %esi, %edx
leal (%rdx,%rdx), %esi
incl %ecx
cmpl %eax, %esi
jle 0x928e
movl %edx, %esi
shll $0x4, %esi
movl %eax, %edi
subl %edx, %edi
shrl $0x4, %edx
movb %dl, 0x6(%rbp)
movb %sil, 0x7(%rbp)
movb %ch, 0x8(%rbp)
movb %cl, 0x9(%rbp)
movl %edi, %ecx
shll $0x4, %ecx
shrl $0x4, %edi
movb %dil, 0xa(%rbp)
movb %cl, 0xb(%rbp)
movq 0x8(%r8), %rcx
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 0x92ce
movl %r12d, %eax
shll $0x4, %eax
cmpl %edx, %eax
jne 0x997f
leaq 0x3(,%r12,4), %rax
xorl %ecx, %ecx
xorl %r15d, %r15d
movl (%rbp,%rcx,4), %edx
bswapl %edx
addl %edx, %r15d
incq %rcx
cmpl %ecx, %eax
jne 0x9319
xorl %r12d, %r12d
testq %rbx, %rbx
jne 0x9345
movq %rbp, %rdi
movq 0x18(%rsp), %r12
movl %r12d, %esi
movq 0x30(%rsp), %rdx
callq *0x20(%rsp)
movq %rbp, %rdi
callq 0x21a0
cmpl $-0x1, %r14d
movq 0x8(%rsp), %r8
je 0x93a8
movq 0x8(%r8), %rdx
movq 0x10(%r8), %rcx
movslq %r14d, %rax
testq %rcx, %rcx
je 0x936c
movslq (%rcx,%rax,4), %rax
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
addq %rax, %rdx
movq %rdx, %r14
movq %rdx, %rdi
callq 0x999e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
movq 0x8(%rsp), %r8
jne 0x9901
movq 0x20(%r14), %rax
addl %r15d, %r13d
movl $0xb1b0afba, %ecx       # imm = 0xB1B0AFBA
subl %r13d, %ecx
bswapl %ecx
movl %ecx, 0x8(%rax)
testq %rbx, %rbx
je 0x9567
movzwl 0x4(%r8), %eax
leal (%rax,%rax,4), %ecx
leal 0x2c(,%rcx,4), %ecx
movl %ecx, 0x38(%rsp)
movl %ecx, %r15d
testl %eax, %eax
je 0x94d3
xorl %r14d, %r14d
xorl %r13d, %r13d
movl 0x38(%rsp), %r15d
movq 0x10(%r8), %rax
testq %rax, %rax
je 0x93f5
movslq (%rax,%r13,4), %rax
leaq (%rax,%rax,4), %rbp
shlq $0x4, %rbp
addq 0x8(%r8), %rbp
jmp 0x93fc
movq 0x8(%r8), %rbp
addq %r14, %rbp
movq %rbp, %rdi
callq 0x999e
movl $0x8, %ecx
testl %eax, %eax
je 0x9417
xorl %eax, %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x9492
movl %r15d, %ebx
movq 0x20(%rbp), %rdi
movl 0x34(%rbp), %esi
leaq 0x40(%rsp), %rdx
callq 0x99df
testq %rax, %rax
je 0x946f
movq %rax, %r15
movq %rbp, %rdi
callq *0x48(%rbp)
movq 0x40(%rsp), %rdi
leal 0x3(%rdi), %eax
movslq %eax, %rdx
andq $-0x4, %rdx
subq %rdi, %rdx
jbe 0x9457
addq %r15, %rdi
xorl %esi, %esi
callq 0x20c0
movq %r15, 0x20(%rbp)
movl 0x40(%rsp), %eax
movl %eax, 0x38(%rbp)
leaq -0x3f8(%rip), %rax      # 0x9071
movq %rax, 0x48(%rbp)
jmp 0x9477
cmpq $-0x1, 0x40(%rsp)
je 0x94b6
movl %ebx, %r15d
movl %ebx, 0x30(%rbp)
movl 0x38(%rbp), %eax
addl $0x3, %eax
andl $-0x4, %eax
addl %eax, %r15d
xorl %ecx, %ecx
movb $0x1, %al
movq 0x28(%rsp), %rbx
testb %al, %al
movq 0x8(%rsp), %r8
je 0x975a
incq %r13
movzwl 0x4(%r8), %eax
addq $0x50, %r14
cmpq %rax, %r13
jb 0x93da
jmp 0x94d3
movq %rbp, %rdi
callq *0x48(%rbp)
xorl %eax, %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %ebx, %r15d
movq 0x28(%rsp), %rbx
movl $0x8, %ecx
jmp 0x9492
movzwl (%rbx), %eax
movl %eax, 0x74(%rsp)
movzwl 0x2(%rbx), %r13d
movq 0x8(%rbx), %rdi
xorl %ebp, %ebp
movl $0x0, %ecx
movl $0x0, %eax
movl $0x0, %r9d
testq %rdi, %rdi
movq 0x20(%rsp), %r14
je 0x9547
movl 0x10(%rbx), %esi
movl $0x0, %ecx
testl %esi, %esi
je 0x9547
movq %rbx, %r12
movl %r15d, %ebx
leaq 0x40(%rsp), %r15
movq %r15, %rdx
callq 0x99df
movq %rax, 0x28(%r12)
movq (%r15), %rcx
movq %rcx, %rdx
notq %rdx
orq %rax, %rdx
je 0x991e
movl 0x10(%r12), %r9d
movl %ebx, %r15d
movl %ebx, %eax
movq 0x8(%rsp), %r8
movq %r12, %rbx
cmpq $0x0, 0x18(%rbx)
je 0x9574
movl 0x20(%rbx), %esi
movl $0x0, %edx
testl %esi, %esi
je 0x9576
leal 0x3(%rcx), %ebp
andl $-0x4, %ebp
addl %r15d, %ebp
movl %esi, %edx
jmp 0x9576
movl 0x14(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x9775
xorl %edx, %edx
leal 0x3(%rcx), %esi
andl $-0x4, %esi
addl %edx, %esi
addl %r15d, %esi
movl %esi, 0x18(%rsp)
movb $0x1, %sil
testb %sil, %sil
je 0x974f
leaq 0x40(%rsp), %rdi
movl $0x46464f77, (%rdi)     # imm = 0x46464F77
movl (%r8), %ebx
movl %ebx, %esi
shrl $0x18, %esi
movb %sil, 0x4(%rdi)
movl %ebx, %esi
shrl $0x10, %esi
movb %sil, 0x5(%rdi)
movb %bh, 0x46(%rsp)
movb %bl, 0x7(%rdi)
movl 0x18(%rsp), %ebx
movl %ebx, %esi
shrl $0x18, %esi
movb %sil, 0x8(%rdi)
movl %ebx, %esi
shrl $0x10, %esi
movb %sil, 0x9(%rdi)
movb %bh, 0x4a(%rsp)
movb %bl, 0xb(%rdi)
movzwl 0x4(%r8), %ebx
movb %bh, 0x4c(%rsp)
movb %bl, 0xd(%rdi)
movw $0x0, 0xe(%rdi)
movl 0x14(%rsp), %ebx
movl %ebx, %esi
shrl $0x18, %esi
movb %sil, 0x10(%rdi)
movl %ebx, %esi
shrl $0x10, %esi
movb %sil, 0x11(%rdi)
movb %bh, 0x52(%rsp)
movb %bl, 0x13(%rdi)
movl 0x74(%rsp), %ebx
movb %bh, 0x54(%rsp)
movb %bl, 0x15(%rdi)
movl %r13d, %ebx
movb %bh, 0x56(%rsp)
movb %bl, 0x17(%rdi)
movl %eax, %esi
shrl $0x18, %esi
movb %sil, 0x18(%rdi)
movl %eax, %esi
shrl $0x10, %esi
movb %sil, 0x19(%rdi)
movb %ah, 0x5a(%rsp)
movb %al, 0x1b(%rdi)
movl %ecx, %eax
shrl $0x18, %eax
movb %al, 0x1c(%rdi)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x1d(%rdi)
movb %ch, 0x5e(%rsp)
movb %cl, 0x1f(%rdi)
movl %r9d, %ecx
movl %r9d, %eax
shrl $0x18, %eax
movb %al, 0x20(%rdi)
movl %r9d, %eax
shrl $0x10, %eax
movb %al, 0x21(%rdi)
movb %ch, 0x62(%rsp)
movb %cl, 0x23(%rdi)
movl %ebp, %ecx
movl %ebp, %eax
shrl $0x18, %eax
movb %al, 0x24(%rdi)
movl %ebp, %eax
shrl $0x10, %eax
movb %al, 0x25(%rdi)
movb %ch, 0x66(%rsp)
movb %cl, 0x27(%rdi)
movl %edx, %eax
shrl $0x18, %eax
movb %al, 0x28(%rdi)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x29(%rdi)
movb %dh, 0x6a(%rsp)
movb %dl, 0x2b(%rdi)
movl $0x2c, %r12d
movl $0x2c, %esi
movq 0x30(%rsp), %r15
movq %r15, %rdx
callq *%r14
movq 0x8(%rsp), %r8
cmpw $0x0, 0x4(%r8)
je 0x9727
movl $0xe, %r12d
xorl %ebx, %ebx
leaq 0x40(%rsp), %r13
movq 0x8(%r8), %rax
movl -0x10(%rax,%r12,4), %ecx
bswapl %ecx
movl %ecx, 0x40(%rsp)
movl -0x8(%rax,%r12,4), %ecx
bswapl %ecx
movl %ecx, 0x44(%rsp)
movl (%rax,%r12,4), %ecx
bswapl %ecx
movl %ecx, 0x48(%rsp)
movl -0x4(%rax,%r12,4), %ecx
bswapl %ecx
movl %ecx, 0x4c(%rsp)
movl -0xc(%rax,%r12,4), %eax
bswapl %eax
movl %eax, 0x50(%rsp)
movq %r13, %rdi
movl $0x14, %esi
movq %r15, %rdx
callq *%r14
movq 0x8(%rsp), %r8
incq %rbx
movzwl 0x4(%r8), %eax
addq $0x14, %r12
cmpq %rax, %rbx
jb 0x96c5
addl $0x1e, %r12d
xorl %ecx, %ecx
cmpl 0x38(%rsp), %r12d
je 0x975f
leaq 0x2480(%rip), %rdi      # 0xbbb7
leaq 0x23bf(%rip), %rsi      # 0xbafd
leaq 0x2405(%rip), %rcx      # 0xbb4a
movl $0x1e3, %edx            # imm = 0x1E3
callq 0x20e0
movl $0x8, %ecx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r15d, 0x18(%rsp)
cmpl $0x8, %ecx
je 0x98af
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0x9901
cmpw $0x0, 0x4(%r8)
je 0x9803
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x10(%r8), %rax
testq %rax, %rax
je 0x97a1
movslq (%rax,%r14,4), %rax
leaq (%rax,%rax,4), %r13
shlq $0x4, %r13
addq 0x8(%r8), %r13
jmp 0x97a8
movq 0x8(%r8), %r13
addq %rbx, %r13
movq %r13, %rdi
callq 0x999e
movl %eax, %r15d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x97df
movl 0x38(%r13), %ebp
addl $0x3, %ebp
andl $-0x4, %ebp
movq 0x20(%r13), %rdi
movl %ebp, %esi
movq 0x30(%rsp), %rdx
callq *0x20(%rsp)
movq %r13, %rdi
callq *0x48(%r13)
addl %r12d, %ebp
testl %r15d, %r15d
movq 0x8(%rsp), %r8
jne 0x98b2
incq %r14
movzwl 0x4(%r8), %eax
addq $0x50, %rbx
movl %ebp, %r12d
cmpq %rax, %r14
jb 0x9786
jmp 0x9806
movl %r12d, %ebp
movq 0x28(%rsp), %rbx
testq %rbx, %rbx
je 0x988a
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
jne 0x9826
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x992d
movl 0x10(%rbx), %esi
movq 0x30(%rsp), %r12
movq %r12, %rdx
movq 0x20(%rsp), %r14
callq *%r14
movq %rbx, %rax
movl 0x10(%rbx), %ebx
movl 0x10(%rax), %eax
leal 0x3(%rax), %r15d
andl $-0x4, %r15d
subl %eax, %r15d
je 0x9866
leaq 0x40(%rsp), %rdi
movl $0x0, (%rdi)
movl %r15d, %esi
movq %r12, %rdx
callq *%r14
addl %r15d, %ebx
movq 0x28(%rsp), %r14
movq 0x18(%r14), %rdi
testq %rdi, %rdi
je 0x9883
movl 0x20(%r14), %esi
movq %r12, %rdx
callq *0x20(%rsp)
addl 0x20(%r14), %ebx
movq 0x8(%rsp), %r8
addl %ebx, %ebp
cmpl 0x18(%rsp), %ebp
je 0x98b2
leaq 0x2333(%rip), %rdi      # 0xbbca
leaq 0x225f(%rip), %rsi      # 0xbafd
leaq 0x22a5(%rip), %rcx      # 0xbb4a
movl $0x1f8, %edx            # imm = 0x1F8
callq 0x20e0
movl %r12d, %ebp
cmpl $-0x1, %ebp
jne 0x98e6
cmpw $0x0, 0x4(%r8)
je 0x98e6
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x8(%r8), %rax
leaq (%rax,%rbx), %rdi
callq *0x48(%rax,%rbx)
movq 0x8(%rsp), %r8
incq %r14
movzwl 0x4(%r8), %eax
addq $0x50, %rbx
cmpq %rax, %r14
jb 0x98c4
movq 0x28(%rsp), %rbx
testq %rbx, %rbx
je 0x9901
movq 0x28(%rbx), %rdi
callq 0x21a0
movq $0x0, 0x28(%rbx)
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2ee0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x9901
xorl %esi, %esi
movq 0x8(%rsp), %r8
movl %ebx, %r15d
jmp 0x9588
movq %rbx, %rax
xorl %ebx, %ebx
movq 0x20(%rsp), %r14
movq 0x30(%rsp), %r12
jmp 0x983f
leaq 0x21f5(%rip), %rdi      # 0xbb3d
leaq 0x21ae(%rip), %rsi      # 0xbafd
leaq 0x21f4(%rip), %rcx      # 0xbb4a
movl $0x1ac, %edx            # imm = 0x1AC
callq 0x20e0
leaq 0x22e1(%rip), %rdi      # 0xbc48
leaq 0x218f(%rip), %rsi      # 0xbafd
leaq 0x22d9(%rip), %rcx      # 0xbc4e
movl $0x12, %edx
callq 0x20e0
leaq 0x221e(%rip), %rdi      # 0xbba4
leaq 0x2170(%rip), %rsi      # 0xbafd
leaq 0x21b6(%rip), %rcx      # 0xbb4a
movl $0x1b6, %edx            # imm = 0x1B6
callq 0x20e0