I feel uneasy about placing the output shape checking code in c_code() method.  If output array is supplied by the optimizer, the optimizer should be able assure that the shape is correct and omit the shape checking code.  I don't know Theano internals well enough to suggest a solution, but I would expect shape checking code to go into a separate c_ method, not in c_code().