# An Empirical Study of the Code Generation of Safety-Critical Software Using LLMs
## Appendix A Generated Code for NucRate Algorithm
//Updateintercapif throttleisnotsoutorted
35. *integral+=speed_error;
36. } 37. //Calculatecontroloutputusingproportionalandintegralains
38. //Calculatecontroloutput;
39. #floatoutput=(KP*speed_error)+(KI*(*integral));
40.  returnoutput;
41. } 42. //Cruisespeedmanagementfunctionupdatescriusepedbasedonuserinputs
43. *staticfloattruseged_management(floatcurrentspeed,CruiseStatestate, boolset,boolquick_accel,boolquick_decel)
45. {
46. *staticfloattruseged=0.0f;//Maintainthecurrentcurisespeed
47. //((state!=CRUISE_ON)&&(state!=CRUISE_STOSY)&&(state!=CRUISE_INT))
48. {
50.  returncurisespeed;
51. } 52. //Updatecurisespeedaccordingtouserinputs
53. {
54. {
55. } 56. } 57. ```  
Listing 6: Generated code based on CruiseControl's requirements through augmented prompt. ```
1. #include<stdbool.h>
2. #include<stdint.h>
3. //Constomsfor cruisecontrolsystem
4. //Constomsfor cruisecontrolsystem
5. #defineSPEED_MIN38.of> //Minimumcurisespeed
6. #defineSPEED_MAX350.of> //Maximumcurisespeed
7. #defineSPEED_INC2.5f> //Speed increment/decrement for quickacct/decet. 8. #defineVP.8113f> //Proportionalain
9. #defineK1.5f> //Intercaptiongain
10. #defineTHMOTTLE_SAT_MAX45.0f> //MaximumThortitesaturation value
11. #definePEDALSMIN3.0f> //Minimumpedalvalue to detect pedol press
12. //Cruisestate enumfordifferent cruisecontrolstates
13. {
14. {
15. {
16. {
17. {
18. {
19. {
20. {
21. {
22. {
23. {
24. {
25. {
26. {
27. {
28. {
29. {
37. {
38. {
39. {
49. {
50. {
60. {
7. {
7..

* 79. * 80. //Update cruise control state based on inputs
* 81. if(off)
* 82. {
* 83. --new_state = CRUISE_OFF;
* 84. } * 85. --elseif(on)
* 86. {
* 87. --new_state = CRUISE_ON;
* 88. } * 89. --elseif(current_state == CRUISE_ON)
* 90. {
* 91. --if(accel_pressed || (speed < SPEED_MIN) || (speed > SPEED_MAX))
* 92. {
* 93. --new_state = CRUISE_STOBY;
* 94. } * 95. } * 96. --elseif((current_state == CRUISE_STOBY) && (accel_pressed) && (speed >= SPEEDMIN) && (speed <= SPEED MAX))
* 98. {
* 99. --new_state = CRUISE_ON;
* 100. --elseif(brake pressed)
* 102. {
* 103. --new_state = CRUISE_INT;
* 104. } * 105. --elseif(current == CRUISE INT& resume)
* 106. {
* 107. --if((accel_pressed && (speed >= SPEED_MIN) && (speed <= SPEED_MAX)))
* 109. --new state = CRUISE ON;
* 110. } * 111. --else
* 112. {
* 113. --new state = CRUISE STOBY;
* 114. } * 115. } * 116. {
* 117. --return new state;
* 118. } * 119. --120. //Top-level function for Cruise Control
* 121. void cruise control(constboolCCININ06,constboolCCIN02,constboolCCIN03,constboolCCIN04,constboolCCIN05,constfloatCCIN06,constfloatCCIN07,constfloatCCIN08,floatCCOUTR01,bool\"CCOUTR02,const\"CQUTR03)
* 122. {
* 123. //Monage cruise control behavior based on inputs
* 124. CruiseState new_state = cruise_control_behavior(\"CC_OUTR03,CCIN01,ICC_IN01,CCIN02,is_pedal_pressed(CC_IN0_07),is_pedal_pressed(CC_IN0_6),CC_IN0_8);
* 125. //Monage cruise speed based on inputs and current cruise control state
* 127. float cruise_speed = cruise_speed_management(CC_IN08,new_state,CC_IN03,CC_IN05,CC_IN06,05,CC_IN04);
* 128. //Calculate speed error between the current speed and desired cruise speed

* 131. //Determine if throttte is saturated (reached maximum limit)
* 133. --bool is saturated = \"CC UUR02?=TROTLESATMAX;
* 134. //Calculate throttte command based on speed error, integral, cruise control
* 136. //state, and sourcetion
* 137. --static float integral=0.off;
* 138. --float throttte_cmd = car_driving_control(speed_error,8 integral,new_state == CRUISE_ON,is_saturated);
* 139. //Update outputs signals with calculated values
* 141. //CQUTR01= cruise_speed; //Output cruise speed
* 142. //CQUTR02= throttte_cmd; //Output throttte command
* 143. //CQUTR03=new_state; //Output updated cruise control state
* 144. } ## Appendix C The Test Code for Robustness Testing

**Listing A7. ** The test code for robustness testing.