// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/transform/permute.h
// Function: fwd_permute(Image&, std::vector<int, std::allocator<int>>&)
bool fwd_permute(Image &input, std::vector<int> &parameters) {
    // TODO: implement this without copying stuff
    Image tmp = input;
    if (parameters.size() < 3) {
        e_printf("Invalid permutation: not enough parameters\n");
        return false;
    }
    bool use_channel = true;
    if (parameters[0] == -1) {
        parameters.erase(parameters.begin(), parameters.begin()+1);
        use_channel = false;
    }
    meta_permute(input, parameters, use_channel);
    if (!use_channel) return true;

    assert(input.channel[0].w == input.channel.size() - input.nb_meta_channels);
    if (parameters.size() != input.channel[0].w) {
        e_printf("Invalid permutation: need to specify %i new channels, gave only %i\n",input.channel[0].w,parameters.size());
        return false;
    }
    v_printf(5,"Permutation: ");
    for (int i=0; i<input.channel[0].w; i++) {
        input.channel[0].value(0,i) = parameters[i];
        int c = input.channel[0].value(0,i);
        if (c < 0 || c >= input.channel[0].w) {
            e_printf("Invalid permutation: %i is not a channel number\n",c);
            return false;
        }
        for (int j=0; j<i; j++) if (input.channel[0].value(0,i) == input.channel[0].value(0,j)) {
            e_printf("Invalid permutation: both %i and %i map to channel number %i\n",i,j,input.channel[0].value(0,i));
            return false;
        }
        input.channel[input.nb_meta_channels+c] = tmp.channel[tmp.nb_meta_channels+i];
        v_printf(5,"[%i -> %i] ",i,c);
    }
    v_printf(5,"\n");
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0xa67c
movq (%r14), %rsi
movq 0x8(%r14), %rax
subq %rsi, %rax
cmpq $0x8, %rax
ja 0x9c14
xorl %ebx, %ebx
leaq 0x143f5(%rip), %rdi     # 0x1dffd
xorl %eax, %eax
callq 0x16b80
jmp 0x9d55
movl (%rsi), %ebp
cmpl $-0x1, %ebp
jne 0x9c27
leaq 0x4(%rsi), %rdx
movq %r14, %rdi
callq 0xaa84
xorl %edx, %edx
cmpl $-0x1, %ebp
setne %dl
movq %r15, %rdi
movq %r14, %rsi
callq 0x9a18
movb $0x1, %bl
cmpl $-0x1, %ebp
je 0x9d55
movq 0x8(%r14), %rdx
subq (%r14), %rdx
sarq $0x2, %rdx
movq (%r15), %rax
movslq 0x18(%rax), %rsi
cmpq %rsi, %rdx
jne 0x9d2d
leaq 0x142d1(%rip), %rsi     # 0x1df38
pushq $0x5
popq %rdi
xorl %eax, %eax
callq 0x16c30
pushq $0x5
popq %rbp
xorl %r12d, %r12d
movq (%r15), %rdi
movslq 0x18(%rdi), %rdx
cmpq %rdx, %r12
jge 0x9d73
movq (%r14), %r8
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
subq %rax, %rcx
sarq %rcx
leaq 0x24(%rdi), %rsi
cmpq %r12, %rcx
leaq (%rax,%r12,2), %r9
cmovbeq %rsi, %r9
movl (%r8,%r12,4), %r8d
movw %r8w, (%r9)
movswl %r8w, %r13d
testw %r13w, %r13w
js 0x9d86
cmpl %r13d, %edx
jle 0x9d86
xorl %edx, %edx
cmpq %rdx, %r12
je 0x9ce5
cmpq %rdx, %rcx
movq %rsi, %r9
cmovaq %rax, %r9
cmpw %r8w, (%r9)
je 0x9d3f
incq %rdx
addq $0x2, %rax
jmp 0x9cc7
movslq 0x74(%rsp), %rax
movslq %r12d, %rsi
addq %rax, %rsi
shlq $0x6, %rsi
addq 0x8(%rsp), %rsi
movslq 0x6c(%r15), %rax
movslq %r13d, %rcx
addq %rax, %rcx
shlq $0x6, %rcx
addq %rcx, %rdi
callq 0xa526
movl %ebp, %edi
leaq 0x142a3(%rip), %rsi     # 0x1dfbb
movl %r12d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x16c30
incq %r12
jmp 0x9c77
xorl %ebx, %ebx
leaq 0x142f3(%rip), %rdi     # 0x1e029
xorl %eax, %eax
callq 0x16b80
jmp 0x9d55
xorl %ebx, %ebx
leaq 0x14356(%rip), %rdi     # 0x1e09e
movl %r12d, %esi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x16b80
leaq 0x8(%rsp), %rdi
callq 0xa776
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15bbc(%rip), %rsi     # 0x1f936
pushq $0x5
popq %rdi
xorl %eax, %eax
callq 0x16c30
jmp 0x9d55
xorl %ebx, %ebx
leaq 0x142de(%rip), %rdi     # 0x1e06d
movl %r13d, %esi
xorl %eax, %eax
callq 0x16b80
jmp 0x9d55
jmp 0x9da5
jmp 0x9da5
jmp 0x9da5
jmp 0x9da5
jmp 0x9da5
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xa776
movq %rbx, %rdi
callq 0x35d0