# Confusing ReleaseSmall optimization of \`asm volatile\` code

**URL:** <https://ziggit.dev/t/confusing-releasesmall-optimization-of-asm-volatile-code/14981>\
**Category:** Help\
**Tags:** learning, compiler\
**Created:** [April 16, 2026, 5:08pm UTC](https://ziggit.dev/t/confusing-releasesmall-optimization-of-asm-volatile-code/14981 "2026-04-16T17:08:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilovapples](https://ziggit.dev/user_avatar/ziggit.dev/ilovapples/32/8327_2.png) [@ilovapples](https://ziggit.dev/u/ilovapples)\
**Post date:** [April 16, 2026, 5:08pm UTC](https://ziggit.dev/t/confusing-releasesmall-optimization-of-asm-volatile-code/14981/1 "2026-04-16T17:08:37Z")

</div>

I noticed while messing around with a custom `_start` function and implementing syscall wrappers on macOS (`aarch64-macos` is the target) that `-O ReleaseSmall` causes some odd optimizations that seem incorrect.

Here’s a minimal example:

```zig
pub export fn main() u8 {
    _ = myWrite(.out, "hi", 2);
    return 0;
}

pub const Fd = extern struct {
    num: i32,

    pub const out: Fd = .{ .num = 1 };
};

const write_sys = 0x200_0004;
pub export fn myWrite(fd: Fd, ptr: [*]const u8, n: usize) usize {
    return syscall3(@intCast(fd.num), @intFromPtr(ptr), n, write_sys);
}

pub inline fn syscall3(arg0: usize, arg1: usize, arg2: usize, sys: u64) usize {
    return asm volatile ("svc #0x80"
        : [ret] "={x0}" (-> usize),
        : [sys] "{x16}" (sys),
          [arg0] "{x0}" (arg0),
          [arg1] "{x1}" (arg1),
          [arg2] "{x2}" (arg2),
        : .{});
}

```

This makes an inline `syscall3` function to call a syscall with three arguments, and uses it to wrap the `write` syscall as `myWrite`.

Reading the generated assembly on [Compiler Explorer](https://godbolt.org/z/b4KvTKzox) without optimizations, you should see this snippet in the main function:

```armasm
        adrp x8, ___anon_1943@PAGE
        add x8, x8, ___anon_1943@PAGEOFF
        ldr x0, [x8]
        adrp x1, ___anon_1947@PAGE
        add x1, x1, ___anon_1947@PAGEOFF
        mov w8, #2
        mov x2, x8
        bl _example.myWrite

```

and this one outside defines those `___anon` values:

```armasm
___anon_1943:
        .long 1

___anon_1947:
        .asciz "hi"

```

The first snippet loads `Fd.out` into the first argument to `myWrite` correctly, and the rest loads the string and other arguments and then calls `myWrite`.

Now, if we look at the generated assembly _with_ `ReleaseSmall` optimizations on [Compiler Explorer](https://godbolt.org/z/v3oros4En), we see this as the contents of the main function:

```armasm
        adrp x1, ___anon_1947@PAGE
        add x1, x1, ___anon_1947@PAGEOFF
        mov w16, #4
        movk w16, #512, lsl #16
        mov x0, #0
        mov w2, #2
        svc #0x80
        mov w0, #0
        ret

```

This is `syscall3` and `myWrite` inlined with the passed arguments. It puts the string into the second argument, the syscall number into `w16`, and the number of characters into the third argument, as it should.

However, the first argument `x0` gets `#0`, which is telling the `write` syscall to write the string to the `stdin` file descriptor. On my macOS machine, running this code without optimizations correctly writes to `stdout` and displays on the terminal, and running it with optimizations also displays to the terminal, but the string is written to `stdin`, which is not the same behavior nor what I meant it to do.

I checked on Compiler Explorer for a few other versions, and the equivalent code seems to have the same apparent miscompilation at least as far back as Zig 0.10.0, though in that version it does `mov x0, xzr` rather than `mov x0, #0`, both of which are ways to zero a register.

Is this use case not supported, or is this a compiler bug, or something else?

---

<div class="post-metadata">

**Author:** ![npc1054657282](https://ziggit.dev/user_avatar/ziggit.dev/npc1054657282/32/5790_2.png) [@npc1054657282](https://ziggit.dev/u/npc1054657282)\
**Post date:** [April 17, 2026, 12:28am UTC](https://ziggit.dev/t/confusing-releasesmall-optimization-of-asm-volatile-code/14981/2 "2026-04-17T00:28:00Z")

</div>

I think this might involve an LLVM bug, but it’s best to report it to Zig first.

---

<div class="post-metadata">

**Author:** ![ilovapples](https://ziggit.dev/user_avatar/ziggit.dev/ilovapples/32/8327_2.png) [@ilovapples](https://ziggit.dev/u/ilovapples)\
**Post date:** [April 22, 2026, 1:40am UTC](https://ziggit.dev/t/confusing-releasesmall-optimization-of-asm-volatile-code/14981/3 "2026-04-22T01:40:59Z")

</div>

Link to the issue: [https://codeberg.org/ziglang/zig/issues/31994](https://codeberg.org/ziglang/zig/issues/31994). It seems it’s an issue with the lowering of the C ABI on the aarch64 LLVM backend.
