Due Unused Variable Effects Operation

So I have been messing with this bug for the majority of a day. This is one of the oddest bugs I have ever encountered, and am wondering if anyone has any ideas for how to deal with it.

Essentially I was having trouble with my code changing operation with no regard to the actual program logic.

The code is supposed to generate a basic clock which occasionally drops a cycle. Timing doesn't have to be incredibly specific, but sometimes it would be thrown off by hundreds of microseconds.
Eventually I reduced the problem down to this ridiculous test case.

This Code Works:

int pin = 5;
unsigned int half_period = 50;
int x = 0;
void setup() {
x=1;
pinMode(pin, OUTPUT);
}
void loop() {
for ( int y = 0; y < 6; y++) {
digitalWrite(pin, HIGH);
delayMicroseconds(half_period);
digitalWrite(pin, LOW);
delayMicroseconds(half_period);
}
delayMicroseconds(half_period * 2);
}

This code does not work:

int pin = 5;
unsigned int half_period = 126;
int x = 0;
void setup() {
//x=1;
pinMode(pin, OUTPUT);
}
void loop() {
for ( int y = 0; y < 6; y++) {
digitalWrite(pin, HIGH);
delayMicroseconds(half_period);
digitalWrite(pin, LOW);
delayMicroseconds(half_period);
}
delayMicroseconds(half_period * 2);
}

As you can see, the only difference is in line 6 the x=1 is commented out. What makes this bug so odd, is the fact that x is never used. Essentially, the code works if the value of a global variable is EVER changed. If no such change is made, the timing is thrown off. See the waveforms (yellow) for the difference in outputs. If green goes high that means the signal is "bad".

Any idea what could be causing it or how to fix it?

both codes are work on my DUE.

NickTesla:
As you can see, the only difference is in line 6 the x=1 is commented out.

That's not the only difference...

Regards,
Ray L.