AMD64 fixes based on patch by Timo Teräs <timo.teras@iki.fi>