// File: /workspace/github_cpp_makefile_star2_O3/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 0xe0fe
leaq 0x20(%rsp), %r13
leaq 0x18(%r15), %rsi
movq %r13, %rdi
callq 0xe6be
movups 0x30(%r15), %xmm0
leaq 0x48(%rsp), %rdi
movups %xmm0, -0x10(%rdi)
leaq 0x40(%r15), %rsi
callq 0x63d8
movups 0x58(%r15), %xmm0
movups 0x64(%r15), %xmm1
leaq 0x80(%rsp), %rdi
movups %xmm1, -0x14(%rdi)
movups %xmm0, -0x20(%rdi)
leaq 0x78(%r15), %rsi
callq 0xe748
movups 0x90(%r15), %xmm0
movups 0x99(%r15), %xmm1
movups %xmm1, 0xa1(%rsp)
movups %xmm0, 0x98(%rsp)
movq (%r14), %rbx
movq 0x8(%r14), %rax
movq %rax, %rcx
subq %rbx, %rcx
cmpq $0x8, %rcx
ja 0xcfe2
xorl %r12d, %r12d
leaq 0x190a7(%rip), %rdi     # 0x2607d
xorl %eax, %eax
callq 0x379e
jmp 0xd19b
movl (%rbx), %ebp
cmpl $-0x1, %ebp
jne 0xd019
leaq 0x4(%rbx), %r12
movq %rax, %rdx
movq %r12, %rcx
subq %r12, %rdx
je 0xd00a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3500
movq 0x8(%r14), %rcx
movq %rcx, %rax
subq %r12, %rcx
addq %rcx, %rbx
cmpq %rbx, %rax
je 0xd019
movq %rbx, 0x8(%r14)
xorl %edx, %edx
cmpl $-0x1, %ebp
setne %dl
movq %r15, %rdi
movq %r14, %rsi
callq 0xcd16
movb $0x1, %r12b
cmpl $-0x1, %ebp
je 0xd19b
movq 0x8(%r14), %rdx
subq (%r14), %rdx
sarq $0x2, %rdx
movq (%r15), %rax
movslq 0x18(%rax), %rsi
cmpq %rsi, %rdx
jne 0xd16d
leaq 0x18f5e(%rip), %rsi     # 0x25fb8
movl $0x5, %edi
xorl %eax, %eax
callq 0x1cede
movq (%r15), %r13
movl 0x18(%r13), %edx
testl %edx, %edx
jle 0xd150
xorl %r12d, %r12d
movq (%r14), %rdi
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
sarq %rcx
leaq 0x24(%r13), %rsi
cmpq %r12, %rcx
leaq (%rax,%r12,2), %r8
cmovbeq %rsi, %r8
movl (%rdi,%r12,4), %edi
movw %di, (%r8)
movswl %di, %ebx
testw %bx, %bx
js 0xd1f6
cmpl %ebx, %edx
jle 0xd1f6
testq %r12, %r12
je 0xd0db
xorl %edx, %edx
cmpq %rdx, %rcx
movq %rsi, %r8
cmovaq %rax, %r8
cmpw %di, (%r8)
je 0xd180
incq %rdx
addq $0x2, %rax
cmpq %rdx, %r12
jne 0xd0bb
movslq 0x74(%rsp), %rax
movslq %r12d, %rbp
addq %rax, %rbp
shlq $0x6, %rbp
addq 0x8(%rsp), %rbp
movslq 0x6c(%r15), %rax
movslq %ebx, %rcx
addq %rax, %rcx
shlq $0x6, %rcx
addq %rcx, %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0xe5dc
movq 0x38(%rbp), %rax
movq %rax, 0x38(%r13)
movups 0x18(%rbp), %xmm0
movups 0x28(%rbp), %xmm1
movups %xmm1, 0x28(%r13)
movups %xmm0, 0x18(%r13)
movl $0x5, %edi
leaq 0x18f0a(%rip), %rsi     # 0x2603b
movl %r12d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1cede
incq %r12
movq (%r15), %r13
movslq 0x18(%r13), %rdx
cmpq %rdx, %r12
jl 0xd078
leaq 0x1a82f(%rip), %rsi     # 0x27986
movl $0x5, %edi
xorl %eax, %eax
callq 0x1cede
leaq 0x20(%rsp), %r13
movb $0x1, %r12b
jmp 0xd19b
xorl %r12d, %r12d
leaq 0x18f32(%rip), %rdi     # 0x260a9
xorl %eax, %eax
callq 0x379e
jmp 0xd19b
leaq 0x18f97(%rip), %rdi     # 0x2611e
movl %r12d, %esi
xorl %r12d, %r12d
movl %ebx, %ecx
xorl %eax, %eax
callq 0x379e
leaq 0x20(%rsp), %r13
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xd1b8
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xd1cf
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq %r13, %rdi
callq 0xe794
leaq 0x8(%rsp), %rdi
callq 0xe188
movl %r12d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
leaq 0x18eed(%rip), %rdi     # 0x260ed
movl %ebx, %esi
xorl %eax, %eax
callq 0x379e
leaq 0x20(%rsp), %r13
jmp 0xd19b
jmp 0xd218
jmp 0xd218
jmp 0xd218
jmp 0xd218
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xde46
jmp 0xd25d
movq %rax, %rbx
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xd246
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
jmp 0xd246
movq %rax, %rbx
movq %r13, %rdi
callq 0xe794
jmp 0xd253
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xe188
movq %rbx, %rdi
callq 0x3590