https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119543
--- Comment #4 from Kevin Tang <2023152001 at email dot szu.edu.cn> --- Dear Andrew Pinski, Thank you very much for your prompt response and especially for providing the well-defined implementation of the isTmax function. I appreciate you taking the time not only to explain why my original implementation was problematic but also to provide a standards-compliant alternative that preserves the core logic of my function. The resources you shared about undefined behavior will be very helpful for my future programming work. This was a valuable lesson in writing robust, portable C code. Thank you again for your help! Best regards, Kevin Tang ------------------ Original ------------------ From: "pinskia at gcc dot gnu.org"; Date: 2025年3月30日(星期天) 下午5:21 To: "唐济宁"<2023152...@email.szu.edu.cn>; Subject: [Bug tree-optimization/119543] At -O1 and higher, GCC incorrectly optimizes isTmax function to always return 0 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119543 --- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> --- here is an implementation like your original one which is well defined: ``` int isTmax(int x) { unsigned temp = x; temp = temp + 1; // do the addition in unsigned rather than signed to avoid undefined behavior and get wrapping behavior rather than an overflow int tmin = 1u << 31; /* 0x80000000 - Minimum two's complement value */ int t = temp ^ tmin; int y = !t; return y; } ```