// File: /workspace/github_cpp_makefile_star2_O1/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), %r15
movl (%r15), %eax
movl 0x4(%r15), %edx
leal (%rax,%rcx), %r13d
addl %edx, %ecx
subl %eax, %edx
movq %rdx, 0x20(%rsp)
leal 0x1(%rdx), %ebx
movslq %r13d, %rdx
movq %rdi, 0x28(%rsp)
movq (%rdi), %rax
movq %rdx, %rsi
shlq $0x6, %rsi
movl 0x18(%rax,%rsi), %ebp
movq %rsi, 0x40(%rsp)
movl 0x1c(%rax,%rsi), %r12d
movl 0x8(%r15), %r8d
leaq 0x1c60e(%rip), %rsi     # 0x25b4c
xorl %r14d, %r14d
movl $0x8, %edi
movq %rcx, 0x58(%rsp)
xorl %eax, %eax
callq 0x1d0d6
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 0x60(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0xe016
movl %r13d, 0x50(%rsp)
movl %r12d, 0x4(%rsp)
testl %r12d, %r12d
setle 0x3(%rsp)
jle 0x9664
testl %ebp, %ebp
setg 0x18(%rsp)
movq 0x40(%rsp), %rax
addq $0x24, %rax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
testl %ebp, %ebp
jle 0x9651
xorl %r12d, %r12d
movb 0x18(%rsp), %r13b
cmpl $0x0, 0x20(%rsp)
js 0x9619
movq 0x60(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
addq 0x8(%rsp), %rcx
xorl %edx, %edx
movl -0xc(%rcx), %esi
imull %r14d, %esi
addl %r12d, %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 0x95e0
leaq 0xf8(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x101b0
movslq 0x8(%r15), %rax
cmpq %rax, 0x120(%rsp)
ja 0x9647
incl %r12d
cmpl %ebp, %r12d
setl %r13b
jne 0x95c5
jmp 0x9651
testb $0x1, %r13b
jne 0x9abf
incl %r14d
cmpl 0x4(%rsp), %r14d
setge 0x3(%rsp)
jne 0x95b5
movq %rbp, 0x30(%rsp)
movl 0x120(%rsp), %r8d
movl %r8d, 0x8(%r15)
leaq 0x1c516(%rip), %rsi     # 0x25b92
movl $0x6, %edi
movl 0x50(%rsp), %edx
movq 0x58(%rsp), %rcx
xorl %eax, %eax
callq 0x1d0d6
movq %r15, 0x38(%rsp)
movl 0x8(%r15), %ebp
movl %ebp, %eax
imull %ebx, %eax
movslq %eax, %rsi
leaq 0x78(%rsp), %rdx
movw $0x0, (%rdx)
leaq 0x90(%rsp), %rdi
leaq 0x17(%rsp), %rcx
callq 0xe312
leaq 0xb4(%rsp), %rcx
movl %ebp, -0xc(%rcx)
movl %ebx, -0x8(%rcx)
movl $0x10000, -0x4(%rcx)    # imm = 0x10000
movl $0x1, 0x4(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rcx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x18(%rcx)
movw $0x0, (%rcx)
movl %eax, 0x8(%rcx)
movq 0x110(%rsp), %rcx
leaq 0x100(%rsp), %rax
cmpq %rax, %rcx
movq 0x38(%rsp), %r14
movq 0x30(%rsp), %rbp
movl 0x4(%rsp), %r15d
je 0x983b
leaq 0x1c4bc(%rip), %r12     # 0x25bdc
movq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
movq %rcx, 0x8(%rsp)
leaq 0x20(%rcx), %rsi
leaq 0x78(%rsp), %rdi
callq 0xe09e
movl $0x9, %edi
leaq 0x1c484(%rip), %rsi     # 0x25bcf
movl %r13d, %edx
xorl %eax, %eax
callq 0x1d0d6
cmpl $0x0, 0x20(%rsp)
leaq 0xb4(%rsp), %r15
js 0x97b7
movq 0x78(%rsp), %rax
movslq 0xa8(%rsp), %rcx
movq 0x90(%rsp), %rdx
movq 0x98(%rsp), %rsi
subq %rdx, %rsi
sarq %rsi
addq 0x18(%rsp), %rdx
leaq (%rcx,%rcx), %rdi
movq %r13, %r8
xorl %r9d, %r9d
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 0x9796
cmpl $0x0, 0x20(%rsp)
js 0x97e2
xorl %r15d, %r15d
movq 0x78(%rsp), %rax
movswl (%rax,%r15,2), %edx
movl $0x9, %edi
movq %r12, %rsi
xorl %eax, %eax
callq 0x1d0d6
incq %r15
cmpq %r15, %rbx
jne 0x97c1
movl $0x9, %edi
leaq 0x1e198(%rip), %rsi     # 0x27986
xorl %eax, %eax
callq 0x1d0d6
movl 0x4(%rsp), %r15d
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x9814
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
incq %r13
movq 0x8(%rsp), %rdi
callq 0x3350
movq %rax, %rcx
addq $0x2, 0x18(%rsp)
leaq 0x100(%rsp), %rax
cmpq %rax, %rcx
jne 0x972c
testl %r15d, %r15d
leaq 0xb4(%rsp), %r9
jle 0x9a1a
movq 0x28(%rsp), %rax
movq (%rax), %rcx
movq 0x40(%rsp), %rax
leaq (%rcx,%rax), %rdx
addq $0x24, %rdx
movq %rdx, 0xe8(%rsp)
addq %rax, %rcx
movq %rcx, 0xd8(%rsp)
addq $0x24, %rax
movq %rax, 0xd0(%rsp)
movq $0x0, 0x48(%rsp)
testl %ebp, %ebp
jle 0x9a05
movq 0x60(%rsp), %rdi
movl 0x8(%r14), %eax
movq %rax, 0x8(%rsp)
movq 0xd8(%rsp), %rax
movslq 0x18(%rax), %rcx
movslq 0x48(%rsp), %r10
imulq %r10, %rcx
movq %rcx, 0x18(%rsp)
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x50(%rsp)
subq %rcx, %rax
sarq %rax
movq %rax, 0xf0(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rax
addq 0xd0(%rsp), %rax
movq %rax, 0xe0(%rsp)
xorl %edx, %edx
cmpl $0x0, 0x20(%rsp)
js 0x9935
movq 0xe0(%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 0x98fe
cmpl $0x0, 0x8(%rsp)
jle 0x99d2
movq 0x60(%rsp), %rsi
movslq 0xa8(%rsp), %r15
movq 0x90(%rsp), %r11
movq 0x98(%rsp), %rbp
subq %r11, %rbp
sarq %rbp
leaq (%r15,%r15), %rcx
xorl %eax, %eax
cmpl $0x0, 0x20(%rsp)
js 0x99c1
movq %r11, %r13
movq %rax, %r8
xorl %r12d, %r12d
cmpq %r8, %rbp
movq %r9, %r14
cmovaq %r13, %r14
movzwl (%rsi,%r12,2), %r9d
cmpw (%r14), %r9w
jne 0x99a6
incq %r12
addq %r15, %r8
addq %rcx, %r13
cmpq %r12, %rbx
leaq 0xb4(%rsp), %r9
jne 0x9979
jmp 0x99c1
incq %rax
addq $0x2, %r11
cmpq 0x8(%rsp), %rax
leaq 0xb4(%rsp), %r9
jne 0x9969
movq 0x8(%rsp), %rax
movq 0x38(%rsp), %r14
movq 0x30(%rsp), %rbp
movl 0x4(%rsp), %r15d
jmp 0x99d4
xorl %eax, %eax
movq 0x18(%rsp), %rcx
addq %rdx, %rcx
cmpq %rcx, 0xf0(%rsp)
movq 0x50(%rsp), %rsi
leaq (%rsi,%rcx,2), %rcx
cmovbeq 0xe8(%rsp), %rcx
movw %ax, (%rcx)
incq %rdx
cmpq %rbp, %rdx
jne 0x98ed
movq 0x48(%rsp), %rax
incl %eax
movq %rax, 0x48(%rsp)
cmpl %r15d, %eax
jne 0x9889
movq 0x28(%rsp), %rbx
incl 0x6c(%rbx)
movq 0x20(%rsp), %rax
subl %eax, 0x64(%rbx)
movq (%rbx), %rax
movq 0x40(%rsp), %rcx
leaq (%rax,%rcx), %rsi
addq $0x40, %rsi
movslq 0x58(%rsp), %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %rdx
addq $0x40, %rdx
movq %rbx, %rdi
callq 0xfd3a
movq (%rbx), %rsi
leaq 0x90(%rsp), %rdx
movq %rbx, %rdi
callq 0xdf1c
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x9a83
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x9a9a
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
leaq 0xf8(%rsp), %rdi
callq 0xf630
movb 0x3(%rsp), %al
andb $0x1, %al
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, 0x3(%rsp)
jne 0x9664
jmp 0x9a83
jmp 0x9ad9
jmp 0x9ad9
jmp 0x9ae0
jmp 0x9ae0
movq %rax, %rbx
jmp 0x9b36
movq %rax, %rbx
jmp 0x9b02
jmp 0x9ae5
movq %rax, %rbx
jmp 0x9b1f
movq %rax, %rbx
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x9b02
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x9b1f
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x9b36
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
leaq 0xf8(%rsp), %rdi
callq 0xf630
movq %rbx, %rdi
callq 0x35a0