Example C to Assembly Translation

Here is an example of a C translation unit (file):

/*
 * gcd.c
 *
 * A GCD function.
 */

unsigned gcd(unsigned x, unsigned y) {
    while (y > 0) {
        unsigned old_x = x;
        x = y;
        y = old_x % y;
    }
    return x;
}

The gcc compiler, when run without optimization, produced:

        .file   "gcd.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
        .align 4
.globl _gcd
        .def    _gcd;   .scl    2;      .type   32;     .endef
_gcd:
        pushl %ebp
        movl %esp,%ebp
        subl $24,%esp
        nop
        .align 4
L3:
        cmpl $0,12(%ebp)
        jne L5
        jmp L4
        .align 4
L5:
        movl 8(%ebp),%eax
        movl %eax,-4(%ebp)
        movl 12(%ebp),%eax
        movl %eax,8(%ebp)
        movl -4(%ebp),%eax
        leal 12(%ebp),%ecx
        xorl %edx,%edx
        divl (%ecx)
        movl %edx,12(%ebp)
        jmp L3
        .align 4
L4:
        movl 8(%ebp),%edx
        movl %edx,%eax
        jmp L2
        .align 4
L2:
        leave
        ret

The gcc compiler, when run with -O3 produced:

        .file   "gcd.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
        .align 4
.globl _gcd
        .def    _gcd;   .scl    2;      .type   32;     .endef
_gcd:
        pushl %ebp
        movl %esp,%ebp
        movl 8(%ebp),%ecx
        movl 12(%ebp),%edx
        testl %edx,%edx
        je L4
        .align 4
L5:
        movl %ecx,%eax
        movl %edx,%ecx
        xorl %edx,%edx
        divl %ecx
        testl %edx,%edx
        jne L5
L4:
        movl %ecx,%eax
        leave
        ret