// File: jimmiebergmann[P]fbx-file/miniz.c
// Function: mz_zip_writer_init_from_reader_v2
mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
{
    mz_zip_internal_state *pState;

    if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
        return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

    if (flags & MZ_ZIP_FLAG_WRITE_ZIP64)
    {
        /* We don't support converting a non-zip64 file to zip64 - this seems like more trouble than it's worth. (What about the existing 32-bit data descriptors that could follow the compressed data?) */
        if (!pZip->m_pState->m_zip64)
            return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
    }

    /* No sense in trying to write to an archive that's already at the support max size */
    if (pZip->m_pState->m_zip64)
    {
        if (pZip->m_total_files == MZ_UINT32_MAX)
            return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
    }
    else
    {
        if (pZip->m_total_files == MZ_UINT16_MAX)
            return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);

        if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)
            return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
    }

    pState = pZip->m_pState;

    if (pState->m_pFile)
    {
#ifdef MINIZ_NO_STDIO
        (void)pFilename;
        return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
#else
        if (pZip->m_pIO_opaque != pZip)
            return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

        if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)
        {
            if (!pFilename)
                return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

            /* Archive is being read from stdio and was originally opened only for reading. Try to reopen as writable. */
            if (NULL == (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile)))
            {
                /* The mz_zip_archive is now in a bogus state because pState->m_pFile is NULL, so just close it. */
                mz_zip_reader_end_internal(pZip, MZ_FALSE);
                return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
            }
        }

        pZip->m_pWrite = mz_zip_file_write_func;
        pZip->m_pNeeds_keepalive = NULL;
#endif /* #ifdef MINIZ_NO_STDIO */
    }
    else if (pState->m_pMem)
    {
        /* Archive lives in a memory block. Assume it's from the heap that we can resize using the realloc callback. */
        if (pZip->m_pIO_opaque != pZip)
            return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

        pState->m_mem_capacity = pState->m_mem_size;
        pZip->m_pWrite = mz_zip_heap_write_func;
        pZip->m_pNeeds_keepalive = NULL;
    }
    /* Archive is being read via a user provided read function - make sure the user has specified a write function too. */
    else if (!pZip->m_pWrite)
        return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);

    /* Start writing new files at the archive's current central directory location. */
    /* TODO: We could add a flag that lets the user start writing immediately AFTER the existing central dir - this would be safer. */
    pZip->m_archive_size = pZip->m_central_directory_file_ofs;
    pZip->m_central_directory_file_ofs = 0;

    /* Clear the sorted central dir offsets, they aren't useful or maintained now. */
    /* Even though we're now in write mode, files can still be extracted and verified, but file locates will be slow. */
    /* TODO: We could easily maintain the sorted central directory offsets. */
    mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets);

    pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;

    return MZ_TRUE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0xb8(%rbp)
movq %rsi, -0xc0(%rbp)
movl %edx, -0xc4(%rbp)
cmpq $0x0, -0xb8(%rbp)
je 0x13414
movq -0xb8(%rbp), %rax
cmpq $0x0, 0x68(%rax)
je 0x13414
movq -0xb8(%rbp), %rax
cmpl $0x1, 0x14(%rax)
je 0x13446
movq -0xb8(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x18, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x13437
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
movl -0xc4(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x1349b
movq -0xb8(%rbp), %rax
movq 0x68(%rax), %rax
cmpl $0x0, 0x64(%rax)
jne 0x13499
movq -0xb8(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x18, -0x2c(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x1348a
movl -0x2c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
jmp 0x1349b
movq -0xb8(%rbp), %rax
movq 0x68(%rax), %rax
cmpl $0x0, 0x64(%rax)
je 0x134f0
movq -0xb8(%rbp), %rax
cmpl $-0x1, 0x10(%rax)
jne 0x134eb
movq -0xb8(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x2, -0x3c(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x134dc
movl -0x3c(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
jmp 0x13582
movq -0xb8(%rbp), %rax
cmpl $0xffff, 0x10(%rax)     # imm = 0xFFFF
jne 0x13532
movq -0xb8(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x2, -0x4c(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x13523
movl -0x4c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
movq -0xb8(%rbp), %rax
movq (%rax), %rax
addq $0x2e, %rax
addq $0x1e, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
jbe 0x13580
movq -0xb8(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x3, -0x5c(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x13571
movl -0x5c(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
jmp 0x13582
movq -0xb8(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x136e3
movq -0xb8(%rbp), %rax
movq 0x60(%rax), %rax
cmpq -0xb8(%rbp), %rax
je 0x135ec
movq -0xb8(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x18, -0x6c(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x135dd
movl -0x6c(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
movq -0xb8(%rbp), %rax
cmpl $0x4, 0x18(%rax)
jne 0x136bd
cmpq $0x0, -0xc0(%rbp)
jne 0x13639
movq -0xb8(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x18, -0x7c(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x1362a
movl -0x7c(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
movq -0xc0(%rbp), %rdi
movq -0xd0(%rbp), %rax
movq 0x70(%rax), %rdx
leaq 0x22bd1(%rip), %rsi     # 0x36223
callq 0x32d0
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, 0x70(%rax)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x136bb
movq -0xb8(%rbp), %rdi
xorl %esi, %esi
callq 0xbbf0
movq -0xb8(%rbp), %rax
movq %rax, -0x88(%rbp)
movl $0x11, -0x8c(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x136ac
movl -0x8c(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
jmp 0x136bd
movq -0xb8(%rbp), %rax
leaq -0x4ab(%rip), %rcx      # 0x13220
movq %rcx, 0x50(%rax)
movq -0xb8(%rbp), %rax
movq $0x0, 0x58(%rax)
jmp 0x137df
movq -0xd0(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0x1378c
movq -0xb8(%rbp), %rax
movq 0x60(%rax), %rax
cmpq -0xb8(%rbp), %rax
je 0x1374d
movq -0xb8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl $0x18, -0x9c(%rbp)
cmpq $0x0, -0x98(%rbp)
je 0x1373e
movl -0x9c(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
movq -0xd0(%rbp), %rax
movq 0x88(%rax), %rcx
movq -0xd0(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0xb8(%rbp), %rax
leaq -0xb87(%rip), %rcx      # 0x12bf0
movq %rcx, 0x50(%rax)
movq -0xb8(%rbp), %rax
movq $0x0, 0x58(%rax)
jmp 0x137dd
movq -0xb8(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x137db
movq -0xb8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl $0x18, -0xac(%rbp)
cmpq $0x0, -0xa8(%rbp)
je 0x137cc
movl -0xac(%rbp), %ecx
movq -0xa8(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0xb0(%rbp)
jmp 0x13862
jmp 0x137dd
jmp 0x137df
movq -0xb8(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0xb8(%rbp), %rax
movq %rcx, (%rax)
movq -0xb8(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0xb8(%rbp), %rcx
movq -0xb8(%rbp), %rax
movq 0x68(%rax), %rax
addq $0x40, %rax
movq %rcx, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x40(%rcx), %rdi
movq -0x10(%rbp), %rcx
movq (%rcx), %rsi
callq *%rax
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3190
movq -0xb8(%rbp), %rax
movl $0x2, 0x14(%rax)
movl $0x1, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)