%% Eliminate Dead Code Paths in Generated Code
% This example shows how the code generator eliminates dead (that is,
% unused) code paths from generated code. This optimization increases execution
% speed and conserves ROM and RAM consumption.
%
% Copyright 2015 The MathWorks, Inc.

%% Example
% In the model <matlab:rtwdemo_deadpathElim rtwdemo_deadpathElim>, the signal leaving the Sum block
% divides into two separate code paths. The top path is not a dead code
% path. If the user disables the Assertion block, the bottom path becomes
% a dead code path.
% 
model = 'rtwdemo_deadpathElim';
open_system(model);

%% Generate Code with an Enabled Assertion Block
% 
% # For the Assertion block, open the block parameters dialog box.
% # Select the *Enable assertion* box. Alternatively, use the command-line
% API to enable the Assertion block.
set_param([model '/Assert1'], 'Enabled', 'on');
%%
% Create a temporary folder  for the build and inspection process.
currentDir = pwd;
[~,cgDir] = rtwdemodir();
%%
% Build the model.
rtwbuild(model)

%%
% Because the Assertion block is enabled, these lines of |rtwdemo_deadpathElim.c|
% include code for the Gain and Assertion blocks.
cfile = fullfile(cgDir,'rtwdemo_deadpathElim_grt_rtw','rtwdemo_deadpathElim.c');
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize function */', 0, 1);

%% Generate Code with a Disabled Assertion Block
% Disable the Assertion block to generate a dead code path. The code
% generator detects the dead code path and eliminates it from the generated
% code.
%
% # For the Assertion block, open the Block Parameters dialog box.
% # Deselect the *Enable assertion* box.
% 
% Alternatively, use the command-line API to disable the Assertion block.
set_param([model '/Assert1'], 'Enabled', 'off');

%% 
% Build the model.
rtwbuild(model)

%%
% Because the Assertion block is disabled, these lines of |rtwdemo_deadpathElim.c|
% do not include code for the Gain and Assertion blocks.
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize function */', 0, 1);

%% Close the model and clean-up.
bdclose(model)
rtwdemoclean;
cd(currentDir)

%%
% For another example of how the code generator eliminates dead code paths
% in the generated code, see <matlab:rtwdemo_deadpath rtwdemo_deadpath>.