| 1 |
/* { dg-do run { target i?86-*-* x86_64-*-* } } */ |
| 2 |
/* { dg-options "-O1" } */ |
| 3 |
|
| 4 |
typedef int i32; |
| 5 |
typedef unsigned int u32; |
| 6 |
|
| 7 |
static inline void write_i32(void *memory, i32 value) { |
| 8 |
// swap i32 bytes as if it was u32: |
| 9 |
u32 u_value = value; |
| 10 |
value = __builtin_bswap32(u_value); |
| 11 |
|
| 12 |
// llvm infers '1' alignment from destination type |
| 13 |
__builtin_memcpy(__builtin_assume_aligned(memory, 1), &value, sizeof(value)); |
| 14 |
} |
| 15 |
|
| 16 |
__attribute__((noipa)) |
| 17 |
static void bug (void) { |
| 18 |
#define assert_eq(lhs, rhs) if (lhs != rhs) __builtin_trap() |
| 19 |
|
| 20 |
unsigned char data[5]; |
| 21 |
write_i32(data, -1362446643); |
| 22 |
assert_eq(data[0], 0xAE); |
| 23 |
assert_eq(data[1], 0xCA); |
| 24 |
write_i32(data + 1, -1362446643); |
| 25 |
assert_eq(data[1], 0xAE); |
| 26 |
} |
| 27 |
|
| 28 |
int main() { |
| 29 |
bug(); |
| 30 |
return 0; |
| 31 |
} |