"What are the ethics of writing code that would result in its "suffering"? "
I doubt that code can suffer. We do not understand feelings enough and this might help to understand it eventually - but for now I see no reason to assume that they can recreated digitally.
And I am much more worried, about many real life beings that suffer in our food chain and co.