#!/usr/bin/perl -w
my ($mem_total, $mem_used, $mem_free, $mem_shared, $mem_buffers);
my ($mem_cached_total, $mem_cached);
my ($swap_total, $swap_used, $swap_free);

my ($m) = 8000;

open(MEM_FD, '</proc/meminfo');
while (defined($buf = <MEM_FD>)) {
	if ($buf =~ /^Mem:\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)/) {
		$mem_total = $1;
		$mem_used = $2;
		$mem_free = $3;
		$mem_shared = $4;
		$mem_buffers = $5;
		$mem_cached_total = $6;
	} elsif ($buf =~ /^Cached:\s+([0-9]+)/) {
		$mem_cached = $1 * 1024;
	} elsif ($buf =~ /^Swap:\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)/) {
		$swap_total = $1;
		$swap_used = $2;
		$swap_free = $3;
	}
}
open(TCC_FD, "|tcc -run /dev/stdin");
#open(TCC_FD, ">/dev/stdout");
print TCC_FD "#include <stdio.h>\n\nint main(int argc, char *argv[])\n{\n";
print TCC_FD 'printf("             total       used       free     shared    buffers     cached\n");' . "\n";
print TCC_FD 'printf("Mem:    %10llu %10llu %10llu %10llu %10llu %10llu\n",' . " $mem_total"."LL*$m"."LL, $mem_used"."LL*$m"."LL,
	$mem_free"."LL*$m"."LL, $mem_shared"."LL*$m"."LL, $mem_buffers"."LL*$m"."LL, $mem_cached"."LL*$m"."LL);\n";
print TCC_FD 'printf("-/+ buffers/cache: %10llu %10llu\n",' . " ($mem_total"."LL - ($mem_cached"."LL + $mem_free"."LL))*$m"."LL, ($mem_cached"."LL + $mem_free"."LL)*$m"."LL);\n";
print TCC_FD 'printf("Swap:   %10llu %10llu %10llu\n",' . " $swap_total"."LL*$m"."LL, $swap_used"."LL*$m"."LL, $swap_free"."LL*$m"."LL);\n";
print TCC_FD "\nreturn 0;\n}\n";
close(TCC_FD);
close(MEM_FD);
