#!/usr/bin/perl


@ARGV=("overlap.txt") unless @ARGV;
%tree=();
$saved=0;
while (<>)
{
	chomp();
	($adr, $name, $opco, $bytes)= split(/\s+/, $_, 4);

	@instr=split(/-/,$bytes);
# The instructions are in correct order - end of block at end.
# We want the same things in front.
# A jump needs 5 bytes, say.
	MakeTree(\%tree, -5, reverse @instr);

#	last if $. > 100;
}

print "Approx. $saved bytes saved.\n";

sub MakeTree
{
	my($t, $depth, $cur, @instr)=@_;

	if ($t->{$cur})
	{
# Element exists, go further.
# Count length
		$l = (length($cur)+1)/3;
#		print " " x $depth, "$cur ... $l, at $depth\n";
		MakeTree($t->{$cur}, $depth+$l, @instr) 
			if @instr;
	}
	else
	{
# Not found. Insert data, and count saved.
# Did we save space? (Might be negative)
		$saved+=$depth if $depth>1;
		for($cur,@instr)
		{
			$t = ( $t->{$_}={} );
		}
	}
}


