Skip to content

Instantly share code, notes, and snippets.

@travitch
Created March 1, 2021 16:56
Show Gist options
  • Save travitch/c88f405dfc9f2d1820d10e67aeb6538a to your computer and use it in GitHub Desktop.
Save travitch/c88f405dfc9f2d1820d10e67aeb6538a to your computer and use it in GitHub Desktop.
int __attribute__((noinline)) test_saturate_add(int x, int y) {
int c = x + y;
if(c < x || c < y)
c = INT_MAX;
return c >= x && c >= y;
}
# Compiles to (with clang -O2)
0000000000401000 <test_saturate_add>:
401000: 85 f6 test %esi,%esi
401002: 78 09 js 40100d <test_saturate_add+0xd>
401004: 85 ff test %edi,%edi
401006: 78 05 js 40100d <test_saturate_add+0xd>
401008: 8d 04 3e lea (%rsi,%rdi,1),%eax
40100b: eb 05 jmp 401012 <test_saturate_add+0x12>
40100d: b8 ff ff ff 7f mov $0x7fffffff,%eax
401012: 39 f8 cmp %edi,%eax
401014: 0f 9d c1 setge %cl
401017: 39 f0 cmp %esi,%eax
401019: 0f 9d c0 setge %al
40101c: 20 c8 and %cl,%al
40101e: 0f b6 c0 movzbl %al,%eax
401021: c3 retq
401022: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
401029: 00 00 00
40102c: 0f 1f 40 00 nopl 0x0(%rax)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment