# Does code motion/hoisting happen on if statements/branches? [closed]

Consider the following C code:

for(int i=0; i<100000; i++) {
if (operation == 0) {
a[i] *= 2;
} else {
a[i] += 1;
}
}


Would a real compiler (GCC) be able to do this optimization? If so, what is it called/how is it found?

if (operation == 0) {
for(int i=0; i<100000; i++) {
a[i] *= 2;
}
} else {
for(int i=0; i<100000; i++) {
a[i] += 1;
}
}


## closed as off-topic by David Richerby, Raphael♦Oct 14 '18 at 13:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – David Richerby, Raphael
If this question can be reworded to fit the rules in the help center, please edit the question.

• Why don't you try and see? You can look at the assembly code produced by the compiler. – Yuval Filmus Oct 13 '18 at 5:17
• Looks like it does with -O3 but not by default – cannon Oct 13 '18 at 7:18
• Voting to close as off-topic. Questions about the principles behind compilers are absolutely on-topic, here, but "Does this actual compiler do this thing?" isn't a question about computer science. – David Richerby Oct 14 '18 at 13:12