Hello [email protected],
Thank you for this detailed explanation of the issue!
The problem you are facing is not related to the generated code. The issue is related to the board.
1. D1 (It's always on but flickers during code upload from Simulink)
The D1 LED is continuously turned on, which means that the MCU is in the reset state all the time. In other words, it is bricked and cannot be recovered from the reset state by the usual flashing procedure. So in order to bring the processor back to like a program called Kinetis_recovery has to be downloaded from PEMicro's website. Please get this application that will help you to recover the MCU.
More details can also be found on this website with a large description of the issue: Bricking and Recovering FRDM-KL25Z Boards: Reset, SWD Clock and Low Power | MCU on Eclipse .
This happens some times, so I will try to guide you through the steps I am usually performing, especially that the link above is for the Kinetis platform, not S32K, but both platforms are similar.
1. Download the Kinetis_recovery.
2. Open the program, select the OpenSDAUSB, and press start. If you get the "Success. Processor halted" message, then you are the right path, close the Recovery Utility and go to the next step. Otherwise, you should start power cycling the board until the message appears. If you can't halt the processor than an external probe might be required, but let's hope it will work with the OpenSDA.

3. Now, I usually flash the program with the S32 Design Studio IDE for Arm® based MCUs | NXP. So, Open S32 Design Studio for ARM, File -> New -> S32DS Project from Example, Import the hello_world_s32k144.

Go to Debug Configurations, select the hello_world_s32k144_debug_flash_pemicro and press Debug. After the process is complete, it will enter the main loop of the hello_eorld program. If you exit the Debug, the RGB LED will start blinking. So the board is now back to life.
4. Now you can regenerate and reprogram the board from Simulink.
I hope this helps you,
Marius