Option B is very busy, and with so few days I would save Barcelona for a future trip. There is lots to see and do that way but it is much further than the rest of your desired destinations. Don't let a fast pace and a desire to see as much as possible defeat your plans to experience the culture. Prioritize what is really important to you and tell yourself that there will be future trips where you can continue to explore and learn.
Public transportation is easy to use in Spain. Trains and busses have very convenient, frequent service. What is your total time for Spain?
I like Madrid. You could spend your whole week using Madrid as a base, combining key sites in Madrid with daytrips of your choice. Daytrip possibilities include Segovia, Ávila, Toledo, Aranjuez, la Granja de San Ildefonso, Alcalá de Henares, Cuenca, etc. You would have to do your research to see what appeals to your interests.
Since you already have plans to visit Sevilla after Málaga, and if Madrid does not appeal to you, then consider Córdoba and Granada. I would try to visit Córdoba either on route to Granada or in combination with your Sevilla visit, depending on how many days you have there. I, personally, would skip Gibraltar and dedicate that time to Spain. If you do decide to go to Gibraltar look carefully at your public transportation options and get advice about that. They may be more limited.