Skip to content

Instantly share code, notes, and snippets.

@thomasloven
Last active August 29, 2015 13:58
Show Gist options
  • Save thomasloven/9974405 to your computer and use it in GitHub Desktop.
Save thomasloven/9974405 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int alt1(const char *s)
{
int i;
for(i=0; s[i]; s[i]==':'?i++:(int)(s++));
return i;
}
int alt2(const char *s)
{
int i = 0;
while(*s){if(*s == ':')i++; s++;};
return i;
}
int alt3(const char *s)
{
int i = 0;
do{ if(*s == ':') i++;}while(*s++);
return i;
}
int main(int argc, const char *argv[])
{
char *string = "a:b:c";
printf("Alt1: %d\n", alt1(string));
printf("Alt2: %d\n", alt2(string));
printf("Alt3: %d\n", alt3(string));
return 0;
}
Compiled with
gcc -Os -m32
Run on a mac, i.e. gcc calls llvm and clang
00001e9d <_alt1>:
1e9d: 55 push ebp
1e9e: 89 e5 mov ebp,esp
1ea0: 31 c0 xor eax,eax
1ea2: 8b 4d 08 mov ecx,DWORD PTR [ebp+0x8]
1ea5: eb 01 jmp 1ea8 <_alt1+0xb>
1ea7: 40 inc eax
1ea8: 8a 14 01 mov dl,BYTE PTR [ecx+eax*1]
1eab: 84 d2 test dl,dl
1ead: 74 08 je 1eb7 <_alt1+0x1a>
1eaf: 80 fa 3a cmp dl,0x3a
1eb2: 74 f3 je 1ea7 <_alt1+0xa>
1eb4: 41 inc ecx
1eb5: eb f1 jmp 1ea8 <_alt1+0xb>
1eb7: 5d pop ebp
1eb8: c3 ret
00001eb9 <_alt2>:
1eb9: 55 push ebp
1eba: 89 e5 mov ebp,esp
1ebc: 8b 4d 08 mov ecx,DWORD PTR [ebp+0x8]
1ebf: 31 c0 xor eax,eax
1ec1: eb 01 jmp 1ec4 <_alt2+0xb>
1ec3: 41 inc ecx
1ec4: 8a 11 mov dl,BYTE PTR [ecx]
1ec6: 84 d2 test dl,dl
1ec8: 74 08 je 1ed2 <_alt2+0x19>
1eca: 80 fa 3a cmp dl,0x3a
1ecd: 75 f4 jne 1ec3 <_alt2+0xa>
1ecf: 40 inc eax
1ed0: eb f1 jmp 1ec3 <_alt2+0xa>
1ed2: 5d pop ebp
1ed3: c3 ret
00001ed4 <_alt3>:
1ed4: 55 push ebp
1ed5: 89 e5 mov ebp,esp
1ed7: 53 push ebx
1ed8: 8b 4d 08 mov ecx,DWORD PTR [ebp+0x8]
1edb: 31 c0 xor eax,eax
1edd: 89 c2 mov edx,eax
1edf: 8a 19 mov bl,BYTE PTR [ecx]
1ee1: 80 fb 3a cmp bl,0x3a
1ee4: 0f 94 c0 sete al
1ee7: 0f b6 c0 movzx eax,al
1eea: 01 d0 add eax,edx
1eec: 41 inc ecx
1eed: 84 db test bl,bl
1eef: 75 ec jne 1edd <_alt3+0x9>
1ef1: 5b pop ebx
1ef2: 5d pop ebp
1ef3: c3 ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment