# An Empirical Study of the Code Generation of Safety-Critical Software Using LLMs
## 4 Experiments
### Code Generation Based on Overall Software Requirements
#### 4.2.2 Code Generation Based on the CruiseControl's Overall Requirements  
Using the Prompt-simple approach, based on the CruiseControl's overall requirements, along with code language constraints, the prompt is as follows in Box 6:  
**Box 6. ** The prompt based on the CruiseControl's overall requirements. Generate the C code according to the following requirements:  
The CC system connects to the car through various inputs such as enabling/disabling the CC, setting/resuming cruise speed, sensors for the accelerator and brake pedals, and car speed. It delivers outputs such as cruise speed value, throttle command, and cruise state. The CC system's behavior is defined by the car's state and driver's actions. Initially, the CC is off when the car starts. It can be turned on by the driver and automatically turns off when the off button is pressed. The CC regulates car speed when the car speed is within the limit and the accelerator pedal is not pressed. It is disabled when the accelerator pedal is pressed or the car speed is outside the limit and resumes to the ON state when the accelerator pedal is not pressed and the car speed is within the limit. The CC is interrupted when the brake is pressed and resumes to either the ON or STDBY states when the resume button is pressed. When the CC is off, the car speed is controlled using the accelerator pedal. When the CC is on, the car speed is automatically regulated. The throttle command is limited to ensure comfort during automatic regulation. The cruise speed is managed only when the CC is enabled. It can be set to the current speed, increased or decreased by a certain increment, and maintained within a specified range. The accelerator and brake pedals are detected as pressed when their values are above a certain minimum. The CC parameters include minimum and maximum speeds, speed increment, proportional and integral gains for the regulation algorithm, maximum throttle saturation, and minimum pedal press detection. Based on this prompt, GPT-4 generates the C code. The entire code is in Appendix B Listing A4. We analyzed the generated code in relation to the overall requirements: the program defines several constants, including minimum speed, maximum speed, speed increment, maximum throttle saturation, and minimum pedal pressure detection CC parameters. The program also defines three enumeration types: CCState, which represents the state of the CC system. The program defines several variables, including cruise speed, vehicle speed, throttle command, CC state, throttle pedal, and brake pedal input signals. The function updateCCState() in the program is used to update the CC state. The function uses if-else statements to determine the next state based on the current state and input signals. In the OFF state, if the vehicle speed is between the minimum and maximum speeds and the throttle pedal is not pressed, the program enters the ON state and sets the cruise speed to the current vehicle speed. In the ON state, if the throttle pedal is pressed, the vehicle speed is below the minimum speed or above the maximum speed, and the program enters the STDBY state. If the brake pedal is pressed, the program enters the OFF state. In the STDBY state, if the throttle pedal is not pressed and the vehicle speed is between the minimum and maximum speeds, the program enters the ON state. The function updateThrottleCommand() in the program is used to update the throttle command. In the ON state, a throttle command is calculated based on the difference between the current vehicle speed and the cruise speed, and it is limited by the maximum throttle saturation. Inother states, the throttle command is set to 0. The main function of the program simulates a scenario where the vehicle speed is 50, and neither the throttle pedal nor the brake pedal is pressed. It updates the CC state and throttle command and outputs the CC state, cruise speed, and throttle command.