// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/transform/palette.h
// Function: fwd_palette(Image&, std::vector<int, std::allocator<int>>&)
bool fwd_palette(Image &input, std::vector<int> &parameters) {
    assert(parameters.size() == 3);
    int begin_c = input.nb_meta_channels+parameters[0];
    int end_c = input.nb_meta_channels+parameters[1];
    int &nb_colors = parameters[2];
    int nb = end_c - begin_c + 1;

    int w = input.channel[begin_c].w;
    int h = input.channel[begin_c].h;

    v_printf(8,"Trying to represent channels %i-%i using at most a %i-color palette.\n",begin_c,end_c,nb_colors);
    std::set< std::vector<pixel_type> > candidate_palette;
    std::vector<pixel_type> color(nb);
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            for (int c=0; c<nb; c++) color[c] = input.channel[begin_c+c].value(y,x);
            candidate_palette.insert(color);
            if (candidate_palette.size() > nb_colors) return false; // too many colors
        }
    }
    nb_colors = candidate_palette.size();
    v_printf(6,"Channels %i-%i can be represented using a %i-color palette.\n",begin_c,end_c,nb_colors);

    Channel pch(nb_colors,nb, 0, 1);
    pch.hshift = -1;
    int x=0;
    for (auto pcol : candidate_palette) {
        v_printf(9,"Color %i :  ",x);
        for (int i=0; i<nb; i++) pch.value(i,x) = pcol[i];
        for (int i=0; i<nb; i++) v_printf(9,"%i ", pcol[i]);
        v_printf(9,"\n");
        x++;
    }
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            for (int c=0; c<nb; c++) color[c] = input.channel[begin_c+c].value(y,x);
            int index=0;
            for ( ; index<nb_colors ; index++ ) {
                bool found=true;
                for (int c=0; c<nb; c++) if (color[c] != pch.value(c,index)) {found = false; break;}
                if (found) break;
            }
            input.channel[begin_c].value(y,x) = index;
        }
    }
    input.nb_meta_channels++;
    input.nb_channels -= nb-1;
    input.channel.erase(input.channel.begin()+begin_c+1,input.channel.begin()+end_c+1);
    input.channel.insert(input.channel.begin(),pch);
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl 0x6c(%rdi), %ecx
movq (%rsi), %r15
movl (%r15), %eax
movl 0x4(%r15), %ebx
leal (%rax,%rcx), %edx
addl %ebx, %ecx
subl %eax, %ebx
leal 0x1(%rbx), %r14d
movl %edx, 0x40(%rsp)
movslq %edx, %rdx
movq %rdi, 0x38(%rsp)
movq (%rdi), %rax
movq %rdx, %r13
shlq $0x6, %r13
movl 0x18(%rax,%r13), %r12d
movl 0x1c(%rax,%r13), %ebp
movl 0x8(%r15), %r8d
leaq 0x1666e(%rip), %rsi     # 0x1dacc
pushq $0x8
popq %rdi
movq %rdx, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
xorl %eax, %eax
callq 0x16c30
leaq 0xa0(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movl %r14d, 0x20(%rsp)
movslq %r14d, %rsi
leaq 0x60(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0xa860
movq %rbx, 0x78(%rsp)
movslq %ebx, %rbx
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
movl %r12d, 0x2c(%rsp)
testl %ebp, %ebp
movl $0x0, %ecx
movl %ebp, 0x44(%rsp)
cmovgl %ebp, %ecx
xorl %eax, %eax
leaq 0x60(%rsp), %r12
movq %r15, 0x58(%rsp)
movl %ecx, 0x28(%rsp)
movq %rax, 0x30(%rsp)
cmpl %ecx, %eax
je 0x7560
xorl %ebp, %ebp
cmpl 0x2c(%rsp), %ebp
je 0x7553
movq %r13, %r14
xorl %r15d, %r15d
cmpq %rbx, %r15
jg 0x7529
movq 0x38(%rsp), %rax
movq (%rax), %rdi
addq %r14, %rdi
movq 0x30(%rsp), %rsi
movl %ebp, %edx
callq 0xa44a
movzwl (%rax), %eax
movq 0x60(%rsp), %rcx
movw %ax, (%rcx,%r15,2)
incq %r15
addq $0x40, %r14
jmp 0x74f7
leaq 0x98(%rsp), %rdi
movq %r12, %rsi
callq 0xbda0
movq 0x58(%rsp), %r15
movslq 0x8(%r15), %rax
incl %ebp
cmpq %rax, 0xc0(%rsp)
jbe 0x74eb
jmp 0x77ce
movq 0x30(%rsp), %rax
incl %eax
movl 0x28(%rsp), %ecx
jmp 0x74e0
movl 0xc0(%rsp), %r8d
movl %r8d, 0x8(%r15)
leaq 0x1659f(%rip), %rsi     # 0x1db12
pushq $0x6
popq %rdi
movl 0x40(%rsp), %edx
movq 0x50(%rsp), %rcx
xorl %eax, %eax
callq 0x16c30
movl 0x8(%r15), %esi
andl $0x0, 0x18(%rsp)
andl $0x0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
leaq 0xc8(%rsp), %rdi
pushq $0x1
popq %r8
movl 0x20(%rsp), %edx
xorl %ecx, %ecx
movl %r8d, %r9d
callq 0xa470
leaq 0xc8(%rsp), %rax
orl $-0x1, 0x2c(%rax)
movq 0xb0(%rsp), %rcx
leaq 0x80(%rsp), %r14
pushq $0x9
popq %r15
leaq 0x1657e(%rip), %rbp     # 0x1db5c
xorl %r12d, %r12d
leaq 0xa0(%rsp), %rax
cmpq %rax, %rcx
je 0x76a5
movq %rcx, 0x20(%rsp)
leaq 0x20(%rcx), %rsi
movq %r14, %rdi
callq 0xa8b2
movl %r15d, %edi
leaq 0x16542(%rip), %rsi     # 0x1db4f
movl %r12d, %edx
xorl %eax, %eax
callq 0x16c30
xorl %r14d, %r14d
cmpq %rbx, %r14
jg 0x7648
movq 0x80(%rsp), %rax
movzwl (%rax,%r14,2), %r13d
leaq 0xc8(%rsp), %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0xa44a
movw %r13w, (%rax)
incq %r14
jmp 0x761a
xorl %r14d, %r14d
cmpq %rbx, %r14
jg 0x766f
movq 0x80(%rsp), %rax
movswl (%rax,%r14,2), %edx
movl %r15d, %edi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x16c30
incq %r14
jmp 0x764b
movl %r15d, %edi
leaq 0x182bd(%rip), %rsi     # 0x1f936
xorl %eax, %eax
callq 0x16c30
incl %r12d
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0xabc2
movq 0x20(%rsp), %rdi
callq 0x3380
movq %rax, %rcx
jmp 0x75e1
movq 0x48(%rsp), %r14
shlq $0x6, %r14
xorl %r15d, %r15d
movq 0x38(%rsp), %r12
movq %r14, 0x20(%rsp)
cmpl 0x28(%rsp), %r15d
je 0x7770
xorl %ebp, %ebp
cmpl 0x2c(%rsp), %ebp
je 0x7768
xorl %r13d, %r13d
cmpq %rbx, %r13
jg 0x7701
movq (%r12), %rdi
addq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0xa44a
movzwl (%rax), %eax
movq 0x60(%rsp), %rcx
movw %ax, (%rcx,%r13,2)
incq %r13
addq $0x40, %r14
jmp 0x76d5
xorl %r13d, %r13d
movq 0x58(%rsp), %rax
cmpl 0x8(%rax), %r13d
jge 0x7747
xorl %r14d, %r14d
cmpq %rbx, %r14
jg 0x7747
movq 0x60(%rsp), %rax
movzwl (%rax,%r14,2), %r12d
leaq 0xc8(%rsp), %rdi
movl %r14d, %esi
movl %r13d, %edx
callq 0xa44a
incq %r14
cmpw (%rax), %r12w
movq 0x38(%rsp), %r12
je 0x7712
incl %r13d
jmp 0x7704
movq (%r12), %rdi
movq 0x20(%rsp), %r14
addq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0xa44a
movw %r13w, (%rax)
incl %ebp
jmp 0x76c8
incl %r15d
jmp 0x76bb
incl 0x6c(%r12)
movq 0x78(%rsp), %rax
subl %eax, 0x64(%r12)
movq (%r12), %rax
movq 0x48(%rsp), %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %rsi
addq $0x40, %rsi
movslq 0x50(%rsp), %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %rdx
addq $0x40, %rdx
movq %r12, %rdi
callq 0xa676
movq (%r12), %rsi
leaq 0xc8(%rsp), %rdx
movq %r12, %rdi
callq 0xa7c8
leaq 0xc8(%rsp), %rdi
callq 0xabc2
movq 0x30(%rsp), %rax
cmpl 0x44(%rsp), %eax
setge %bl
leaq 0x60(%rsp), %rdi
callq 0xabc2
leaq 0x98(%rsp), %rdi
callq 0xb1a8
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7812
jmp 0x7812
jmp 0x7819
jmp 0x7819
movq %rax, %rbx
jmp 0x7845
movq %rax, %rbx
jmp 0x782e
jmp 0x781e
movq %rax, %rbx
jmp 0x783b
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0xabc2
leaq 0xc8(%rsp), %rdi
callq 0xabc2
leaq 0x60(%rsp), %rdi
callq 0xabc2
leaq 0x98(%rsp), %rdi
callq 0xb1a8
movq %rbx, %rdi
callq 0x35d0