https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129014
Bug ID: 129014
Summary: [SRA] wrong code at -O1 and above: big-endian
scalar_storage_order field stored in native byte order
after memcpy of a copied const initializer and a store
to another field
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: 0599jiangyc at gmail dot com
Target Milestone: ---
The following code:
```c
/* GCC: wrong code at -O1 and above (also GCC 11): a big-endian
scalar_storage_order
* field is written in native order after copying a const initializer through a
local,
* memcpy'ing it into another object and storing a different field.
* Godbolt: x86-64 gcc (trunk), flags: -O2 (correct: -O0, -O2 -fno-tree-sra,
-O2 -fno-tree-dse)
* EXPECTED: "80 f0 8f", exit 0. BUGGY: "80 8f f0", exit 1.
*/
#include <stdio.h>
#include <string.h>
struct __attribute__((scalar_storage_order("big-endian"), packed)) S { unsigned
char x0; unsigned short x1; };
__attribute__((noinline)) void f(unsigned char *out) {
static const struct S C = { 0x20, 0xf08f };
struct S s;
struct S t = C;
memcpy(&s, &t, sizeof t);
s.x0 = 0x80;
memcpy(out, &s, sizeof s); /* x1 is big-endian: bytes f0 8f */
}
int main(void) {
unsigned char b[3];
f(b);
printf("%02x %02x %02x\n", b[0], b[1], b[2]);
return !(b[0] == 0x80 && b[1] == 0xf0 && b[2] == 0x8f);
}
```
Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out (also -O1, -O3; GCC 11.4 too)
80 8f f0
(exit 1; -O0, -O2 -fno-tree-sra and -O2 -fno-tree-dse print 80 f0 8f)
```
Trunk: https://godbolt.org/z/7EPf7dcjz (x86-64 gcc trunk, -O2, executed;
reproduces as of 2026-11-12)