Reverse Engineering challenge #17.

Tags: L1 X64 ASM .

This is a quite esoteric piece of code, but nevertheless, the task it does is very mundane and well-known to anyone. The function has 4 32-bit arguments and returns a 32-bit one. What does it do?

Optimizing GCC 4.8.2:


f:
        sub     edx, edi
        mov     r8d, ecx
        mov     ecx, 63
        mov     eax, edx
        sub     r8d, esi
        sar     eax, cl
        and     eax, edx
        mov     edx, r8d
        sar     edx, cl
        add     edi, eax
        and     edx, r8d
        add     esi, edx
        sub     esi, edi
        mov     eax, esi
        sar     eax, cl
        and     eax, esi
        add     eax, edi
        ret

It had a bug which was fixed 21-Apr-2016, thanks to Diego Boy.

More challenges: challenges.re; about solutions: challenges.re/#Solutions.