// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/encoding/encoding.cpp
// Function: bool fuif_encode_channels<BlobIO, RacOutput24<BlobIO>, FinalPropertySymbolCoder<SimpleBitChance, RacOutput24<BlobIO>, 15>, false, true>(BlobIO&, Tree&, fuif_options&, int, int, int, Image const&, unsigned long&)
bool fuif_encode_channels(IO& io, Tree &tree, fuif_options &options, int predictor, int beginc, int endc, const Image &image, size_t &header_pos) {
  assert(endc >= beginc);
  write_big_endian_varint(io, ((endc-beginc) << 4) + (predictor << 1) + (compress?1:0));
  if (endc>beginc) v_printf(5,"Encoding%s channels %i-%i\n", (compress?"":" uncompressed"), beginc, endc);

  pixel_type global_minv=LARGEST_VAL;
  pixel_type global_maxv=SMALLEST_VAL;
  for (int i=beginc; i<=endc; i++) {
    const Channel &channel = image.channel[i];
    if (channel.w * channel.h <= 0) continue; // zero pixel channel? could happen
    pixel_type minv = channel.minval;
    pixel_type maxv = channel.maxval;
    if (minv<global_minv) global_minv=minv;
    if (maxv>global_maxv) global_maxv=maxv;
  }
  if (global_minv <= 0) {
    write_big_endian_varint(io, 1-global_minv);
  } else {
    write_big_endian_varint(io, 0);
    write_big_endian_varint(io, global_minv);
  }
  write_big_endian_varint(io, global_maxv-global_minv);


  int firstrealc = beginc;
  for (int i=beginc; i<=endc; i++) {
    const Channel &channel = image.channel[i];
    if (channel.w * channel.h <= 0) continue; // zero pixel channel? could happen

    pixel_type minv = channel.minval;
    pixel_type maxv = channel.maxval;

    if (endc > beginc && global_minv < global_maxv) {
      write_big_endian_varint(io, minv-global_minv);
      write_big_endian_varint(io, maxv-minv);
    }
    if (minv == maxv) { firstrealc++; }

    if (!check_bit_depth(minv,maxv,predictor)) return false;

    if (minv == 0 && maxv == 0) continue; // don't need quantization factor if it's all zeroes anyway

    write_big_endian_varint(io, channel.q);

    if (!learn) v_printf(6,"Encoding %ix%i channel %i with range %i..%i (predictor %i, quantization %i), (shift=%i,%i, cshift=%i,%i)\n", channel.w, channel.h, i, minv, maxv, predictor, channel.q, channel.hshift, channel.vshift, channel.hcshift, channel.vcshift);
    else  v_printf(7,"Learning %ix%i channel %i with range %i..%i (predictor %i)\n", channel.w, channel.h, i, minv, maxv, predictor);

    channel.setzero();
  }

  header_pos = io.ftell();

  if (firstrealc > endc) return true; // all trivial


  Ranges propRanges;
  init_properties(propRanges, image, beginc, endc, options);

  int predictability = 2048; // 50%
  {
    const Channel &channel = image.channel[firstrealc];
    if (predictor == 0 && compress) {
        uint64_t zeroes = 0;
        uint64_t pixels = channel.h*channel.w;
        for (int y=0; y<channel.h; y++)
          for (int x=0; x<channel.w; x++)
            if ( channel.value(y,x) == 0) zeroes++;
        int rounded = zeroes * 128 / pixels;
        if (rounded < 1) rounded = 1;
        if (rounded > 127) rounded = 127;
        write_big_endian_varint(io, rounded);
        predictability = rounded * 32;
        v_printf(5,"Found %i zeroes in %i pixels (zero chance=%i/4096)\n",zeroes,pixels,predictability);
    }
  }

  Rac rac(io);

  if (!compress) {
    UniformSymbolCoder<Rac> coder(rac);
    for (int i=beginc; i<=endc; i++) {
        const Channel &channel = image.channel[i];
        for (int y=0; y<channel.h; y++) {
            for (int x=0; x<channel.w; x++) {
                coder.write_int(channel.minval,channel.maxval,channel.value(y,x)); // TODO: use predictor? (only makes sense if the predictor residues have a smaller range)
                if (!learn && options.debug)
                    options.heatmap.channel[i].value(y,x) = (x);
            }
        }
    }
  } else {
    if (!learn) {
        // encode tree here
        MetaPropertySymbolCoder<FUIFBitChanceTree, Rac> metacoder(rac, propRanges);
        metacoder.write_tree(tree);
    }
    Coder coder(rac, propRanges, tree, predictability, CONTEXT_TREE_SPLIT_THRESHOLD, options.maniac_cutoff, options.maniac_alpha);
    Properties properties(propRanges.size());


    // planar
    for (int i=beginc; i<=endc; i++) {
        const Channel &channel = image.channel[i];
        pixel_type minv = channel.minval;
        pixel_type maxv = channel.maxval;
        if (minv==maxv) continue;
        int rowslearned=0;
        Channel references(properties.size() - NB_NONREF_PROPERTIES, channel.w, 0, 0);
        for (int y=0; y<channel.h; y++) {
            if (learn) { if (++rowslearned > options.nb_repeats*channel.h) break; }
            if (learn) y=rand()%channel.h; // try random rows, to avoid giving priority to the top of the image (because then the y property cannot be learned)
            precompute_references(channel, y, image, beginc, options, references);
            for (int x=0; x<channel.w; x++) {
                pixel_type guess;
        //        guess = predict_and_compute_properties_with_reference(properties, channel, x, y, predictor, image, beginc, options);
                guess = predict_and_compute_properties_with_precomputed_reference(properties, channel, x, y, predictor, image, beginc, options, references);
                pixel_type diff = channel.value(y,x)-guess;
                if (!learn && options.debug) {
                    int estimate = coder.estimate_int(properties, minv-guess, maxv-guess, diff);
                    options.heatmap.channel[i].value(y,x) = estimate;
                }
                coder.write_int(properties, minv-guess, maxv-guess, diff);
            }
            if (learn) { y=0; } // set to zero in case random row was channel.h-1
        }
    }
    coder.simplify();
  }

  rac.flush();
  io.flush();
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4138, %rsp           # imm = 0x4138
movl %r9d, %ebp
movl %r8d, %r14d
movq %rdx, 0x30(%rsp)
movq %rsi, %r12
movl %r9d, %eax
subl %r8d, %eax
shll $0x4, %eax
movq %rcx, 0x40(%rsp)
leal (%rax,%rcx,2), %eax
incl %eax
movslq %eax, %rsi
pushq $0x1
popq %rdx
movq %rdi, 0x8(%rsp)
movq %rdx, %rbx
callq 0xdaea
movq %r14, %rdi
cmpl %edi, %ebp
movq %r14, 0x10(%rsp)
jle 0xe599
leaq 0x104c6(%rip), %rsi     # 0x1ea41
leaq 0x113b5(%rip), %rdx     # 0x1f937
pushq $0x5
popq %rdi
movq 0x10(%rsp), %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x16c30
movq 0x10(%rsp), %rdi
movq 0x4170(%rsp), %rax
movq (%rax), %rax
movslq %edi, %rcx
movl %ebp, 0x18(%rsp)
movslq %ebp, %r8
movq %rcx, %r15
shlq $0x6, %r15
addq %r15, %rax
addq $0x22, %rax
movw $0x7fff, %r14w          # imm = 0x7FFF
movw $0x8001, %bp            # imm = 0x8001
movq %rcx, 0x28(%rsp)
cmpq %r8, %rcx
jg 0xe5fc
movl -0x6(%rax), %edx
imull -0xa(%rax), %edx
testl %edx, %edx
jle 0xe5f3
movl -0x2(%rax), %edx
movzwl (%rax), %esi
cmpw %r14w, %dx
cmovgel %r14d, %edx
cmpw %bp, %si
cmovlel %ebp, %esi
movl %esi, %ebp
movl %edx, %r14d
incq %rcx
addq $0x40, %rax
jmp 0xe5ca
movq %r8, 0x20(%rsp)
movswl %r14w, %eax
testw %r14w, %r14w
movq %r12, 0x80(%rsp)
jle 0xe633
pushq $0x1
popq %rdx
movq 0x8(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
movl %eax, %r12d
callq 0xdaea
movq 0x10(%rsp), %rdi
movzwl %r14w, %esi
jmp 0xe640
movq %rbx, %rsi
subl %eax, %esi
movq 0x8(%rsp), %r13
movl %eax, %r12d
cmpl %edi, 0x18(%rsp)
setg 0x4(%rsp)
pushq $0x1
popq %rbx
movq %r13, %rdi
movl %ebx, %edx
callq 0xdaea
movswl %bp, %eax
movl %r12d, 0x5c(%rsp)
subl %r12d, %eax
movslq %eax, %rsi
movq %r13, %rdi
movl %ebx, %edx
callq 0xdaea
cmpw %bp, %r14w
setl %al
andb 0x4(%rsp), %al
movb %al, 0x1c(%rsp)
movq 0x28(%rsp), %r12
movq 0x10(%rsp), %rax
movl %eax, 0x4(%rsp)
movq 0x20(%rsp), %rcx
cmpq %rcx, %r12
jg 0xe7d3
movq 0x4170(%rsp), %rax
movq (%rax), %rbp
movl 0x1c(%rbp,%r15), %eax
imull 0x18(%rbp,%r15), %eax
testl %eax, %eax
jle 0xe7c7
movq %r12, 0x38(%rsp)
movswl 0x20(%rbp,%r15), %r13d
movswl 0x22(%rbp,%r15), %ebx
cmpb $0x0, 0x1c(%rsp)
je 0xe700
movl %r13d, %eax
subl 0x5c(%rsp), %eax
movslq %eax, %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdi
pushq $0x1
popq %r12
movl %r12d, %edx
callq 0xdaea
movl %ebx, %eax
subl %r13d, %eax
movslq %eax, %rsi
movq %r14, %rdi
movl %r12d, %edx
callq 0xdaea
xorl %r14d, %r14d
cmpw %bx, %r13w
sete %r12b
movl %r13d, %edi
movl %ebx, %esi
movq 0x40(%rsp), %rdx
callq 0xccd0
testb %al, %al
movq 0x20(%rsp), %rcx
je 0xebb7
movb %r12b, %r14b
addl %r14d, 0x4(%rsp)
movl %ebx, %eax
orw %r13w, %ax
jne 0xe741
movq 0x38(%rsp), %r12
jmp 0xe7c7
leaq (%r15,%rbp), %rax
movq %rax, 0x50(%rsp)
movslq 0x28(%rbp,%r15), %rsi
movq 0x8(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0xdaea
movl 0x18(%rbp,%r15), %edx
movl 0x1c(%rbp,%r15), %ecx
movl 0x28(%rbp,%r15), %eax
movq %rax, 0x48(%rsp)
movl 0x2c(%rbp,%r15), %r11d
movl 0x30(%rbp,%r15), %r10d
movl 0x34(%rbp,%r15), %r14d
movl 0x38(%rbp,%r15), %ebp
subq $0x8, %rsp
pushq $0x6
popq %rdi
leaq 0x102d8(%rip), %rsi     # 0x1ea6a
movq 0x40(%rsp), %r12
movl %r12d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
pushq %rbp
pushq %r14
pushq %r10
pushq %r11
pushq 0x70(%rsp)
pushq 0x70(%rsp)
pushq %rbx
callq 0x16c30
addq $0x40, %rsp
movq 0x50(%rsp), %rdi
callq 0xab78
movq 0x20(%rsp), %rcx
incq %r12
addq $0x40, %r15
jmp 0xe690
movq %r12, 0x38(%rsp)
movq 0x8(%rsp), %rax
movslq 0x18(%rax), %rax
movq 0x4178(%rsp), %rdx
movq %rax, (%rdx)
movl 0x4(%rsp), %eax
cmpl 0x18(%rsp), %eax
jg 0xebb7
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq 0x4170(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl 0x18(%rsp), %ecx
movq 0x30(%rsp), %r15
movq %r15, %r8
callq 0xc4ca
movw $0x800, %r14w           # imm = 0x800
cmpl $0x0, 0x40(%rsp)
movq 0x80(%rsp), %r13
movq 0x8(%rsp), %r12
jne 0xe8f6
movslq 0x4(%rsp), %r14
shlq $0x6, %r14
movq 0x4170(%rsp), %rax
addq (%rax), %r14
movl 0x18(%r14), %eax
movl 0x1c(%r14), %ecx
movl %eax, %ebp
imull %ecx, %ebp
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpl %ecx, %r12d
jge 0xe8a0
xorl %r15d, %r15d
cmpl %eax, %r15d
jge 0xe897
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq 0xa79e
cmpw $0x1, %ax
adcq $0x0, %rbx
incl %r15d
movl 0x18(%r14), %eax
jmp 0xe873
incl %r12d
movl 0x1c(%r14), %ecx
jmp 0xe86b
movslq %ebp, %r15
movq %rbx, %rax
shlq $0x7, %rax
xorl %edx, %edx
divq %r15
cmpl $0x2, %eax
pushq $0x1
popq %rdx
cmovll %edx, %eax
cmpl $0x7f, %eax
pushq $0x7f
popq %r14
cmovll %eax, %r14d
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0xdaea
shll $0x5, %r14d
leaq 0x101f6(%rip), %rsi     # 0x1ead4
pushq $0x5
popq %rdi
movq %rbx, %rdx
movq %r15, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x16c30
movq 0x30(%rsp), %r15
leaq 0xe0(%rsp), %rsi
movq %r12, (%rsi)
movq $0x1000000, 0x8(%rsi)   # imm = 0x1000000
andq $0x0, 0x10(%rsi)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsi)
leaq 0x100(%rsp), %rdi
leaq 0x60(%rsp), %rdx
pushq $0x2
popq %rcx
movl $0xd79435e, %r8d        # imm = 0xD79435E
callq 0x14b58
leaq 0x100(%rsp), %rdi
movq %r13, %rsi
callq 0x14bca
leaq 0x100(%rsp), %rdi
callq 0x14c24
movl 0x14(%r15), %eax
movl 0x18(%r15), %r10d
movzwl %r14w, %r8d
leaq 0x100(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movq %r13, %rcx
movl $0x15550, %r9d          # imm = 0x15550
pushq %r10
pushq %rax
callq 0x14c3a
popq %rax
popq %rcx
movq 0x68(%rsp), %rsi
subq 0x60(%rsp), %rsi
sarq $0x3, %rsi
leaq 0x88(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0xa562
leaq 0x88(%rsp), %rbx
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpq %rax, %rcx
jg 0xeb81
movq %rcx, 0x28(%rsp)
movq 0x4170(%rsp), %rcx
movq (%rcx), %r14
movq 0x28(%rsp), %rcx
movq %rcx, %rdx
shlq $0x6, %rdx
movswl 0x20(%r14,%rdx), %esi
movswl 0x22(%r14,%rdx), %edi
movl %esi, 0x4(%rsp)
movl %edi, 0x1c(%rsp)
cmpw %di, %si
je 0xeb79
movq 0x90(%rsp), %rsi
subq 0x88(%rsp), %rsi
movq %rdx, 0x48(%rsp)
addq %rdx, %r14
shrq $0x2, %rsi
addl $-0xd, %esi
movl 0x18(%r14), %edx
leaq 0xa0(%rsp), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
pushq $0x1
popq %r9
pushq $0x0
pushq $0x0
pushq $0x0
pushq $0x0
callq 0xa470
addq $0x20, %rsp
xorl %esi, %esi
movq %r14, 0x50(%rsp)
cmpl 0x1c(%r14), %esi
jge 0xeb62
movq %r14, %rdi
movl %esi, 0x8(%rsp)
movq 0x4170(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %r15, %r8
leaq 0xa0(%rsp), %r9
callq 0xc89a
xorl %ebp, %ebp
cmpl 0x18(%r14), %ebp
jge 0xeb52
subq $0x8, %rsp
movq %rbx, %r13
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl 0x10(%rsp), %r15d
movl %r15d, %ecx
movq 0x48(%rsp), %r8
movq 0x4178(%rsp), %r9
leaq 0xa8(%rsp), %rax
pushq %rax
movq 0x40(%rsp), %rbx
pushq %rbx
pushq 0x28(%rsp)
callq 0x4847
addq $0x20, %rsp
movq %r14, %rdi
movl %eax, %r14d
movl %r15d, %esi
movl %ebp, %edx
callq 0xa79e
subl %r14d, %eax
movswl %r14w, %ecx
movl 0x4(%rsp), %r15d
subl %ecx, %r15d
movl 0x1c(%rsp), %r12d
subl %ecx, %r12d
movswl %ax, %r8d
cmpb $0x1, 0x24(%rbx)
movq %r13, %rbx
jne 0xeb30
leaq 0x100(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
movl %r12d, %ecx
movl %r8d, %r13d
callq 0x14cfc
movl %eax, %r14d
movq 0x30(%rsp), %rax
movq 0x40(%rax), %rdi
addq 0x48(%rsp), %rdi
movl 0x8(%rsp), %esi
movl %ebp, %edx
callq 0xa44a
movw %r14w, (%rax)
movl %r13d, %r8d
leaq 0x100(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
movl %r12d, %ecx
callq 0x14d38
incl %ebp
movq 0x50(%rsp), %r14
jmp 0xea76
movl 0x8(%rsp), %esi
incl %esi
movq 0x30(%rsp), %r15
jmp 0xea46
leaq 0xa0(%rsp), %rdi
callq 0xabc2
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
incq %rcx
jmp 0xe9b8
leaq 0x88(%rsp), %rdi
callq 0x5374
leaq 0x4118(%rsp), %rdi
callq 0x152b8
leaq 0xe0(%rsp), %rdi
callq 0x129be
leaq 0x60(%rsp), %rdi
callq 0xc00e
movq 0x20(%rsp), %rcx
cmpq %rcx, 0x38(%rsp)
setg %al
addq $0x4138, %rsp           # imm = 0x4138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xebf0
jmp 0xebf0
movq %rax, %rbx
jmp 0xec1b
jmp 0xebf0
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x14c24
jmp 0xec28
jmp 0xebf0
movq %rax, %rbx
jmp 0xec28
movq %rax, %rbx
jmp 0xec0e
jmp 0xebfe
jmp 0xebfe
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0xabc2
leaq 0x88(%rsp), %rdi
callq 0x5374
leaq 0x4118(%rsp), %rdi
callq 0x152b8
leaq 0x60(%rsp), %rdi
callq 0xc00e
movq %rbx, %rdi
callq 0x35d0