function [deviation,simStateSlice] = detectCompileTimeMismatch(msObj,slicedMdl,throwError,UImode,origMdlH,expandLib,hasGlobal,sliceXfrmr, origAttrMap)
    %

    %   Copyright 2014-2017 The MathWorks, Inc.
        
    import Transform.*
    deviation = struct([]);
    simStateSlice = [];
    
	sliceMdlH=get_param(slicedMdl,'Handle');
    origSortedH = [];
    if hasGlobal
        origSortedH = Analysis.fixSortedOrder(origMdlH,sliceMdlH,expandLib,msObj.sliceSubSystemH);
    end
    if isa(origAttrMap,'containers.Map')
        % Compile time port attribute for the original model is cached.
        msObj.terminateModel;
    end
    simHandler = [];
    if msObj.isSimStateSlice
        simHandler = configureSlicedModel4SimState(slicedMdl, msObj, true);
    end

    try
        % Set sliced models to creating IR mode so SLEEK will create graphs
        slicedRefMdls = find_mdlrefs(slicedMdl,true);
        ModelSlicer.setModelsCreatingIR(slicedRefMdls,true);

        % Whenever exiting from this function, sliced model should be
        % terminated.
        termCleanUp = onCleanup(@() terminateSlicedModel(msObj, slicedMdl, simHandler));
        simStateSlice = compileSlicedModel(msObj,slicedMdl, simHandler);
    catch Mex
        switch Mex.identifier
            case 'Simulink:blocks:BusCannotCacheCompBus'
                % Just try to compile a 2nd time
                try
                    
                    simStateSlice = compileSlicedModel(msObj,slicedMdl,simHandler);
                catch Mx
                    handleCompileFailure(Mx, slicedRefMdls);
                    return;
                end
            case 'Simulink:SampleTime:NoMultiTasking'
                try
                    % Try to fix problem by changing the solver mode
                    if strcmp(get_param(slicedMdl,'SolverMode'),'MultiTasking')
                        set_param(slicedMdl,'SolverMode','Auto');
                        save_system(slicedMdl, slicedMdl, 'OverwriteIfChangedOnDisk', true);
                        simStateSlice = compileSlicedModel(msObj,slicedMdl,simHandler);
                    else
                        throw(Mex);
                    end
                catch MExp            
                    % If it fails to compile, we just throw errors in message viewer.
                    handleCompileFailure(MExp, slicedrefMdls);
                    return;
                end
			case 'Simulink:Masking:Bad_Init_Commands'
                ex = MException('ModelSlicer:MaskCallbackFailure',...
                    getString(message('Sldv:ModelSlicer:gui:MaskCallbackFailure')));
                ex = ex.addCause(Mex);
                modelslicerprivate('MessageHandler','warning',ex, slicedMdl);
                Mex2 = MException('ModelSlicer:PortVerificationSkipped',...
                    getString(message('Sldv:ModelSlicer:gui:PortVerificationSkipped')));
                modelslicerprivate('MessageHandler','warning',Mex2, slicedMdl)
                % Remove the ModelSlicerActive flag on the models
                ModelSlicer.setModelsCreatingIR(slicedRefMdls,false);
                return;
				
            otherwise
                % If it fails to compile, we just throw errors in message viewer.
                handleCompileFailure(Mex, slicedRefMdls);
                return;
        end
    end
    try 
        deviation = Transform.detectPortAttrMismatch(slicedMdl,throwError,UImode,sliceXfrmr,origAttrMap);
    catch ex
        if ~strcmp(ex.identifier,'ModelSlicer:EmptyModel')
            % We don't need to diagnostic empty model.
            modelslicerprivate('MessageHandler','error',ex);
        end
        return;
    end
            
    % Remove the ModelSlicerActive flag on the models
    ModelSlicer.setModelsCreatingIR(slicedRefMdls,false);
    
    slicerMap = modelslicerprivate('sliceMdlMapperObj','get',sliceMdlH);
    
    if hasGlobal
        try
           %Analysis.checkSortingOrder(sliceMdlH,origMdlH,slicerMap,refMdlToMdlBlk);
           Analysis.checkGlobalAccess(sliceMdlH,slicerMap,origSortedH);
        catch ex
        end
    end
                
    function handleCompileFailure(Mex, slicedrefMdls)
        % Remove the ModelSlicerActive flag on the models
        ModelSlicer.setModelsCreatingIR(slicedrefMdls,false);
        mexp = MException('ModelSlicer:SlicedModelFailedToCompile',...
            getString(message('Sldv:ModelSlicer:Transform:FailedCompileSlicedModel')));
        mexp = mexp.addCause(Mex);
        if UImode
            modelslicerprivate('MessageHandler','error',mexp);
            return;
        else
            throw(mexp)
        end
    end
end

