#!/usr/bin/perl -w
use strict;

# This is supposed to take an objdump and produce a Graphviz dot file showing
# relation between basic blocks. This is a 20 minutes hack, it's broken in
# many ways and it's probably unfixable without a proper clean rewrite.
# Don't even think of running this over obfuscated code.

print "digraph disass {\n";

my $func;
my $previnstr = 'j';
my $current_off = '000';
for my $line (<>) {

	if ($line =~ /^[^\s]/) {
		die "only one function please" if defined $func;
		$func = $line;
		next;
	}

	$line =~ /^\s+([0-9a-f]+):\s+((?:[0-9a-f]{2}\s)+)(.+)$/;
	my ($off, $hex, $instr) = ($1, $2, $3);
	warn "skipping unparseable line: $line", next if not defined $instr;
	$instr =~ s/^\s+//;

	next if $instr =~ /^nop/;

	if (defined $previnstr and $previnstr =~ /^j/) {
		print "block_$current_off -> block_$off\n";
	}
	if (defined $previnstr and $previnstr =~ /^j|ret/) {
		$current_off = $off;
		$current_off =~ s/:$//;
	}
	$previnstr = $instr;

	next if $instr !~ /^j/;

	my $target = (split /\s+/, $instr)[1];
	print "block_$current_off -> block_$target\n";
}

print "}\n";
