Don't just let your compiler boss you around, you're better than that.

*suddenly switches to a more sinister tone*
Just write naked functions for a while and let it see how much you don't need it.

@loganer Well, this is kinda compiler-dependent but at least on gcc, there is `__attribute__((naked))`

@mage_of_dragons actually this could be useful for me.

later of course.

@mage_of_dragons assuming of course that the compiler wont just opti-fuck the code away of course... I will need to do experiments later.

@loganer it tends to do that yeah x3

There's usually a volatile or some constraint you can do though

@mage_of_dragons I really don't want to jump through all these whoops to get the compiler to just compile what I wrote, it's kind of really silly how this came about.

you'd think these optimizations would be opt-in but nope ... because supposedly the compiler people know better (they really don't but they like to think they do.)

@loganer Wdym, they are usually opt-in. And (not a compiler dragon but) compiler people do usually know better since most code doesn't require inline assembly, precise struct alignment, non-cached writes, parallelism etc., which is in my experience the only time I had problems with them.

@mage_of_dragons

most of the basic optimizations are opt-out actually.
to do debugging for instance you have to pass in -O0 to the commandline but even that doesn't turn off all the optimization stuff.

the existence of volatile as a keyword demonstrates this as well, this keyword would not need to exist if the optimizations were opt-in.

@loganer Well yeah, if the compiler were to make something like struct alignment opt-in, it'd do a lot more harm than good.

> the existence of volatile as a keyword demonstrates this as well, this keyword would not need to exist if the optimizations were opt-in.

I don't see how its existence makes optimizations opt-in. It's there that *if* some optimization that would cause it to break were enabled, the compiler doesn't apply it.

@mage_of_dragons it's existence proves that it's opt-out and it would not need to exist if the optimization was opt-in as the optimization would be selected on the commmandline during compile time.

I just want the compiler to take the input code and do absolutely nothing to it and translate it directly into assemble... is that too hard?

@loganer Say I have
```c
#include <stdio.h>
void test() {
int test = 32;
printf("%d", test);
}
```

The memory write will get executed with no opts, but will be optimized to use a register with as low as -O1.

If I make it volatile, the memory write is preserved even with -O3. This is what volatile is for, to mark the places that have side effects the compiler might not understand, *should* the program ever be compiled with optimizations. Without opts, volatile may as well be ignored.

@mage_of_dragons

let me demonstrate the real problem here...

```c
void test() {
int test = 0;
if(test){
printf("%d", test);
}
}
```

@loganer dead code removal is the real problem?

@mage_of_dragons

it is part of the problem, yes, and the most easily demonstrable part...
but it is not the whole problem, which is a bit more broader.

@loganer Could you explain further? I'm at the end of my compiler knowledge x3

@mage_of_dragons

if I wanted to keep this "dead code" for whatever reason, I would need to explicitly opt-out of the optimization via usage of volatile.
but on a much large or more complex system this could easily be forgotten ... unless I choose to make litterally everything volatile.

@mage_of_dragons and same goes with the struct thing as well ... I will have to make every single struct explicitly __attribute__((packed)) just to make sure the compiler makes it exactly as I wrote it.
@loganer Well, here you run into the risk of accidentally misaligning something though, which can cause an exception on architectures like x86

@mage_of_dragons then let it do that and throw a warning if you absolutely must do anything.

at least if you don't tamper with the code then I know it's 100% my fault and then I can handle that.

@loganer Sure, I can see some cases where disabling that helps (the limine boot protocol literally works like that x3). But imo in most situations that is not necessary. Though it can definitely be annoying if you debug something and it turns out some compiler optimization caused the bug x3

@mage_of_dragons and I am always debugging because i am also learning how this stuff works.

how am I suppose to do that if I'm not even sure if it was me that made the problem or the optimizer?