// File: /workspace/github_c_makefile_star2_O2/Paxa[P]fast_excel/libxlsxwriter/third_party/minizip/zip.c
// Function: zipCloseFileInZipRaw64
extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_size, uLong crc32) {
    zip64_internal* zi;
    ZPOS64_T compressed_size;
    uLong invalidValue = 0xffffffff;
    unsigned datasize = 0;
    int err=ZIP_OK;

    if (file == NULL)
        return ZIP_PARAMERROR;
    zi = (zip64_internal*)file;

    if (zi->in_opened_file_inzip == 0)
        return ZIP_PARAMERROR;
    zi->ci.stream.avail_in = 0;

    if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
                {
                        while (err==ZIP_OK)
                        {
                                uLong uTotalOutBefore;
                                if (zi->ci.stream.avail_out == 0)
                                {
                                        if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
                                                err = ZIP_ERRNO;
                                        zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
                                        zi->ci.stream.next_out = zi->ci.buffered_data;
                                }
                                uTotalOutBefore = zi->ci.stream.total_out;
                                err=deflate(&zi->ci.stream,  Z_FINISH);
                                zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
                        }
                }
    else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
    {
#ifdef HAVE_BZIP2
      err = BZ_FINISH_OK;
      while (err==BZ_FINISH_OK)
      {
        uLong uTotalOutBefore;
        if (zi->ci.bstream.avail_out == 0)
        {
          if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
            err = ZIP_ERRNO;
          zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
          zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
        }
        uTotalOutBefore = zi->ci.bstream.total_out_lo32;
        err=BZ2_bzCompress(&zi->ci.bstream,  BZ_FINISH);
        if(err == BZ_STREAM_END)
          err = Z_STREAM_END;

        zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore);
      }

      if(err == BZ_FINISH_OK)
        err = ZIP_OK;
#endif
    }

    if (err==Z_STREAM_END)
        err=ZIP_OK; /* this is normal */

    if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
                {
        if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO)
            err = ZIP_ERRNO;
                }

    if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
    {
        int tmp_err = deflateEnd(&zi->ci.stream);
        if (err == ZIP_OK)
            err = tmp_err;
        zi->ci.stream_initialised = 0;
    }
#ifdef HAVE_BZIP2
    else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
    {
      int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream);
                        if (err==ZIP_OK)
                                err = tmperr;
                        zi->ci.stream_initialised = 0;
    }
#endif

    if (!zi->ci.raw)
    {
        crc32 = (uLong)zi->ci.crc32;
        uncompressed_size = zi->ci.totalUncompressedData;
    }
    compressed_size = zi->ci.totalCompressedData;

#    ifndef NOCRYPT
    compressed_size += zi->ci.crypt_header_size;
#    endif

    // update Current Item crc and sizes,
    if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff)
    {
      /*version Made by*/
      zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2);
      /*version needed*/
      zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2);

    }

    zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/


    if(compressed_size >= 0xffffffff)
      zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/
    else
      zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/

    /// set internal file attributes field
    if (zi->ci.stream.data_type == Z_ASCII)
        zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);

    if(uncompressed_size >= 0xffffffff)
      zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/
    else
      zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/

    // Add ZIP64 extra info field for uncompressed size
    if(uncompressed_size >= 0xffffffff)
      datasize += 8;

    // Add ZIP64 extra info field for compressed size
    if(compressed_size >= 0xffffffff)
      datasize += 8;

    // Add ZIP64 extra info field for relative offset to local file header of current file
    if(zi->ci.pos_local_header >= 0xffffffff)
      datasize += 8;

    if(datasize > 0)
    {
      char* p = NULL;

      if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree)
      {
        // we can not write more data to the buffer that we have room for.
        return ZIP_BADZIPFILE;
      }

      p = zi->ci.central_header + zi->ci.size_centralheader;

      // Add Extra Information Header for 'ZIP64 information'
      zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID
      p += 2;
      zip64local_putValue_inmemory(p, datasize, 2); // DataSize
      p += 2;

      if(uncompressed_size >= 0xffffffff)
      {
        zip64local_putValue_inmemory(p, uncompressed_size, 8);
        p += 8;
      }

      if(compressed_size >= 0xffffffff)
      {
        zip64local_putValue_inmemory(p, compressed_size, 8);
        p += 8;
      }

      if(zi->ci.pos_local_header >= 0xffffffff)
      {
        zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8);
        p += 8;
      }

      // Update how much extra free space we got in the memory buffer
      // and increase the centralheader size so the new ZIP64 fields are included
      // ( 4 below is the size of HeaderID and DataSize field )
      zi->ci.size_centralExtraFree -= datasize + 4;
      zi->ci.size_centralheader += datasize + 4;

      // Update the extra info size field
      zi->ci.size_centralExtra += datasize + 4;
      zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2);
    }

    if (err==ZIP_OK)
        err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader);

    free(zi->ci.central_header);

    if (err==ZIP_OK)
    {
        // Update the LocalFileHeader with the new values.

        ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);

        if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
            err = ZIP_ERRNO;

        if (err==ZIP_OK)
            err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */

        if(uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff )
        {
          if(zi->ci.pos_zip64extrainfo > 0)
          {
            // Update the size in the ZIP64 extended field.
            if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0)
              err = ZIP_ERRNO;

            if (err==ZIP_OK) /* compressed size, unknown */
              err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8);

            if (err==ZIP_OK) /* uncompressed size, unknown */
              err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8);
          }
          else
              err = ZIP_BADZIPFILE; // Caller passed zip64 = 0, so no room for zip64 info -> fatal
        }
        else
        {
          if (err==ZIP_OK) /* compressed size, unknown */
              err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);

          if (err==ZIP_OK) /* uncompressed size, unknown */
              err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
        }

        if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
            err = ZIP_ERRNO;
    }

    zi->number_entry ++;
    zi->in_opened_file_inzip = 0;

    return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
