its just the way gcc behaves, you kind of declare a null input as undefined behavior and gcc is allowed to optimize that away. The problem is gcc does not tell you that the check contradicts the attribute, I bet you can find plenty already bug reports about that.