That is impossible to tell in advance. The gates used by TATL flights are not given, i.e. can be any gate at IST. The same applies for MAD flights.
You pass immigration upon arrival at MAD. You only go through transfer security check at IST (unless TK staff will greet you on the aero bridge and allow you to go straight to the departures level bypassing transfer security checks).