function [ProfilingResults] = getSectionResults(executionTimeObject)
% This function takes as input the executionTime object of the resulting
% code profiling and returns the ProfilingResults object, which holds the
% SectionResults objects as a tree.

% Copyright 2020-2022 The MathWorks, Inc.
    
    if numel(executionTimeObject.Sections) > 0
        Root = executionTimeObject.Sections(1).getParent;
        Section_Root = coder.profile.SectionRoot(Root);
        i_createProfilingTree(Root, Section_Root);
        Section_Root.setTimeCreated(executionTimeObject.getTimeCreated);
        ProfilingResults = Section_Root;
    else
        ProfilingResults = [];
    end
end

function i_createProfilingTree(executionTimeObject, sectionResultsObject)
    children_temp = executionTimeObject.getChildren();
    if isempty(children_temp)
        return;
    end
    children = coder.profile.SectionResult.empty(0, length(children_temp));
	for i = 1:length(children_temp)
        children(i) = coder.profile.SectionResult(children_temp(i));
    	i_createProfilingTree(children_temp(i), children(i));
        children(i).setParent(sectionResultsObject);
	end
    sectionResultsObject.setChildren(children);
end
        

