function breakLibraryLinks(sys)
%

%   Copyright 2018-2021 The MathWorks, Inc.

    persistent physModBlockTypes;
    try
        if isempty(physModBlockTypes)
            %Physmod block types registered.
            pmsl_blocktyperegistry = pmsl_private('pmsl_blocktyperegistry');
            physModBlockTypes = pmsl_blocktyperegistry();
        end
    catch
        physModBlockTypes = [];
    end
    
    % @todo update the usage of edit-time filter filterOutInactiveVariantSubsystemChoices()
    % instead use the post-compile filter activeVariants() - g2603143
    blks = find_system(sys,'MatchFilter',@Simulink.match.internal.filterOutInactiveVariantSubsystemChoices, 'LookUnderMasks', 'all', 'LinkStatus', 'resolved' ); % look only inside active choice of VSS
    seenPhysModBlks = {};
    
    %setting the ModelSlicerActive parameter to 1 so that set_param of 
    %'LinkStatus' parameter on read-only blocks is permitted. Using
    %'bdroot(sys)', since the 'sys' may be a subsystem block for example
    %and 'ModelSlicerActive' parameter doesn't exist for it
    originalSlicerActiveInfo = get_param(bdroot(sys),'ModelSlicerActive');
    set_param(bdroot(sys), 'ModelSlicerActive',1);
    slicerActiveCleanup = onCleanup(@()(set_param(bdroot(sys), 'ModelSlicerActive',originalSlicerActiveInfo)));
    
    while ~isempty(blks)
        for i=1:length(blks)
            %Do not break links for physmod blocks (g1523685)
            if ~isBuiltInPhysmodBlock(blks{i})
                set_param(blks{i}, 'LinkStatus', 'none');
            else
                seenPhysModBlks = [seenPhysModBlks;blks{i}];
            end
        end
    % @todo update the usage of edit-time filter filterOutInactiveVariantSubsystemChoices()
    % instead use the post-compile filter activeVariants() - g2603143
        blks = find_system(sys,'MatchFilter',@Simulink.match.internal.filterOutInactiveVariantSubsystemChoices, 'LookUnderMasks', 'all', 'LinkStatus', 'resolved' ); % look only inside active choice of VSS
        blks = setdiff(blks,seenPhysModBlks);
    end
    
    function yesno = isBuiltInPhysmodBlock(blk)
        % We skip blocks of the registered block types
        % and subsystems with the physical domain parameter set.
        % See private/pmsl_verifylibrarylinks, which throws the 
        % compile time error
        blkType = get_param(blk,'BlockType');
        blkStruct = get(get_param(blk,'handle'));
        try
            yesno = ismember(blkType,physModBlockTypes) || ...
                isfield(blkStruct,'PhysicalDomain');
        catch
            yesno = false;
        end
    end
end