pushq $-0x66
popq %rbp
testq %rdi, %rdi
je 0xefe4
movq %rdi, %rbx
cmpl $0x0, 0x70(%rdi)
je 0xefe4
movq %rdx, %r15
movq %rsi, %r13
andl $0x0, 0x80(%rbx)
leaq 0x78(%rbx), %r12
xorl %ebp, %ebp
cmpl $0x8, 0x120(%rbx)
jne 0xec0b
cmpl $0x0, 0x124(%rbx)
jne 0xec0b
movq %r15, 0x18(%rsp)
leaq 0x128(%rbx), %r14
pushq $0x4
popq %rbp
cmpl $0x0, 0x98(%rbx)
jne 0xebda
movq %rbx, %rdi
callq 0xeb04
movl $0x10000, 0x98(%rbx)    # imm = 0x10000
movq %r14, 0x90(%rbx)
movl 0xa0(%rbx), %r15d
movq %r12, %rdi
movl %ebp, %esi
callq 0xc660
movl 0xa0(%rbx), %ecx
subl %r15d, %ecx
addl %ecx, 0xec(%rbx)
testl %eax, %eax
je 0xebb8
xorl %ebp, %ebp
cmpl $0x1, %eax
cmovnel %eax, %ebp
movq 0x18(%rsp), %r15
testl %ebp, %ebp
jne 0xec2b
cmpl $0x0, 0xec(%rbx)
je 0xec2b
movq %rbx, %rdi
callq 0xeb04
xorl %ebp, %ebp
cmpl $-0x1, %eax
sete %bpl
negl %ebp
cmpl $0x8, 0x120(%rbx)
jne 0xec51
cmpl $0x0, 0x124(%rbx)
jne 0xec51
movq %r12, %rdi
callq 0xcbe0
testl %ebp, %ebp
cmovel %eax, %ebp
andl $0x0, 0xe8(%rbx)
cmpl $0x0, 0x124(%rbx)
jne 0xec68
movq 0x10130(%rbx), %r15
movq 0x10150(%rbx), %r13
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
movq 0x10148(%rbx), %rax
movq %rax, 0x8(%rsp)
cmpq %rcx, %rax
seta %al
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movq %r13, 0x10(%rsp)
cmpq %rcx, %r13
seta %cl
orb %al, %cl
jne 0xec9c
cmpq %rdx, 0xf0(%rbx)
jbe 0xecd6
movq 0xf8(%rbx), %rdi
addq $0x4, %rdi
pushq $0x2d
popq %r14
movq %r15, %r13
pushq $0x2
popq %r15
movq %r14, %rsi
movl %r15d, %edx
callq 0xe5ef
movq 0xf8(%rbx), %rdi
addq $0x6, %rdi
movq %r14, %rsi
movl %r15d, %edx
movq %r13, %r15
callq 0xe5ef
movq 0xf8(%rbx), %rdi
addq $0x10, %rdi
pushq $0x4
popq %r14
movq %r15, 0x18(%rsp)
movq %r15, %rsi
movl %r14d, %edx
callq 0xe5ef
movq 0xf8(%rbx), %rdi
addq $0x14, %rdi
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
leaq 0x1(%rax), %r13
movq 0x8(%rsp), %rsi
cmpq %rax, %rsi
cmovaq %r13, %rsi
movl %r14d, %edx
callq 0xe5ef
cmpl $0x1, 0xd0(%rbx)
jne 0xed3c
movq 0xf8(%rbx), %rdi
addq $0x24, %rdi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
callq 0xe5ef
movq 0xf8(%rbx), %rdi
addq $0x18, %rdi
xorl %r14d, %r14d
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
movq 0x10(%rsp), %rax
cmpq %r12, %rax
seta %r14b
cmovbeq %rax, %r13
leal (,%r14,8), %r15d
pushq $0x4
popq %rdx
movq %r13, %rsi
callq 0xe5ef
cmpq %r12, 0x8(%rsp)
leal 0x8(,%r14,8), %eax
cmovbel %r15d, %eax
leal 0x8(%rax), %r15d
cmpq %r12, 0xf0(%rbx)
cmovbel %eax, %r15d
testl %r15d, %r15d
je 0xee80
movl %r15d, %r13d
orl $0x4, %r13d
cmpq %r13, 0x110(%rbx)
jae 0xedb4
pushq $-0x67
popq %rbp
jmp 0xefe4
movq 0xf8(%rbx), %r12
movq 0x108(%rbx), %r14
leaq (%r12,%r14), %rdi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %rdi, 0x20(%rsp)
callq 0xe5ef
leaq (%r12,%r14), %rdi
addq $0x2, %rdi
movl %r15d, %esi
pushq $0x2
popq %rdx
callq 0xe5ef
leaq (%r12,%r14), %r15
addq $0x4, %r15
movq 0x10(%rsp), %rsi
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, %rsi
jbe 0xee14
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0xe5ef
movq 0x20(%rsp), %r15
addq $0xc, %r15
movq 0x8(%rsp), %rsi
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, %rsi
jbe 0xee32
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0xe5ef
addq $0x8, %r15
movq 0xf0(%rbx), %rsi
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, %rsi
jbe 0xee4e
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0xe5ef
subq %r13, 0x110(%rbx)
addq %r13, 0x108(%rbx)
addq 0x100(%rbx), %r13
movq %r13, 0x100(%rbx)
movq 0xf8(%rbx), %rdi
addq $0x1e, %rdi
pushq $0x2
popq %rdx
movq %r13, %rsi
callq 0xe5ef
testl %ebp, %ebp
jne 0xee9d
leaq 0x60(%rbx), %rdi
movq 0xf8(%rbx), %rsi
movq 0x108(%rbx), %rdx
callq 0xeff5
movl %eax, %ebp
movq 0xf8(%rbx), %rdi
callq 0xc0d0
testl %ebp, %ebp
jne 0xefd9
movq 0x58(%rbx), %rsi
movq %rbx, %rdi
callq 0xcf30
movq %rax, %r12
movq 0x58(%rbx), %rsi
movq 0xf0(%rbx), %rdx
addq $0xe, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0xc520
pushq $-0x1
popq %r13
testq %rax, %rax
je 0xef03
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
cmpq %rcx, 0x10(%rsp)
setbe %al
cmpq %rcx, 0x8(%rsp)
setbe %cl
movl %r13d, %ebp
testb %cl, %al
je 0xef35
jmp 0xefbe
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, 0x10(%rsp)
seta %r14b
cmpq %rax, 0x8(%rsp)
seta %r15b
movq 0x58(%rbx), %rsi
pushq $0x4
popq %rcx
movq %rbx, %rdi
movq 0x18(%rsp), %rdx
callq 0xf0a2
movl %eax, %ebp
orb %r14b, %r15b
je 0xef85
movq 0x10140(%rbx), %rdx
testq %rdx, %rdx
je 0xefba
movq 0x58(%rbx), %rsi
addq $0x4, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0xc520
testq %rax, %rax
jne 0xefbe
testl %ebp, %ebp
jne 0xefbe
movq 0x58(%rbx), %rsi
pushq $0x8
popq %rcx
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0xf0a2
testl %eax, %eax
jne 0xefbe
movq 0x58(%rbx), %rsi
pushq $0x8
popq %rcx
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
jmp 0xefb0
testl %ebp, %ebp
jne 0xefbe
movq 0x58(%rbx), %rsi
pushq $0x4
popq %rcx
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
callq 0xf0a2
testl %eax, %eax
jne 0xefbe
movq 0x58(%rbx), %rsi
pushq $0x4
popq %rcx
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0xf0a2
movl %eax, %r13d
jmp 0xefbe
pushq $-0x67
popq %r13
movq 0x58(%rbx), %rsi
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xc520
negq %rax
sbbl %ebp, %ebp
orl %r13d, %ebp
incq 0x10168(%rbx)
andl $0x0, 0x70(%rbx)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq