// File: jimmiebergmann[P]fbx-file/miniz.c
// Function: tdefl_write_image_to_png_file_in_memory_ex
void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, mz_uint level, mz_bool flip)
{
    /* Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined. */
    static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
    tdefl_compressor *pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
    tdefl_output_buffer out_buf;
    int i, bpl = w * num_chans, y, z;
    mz_uint32 c;
    *pLen_out = 0;
    if (!pComp)
        return NULL;
    MZ_CLEAR_OBJ(out_buf);
    out_buf.m_expandable = MZ_TRUE;
    out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h);
    if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity)))
    {
        MZ_FREE(pComp);
        return NULL;
    }
    /* write dummy header */
    for (z = 41; z; --z)
        tdefl_output_buffer_putter(&z, 1, &out_buf);
    /* compress image data */
    tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER);
    for (y = 0; y < h; ++y)
    {
        tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH);
        tdefl_compress_buffer(pComp, (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH);
    }
    if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE)
    {
        MZ_FREE(pComp);
        MZ_FREE(out_buf.m_pBuf);
        return NULL;
    }
    /* write real header */
    *pLen_out = out_buf.m_size - 41;
    {
        static const mz_uint8 chans[] = { 0x00, 0x00, 0x04, 0x02, 0x06 };
        mz_uint8 pnghdr[41] = { 0x89, 0x50, 0x4e, 0x47, 0x0d,
                                0x0a, 0x1a, 0x0a, 0x00, 0x00,
                                0x00, 0x0d, 0x49, 0x48, 0x44,
                                0x52, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x08,
                                0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x49, 0x44, 0x41,
                                0x54 };
        pnghdr[18] = (mz_uint8)(w >> 8);
        pnghdr[19] = (mz_uint8)w;
        pnghdr[22] = (mz_uint8)(h >> 8);
        pnghdr[23] = (mz_uint8)h;
        pnghdr[25] = chans[num_chans];
        pnghdr[33] = (mz_uint8)(*pLen_out >> 24);
        pnghdr[34] = (mz_uint8)(*pLen_out >> 16);
        pnghdr[35] = (mz_uint8)(*pLen_out >> 8);
        pnghdr[36] = (mz_uint8)*pLen_out;
        c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17);
        for (i = 0; i < 4; ++i, c <<= 8)
            ((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24);
        memcpy(out_buf.m_pBuf, pnghdr, 41);
    }
    /* write footer (IDAT CRC-32, followed by IEND chunk) */
    if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf))
    {
        *pLen_out = 0;
        MZ_FREE(pComp);
        MZ_FREE(out_buf.m_pBuf);
        return NULL;
    }
    c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, *pLen_out + 4);
    for (i = 0; i < 4; ++i, c <<= 8)
        (out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24);
    /* compute final size of file, grab compressed data buffer and return */
    *pLen_out += 57;
    MZ_FREE(pComp);
    return out_buf.m_pBuf;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl $0x4df78, %edi          # imm = 0x4DF78
