x86 vm fails to stop on Darwin aarch64 when qemu compiled with -O1/-O2
<!--
This is the upstream QEMU issue tracker.
If you are able to, it will greatly facilitate bug triage if you attempt
to reproduce the problem with the latest qemu.git master built from
source. See https://www.qemu.org/download/#source for instructions on
how to do this.
QEMU generally supports the last two releases advertised on
https://www.qemu.org/. Problems with distro-packaged versions of QEMU
older than this should be reported to the distribution instead.
See https://www.qemu.org/contribute/report-a-bug/ for additional
guidance.
If this is a security issue, please consult
https://www.qemu.org/contribute/security-process/
-->
## Host environment
- Operating system: MacOS Ventura 13.3.1 (a) <!-- Windows 10 21H1, Fedora 37, etc. -->
- OS/kernel version: Darwin mbp.local 22.4.0 Darwin Kernel Version 22.4.0 <!-- For POSIX hosts, use `uname -a` -->
- Architecture: ARM64 <!-- x86, ARM, s390x, etc. -->
- QEMU flavor: qemu-system-x86_64 <!-- qemu-system-x86_64, qemu-aarch64, qemu-img, etc. -->
- QEMU version: 8.0.0
- QEMU command line:
<!--
Give the smallest, complete command line that exhibits the problem.
If you are using libvirt, virsh, or vmm, you can likely find the QEMU
command line arguments in /var/log/libvirt/qemu/$GUEST.log.
-->
```
./qemu-system-x86_64 -serial mon:stdio -drive file=ubuntu_20_04.qcow2,if=virtio,cache=writeback,discard=ignore,format=qcow2 -drive file=cidata.iso,media=cdrom -machine type=pc,accel=tcg -vnc 127.0.0.1:16 -name ubuntu_20_04.qcow2 -nographic -netdev user,id=user.0,hostfwd=tcp::2640-:22 -m 2048M -smp 1 -device virtio-net,netdev=user.0 -boot c
```
## Emulated/Virtualized environment
- Operating system: Ubuntu 20.04 <!-- Windows 10 21H1, Fedora 37, etc. -->
- OS/kernel version: 5.4.0 <!-- For POSIX guests, use `uname -a`. -->
- Architecture: x86 <!-- x86, ARM, s390x, etc. -->
## Description of problem
<!-- Describe the problem, including any error/crash messages seen. -->
When compiled with `-O2` or `-O1` qemu process hangs on full VM stopping on macOS aarch64 host if `shutdown -P now` initiated from guest system.
## Steps to reproduce
1. Compile latest qemu version with -O2 (default value) or -O1 passed
2. Run qemu-system-x86_64 with ubuntu image, e.g. https://cloud-images.ubuntu.com/focal/20230215/focal-server-cloudimg-amd64.img and custom cloud-init (for user/password authentication)
3. Wait until image is loaded, connect via vnc or provide login/password in stdio
4. Initiate shutdown with `sudo shutdown -P now`
5. See that VM indefinitely shutdowns
6. Kill VM from host system with kill -9 <qemu-system-x86_64-process-pid>
7. Recompile qemu with -O0
8. Repeat steps 2-4
9. See that vm successfully stopped, and qemu process exited with code 0
## Additional information
<!--
Attach logs, stack traces, screenshots, etc. Compress the files if necessary.
If using libvirt, libvirt logs and XML domain information may be relevant.
-->
I've created thread dump from activity monitor with threads which qemu hanging on, attached below
[sample-qemu-system-x86_64.txt](/uploads/119b89b7f55f4374acb9ae1f9dc2e517/sample-qemu-system-x86_64.txt)
Probably there is some compiler optimisation which prevents qemu threads from receive shutdown signal or appropriate notification from another threads.
The compiler version with which qemu is built:
```bash
% cc --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
```
<!--
The line below ensures that proper tags are added to the issue.
Please do not remove it.
-->
issue