Walmart swings to net loss after agreeing $3.1bn opioid settlement