If like me you don't know much about the way in which compilers optimise your code its actually a bit impressive.
Your code is saying -
PORTE |= 1<<5;
PORTE &= ~(1<<5);
Which is something like -
1) If val use this set of cases, else use that set of cases
2) based on which one of 13 pins this is, do the following
3) use a given port
4) shift a bit n places to the left
5) or the port with the bit shifted in 3
Each time your code calls this macro the compiler is able to say -
1) I know val and its isnt going to change, so I will not use the if, instead I will just go straight to the correct switch/case statement
2) I know the pin and it isnt going to change so I will just choose the correct case statement and forget about the switch and other case statements - I wont be using them because the pin isnt going to change
So now we have just this
PORTE |= 1<<5;
At this point the compiler knows -
1) ok, you want me to shift 1 five places to the left
2) Then you want me to or this with the value of a port
It then figures out, ok, all you actually want me to do with all of this code, is to simply change the value of one bit in a register, I have a dedicated instruction for that which is SBI, so thats what I will use.
The entire macro get reduced to a single instruction to set or clear a bit in a port.
To me, its pretty impressive that the compiler is doing all of that work to optimise out if else,switch case, shift, or and assignment and replace it all with a single instruction.
I am sure its all run of the mill stuff and compilers can be far more impressive in thier optimisation, but it does also illustrate what a nonsense it is for beginners to stress about optimising C code rather than writing well formed code when the compiler already has you pretty well covered on the optimisation side.