#!/usr/bin/perl

$limit = ($ARGV[0] || 1000);

@libs = ();
for $i (1 .. $limit) {
  my $name = sprintf("foo_%04d", $i);
  my $stmt = ($i == $limit) ? "abort()" : sprintf("return foo_%04d()", $i+1);
  open(OUT, "> $name.c") || die;
  print OUT "int $name() { $stmt; }\n";
  close(OUT);
  system("gcc -fPIC -shared -o $name.so $name.c 2>/dev/null") && die;
  push(@libs, "./$name.so");
}

open(OUT, "> main.c") || die;
print OUT "int main() { return foo_0001(); }\n";
close(OUT);

system("gcc", "main.c", @libs) && die;
