Hello, I'm sorry to bother you. And I have some gcc compiler optimization questions to ask you. First of all, I used csmith tools to generate c files randomly. Meanwhile, the final running result was the checksum for global variables in a c file. For the two c files in the attachment, I performed the equivalent transformation of loop from *initial.**c* to *transformed.c*. And the two files produced different results (i.e. different checksum values) when using *-O2* optimization level, while the results of both were the same when using other levels of optimization such as *-O0*, *-O1*, *-O3*, *-Os*, *-Ofast*. Please help me to explain why this is, thank you.
command line: *gcc file.c -O2 -lm -I $CSMITH_HOME/include && ./a.out* version: gcc 12.2.0 os: ubuntu 22.04
#include "csmith.h" static int32_t g_a321[3]; static int64_t g_b321[3]; union U0 { const int32_t f0; }; union U1 { const uint16_t f0; const uint64_t f1; int32_t f2; const signed f3 : 29; }; union U2 { int64_t f0; int16_t f1; }; static uint32_t g_39 = 0x78C76445L; static union U0 g_87 = {0xA9ACF304L}; static union U0 *g_89[1][3][2] = {{{&g_87, &g_87}, {&g_87, &g_87}, {&g_87, &g_87}}}; static union U0 **volatile g_88[3][1] = {{&g_89[0][2][1]}, {&g_89[0][2][1]}, {&g_89[0][2][1]}}; static int32_t g_165 = 2L; static union U2 g_261[3] = {{0xDF9C6E8BCC63CCAELL}, {0xDF9C6E8BCC63CCAELL}, {0xDF9C6E8BCC63CCAELL}}; static union U1 g_412 = {3UL}; static void func_1(void); static void func_1() { uint8_t l_19 = 1UL; for (l_19 = 0; (l_19 <= 2); l_19 += 1) { int i, j; for (i = 0; i < 1; i++) { for (j = 0; j < 3; j++) { } } int ii_7; // fusion in add execTimes for (g_39 = 0, ii_7 = 0; (g_39 <= 1); g_39 += 1, ii_7++) { g_a321[ii_7] = (*g_89[0][2][0]).f0 * l_19 + g_165; } int jj_7; for (jj_7 = 0; jj_7 < 3; jj_7++) { g_b321[jj_7] = g_261[1].f0 * g_a321[jj_7] + g_412.f2; } } } int main(void) { int i; int print_hash_value = 0; platform_main_begin(); crc32_gentab(); func_1(); transparent_crc(g_39, "g_39", print_hash_value); transparent_crc(g_87.f0, "g_87.f0", print_hash_value); transparent_crc(g_165, "g_165", print_hash_value); for (i = 0; i < 3; i++) { transparent_crc(g_261[i].f0, "g_261[i].f0", print_hash_value); } transparent_crc(g_412.f0, "g_412.f0", print_hash_value); for (i = 0; i < 3; i++) { transparent_crc(g_a321[i], "g_a321[i]", print_hash_value); } for (i = 0; i < 3; i++) { transparent_crc(g_b321[i], "g_b321[i]", print_hash_value); } platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value); return 0; }
#include "csmith.h" static int32_t g_a321[3]; static int64_t g_b321[3]; union U0 { const int32_t f0; }; union U1 { const uint16_t f0; const uint64_t f1; int32_t f2; const signed f3 : 29; }; union U2 { int64_t f0; int16_t f1; }; static uint32_t g_39 = 0x78C76445L; static union U0 g_87 = {0xA9ACF304L}; static union U0 *g_89[1][3][2] = {{{&g_87, &g_87}, {&g_87, &g_87}, {&g_87, &g_87}}}; static union U0 **volatile g_88[3][1] = {{&g_89[0][2][1]}, {&g_89[0][2][1]}, {&g_89[0][2][1]}}; static int32_t g_165 = 2L; static union U2 g_261[3] = {{0xDF9C6E8BCC63CCAELL}, {0xDF9C6E8BCC63CCAELL}, {0xDF9C6E8BCC63CCAELL}}; static union U1 g_412 = {3UL}; static void func_1(void); static void func_1() { uint8_t l_19 = 1UL; for (l_19 = 0; (l_19 <= 2); l_19 += 1) { int i, j; for (i = 0; i < 1; i++) { for (j = 0; j < 3; j++) { } } int ii_7; int jj_7; int ij_7; // fusion in add execTimes for (g_39 = 0, ii_7 = 0, jj_7 = 0, ij_7 = 0; ij_7 < 5; ij_7++) { if (ij_7 <= 2 && (g_39 <= 1)) { g_a321[ii_7] = (*g_89[0][2][0]).f0 * l_19 + g_165; g_39 += 1; ii_7++; } else { jj_7 = ij_7 - 2; g_b321[jj_7] = g_261[1].f0 * g_a321[jj_7] + g_412.f2; } } } } int main(void) { int i; int print_hash_value = 0; platform_main_begin(); crc32_gentab(); func_1(); transparent_crc(g_39, "g_39", print_hash_value); transparent_crc(g_87.f0, "g_87.f0", print_hash_value); transparent_crc(g_165, "g_165", print_hash_value); for (i = 0; i < 3; i++) { transparent_crc(g_261[i].f0, "g_261[i].f0", print_hash_value); } transparent_crc(g_412.f0, "g_412.f0", print_hash_value); for (i = 0; i < 3; i++) { transparent_crc(g_a321[i], "g_a321[i]", print_hash_value); } for (i = 0; i < 3; i++) { transparent_crc(g_b321[i], "g_b321[i]", print_hash_value); } platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value); return 0; }