// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movslq 0x6c(%rdi), %rax
testq %rax, %rax
jle 0x71a0
movq %rsi, %r12
movq (%rsi), %rcx
movq 0x8(%rsi), %rdx
subq %rcx, %rdx
cmpq $0xc, %rdx
jne 0x71a9
movq %rdi, %r15
movq (%rdi), %rsi
movq 0x8(%rdi), %rdx
movslq (%rcx), %rcx
addq %rax, %rcx
subq %rsi, %rdx
sarq $0x6, %rdx
cmpq %rcx, %rdx
jbe 0x71ab
movslq 0x1c(%rsi), %rax
movq %rax, 0x10(%rsp)
shlq $0x6, %rcx
movl 0x18(%rsi,%rcx), %eax
movl %eax, 0xc(%rsp)
movq %rsi, 0x20(%rsp)
movl 0x1c(%rsi,%rcx), %eax
movl %eax, 0x8(%rsp)
movq %rcx, 0x18(%rsp)
leaq 0x7c(%rcx), %rbx
pushq $0x1
popq %rbp
leaq 0x38(%rsp), %r13
cmpq 0x10(%rsp), %rbp
jge 0x71cc
movq (%r15), %rax
movq 0x18(%rsp), %rcx
leaq (%rax,%rcx), %r14
addq $0x40, %r14
movq %r13, %rdi
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
xorl %ecx, %ecx
pushq $0x1
popq %r8
movl %r8d, %r9d
pushq $0x0
pushq $0x0
pushq $0x0
pushq $0x0
callq 0xa470
addq $0x20, %rsp
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xbb60
movq %r13, %rdi
callq 0xabc2
movq (%r12), %rax
movl (%rax), %eax
addl %ebp, %eax
movq (%r15), %rcx
movq %rcx, 0x20(%rsp)
movl %eax, (%rcx,%rbx)
incq %rbp
addq $0x40, %rbx
jmp 0x712e
leaq 0x168c5(%rip), %rdi     # 0x1da6c
jmp 0x71b2
jmp 0x71ab
leaq 0x168e5(%rip), %rdi     # 0x1da97
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x16b80
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0x30(%rsp)
movl 0xc(%rsp), %eax
testl %eax, %eax
cmovlel %ebp, %eax
movl %eax, 0xc(%rsp)
movl 0x8(%rsp), %eax
testl %eax, %eax
cmovlel %ebp, %eax
movl %eax, 0x8(%rsp)
movq %r15, 0x28(%rsp)
movq 0x18(%rsp), %rbx
cmpl 0x8(%rsp), %ebp
je 0x72d1
xorl %r13d, %r13d
movq %rbp, %r12
cmpl 0xc(%rsp), %r13d
je 0x72ca
movq (%r15), %rdi
addq %rbx, %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0xa44a
cmpw $0x0, (%rax)
js 0x726e
movq (%r15), %rdi
addq %rbx, %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0xa44a
movswl (%rax), %eax
movq 0x20(%rsp), %rcx
movl 0x18(%rcx), %r14d
cmpl %eax, %r14d
jle 0x7273
movq (%r15), %rdi
addq %rbx, %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0xa44a
movswl (%rax), %r14d
jmp 0x7276
xorl %r14d, %r14d
jmp 0x7276
decl %r14d
movq %rbx, %r15
xorl %ebp, %ebp
cmpq %rbp, 0x30(%rsp)
je 0x72b5
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xa79e
movl %eax, %ebx
movq 0x28(%rsp), %rax
movq (%rax), %rdi
addq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0xa44a
movw %bx, (%rax)
incq %rbp
addq $0x40, %r15
jmp 0x727b
incl %r13d
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %rbx
movq %r12, %rbp
jmp 0x7216
incl %ebp
jmp 0x7206
movl 0x64(%r15), %eax
movq 0x10(%rsp), %rcx
addl %ecx, %eax
decl %eax
movl %eax, 0x64(%r15)
decl 0x6c(%r15)
movq (%r15), %rsi
leaq 0x40(%rsi), %rdx
movq %r15, %rdi
callq 0xa676
movb $0x1, %bl
jmp 0x71bb
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0xabc2
movq %rbx, %rdi
callq 0x35d0