: "@(#)perfmerge	5.3	7/16/86"
# usage - 'perfmerge stat.output mapfile'
# "produces merged map and stat output on stdout
# "Turn the map into a sort-able list of procedure names and addresses
if grep -n Linker $2 | grep "^1.*Linker" > /dev/null
then
sed < $2 > /tmp/pm$$ -n -e \
'/^\(...................\)\(.........\)\(.\{47,\}\)/s//\2\1/p'
else
sed < $2 > /tmp/pm$$ -e \
'/^\(.....................\)\(..........\)code\(.*\)/s//\2\1/' \
-e \
'/^\(.....................\)\(..........\)data\(.*\)/d'
fi

# Sort/merge the stat output with the translated map
sort -f $1 /tmp/pm$$ > /tmp/pm2$$

# Find the total number of samples per procedure
awk ' /Total samples/	{print "BEGIN	{TotalSamples  = " substr($8, 1, length ($8)-1) "}"} ' /tmp/pm2$$ > /tmp/pm3$$

# Add the rule which produces the sums per procedure to the awk script
cat /tmp/pm3$$  > /tmp/pm4$$
cat << ! >> /tmp/pm4$$
/^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]/ {
if (\$2 ~ /[A-Za-z]/) 
	{if (total != 0)
		{printf "Total      %6d %3d%%\n", total, (total*100)/TotalSamples; total = 0}
	else
		{}
	}
else
	total += \$2}
	{print}
!

# Actually produce the procedure sums
awk -f /tmp/pm4$$ /tmp/pm2$$

# clean up the temp files
rm /tmp/pm*$$
