You could probably place or embed the IP to Nation loop into the Freeform Form and have it populate a Freeform Custom Field.
From there, on your Freeform Entries page, you can sort your entries by any Freeform Field, and just make the output of the that Field match up for correct URL to flag image.
I haven’t tested this or anything, but I imagine it should work