function x = flipdim(x,dim)
%MATLAB Code Generation Library Function
%
% Limitations:
% 
%   1. Cell input for first argument is not supported

%   Copyright 1984-2019 The MathWorks, Inc.
%#codegen

coder.internal.allowEnumInputs;
coder.internal.allowHalfInputs;
eml_invariant(nargin == 2, ...
    'Coder:toolbox:flipdim_1', ...
    'IfNotConst','Fail');
eml_invariant(~iscell(x), ...
    'Coder:toolbox:CellArraysNotSupported', mfilename);
coder.internal.prefer_const(dim);
coder.internal.assertValidDim(dim);
vlen = size(x,dim);
if ~isempty(x) && dim <= ndims(x) && vlen > 1
    vstride = coder.internal.prodsize(x,'below',dim);
    vspread = coder.internal.indexTimes(coder.internal.indexMinus(vlen,1),vstride);
    npages  = coder.internal.prodsize(x,'above',dim);
    i2      = zeros(coder.internal.indexIntClass);
    for i = 1:npages
        i1 = i2;
        i2 = coder.internal.indexPlus(i2,vspread);
        for j = 1:vstride
            i1 = coder.internal.indexPlus(i1,1);
            i2 = coder.internal.indexPlus(i2,1);
            ixleft = i1;
            ixright = i2;
            while ixleft < ixright
                tmp = x(ixleft);
                x(ixleft) = x(ixright);
                x(ixright) = tmp;
                ixleft = coder.internal.indexPlus(ixleft,vstride);
                ixright = coder.internal.indexMinus(ixright,vstride);
            end
        end
    end
end