callq 0x3400
movq %rax, -0x38(%rbp)
movl -0x14(%rbp), %eax
imull -0x1c(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
cmpq $0x0, -0x38(%rbp)
jne 0xb2ca
movq $0x0, -0x8(%rbp)
jmp 0xb659
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3190
movl $0x1, -0x40(%rbp)
movl -0x60(%rbp), %ecx
addl $0x1, %ecx
imull -0x18(%rbp), %ecx
movl $0x40, %eax
cmpl %ecx, %eax
jle 0xb301
movl $0x40, %eax
movl %eax, -0xa4(%rbp)
jmp 0xb311
movl -0x60(%rbp), %eax
addl $0x1, %eax
imull -0x18(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
addl $0x39, %eax
cltq
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x3400
movq %rax, %rcx
movq %rcx, -0x48(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0xb34d
movq -0x38(%rbp), %rdi
callq 0x3390
movq $0x0, -0x8(%rbp)
jmp 0xb659
movl $0x29, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0xb377
leaq -0x68(%rbp), %rdi
movl $0x1, %esi
leaq -0x58(%rbp), %rdx
callq 0xb0d0
movl -0x68(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0xb354
movq -0x38(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl $0xa, %eax
cmpl -0x2c(%rbp), %eax
jae 0xb399
movl $0xa, %eax
movl %eax, -0xb4(%rbp)
jmp 0xb3a2
movl -0x2c(%rbp), %eax
movl %eax, -0xb4(%rbp)
movq -0xb0(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x2a1e6(%rip), %rax     # 0x355a0
movl (%rax,%rcx,4), %ecx
orl $0x1000, %ecx           # imm = 0x1000
leaq -0x2fa(%rip), %rsi      # 0xb0d0
leaq -0x58(%rbp), %rdx
callq 0x3da0
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0xb466
movq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0xaed0
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpl $0x0, 0x10(%rbp)
je 0xb427
movl -0x18(%rbp), %eax
subl $0x1, %eax
subl -0x64(%rbp), %eax
movl %eax, -0xcc(%rbp)
jmp 0xb430
movl -0x64(%rbp), %eax
movl %eax, -0xcc(%rbp)
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl -0xcc(%rbp), %eax
imull -0x60(%rbp), %eax
cltq
addq %rax, %rsi
movslq -0x60(%rbp), %rdx
xorl %ecx, %ecx
callq 0xaed0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xb3da
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x4, %ecx
movq %rdx, %rsi
callq 0xaed0
cmpl $0x1, %eax
je 0xb49f
movq -0x38(%rbp), %rdi
callq 0x3390
movq -0x48(%rbp), %rdi
callq 0x3390
movq $0x0, -0x8(%rbp)
jmp 0xb659
movq -0x58(%rbp), %rcx
subq $0x29, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
leaq -0xa0(%rbp), %rdi
leaq 0x2a124(%rip), %rsi     # 0x355e0
movl $0x29, %edx
callq 0x3280
movl -0x14(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x8e(%rbp)
movl -0x14(%rbp), %eax
movb %al, -0x8d(%rbp)
movl -0x18(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x8a(%rbp)
movl -0x18(%rbp), %eax
movb %al, -0x89(%rbp)
movslq -0x1c(%rbp), %rcx
leaq 0x2a0d1(%rip), %rax     # 0x355cc
movb (%rax,%rcx), %al
movb %al, -0x87(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
shrq $0x18, %rax
movb %al, -0x7f(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
shrq $0x10, %rax
movb %al, -0x7e(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
shrq $0x8, %rax
movb %al, -0x7d(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb %al, -0x7c(%rbp)
leaq -0xa0(%rbp), %rsi
addq $0xc, %rsi
xorl %eax, %eax
movl %eax, %edi
movl $0x11, %edx
callq 0x3910
movl %eax, -0x6c(%rbp)
movl $0x0, -0x5c(%rbp)
cmpl $0x4, -0x5c(%rbp)
jge 0xb588
movl -0x6c(%rbp), %eax
shrl $0x18, %eax
movb %al, %cl
movslq -0x5c(%rbp), %rax
movb %cl, -0x83(%rbp,%rax)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x6c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x6c(%rbp)
jmp 0xb55b
movq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x29, %edx
callq 0x3280
leaq 0x2a065(%rip), %rdi     # 0x35609
movl $0x10, %esi
leaq -0x58(%rbp), %rdx
callq 0xb0d0
cmpl $0x0, %eax
jne 0xb5de
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x38(%rbp), %rdi
callq 0x3390
movq -0x48(%rbp), %rdi
callq 0x3390
movq $0x0, -0x8(%rbp)
jmp 0xb659
movq -0x48(%rbp), %rsi
addq $0x29, %rsi
addq $-0x4, %rsi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
addq $0x4, %rdx
xorl %eax, %eax
movl %eax, %edi
callq 0x3910
movl %eax, -0x6c(%rbp)
movl $0x0, -0x5c(%rbp)
cmpl $0x4, -0x5c(%rbp)
jge 0xb63a
movl -0x6c(%rbp), %eax
shrl $0x18, %eax
movb %al, %dl
movq -0x48(%rbp), %rax
addq -0x58(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb %dl, -0x10(%rax,%rcx)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x6c(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x6c(%rbp)
jmp 0xb608
movq -0x28(%rbp), %rax
movq (%rax), %rcx
addq $0x39, %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rdi
callq 0x3390
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)