G1, G3, G4, Y1, Y2, Y3, Y4, R2 = 0;
I cannot see any evident reason for a stack underflow, but I haven't ever touched a PIC, so my analysis is limited from
int main() to the closing brace.
But I'm quite sure of one thing: the above quoted assignment is not doing what you hope it would.
The comma operator in C evaluates then discards its first operand, then returns the result of the evaluation of the second operand.
Also, it has left to right associativity and inserts a sequence point, completing the side effects of the first operand evaluation before evaluating the second one.
This means that G1 is evaluated and discarded, then G3 and so on, until finally R2 = 0 is: apart from R2, all the other variable will keep their original values.