// File: /workspace/github_c_makefile_star2_O3/Cyan4973[P]FiniteStateEntropy/programs/fileio.c
// Function: FIO_compressFilename
unsigned long long FIO_compressFilename(const char* output_filename, const char* input_filename)
{
    U64 filesize = 0;
    U64 compressedfilesize = 0;
    FILE* finput;
    FILE* foutput;
    size_t const inputBlockSize = FIO_blockID_to_blockSize(g_blockSizeId);
    char* const in_buff = (char*)malloc(inputBlockSize);
    char* const out_buff = (char*)malloc(FSE_compressBound(inputBlockSize) + 5);
    XXH32_state_t xxhState;
    typedef size_t (*compressor_t) (void* dst, size_t dstSize, const void* src, size_t srcSize);
    compressor_t compressor;
    unsigned magicNumber;


    /* Init */
    if (!in_buff || !out_buff) EXM_THROW(21, "Allocation error : not enough memory");
    XXH32_reset (&xxhState, FSE_CHECKSUM_SEED);
    get_fileHandle(input_filename, output_filename, &finput, &foutput);
    switch (g_compressor)
    {
    case FIO_fse:
        compressor = FSE_compress;
        magicNumber = FIO_magicNumber_fse;
        break;
    case FIO_huf:
        compressor = HUF_compress;
        magicNumber = FIO_magicNumber_huf;
        break;
    case FIO_zlibh:
        compressor = FIO_ZLIBH_compress;
        magicNumber = FIO_magicNumber_zlibh;
        break;
    default :
        EXM_THROW(20, "unknown compressor selection");
    }

    /* Write Frame Header */
    FIO_writeLE32(out_buff, magicNumber);
    out_buff[4] = (char)g_blockSizeId;          /* Max Block Size descriptor */
    { size_t const sizeCheck = fwrite(out_buff, 1, FIO_FRAMEHEADERSIZE, foutput);
      if (sizeCheck!=FIO_FRAMEHEADERSIZE) EXM_THROW(22, "Write error : cannot write header"); }
    compressedfilesize += FIO_FRAMEHEADERSIZE;

    /* Main compression loop */
    while (1) {
        /* Fill input Buffer */
        size_t cSize;
        size_t const inSize = fread(in_buff, (size_t)1, (size_t)inputBlockSize, finput);
        DISPLAYLEVEL(6, "reading %zu bytes from input (%s)\n",
                        inSize, input_filename);
        if (inSize==0) break;
        filesize += inSize;
        XXH32_update(&xxhState, in_buff, inSize);
        DISPLAYUPDATE(2, "\rRead : %u MB   ", (U32)(filesize>>20));

        /* Compress Block */
        cSize = compressor(out_buff + FIO_maxBlockHeaderSize, FSE_compressBound(inputBlockSize), in_buff, inSize);
        if (FSE_isError(cSize)) EXM_THROW(23, "Compression error : %s ", FSE_getErrorName(cSize));

        /* Write cBlock */
        switch(cSize)
        {
        size_t headerSize;
        case 0: /* raw */
            DISPLAYLEVEL(6, "packing uncompressed block, of size %zu \n", inSize);
            if (inSize == inputBlockSize) {
                out_buff[0] = (BYTE)((bt_raw << 6) + BIT5);
                headerSize = 1;
            } else {
                out_buff[0] = (BYTE)(bt_raw << 6);
                out_buff[1] = (BYTE)(inSize >> 8);
                out_buff[2] = (BYTE)inSize;
                headerSize = 3;
            }
            { size_t const sizeCheck = fwrite(out_buff, 1, headerSize, foutput);
              if (sizeCheck!=headerSize) EXM_THROW(24, "Write error : cannot write block header"); }
            { size_t const sizeCheck = fwrite(in_buff, 1, inSize, foutput);
              if (sizeCheck!=(size_t)(inSize)) EXM_THROW(25, "Write error : cannot write block"); }
            compressedfilesize += inSize + headerSize;
            break;
        case 1: /* rle */
            DISPLAYLEVEL(6, "packing RLE block, of size %zu \n", inSize);
            if (inSize == inputBlockSize) {
                out_buff[0] = (BYTE)((bt_rle << 6) + BIT5);
                headerSize = 1;
            } else {
                out_buff[0] = (BYTE)(bt_rle << 6);
                out_buff[1] = (BYTE)(inSize >> 8);
                out_buff[2] = (BYTE)inSize;
                headerSize = 3;
            }
            out_buff[headerSize] = in_buff[0];
            { size_t const sizeCheck = fwrite(out_buff, 1, headerSize+1, foutput);
              if (sizeCheck!=(headerSize+1)) EXM_THROW(26, "Write error : cannot write rle block"); }
            compressedfilesize += headerSize + 1;
            break;
        default : /* compressed */
            DISPLAYLEVEL(6, "packing compressed block, of size %zu, into %zu bytes \n",
                            inSize, cSize);
            if (inSize == inputBlockSize) {
                out_buff[2] = (BYTE)((bt_compressed << 6) + BIT5);
                DISPLAYLEVEL(7, "generated block descriptor : %u \n", out_buff[2]);
                out_buff[3] = (BYTE)(cSize >> 8);
                out_buff[4] = (BYTE)cSize;
                headerSize = 3;
            } else {
                out_buff[0] = (BYTE)(bt_compressed << 6);
                out_buff[1] = (BYTE)(inSize >> 8);
                out_buff[2] = (BYTE)inSize;
                out_buff[3] = (BYTE)(cSize >> 8);
                out_buff[4] = (BYTE)cSize;
                headerSize = FIO_maxBlockHeaderSize;
            }
            { size_t const sizeCheck = fwrite(out_buff+(FIO_maxBlockHeaderSize-headerSize), 1, headerSize+cSize, foutput);
              if (sizeCheck!=(headerSize+cSize)) EXM_THROW(27, "Write error : cannot write rle block"); }
            compressedfilesize += headerSize + cSize;
            break;
        }

        DISPLAYUPDATE(2, "\rRead : %u MB  ==> %.2f%%   ", (U32)(filesize>>20), (double)(compressedfilesize/filesize)*100);
    }

    /* Checksum */
    {   U32 checksum = XXH32_digest(&xxhState);
        checksum = (checksum >> 5) & ((1U<<22)-1);
        out_buff[2] = (BYTE)checksum;
        out_buff[1] = (BYTE)(checksum >> 8);
        out_buff[0] = (BYTE)((checksum >> 16) + (bt_crc << 6));
        { size_t const sizeCheck = fwrite(out_buff, 1, 3, foutput);
          if (sizeCheck!=3) EXM_THROW(28, "Write error : cannot write checksum"); }
        compressedfilesize += 3;
    }

    /* Status */
    DISPLAYLEVEL(2, "\r%79s\r", "");
    DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
        (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)(compressedfilesize/filesize)*100);

    /* clean */
    free(in_buff);
    free(out_buff);
    fclose(finput);
    fclose(foutput);

    return compressedfilesize;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movl $0x8000, %edi           # imm = 0x8000
