https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112409
Bug ID: 112409 Summary: Structure is not initialized as expected Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: freddy77 at gmail dot com Target Milestone: --- I was writing a small network utility till I found a weird behaviour computing TCP checksums. After some debugging I found that the error disappeared with either -O1 or -O0. So I reduced the program, trying using multiple GCC versions and it kept happening. The final program (stripped down) is: typedef long unsigned int size_t; typedef unsigned char uint8_t; typedef unsigned short int uint16_t; typedef unsigned int uint32_t; #define ntohs __builtin_bswap16 #define htonl __builtin_bswap32 static inline unsigned reduce_cksum(unsigned sum) { return (sum >> 16) + (sum & 0xffffu); } static unsigned cksum(const void *pkt, size_t len, unsigned int start) { const uint16_t *data = (const uint16_t *) pkt; unsigned sum = start; for (; len >= 2; len -= 2) sum += *data++; if (len) sum += ntohs(*((const uint8_t *)data) << 8); sum = reduce_cksum(sum); sum = reduce_cksum(sum); return sum; } static unsigned tcpdump_flow_new(uint32_t saddr, uint32_t daddr) { struct { uint32_t saddr, daddr; uint8_t zero, proto; } pseudo = { htonl(saddr), htonl(daddr), 0, 6 }; return cksum(&pseudo, 10, 0); } int main(void) { unsigned res = tcpdump_flow_new(0x01020304, 0xa1b2c3d4); return res; } Using -O2 option and Compiler Explorer (but I tried multiple versions locally) produced this: main: xor edx, edx lea rax, [rsp-12] lea rsi, [rsp-2] .L2: movzx ecx, WORD PTR [rax] add rax, 2 add edx, ecx cmp rax, rsi jne .L2 mov eax, edx movzx edx, dx shr eax, 16 add edx, eax mov eax, edx movzx edx, dx shr eax, 16 add eax, edx ret It's easy to spot that the constants disappeared from the code and program uses not initialized memory.