// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/encoding/encoding.cpp
// Function: bool fuif_decode<FileIO>(FileIO&, Image&, fuif_options)
bool fuif_decode(IO& io, Image &image, fuif_options options) {
    char buff[5];
    if (!io.gets(buff,5)) { e_printf("Could not read header from file: %s\n",io.getName()); return false; }
    bool multi_frame = false;
    if (!strcmp(buff,"FUAF")) { multi_frame = true; }
    else if (strcmp(buff,"FUIF")) { e_printf("%s is not a FUIF file\n",io.getName()); return false; }
    int nb_channels = read_big_endian_varint(io) - '0';
    int bit_depth = read_big_endian_varint(io) - '&';
    int w = read_big_endian_varint(io)+1;
    int h = read_big_endian_varint(io)+1;
    int nb_frames = 1;
    int loops = 0;
    int den = 0;
    std::vector<int> num;
    if (multi_frame) {
        nb_frames = read_big_endian_varint(io)+2;
        den = read_big_endian_varint(io)+1;
        int numerator = read_big_endian_varint(io);
        if (numerator) {
            num.push_back(numerator);
            for (int i=1; i<nb_frames; i++) num.push_back(read_big_endian_varint(io));
        }
        loops = read_big_endian_varint(io);
    }
    int colormodel = read_big_endian_varint(io);
    if (options.identify) {
        v_printf(1,"%s: %i-channel, %i-bit, ", io.getName(), nb_channels, bit_depth);
        if (multi_frame) v_printf(1,"%ix%i %s%s animation (%i frames)\n", w, h/nb_frames, colormodel_name(colormodel,nb_channels), colorprofile_name(colormodel), nb_frames);
        else v_printf(1,"%ix%i %s%s image\n", w, h, colormodel_name(colormodel,nb_channels), colorprofile_name(colormodel));
    }
    options.max_properties = read_big_endian_varint(io);

    v_printf(4,"Global option: up to %i back-referencing MANIAC properties.\n", options.max_properties);

    v_printf(7,"First part of header decoded (basic info). Read %i bytes so far.\n",io.ftell());

    if (!options.identify) {
        image = Image(w, h, (1<<bit_depth)-1, nb_channels, colormodel);
        image.nb_frames = nb_frames;
        image.den = den;
        image.num = num;
        image.loops = loops;
    }

    if (nb_channels < 1) return true; // is there any use for a zero-channel image?

    int responsive_offsets[5];
    int relative_offset = 0;
    for (int s=0; s<5; s++) {
        responsive_offsets[s] = read_big_endian_varint(io)*TRUNCATION_OFFSET_RESOLUTION + relative_offset;
        relative_offset = responsive_offsets[s];
    }
    relative_offset = io.ftell();
    for (int s=0; s<5; s++) {
        responsive_offsets[s] += relative_offset;
        if (s) v_printf(3,"Responsive truncation point for size 1/%i at position %i\n",responsive_sizes[s],responsive_offsets[s]);
        else v_printf(3,"Responsive truncation point for LQIP at position %i\n",responsive_offsets[s]);
    }



    if (!options.identify) {
    if (multi_frame) v_printf(2,"Decoding %i-channel, %i-bit, %ix%i %s%s animation (%i frames)", nb_channels, bit_depth, w, h/nb_frames, colormodel_name(colormodel,nb_channels), colorprofile_name(colormodel), nb_frames);
    else v_printf(2,"Decoding %i-channel, %i-bit, %ix%i %s%s image", nb_channels, bit_depth, w, h, colormodel_name(colormodel,nb_channels), colorprofile_name(colormodel));
    if (options.preview == 0) v_printf(2, " as a low-quality image placeholder");
    else if (options.preview > 0) v_printf(2," at scale 1:%i",responsive_sizes[options.preview]);
    v_printf(2,".\n");
    }


    v_printf(7,"Second part of header decoded (responsive offsets and global parameters; before transforms). Read %i bytes so far.\n",io.ftell());

    // decode transforms
    int nb_transforms = read_big_endian_varint(io);
    v_printf(2,"Image data underwent %i transformations: ",nb_transforms);
    for (int i=0; i<nb_transforms; i++) {
        int id_and_nb_params = read_big_endian_varint(io);
        Transform t(id_and_nb_params & 0xf);
        if (t.has_parameters()) {
            int nb_params = (id_and_nb_params >> 4);
            for (int j=0; j<nb_params; j++) t.parameters.push_back(read_big_endian_varint(io));
        }
        if (!options.identify) {
            t.meta_apply(image);
            image.transform.push_back(t);
        }
        if (i) v_printf(2,", ");
        v_printf(2,"%s",t.name());
        if (t.ID == TRANSFORM_PALETTE) {
            if (t.parameters[0] == t.parameters[1]) v_printf(3,"[Compact channel %i to ",t.parameters[0]);
            else v_printf(3,"[channels %i-%i with ",t.parameters[0],t.parameters[1]);
            v_printf(3,"%i colors]",t.parameters[2]);
        }
    }
    v_printf(2,"\n");
    v_printf(6,"Header decoded. Read %i bytes so far.\n",io.ftell());
    if (options.identify) return true;
    if (image.error) {
        e_printf("Corrupt file. Aborting.\n");
        return false;
    }

    nb_channels = image.channel.size();

    size_t bytes_to_load = 0;
    if (options.preview >= 0) bytes_to_load = responsive_offsets[options.preview];

    // decode channel data
    for (int i=0; i<nb_channels; i++) {
        if ((options.preview < 0 || io.ftell() < bytes_to_load) && !io.isEOF()) {
            if (! image.channel[i].w || ! image.channel[i].h ) continue; // skip empty channels
            if (!fuif_decode_channel<IO, FinalPropertySymbolCoder<FUIFBitChancePass2, RacIn<IO>, MAX_BIT_DEPTH> >(io, options, i, image, bytes_to_load)) return false;
            if (image.transform.size() > 0 && image.transform.back().ID == TRANSFORM_PERMUTE && image.transform.back().parameters.size() == 0 && i==0) inv_permute_meta(image);
        } else {
            v_printf(3,"Skipping decode of channels %i-%i.\n",i,nb_channels-1);
            break;
        }
    }
    v_printf(3,"Done decoding. Read %i bytes.\n",io.ftell());
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdx, 0x28(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x37(%rsp), %rsi
pushq $0x5
popq %rdx
callq 0xfdd8
testq %rax, %rax
je 0xf6b1
leaq 0xef15(%rip), %rsi      # 0x1e52a
leaq 0x37(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x3300
movl %eax, 0x30(%rsp)
testl %eax, %eax
je 0xf64f
leaq 0xeef4(%rip), %rsi      # 0x1e525
leaq 0x37(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x3300
testl %eax, %eax
je 0xf64f
movq 0x8(%rbx), %rsi
leaq 0xf06e(%rip), %rdi      # 0x1e6bb
jmp 0xf6bc
movq %rbx, %rdi
callq 0xfde8
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
callq 0xfde8
movl %eax, %r13d
movq %rbx, %rdi
callq 0xfde8
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
callq 0xfde8
andq $0x0, 0x70(%rsp)
movl %eax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0xf6ca
movl $0x0, 0x4(%rsp)
pushq $0x1
popq %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
jmp 0xf74d
movq 0x8(%rbx), %rsi
leaq 0xefda(%rip), %rdi      # 0x1e696
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x16b80
jmp 0xfd84
movq %rbx, %rdi
callq 0xfde8
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0xfde8
movl %eax, 0x1c(%rsp)
movq %rbx, %rdi
callq 0xfde8
movq 0x10(%rsp), %rcx
addl $0x2, %ecx
movq %rcx, 0x10(%rsp)
movl %eax, 0x78(%rsp)
testl %eax, %eax
je 0xf73d
leaq 0x60(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xa60a
pushq $0x1
popq %rbp
leaq 0x60(%rsp), %r14
leaq 0x40(%rsp), %r15
cmpl 0x10(%rsp), %ebp
jge 0xf73d
movq %rbx, %rdi
callq 0xfde8
movl %eax, 0x40(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xb266
incl %ebp
jmp 0xf71c
movq %rbx, %rdi
callq 0xfde8
movl %eax, 0x4(%rsp)
incl 0x1c(%rsp)
movq %rbx, %rdi
callq 0xfde8
movl %eax, (%rsp)
movq 0x20(%rsp), %rax
leal -0x30(%rax), %r15d
addl $-0x26, %r13d
incl 0xc(%rsp)
incl 0x8(%rsp)
movq 0x28(%rsp), %rax
cmpb $0x1, 0x4(%rax)
jne 0xf81d
movq 0x8(%rbx), %rdx
leaq 0xef4b(%rip), %rsi      # 0x1e6d2
pushq $0x1
popq %rdi
movl %r15d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x16c30
cmpl $0x0, 0x30(%rsp)
je 0xf7d5
movl (%rsp), %edi
movl %r15d, %esi
callq 0x4eec
movq %rax, %r14
movl (%rsp), %edi
callq 0x4fd5
leaq 0xef52(%rip), %rsi      # 0x1e70d
pushq $0x1
popq %rdi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x16c30
jmp 0xf81d
movl 0x8(%rsp), %eax
cltd
idivl 0x10(%rsp)
movl %eax, %ebp
movl (%rsp), %edi
movl %r15d, %esi
callq 0x4eec
movq %rax, %r14
movl (%rsp), %edi
callq 0x4fd5
subq $0x8, %rsp
leaq 0xeeea(%rip), %rsi      # 0x1e6eb
pushq $0x1
popq %rdi
movl 0x14(%rsp), %edx
movl %ebp, %ecx
movq %r14, %r8
movq %rax, %r9
xorl %eax, %eax
pushq 0x18(%rsp)
callq 0x16c30
popq %rax
popq %rcx
movq %rbx, %rdi
callq 0xfde8
movq 0x28(%rsp), %rcx
movl %eax, 0x10(%rcx)
leaq 0xeeeb(%rip), %rsi      # 0x1e71f
pushq $0x4
popq %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x16c30
movq %rbx, %rdi
callq 0xfe52
leaq 0xef0d(%rip), %rsi      # 0x1e75c
pushq $0x7
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x16c30
movq 0x28(%rsp), %rax
cmpb $0x0, 0x4(%rax)
jne 0xf8cf
pushq $-0x1
popq %rax
movl %r13d, %ecx
shll %cl, %eax
notl %eax
leaq 0x78(%rsp), %rdi
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
movl %eax, %ecx
movl %r15d, %r8d
movl (%rsp), %r9d
callq 0xfe5a
leaq 0x78(%rsp), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0xff86
movq %r14, %rdi
callq 0xa776
movq 0x10(%rsp), %rax
movl %eax, 0x38(%r12)
movl 0x1c(%rsp), %eax
movl %eax, 0x3c(%r12)
leaq 0x40(%r12), %rdi
leaq 0x60(%rsp), %rsi
callq 0x10000
movl 0x4(%rsp), %eax
movl %eax, 0x58(%r12)
movb $0x1, %bpl
cmpl $0x31, 0x20(%rsp)
jl 0xfd7a
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq $0x5, %r14
je 0xf8fc
movq %rbx, %rdi
callq 0xfde8
addl %eax, %ebp
movl %ebp, 0x40(%rsp,%r14,4)
incq %r14
jmp 0xf8e2
movl %r15d, 0x4(%rsp)
movq %r12, 0x20(%rsp)
movq %rbx, %rdi
callq 0xfe52
movq %rax, %r14
movl %r13d, 0x1c(%rsp)
leaq 0xeebb(%rip), %r15      # 0x1e7d8
pushq $0x3
popq %rbp
leaq 0xee77(%rip), %r13      # 0x1e79e
xorl %r12d, %r12d
cmpq $0x14, %r12
je 0xf96f
movl 0x40(%rsp,%r12), %ecx
addl %r14d, %ecx
movl %ecx, 0x40(%rsp,%r12)
testq %r12, %r12
je 0xf95b
leaq 0xe9b7(%rip), %rax      # 0x1e300
movl (%r12,%rax), %edx
movl %ebp, %edi
movq %r13, %rsi
xorl %eax, %eax
callq 0x16c30
jmp 0xf969
movl %ebp, %edi
movq %r15, %rsi
movl %ecx, %edx
xorl %eax, %eax
callq 0x16c30
addq $0x4, %r12
jmp 0xf92a
movq 0x28(%rsp), %rax
cmpb $0x0, 0x4(%rax)
movl 0x1c(%rsp), %r15d
jne 0xfa72
cmpl $0x0, 0x30(%rsp)
je 0xf9ce
movl (%rsp), %edi
movl 0x4(%rsp), %esi
callq 0x4eec
movq %rax, %r14
movl (%rsp), %edi
callq 0x4fd5
movq %rax, %r10
leaq 0xeea0(%rip), %rsi      # 0x1e84b
pushq $0x2
popq %rdi
movl 0x4(%rsp), %edx
movl %r15d, %ecx
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
xorl %eax, %eax
pushq %r10
pushq %r14
callq 0x16c30
popq %rax
popq %rcx
jmp 0xfa23
movl 0x8(%rsp), %eax
cltd
idivl 0x10(%rsp)
movl %eax, %ebp
movl (%rsp), %edi
movl 0x4(%rsp), %esi
callq 0x4eec
movq %rax, %r14
movl (%rsp), %edi
callq 0x4fd5
movq %rax, %r10
subq $0x8, %rsp
leaq 0xee0f(%rip), %rsi      # 0x1e80d
pushq $0x2
popq %rdi
movl 0xc(%rsp), %edx
movl %r15d, %ecx
movl 0x14(%rsp), %r8d
movl %ebp, %r9d
xorl %eax, %eax
pushq 0x18(%rsp)
pushq %r10
pushq %r14
callq 0x16c30
addq $0x20, %rsp
movq 0x28(%rsp), %rax
movl (%rax), %eax
testq %rax, %rax
je 0xfa50
testl %eax, %eax
jle 0xfa61
leaq 0xe8c6(%rip), %rcx      # 0x1e300
movl (%rcx,%rax,4), %edx
leaq 0xee59(%rip), %rsi      # 0x1e89d
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x16c30
jmp 0xfa61
leaq 0xee22(%rip), %rsi      # 0x1e879
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x16c30
leaq 0xdfd5(%rip), %rsi      # 0x1da3d
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x16c30
movq %rbx, %rdi
callq 0xfe52
leaq 0xee2b(%rip), %rsi      # 0x1e8ac
pushq $0x7
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x16c30
movq %rbx, %rdi
callq 0xfde8
movl %eax, %ebp
leaq 0xee81(%rip), %rsi      # 0x1e920
xorl %r14d, %r14d
pushq $0x2
popq %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x16c30
leaq 0x80(%rsp), %r12
movq 0x20(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x10(%rsp)
testl %ebp, %ebp
cmovlel %r14d, %ebp
leaq 0x3c(%rsp), %r15
xorl %r14d, %r14d
cmpl %ebp, %r14d
je 0xfc08
movq %rbx, %rdi
callq 0xfde8
movl %eax, %r13d
andl $0xf, %eax
movl %eax, 0x78(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
andq $0x0, 0x10(%r12)
cmpl $0xa, %eax
ja 0xfb39
movl $0x7d8, %ecx            # imm = 0x7D8
btl %eax, %ecx
jae 0xfb39
sarl $0x4, %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
subl $0x1, %r13d
jb 0xfb39
movq %rbx, %rdi
callq 0xfde8
movl %eax, 0x3c(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0xb266
jmp 0xfb1a
movq 0x28(%rsp), %rax
cmpb $0x0, 0x4(%rax)
jne 0xfb62
leaq 0x78(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0xa330
movq 0x10(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x52fa
testl %r14d, %r14d
je 0xfb78
pushq $0x2
popq %rdi
leaq 0xe164(%rip), %rsi      # 0x1dcd5
xorl %eax, %eax
callq 0x16c30
movslq 0x78(%rsp), %rax
leaq 0x1cab4(%rip), %rcx     # 0x2c638
movq (%rcx), %rcx
shlq $0x5, %rax
movq (%rcx,%rax), %rdx
pushq $0x2
popq %rdi
leaq 0xedb1(%rip), %rsi      # 0x1e94a
xorl %eax, %eax
callq 0x16c30
cmpl $0x6, 0x78(%rsp)
jne 0xfbf8
movq 0x80(%rsp), %rax
movl (%rax), %edx
movl 0x4(%rax), %ecx
cmpl %ecx, %edx
jne 0xfbcb
pushq $0x3
popq %rdi
leaq 0xed8b(%rip), %rsi      # 0x1e94d
xorl %eax, %eax
callq 0x16c30
jmp 0xfbdc
pushq $0x3
popq %rdi
leaq 0xed90(%rip), %rsi      # 0x1e965
xorl %eax, %eax
callq 0x16c30
movq 0x80(%rsp), %rax
movl 0x8(%rax), %edx
pushq $0x3
popq %rdi
leaq 0xed8a(%rip), %rsi      # 0x1e97b
xorl %eax, %eax
callq 0x16c30
movq %r12, %rdi
callq 0x5374
incl %r14d
jmp 0xfad2
leaq 0xfd27(%rip), %rsi      # 0x1f936
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x16c30
movq %rbx, %rdi
callq 0xfe52
leaq 0xed5e(%rip), %rsi      # 0x1e986
pushq $0x6
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x16c30
movb $0x1, %bpl
movq 0x28(%rsp), %rax
cmpb $0x0, 0x4(%rax)
jne 0xfd7a
movq 0x20(%rsp), %rax
cmpb $0x1, 0xa8(%rax)
jne 0xfc6a
xorl %ebp, %ebp
leaq 0xed4f(%rip), %rdi      # 0x1e9ad
xorl %eax, %eax
callq 0x16b80
jmp 0xfd7a
movq 0x8(%rax), %r14
subq (%rax), %r14
shrq $0x6, %r14
movq 0x28(%rsp), %rax
movslq (%rax), %rcx
xorl %eax, %eax
testq %rcx, %rcx
js 0xfc8b
movslq 0x40(%rsp,%rcx,4), %r15
jmp 0xfc8e
xorl %r15d, %r15d
leaq 0x78(%rsp), %r12
movl %eax, 0x78(%rsp)
cmpl %r14d, %eax
jge 0xfd5e
movq 0x28(%rsp), %rax
cmpl $0x0, (%rax)
js 0xfcbb
movq %rbx, %rdi
callq 0xfe52
cmpq %r15, %rax
jae 0xfd43
movq %rbx, %rdi
callq 0x100ca
testb %al, %al
jne 0xfd43
movslq 0x78(%rsp), %rcx
movq 0x20(%rsp), %rax
movq (%rax), %rax
shlq $0x6, %rcx
cmpl $0x0, 0x18(%rax,%rcx)
je 0xfd38
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
je 0xfd38
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
movq 0x20(%rsp), %rcx
movq %r15, %r8
callq 0x100da
testb %al, %al
je 0xfd98
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %rax
cmpq 0x18(%rcx), %rax
je 0xfd38
cmpl $0x9, -0x20(%rax)
jne 0xfd38
movq -0x10(%rax), %rcx
cmpq -0x18(%rax), %rcx
jne 0xfd38
cmpl $0x0, 0x78(%rsp)
jne 0xfd38
movq 0x20(%rsp), %rdi
callq 0xce90
movl 0x78(%rsp), %eax
incl %eax
jmp 0xfc93
movl 0x78(%rsp), %edx
decl %r14d
leaq 0xec75(%rip), %rsi      # 0x1e9c6
pushq $0x3
popq %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x16c30
movq %rbx, %rdi
callq 0xfe52
leaq 0xec7d(%rip), %rsi      # 0x1e9ea
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x16c30
leaq 0x60(%rsp), %rdi
callq 0x5374
movl %ebp, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0xfd7a
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
jmp 0xfdb4
movq %rax, %rbx
jmp 0xfdc6
jmp 0xfdbb
movq %rax, %rbx
movq %r12, %rdi
callq 0x5374
leaq 0x60(%rsp), %rdi
callq 0x5374
movq %rbx, %rdi
callq 0x35d0