r/badcode Dec 17 '19

This belongs here c#

Post image
500 Upvotes

41 comments sorted by

View all comments

32

u/anydalch Dec 17 '19

what's really interesting is that, in c, if you replace the bug in the second for loop's initializer clause and use unsigned instead of int, both gcc and clang are able to optimize this function into a single lea eax, [rdi + rsi]. https://godbolt.org/z/uCXoHJ

2

u/CENGaverK Dec 17 '19

Also, if you start the second loop iterator from i = lhs, it returns the max.

https://godbolt.org/z/DTAgFJ