// File: /workspace/github_cpp_makefile_star2_O3/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 $0x128, %rsp            # imm = 0x128
movl 0x6c(%rdi), %ecx
movq (%rsi), %r12
movl (%r12), %eax
movl 0x4(%r12), %r15d
leal (%rax,%rcx), %r13d
addl %r15d, %ecx
subl %eax, %r15d
leal 0x1(%r15), %ebx
movslq %r13d, %rdx
movq %rdi, 0x8(%rsp)
movq (%rdi), %rax
movq %rdx, %rsi
shlq $0x6, %rsi
movl 0x18(%rax,%rsi), %edi
movq %rdi, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movl 0x1c(%rax,%rsi), %ebp
movl 0x8(%r12), %r8d
leaq 0x1c681(%rip), %rsi     # 0x25b4c
xorl %r14d, %r14d
movl $0x8, %edi
movq %rcx, 0x48(%rsp)
xorl %eax, %eax
callq 0x1cede
leaq 0x100(%rsp), %rax
movl $0x0, (%rax)
movq %r14, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r14, 0x20(%rax)
movslq %ebx, %rsi
leaq 0x50(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0xdf84
movl %r13d, 0x10(%rsp)
movq %r12, 0x38(%rsp)
testl %ebp, %ebp
movl %ebp, (%rsp)
jle 0x95e7
movq 0x20(%rsp), %rax
leaq 0x24(%rax), %r12
movq $0x0, 0x28(%rsp)
leaq 0xf8(%rsp), %rbp
xorl %r14d, %r14d
cmpl $0x0, 0x18(%rsp)
jle 0x95d0
xorl %r13d, %r13d
testl %r15d, %r15d
js 0x95a2
movq 0x50(%rsp), %rax
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
addq %r12, %rcx
xorl %edx, %edx
movl -0xc(%rcx), %esi
imull %r14d, %esi
addl %r13d, %esi
movslq %esi, %rsi
movq -0x24(%rcx), %rdi
movq -0x1c(%rcx), %r8
subq %rdi, %r8
sarq %r8
cmpq %rsi, %r8
leaq (%rdi,%rsi,2), %rsi
cmovbeq %rcx, %rsi
movzwl (%rsi), %esi
movw %si, (%rax,%rdx,2)
incq %rdx
addq $0x40, %rcx
cmpq %rdx, %rbx
jne 0x9569
movq %rbp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x10140
movq 0x38(%rsp), %rax
movslq 0x8(%rax), %rax
cmpq %rax, 0x120(%rsp)
ja 0x99fc
incl %r13d
cmpl 0x18(%rsp), %r13d
jne 0x9552
incl %r14d
cmpl (%rsp), %r14d
setge %al
movq %rax, 0x28(%rsp)
jne 0x9544
jmp 0x95ee
movb $0x1, %al
movq %rax, 0x28(%rsp)
movq %r15, 0x40(%rsp)
movl 0x120(%rsp), %r8d
movq 0x38(%rsp), %r14
movl %r8d, 0x8(%r14)
leaq 0x1c587(%rip), %rsi     # 0x25b92
movl $0x6, %edi
movl 0x10(%rsp), %edx
movq 0x48(%rsp), %rcx
xorl %eax, %eax
callq 0x1cede
movl 0x8(%r14), %ebp
movl %ebp, %eax
imull %ebx, %eax
movslq %eax, %rsi
leaq 0x68(%rsp), %rdx
movw $0x0, (%rdx)
leaq 0xb8(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0xe282
leaq 0xdc(%rsp), %rcx
movl %ebp, -0xc(%rcx)
movl %ebx, -0x8(%rcx)
movl $0x10000, -0x4(%rcx)    # imm = 0x10000
movabsq $-0xffffffff, %rax      # imm = 0xFFFFFFFF00000001
movq %rax, 0x4(%rcx)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x14(%rcx)
movq $0x0, 0xc(%rcx)
movw $0x0, (%rcx)
movq 0x110(%rsp), %r15
leaq 0x100(%rsp), %rbp
cmpq %rbp, %r15
movq 0x40(%rsp), %r12
je 0x97ad
xorl %r14d, %r14d
xorl %r13d, %r13d
leaq 0x20(%r15), %rsi
leaq 0x68(%rsp), %rdi
callq 0xe010
movq %r15, %rbp
movl $0x9, %edi
leaq 0x1c508(%rip), %rsi     # 0x25bcf
movl %r13d, %edx
xorl %eax, %eax
callq 0x1cede
testl %r12d, %r12d
js 0x9760
movq 0x68(%rsp), %rax
movslq 0xd0(%rsp), %rcx
movq 0xb8(%rsp), %rdx
movq 0xc0(%rsp), %rsi
subq %rdx, %rsi
sarq %rsi
addq %r14, %rdx
leaq (%rcx,%rcx), %rdi
movq %r13, %r8
xorl %r9d, %r9d
leaq 0xdc(%rsp), %r15
movzwl (%rax,%r9,2), %r10d
cmpq %r8, %rsi
movq %r15, %r11
cmovaq %rdx, %r11
movw %r10w, (%r11)
incq %r9
addq %rcx, %r8
addq %rdi, %rdx
cmpq %r9, %rbx
jne 0x9712
testl %r12d, %r12d
js 0x9760
xorl %r15d, %r15d
movq 0x68(%rsp), %rax
movswl (%rax,%r15,2), %edx
movl $0x9, %edi
leaq 0x1c48b(%rip), %rsi     # 0x25bdc
xorl %eax, %eax
callq 0x1cede
incq %r15
cmpq %r15, %rbx
jne 0x973b
movl $0x9, %edi
leaq 0x1e21a(%rip), %rsi     # 0x27986
xorl %eax, %eax
callq 0x1cede
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x978a
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
incq %r13
movq %rbp, %rdi
callq 0x3350
movq %rax, %r15
addq $0x2, %r14
leaq 0x100(%rsp), %rax
cmpq %rax, %r15
jne 0x96aa
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movl (%rsp), %r9d
testl %r9d, %r9d
jle 0x999a
movq 0x20(%rsp), %rax
leaq (%rcx,%rax), %rdx
addq $0x24, %rdx
movq %rdx, 0x98(%rsp)
addq %rax, %rcx
movq %rcx, 0x88(%rsp)
addq $0x24, %rax
movq %rax, 0x80(%rsp)
movq $0x0, 0x30(%rsp)
cmpl $0x0, 0x18(%rsp)
jle 0x997b
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rax
movl 0x8(%rax), %eax
movq %rax, 0x10(%rsp)
movq 0x88(%rsp), %rax
movslq 0x18(%rax), %rcx
movslq 0x30(%rsp), %r10
imulq %r10, %rcx
movq %rcx, 0xb0(%rsp)
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0xa8(%rsp)
subq %rcx, %rax
sarq %rax
movq %rax, 0xa0(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
addq 0x80(%rsp), %rax
movq %rax, 0x90(%rsp)
xorl %edx, %edx
testl %r12d, %r12d
js 0x98ae
movq 0x90(%rsp), %rcx
xorl %esi, %esi
movslq -0xc(%rcx), %rax
imulq %r10, %rax
addq %rdx, %rax
movq -0x24(%rcx), %r8
movq -0x1c(%rcx), %r11
subq %r8, %r11
sarq %r11
cmpq %rax, %r11
leaq (%r8,%rax,2), %rax
cmovbeq %rcx, %rax
movzwl (%rax), %eax
movw %ax, (%rdi,%rsi,2)
incq %rsi
addq $0x40, %rcx
cmpq %rsi, %rbx
jne 0x9877
cmpl $0x0, 0x10(%rsp)
jle 0x9940
movq 0x50(%rsp), %rsi
movslq 0xd0(%rsp), %r15
movq 0xb8(%rsp), %r11
movq 0xc0(%rsp), %r13
subq %r11, %r13
sarq %r13
leaq (%r15,%r15), %rcx
xorl %eax, %eax
testl %r12d, %r12d
js 0x993a
movq %r11, %rbp
movq %rax, %r8
xorl %r14d, %r14d
cmpq %r8, %r13
leaq 0xdc(%rsp), %r12
cmovaq %rbp, %r12
movzwl (%rsi,%r14,2), %r9d
cmpw (%r12), %r9w
jne 0x991b
incq %r14
addq %r15, %r8
addq %rcx, %rbp
cmpq %r14, %rbx
jne 0x98f0
jmp 0x9935
incq %rax
addq $0x2, %r11
cmpq 0x10(%rsp), %rax
movq 0x40(%rsp), %r12
jne 0x98e2
movq 0x10(%rsp), %rax
jmp 0x993a
movq 0x40(%rsp), %r12
movl (%rsp), %r9d
jmp 0x9942
xorl %eax, %eax
movq 0xb0(%rsp), %rcx
addq %rdx, %rcx
cmpq %rcx, 0xa0(%rsp)
movq 0xa8(%rsp), %rsi
leaq (%rsi,%rcx,2), %rcx
cmovbeq 0x98(%rsp), %rcx
movw %ax, (%rcx)
incq %rdx
cmpq 0x18(%rsp), %rdx
jne 0x9868
movq 0x30(%rsp), %rax
incl %eax
movq %rax, 0x30(%rsp)
cmpl %r9d, %eax
jne 0x97f7
movq 0x8(%rsp), %rbx
movq (%rbx), %rcx
jmp 0x999f
movq 0x8(%rsp), %rbx
incl 0x6c(%rbx)
subl %r12d, 0x64(%rbx)
movq 0x20(%rsp), %rax
leaq (%rcx,%rax), %rsi
addq $0x40, %rsi
movslq 0x48(%rsp), %rax
shlq $0x6, %rax
leaq (%rcx,%rax), %rdx
addq $0x40, %rdx
movq %rbx, %rdi
callq 0xfcc2
movq (%rbx), %rsi
leaq 0xb8(%rsp), %rdx
movq %rbx, %rdi
callq 0xde8a
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x99fc
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x9a13
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
leaq 0xf8(%rsp), %rdi
callq 0xf5c4
movq 0x28(%rsp), %rax
andb $0x1, %al
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9a46
jmp 0x9a46
jmp 0x9a4d
jmp 0x9a4d
movq %rax, %rbx
jmp 0x9aa0
movq %rax, %rbx
jmp 0x9a6c
jmp 0x9a52
movq %rax, %rbx
jmp 0x9a89
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x9a6c
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x9a89
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x9aa0
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
leaq 0xf8(%rsp), %rdi
callq 0xf5c4
movq %rbx, %rdi
callq 0x3590