// File: /workspace/github_cpp_makefile_star2_O3/cloudinary[P]fuif/transform/palette.h
// Function: inv_palette(Image&, std::vector<int, std::allocator<int>>)
bool inv_palette(Image &input, std::vector<int> parameters) {
    if (input.nb_meta_channels < 1) {
        e_printf("Error: Palette transform without palette.\n");
        return false;
    }
    if (parameters.size() != 3) {
        e_printf("Error: Palette transform with incorrect parameters.\n");
        return false;
    }
    int nb = input.channel[0].h;
    int c0 = input.nb_meta_channels + parameters[0];
    if (c0 >= input.channel.size()) {
        e_printf("Error: Palette transform with incorrect parameters.\n");
        return false;
    }
    int w = input.channel[c0].w;
    int h = input.channel[c0].h;
    // might be false in case of lossy
    //assert(input.channel[c0].minval == 0);
    //assert(input.channel[c0].maxval == palette.w-1);
    for (int i=1; i<nb; i++) {
        input.channel.insert(input.channel.begin()+c0+1, Channel(w,h,0,1));
        input.channel[c0+i].component = parameters[0]+i;
    }
    const Channel &palette = input.channel[0];
    for (int y=0; y<h; y++) {
        for (int x=0; x<w; x++) {
            int index = CLAMP(input.channel[c0].value(y,x),0,palette.w-1);
            for (int c=0; c<nb; c++)
                input.channel[c0+c].value(y,x) = palette.value(c,index);
        }
    }
    input.nb_channels += nb-1;
    input.nb_meta_channels--;
    input.channel.erase(input.channel.begin(),input.channel.begin()+1);
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movslq 0x6c(%rdi), %rax
testq %rax, %rax
jle 0x92d8
movq %rsi, %r13
movq (%rsi), %rcx
movq 0x8(%rsi), %rdx
subq %rcx, %rdx
cmpq $0xc, %rdx
jne 0x92cf
movq %rdi, %rbx
movq (%rdi), %rsi
movq 0x8(%rdi), %rdx
movslq (%rcx), %r8
addq %rax, %r8
subq %rsi, %rdx
sarq $0x6, %rdx
cmpq %r8, %rdx
jbe 0x92cf
movl 0x1c(%rsi), %eax
shlq $0x6, %r8
movl 0x18(%rsi,%r8), %ecx
movq %rcx, 0x10(%rsp)
movl 0x1c(%rsi,%r8), %ecx
movq %rax, 0x20(%rsp)
cmpl $0x2, %eax
movl %ecx, 0x8(%rsp)
jl 0x9173
movl %ecx, %eax
imull 0x10(%rsp), %eax
cltq
movq %rax, 0x30(%rsp)
leaq 0x7c(%r8), %r14
movl $0x1, %r12d
movq (%rbx), %rax
movq %r8, %rbp
addq %r8, %rax
addq $0x40, %rax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
movw %r15w, 0xc(%rsp)
leaq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0xf(%rsp), %rcx
callq 0xe282
movq 0x10(%rsp), %rax
movl %eax, 0x70(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x74(%rsp)
movl $0x10000, 0x78(%rsp)    # imm = 0x10000
movl $0x1, 0x80(%rsp)
leaq 0x84(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
movw %r15w, 0x7c(%rsp)
movq %rbx, %r15
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0xfe74
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x9144
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq (%r13), %rax
movl (%rax), %eax
addl %r12d, %eax
movq %r15, %rbx
movq (%r15), %rcx
movl %eax, (%rcx,%r14)
incq %r12
addq $0x40, %r14
cmpq %r12, 0x20(%rsp)
movq %rbp, %r8
jne 0x90a1
movq (%rbx), %rsi
movl 0x8(%rsp), %ecx
movq %rbx, 0x38(%rsp)
testl %ecx, %ecx
jle 0x92ab
leaq (%rsi,%r8), %rax
movq %rax, 0x40(%rsp)
leaq (%rsi,%r8), %rax
addq $0x24, %rax
movq %rax, 0x18(%rsp)
leaq 0x24(%rsi), %r8
movq $0x0, 0x28(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x9295
movq 0x40(%rsp), %rax
movslq 0x18(%rax), %rcx
movslq 0x28(%rsp), %r9
imulq %r9, %rcx
movq %rcx, 0x30(%rsp)
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x50(%rsp)
subq %rcx, %rax
sarq %rax
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
movq 0x30(%rsp), %rax
addq %r14, %rax
cmpq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,2), %rax
cmovbeq 0x18(%rsp), %rax
movswl (%rax), %eax
testl %eax, %eax
js 0x9212
movl 0x18(%rsi), %ecx
leal -0x1(%rcx), %edi
cmpl %eax, %ecx
cmovgl %eax, %edi
movslq %edi, %r12
jmp 0x9215
xorl %r12d, %r12d
cmpl $0x0, 0x20(%rsp)
jle 0x9287
movslq 0x18(%rsi), %r13
movq (%rsi), %rax
movq 0x8(%rsi), %rbx
subq %rax, %rbx
sarq %rbx
leaq (%rax,%r12,2), %rax
leaq (,%r13,2), %rdi
movq 0x20(%rsp), %rbp
movq 0x18(%rsp), %rcx
cmpq %r12, %rbx
movq %r8, %rdx
cmovaq %rax, %rdx
movzwl (%rdx), %edx
movslq -0xc(%rcx), %r10
imulq %r9, %r10
addq %r14, %r10
movq -0x24(%rcx), %r11
movq -0x1c(%rcx), %r15
subq %r11, %r15
sarq %r15
cmpq %r10, %r15
leaq (%r11,%r10,2), %r10
cmovbeq %rcx, %r10
movw %dx, (%r10)
addq $0x40, %rcx
addq %r13, %r12
addq %rdi, %rax
decq %rbp
jne 0x9243
incq %r14
cmpq 0x10(%rsp), %r14
jne 0x91df
movq 0x28(%rsp), %rax
incl %eax
movq %rax, 0x28(%rsp)
cmpl 0x8(%rsp), %eax
jne 0x91a3
movq 0x38(%rsp), %rdi
movl 0x64(%rdi), %eax
movq 0x20(%rsp), %rcx
addl %ecx, %eax
decl %eax
movl %eax, 0x64(%rdi)
decl 0x6c(%rdi)
leaq 0x40(%rsi), %rdx
callq 0xfcc2
movb $0x1, %bl
jmp 0x92e8
leaq 0x1c841(%rip), %rdi     # 0x25b17
jmp 0x92df
leaq 0x1c80d(%rip), %rdi     # 0x25aec
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x379e
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x9316
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq %rbx, %rdi
callq 0x3590