callq 0x1150
movq %rax, %r15
movl $0x8000, %edi           # imm = 0x8000
callq 0x9aca
leaq 0x5(%rax), %rdi
callq 0x1150
testq %r15, %r15
je 0x4db8
movq %rax, %rbp
testq %rax, %rax
je 0x4db8
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x57f1
leaq 0x40(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x4e18
movl 0x1582b(%rip), %eax     # 0x1a140
cmpq $0x3, %rax
jae 0x4dd0
movl %eax, %ecx
shlb $0x4, %cl
addb $0x23, %cl
leaq 0x15482(%rip), %rdx     # 0x19db0
movq (%rdx,%rax,8), %rax
movq %rax, 0x30(%rsp)
movb $0x9, (%rbp)
movb %cl, 0x1(%rbp)
movw $0x183e, 0x2(%rbp)      # imm = 0x183E
movb $0x5, 0x4(%rbp)
movq 0x38(%rsp), %rcx
movl $0x5, %r12d
movl $0x1, %esi
movl $0x5, %edx
movq %rbp, %rdi
movq %rcx, 0x8(%rsp)
callq 0x1190
cmpq $0x5, %rax
jne 0x4de8
movq 0x40(%rsp), %r14
leaq 0x5(%rbp), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
movq %r14, 0x20(%rsp)
movl $0x1, %esi
movl $0x8000, %edx           # imm = 0x8000
movq %r15, %rdi
movq %r14, %rcx
callq 0x1060
movq %rax, %rbx
cmpl $0x6, 0x15745(%rip)     # 0x1a0f4
jge 0x4c17
testq %rbx, %rbx
je 0x4cae
addq %rbx, 0x10(%rsp)
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x587d
cmpl $0x2, 0x1571a(%rip)     # 0x1a0f4
jl 0x4a1e
movq 0x15765(%rip), %r13     # 0x1a148
callq 0x1070
subq %r13, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x7, %rdx
addl %eax, %edx
cmpl $0x96, %edx
ja 0x4c3c
cmpl $0x4, 0x156dc(%rip)     # 0x1a0f4
jge 0x4c3c
movl $0x8000, %edi           # imm = 0x8000
callq 0x9aca
movq 0x28(%rsp), %rdi
movq %rax, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq *0x30(%rsp)
movq %rax, %r13
movq %rax, %rdi
callq 0x7712
movl 0x156a9(%rip), %ecx     # 0x1a0f4
testl %eax, %eax
jne 0x4d42
cmpq $0x1, %r13
je 0x4a7a
testq %r13, %r13
jne 0x4a9d
cmpl $0x6, %ecx
jge 0x4c4b
cmpq $0x8000, %rbx           # imm = 0x8000
jne 0x4ad3
movl $0x1, %r13d
movb $0x60, %al
jmp 0x4ae5
cmpl $0x6, %ecx
jge 0x4c6b
cmpq $0x8000, %rbx           # imm = 0x8000
jne 0x4b2f
movl $0x1, %r13d
movb $-0x60, %al
jmp 0x4b3f
cmpl $0x6, %ecx
jge 0x4c8b
cmpq $0x8000, %rbx           # imm = 0x8000
jne 0x4b6c
movb $0x20, 0x2(%rbp)
movl $0x3, %ebx
cmpl $0x7, 0x15631(%rip)     # 0x1a0f4
jl 0x4b7b
callq 0x22d7
jmp 0x4b7b
movl %ebx, %eax
rolw $0x8, %ax
movw %ax, 0x1(%rbp)
movl $0x3, %r13d
movb $0x40, %al
movb %al, (%rbp)
movl $0x1, %esi
movq %rbp, %rdi
movq %r13, %rdx
movq 0x8(%rsp), %r14
movq %r14, %rcx
callq 0x1190
cmpq %r13, %rax
jne 0x4da0
movl $0x1, %esi
movq %r15, %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x1190
cmpq %rbx, %rax
jne 0x4d58
addq %rbx, %r13
movq 0x20(%rsp), %r14
jmp 0x4bae
rolw $0x8, %bx
movw %bx, 0x1(%rbp)
movl $0x3, %r13d
movb $-0x80, %al
movb %al, (%rbp)
movb (%r15), %al
movb %al, (%rbp,%r13)
incq %r13
movl $0x1, %esi
movq %rbp, %rdi
movq %r13, %rdx
movq 0x8(%rsp), %rcx
callq 0x1190
cmpq %r13, %rax
je 0x4bae
jmp 0x4d70
movb $0x0, (%rbp)
movb %bh, 0x1(%rbp)
movb %bl, 0x2(%rbp)
movl $0x5, %ebx
movl %r13d, %eax
rolw $0x8, %ax
movw %ax, 0x3(%rbp)
movq %rbp, %rdi
subq %rbx, %rdi
addq $0x5, %rdi
addq %rbx, %r13
movl $0x1, %esi
movq %r13, %rdx
movq 0x8(%rsp), %rcx
callq 0x1190
cmpq %r13, %rax
jne 0x4d88
addq %r12, %r13
cmpl $0x2, 0x1553c(%rip)     # 0x1a0f4
movq %r13, %r12
jl 0x4990
movq 0x15580(%rip), %rbx     # 0x1a148
callq 0x1070
subq %rbx, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x7, %rdx
addl %eax, %edx
cmpl $0x96, %edx
ja 0x4c02
cmpl $0x4, 0x154fb(%rip)     # 0x1a0f4
movq %r13, %r12
jl 0x4990
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x234a
movq %r13, %r12
jmp 0x4990
movq 0x153da(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xee20(%rip), %rsi      # 0x13a48
movq %rbx, %rdx
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x1110
jmp 0x49b5
movq 0x10(%rsp), %rdi
callq 0x2132
jmp 0x4a1e
movq 0x153a6(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xee38(%rip), %rsi      # 0x13a94
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1110
jmp 0x4a67
movq 0x15386(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xee8b(%rip), %rsi      # 0x13b07
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1110
jmp 0x4a83
movq 0x15366(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xeeb1(%rip), %rsi      # 0x13b4d
movq %rbx, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x1110
jmp 0x4aa6
leaq 0x48(%rsp), %rdi
callq 0x5a08
movl %eax, %ecx
shrl $0x5, %ecx
movb %cl, 0x2(%rbp)
movl %eax, %ecx
shrl $0xd, %ecx
movb %cl, 0x1(%rbp)
shrl $0x15, %eax
orb $-0x40, %al
movb %al, (%rbp)
movl $0x1, %esi
movl $0x3, %edx
movq %rbp, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x1190
cmpq $0x3, %rax
jne 0x4e00
addq $0x3, %r12
cmpl $0x2, 0x153f5(%rip)     # 0x1a0f4
jge 0x4d33
movq %r15, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x1080
movq %r12, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x2424
jmp 0x4d01
testl %ecx, %ecx
jle 0x4d4e
movq %r13, %rdi
callq 0x217a
movl $0x17, %edi
callq 0x1180
cmpl $0x0, 0x15395(%rip)     # 0x1a0f4
jle 0x4d66
callq 0x2285
movl $0x19, %edi
callq 0x1180
cmpl $0x0, 0x1537d(%rip)     # 0x1a0f4
jle 0x4d7e
callq 0x21e1
movl $0x1a, %edi
callq 0x1180
cmpl $0x0, 0x15365(%rip)     # 0x1a0f4
jle 0x4d96
callq 0x22f8
movl $0x1b, %edi
callq 0x1180
cmpl $0x0, 0x1534d(%rip)     # 0x1a0f4
jle 0x4dae
callq 0x2233
movl $0x18, %edi
callq 0x1180
cmpl $0x0, 0x15335(%rip)     # 0x1a0f4
jle 0x4dc6
callq 0x203d
movl $0x15, %edi
callq 0x1180
cmpl $0x0, 0x1531d(%rip)     # 0x1a0f4
jle 0x4dde
callq 0x208e
movl $0x14, %edi
callq 0x1180
cmpl $0x0, 0x15305(%rip)     # 0x1a0f4
jle 0x4df6
callq 0x20e0
movl $0x16, %edi
callq 0x1180
cmpl $0x0, 0x152ed(%rip)     # 0x1a0f4
jle 0x4e0e
callq 0x23d2
movl $0x1c, %edi
callq 0x1180